{"version":3,"file":"ChannelFilesView.utils.mjs","names":[],"sources":["../../../../../../src/plugins/ChannelDetail/Views/ChannelFilesView/ChannelFilesView.utils.ts"],"sourcesContent":["import {\n  type Attachment,\n  isScrapedContent,\n  type LocalMessage,\n  type MessageResponse,\n  type TimestampNS,\n} from 'stream-chat';\n\nimport { convertTimestampToDate } from 'stream-chat';\n\n/** Attachment types listed by the files view. */\nexport const FILE_ATTACHMENT_TYPES = ['file', 'audio'] as const;\n\nexport type ChannelFileAttachmentType = (typeof FILE_ATTACHMENT_TYPES)[number];\n\nconst FILE_ATTACHMENT_TYPE_SET = new Set<string>(FILE_ATTACHMENT_TYPES);\n\nexport type ChannelFileItem = {\n  /** Attachment as received, untransformed. */\n  attachment: Attachment;\n  /** Stable identity: message ID and attachment index. */\n  id: string;\n  /** ISO timestamp of the carrying message, used to pick the month section. */\n  createdAt?: string;\n};\n\nexport type ChannelFileSection = {\n  /** Stable grouping key (`YYYY-MM` or `unknown`). */\n  key: string;\n  /** Timestamp of the section's newest item, rendered in the section header. */\n  timestamp?: string;\n};\n\nexport type ChannelFileSections = {\n  /** Item count per section, aligned with `sections`; the sum equals `items.length`. */\n  groupCounts: number[];\n  /** Flat list of items in display order, grouped contiguously by section. */\n  items: ChannelFileItem[];\n  /** Month sections, aligned with `groupCounts`. */\n  sections: ChannelFileSection[];\n};\n\n/**\n * A wire timestamp as an ISO string, which `getDateString` accepts, the month key slices and\n * `byCreatedAtDesc` compares. A nanosecond value is out of `Date`'s range, so it goes through\n * `convertTimestampToDate` instead of `new Date`.\n */\nconst normalizeTimestamp = (timestamp?: TimestampNS) =>\n  timestamp == null ? undefined : convertTimestampToDate(timestamp)?.toISOString();\n\nconst isChannelFileAttachment = (attachment: Attachment) =>\n  !isScrapedContent(attachment) &&\n  !!attachment.type &&\n  FILE_ATTACHMENT_TYPE_SET.has(attachment.type);\n\nconst byCreatedAtDesc = (a: ChannelFileItem, b: ChannelFileItem) =>\n  (b.createdAt ?? '').localeCompare(a.createdAt ?? '');\n\n/**\n * Flattens messages into file and audio attachment items in month sections, newest first.\n *\n * The result is shaped for GroupedVirtuoso: a flat `items` array grouped contiguously by section,\n * with `groupCounts` and `sections` aligned to it.\n */\nexport const toChannelFileSections = (\n  messages: Array<MessageResponse | LocalMessage>,\n): ChannelFileSections => {\n  const groups: Array<ChannelFileSection & { items: ChannelFileItem[] }> = [];\n  const groupIndexByKey = new Map<string, number>();\n\n  messages.forEach((message) => {\n    const createdAt = normalizeTimestamp(message.created_at);\n    const key = createdAt ? createdAt.slice(0, 7) : 'unknown';\n\n    message.attachments?.forEach((attachment, index) => {\n      if (!isChannelFileAttachment(attachment)) return;\n\n      const item: ChannelFileItem = {\n        attachment,\n        createdAt,\n        id: `${message.id}-${index}`,\n      };\n      const existingIndex = groupIndexByKey.get(key);\n\n      if (existingIndex === undefined) {\n        groupIndexByKey.set(key, groups.length);\n        groups.push({ items: [item], key, timestamp: createdAt });\n      } else {\n        groups[existingIndex].items.push(item);\n      }\n    });\n  });\n\n  groups.forEach((group) => {\n    group.items.sort(byCreatedAtDesc);\n    group.timestamp = group.items[0]?.createdAt;\n  });\n  groups.sort((a, b) => (b.timestamp ?? '').localeCompare(a.timestamp ?? ''));\n\n  return {\n    groupCounts: groups.map((group) => group.items.length),\n    items: groups.flatMap((group) => group.items),\n    sections: groups.map(({ key, timestamp }) => ({ key, timestamp })),\n  };\n};\n"],"mappings":";;;AAWA,IAAa,wBAAwB,CAAC,QAAQ,OAAO;AAIrD,IAAM,2BAA2B,IAAI,IAAY,qBAAqB;;;;;;AAgCtE,IAAM,sBAAsB,cAC1B,aAAa,OAAO,KAAA,IAAY,uBAAuB,SAAS,CAAC,EAAE,YAAY;AAEjF,IAAM,2BAA2B,eAC/B,CAAC,iBAAiB,UAAU,KAC5B,CAAC,CAAC,WAAW,QACb,yBAAyB,IAAI,WAAW,IAAI;AAE9C,IAAM,mBAAmB,GAAoB,OAC1C,EAAE,aAAa,GAAA,CAAI,cAAc,EAAE,aAAa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