1import type { ComposerContextRecord, EnvironmentId } from "@t3tools/contracts";
2import { imageMimeType } from "@t3tools/shared/image";
3import type { FilePreviewItem } from "../components/FilePreviewModal.types";
4import { composerImageGallery } from "./composerImageGallery";
5import type { DraftComposerAttachment } from "./composerImages";
6
7export function contextImageGallery(
8 records: readonly ComposerContextRecord[],
9 attachments: readonly DraftComposerAttachment[],
10 environmentId?: EnvironmentId,
11): FilePreviewItem[] {
12 return records.flatMap((record): FilePreviewItem[] => {
13 if (!("attachmentId" in record) || (record.kind !== "image" && imageMimeType(record) === null))
14 return [];
15 const local = attachments.find((item) => item.id === record.attachmentId);
16 if (local) return composerImageGallery([local]);
17 return environmentId
18 ? [
19 {
20 kind: "image",
21 name: record.name,
22 environmentId,
23 resource: {
24 _tag: "attachment",
25 attachmentId: record.attachmentId,
26 fileName: record.name,
27 mimeType: record.mimeType,
28 },
29 },
30 ]
31 : [];
32 });
33}