{"version":3,"file":"ChannelMediaView.utils.mjs","names":[],"sources":["../../../../../../src/plugins/ChannelDetail/Views/ChannelMediaView/ChannelMediaView.utils.ts"],"sourcesContent":["import {\n  type Attachment,\n  convertTimestampToDate,\n  formatMessage,\n  isImageAttachment,\n  isVideoAttachment,\n  type LocalMessage,\n  type MessageResponse,\n  type UserResponse,\n  type VoiceRecordingAttachment,\n} from 'stream-chat';\n\nimport { toBaseImageDescriptors } from '../../../../components/BaseImage';\nimport type { GalleryItem } from '../../../../components/Gallery';\n\n/** Attachment types rendered by the media gallery. */\nexport const MEDIA_ATTACHMENT_TYPES = ['image', 'video'] as const;\n\nexport type ChannelMediaItemType = (typeof MEDIA_ATTACHMENT_TYPES)[number];\n\nexport type ChannelMediaItem = {\n  /** Item to hand over to the full-screen `Gallery` viewer. */\n  galleryItem: GalleryItem;\n  /** Stable identity: message ID and attachment index. */\n  id: string;\n  type: ChannelMediaItemType;\n  /** Video duration in seconds, when known. */\n  durationSeconds?: number;\n  /** User who shared the media. */\n  user?: UserResponse;\n};\n\nconst getMediaAttachmentType = (\n  attachment: Attachment,\n): ChannelMediaItemType | undefined => {\n  if (isVideoAttachment(attachment)) return 'video';\n  if (isImageAttachment(attachment)) return 'image';\n  return undefined;\n};\n\n/** Flattens messages into one media item per renderable image or video attachment. */\nexport const toChannelMediaItems = (\n  messages: Array<MessageResponse | LocalMessage>,\n): ChannelMediaItem[] => {\n  const items: ChannelMediaItem[] = [];\n\n  messages.forEach((message) => {\n    // Formatted once per message: the gallery header passes it to a `MessageTimestamp` override,\n    // which takes a `LocalMessage`.\n    let localMessage: LocalMessage | undefined;\n    message.attachments?.forEach((attachment, index) => {\n      const type = getMediaAttachmentType(attachment);\n      if (!type) return;\n\n      const descriptor = toBaseImageDescriptors(attachment);\n      if (!descriptor) return;\n\n      const hasRenderableSource =\n        type === 'video'\n          ? Boolean(descriptor.videoThumbnailUrl || descriptor.videoUrl)\n          : Boolean(descriptor.imageUrl);\n      if (!hasRenderableSource) return;\n\n      const attachmentDuration = (attachment as VoiceRecordingAttachment).custom\n        ?.duration;\n\n      items.push({\n        durationSeconds:\n          typeof attachmentDuration === 'number' ? attachmentDuration : undefined,\n        galleryItem: {\n          ...descriptor,\n          // the gallery header reads sender and timestamp off the item\n          createdAt: convertTimestampToDate(message.created_at),\n          message: (localMessage ??= formatMessage(message)),\n          user: message.user ?? undefined,\n        },\n        id: `${message.id}-${index}`,\n        type,\n        user: message.user ?? undefined,\n      });\n    });\n  });\n\n  return items;\n};\n"],"mappings":";;;;AAgBA,IAAa,yBAAyB,CAAC,SAAS,OAAO;AAgBvD,IAAM,0BACJ,eACqC;CACrC,IAAI,kBAAkB,UAAU,GAAG,OAAO;CAC1C,IAAI,kBAAkB,UAAU,GAAG,OAAO;AAE5C;;AAGA,IAAa,uBACX,aACuB;CACvB,MAAM,QAA4B,CAAC;CAEnC,SAAS,SAAS,YAAY;EAG5B,IAAI;EACJ,QAAQ,aAAa,SAAS,YAAY,UAAU;GAClD,MAAM,OAAO,uBAAuB,UAAU;GAC9C,IAAI,CAAC,MAAM;GAEX,MAAM,aAAa,uBAAuB,UAAU;GACpD,IAAI,CAAC,YAAY;GAMjB,IAAI,EAHF,SAAS,UACL,QAAQ,WAAW,qBAAqB,WAAW,QAAQ,IAC3D,QAAQ,WAAW,QAAQ,IACP;GAE1B,MAAM,qBAAsB,WAAwC,QAChE;GAEJ,MAAM,KAAK;IACT,iBACE,OAAO,uBAAuB,WAAW,qBAAqB,KAAA;IAChE,aAAa;KACX,GAAG;KAEH,WAAW,uBAAuB,QAAQ,UAAU;KACpD,SAAU,iBAAiB,cAAc,OAAO;KAChD,MAAM,QAAQ,QAAQ,KAAA;IACxB;IACA,IAAI,GAAG,QAAQ,GAAG,GAAG;IACrB;IACA,MAAM,QAAQ,QAAQ,KAAA;GACxB,CAAC;EACH,CAAC;CACH,CAAC;CAED,OAAO;AACT"}