/**
 * Attachments Store - manages user attachments through Context.
 *
 * Attachments are persisted through Context.setAttachments() which ensures
 * they are included in Context serialization (toRecord/fromRecord).
 */
import { getLogger } from "../../util/logger.js";
/** Maximum length for the original message stored with attachments */
const MAX_MESSAGE_LENGTH = 300;
/**
 * Truncates a message to the max length, adding [...] if truncated.
 */
function truncateMessage(message) {
    if (!message)
        return undefined;
    const trimmed = message.trim();
    if (trimmed.length <= MAX_MESSAGE_LENGTH)
        return trimmed;
    return trimmed.slice(0, MAX_MESSAGE_LENGTH) + "[...]";
}
export class AttachmentsStore {
    getAttachments(context) {
        return (context.getAttachments() ?? {
            nextId: 1,
            attachments: [],
        });
    }
    appendAttachments(context, attachments, originalMessage) {
        if (attachments.length === 0) {
            return [];
        }
        const data = this.getAttachments(context);
        const now = Date.now();
        const truncatedMessage = truncateMessage(originalMessage);
        const newAttachments = [];
        for (const attachment of attachments) {
            const baseFields = {
                id: data.nextId++,
                fileName: attachment.fileName,
                label: attachment.label,
                insight: attachment.insight,
                originalMessage: truncatedMessage,
                createdAt: now,
                updatedAt: now,
            };
            let stored;
            if (attachment.type === "image") {
                stored = {
                    ...baseFields,
                    type: "image",
                    imageDataUrl: attachment.image,
                };
            }
            else if (attachment.type === "pdf") {
                stored = {
                    ...baseFields,
                    type: "pdf",
                    data: attachment.data,
                    size: attachment.size,
                };
            }
            else if (attachment.type === "csv" ||
                attachment.type === "json" ||
                attachment.type === "yaml" ||
                attachment.type === "css" ||
                attachment.type === "txt") {
                stored = {
                    ...baseFields,
                    type: attachment.type,
                    content: attachment.content,
                    mimeType: attachment.mimeType,
                    size: attachment.size,
                };
            }
            else {
                getLogger().warn(`[attachments-store] Skipping unknown attachment type: ${attachment.type}`);
                continue;
            }
            data.attachments.push(stored);
            newAttachments.push(stored);
        }
        context.setAttachments(data);
        getLogger().info(`[attachments-store] Persisted ${newAttachments.length} attachment(s)`, newAttachments.map((a) => ({
            id: a.id,
            type: a.type,
            fileName: a.fileName,
            label: a.label,
            insight: a.insight,
            originalMessage: a.originalMessage,
        })));
        return newAttachments;
    }
    listAttachments(context) {
        const data = this.getAttachments(context);
        return data.attachments.map((a) => ({
            id: a.id,
            type: a.type,
            fileName: a.fileName,
            label: a.label,
            insight: a.insight,
            originalMessage: a.originalMessage,
        }));
    }
    getAttachmentsByIds(context, ids) {
        const data = this.getAttachments(context);
        const idSet = new Set(ids);
        const found = [];
        const foundIds = new Set();
        for (const attachment of data.attachments) {
            if (idSet.has(attachment.id)) {
                found.push(attachment);
                foundIds.add(attachment.id);
            }
        }
        // Log any missing IDs
        const missingIds = ids.filter((id) => !foundIds.has(id));
        if (missingIds.length > 0) {
            getLogger().warn(`[attachments-store] Requested attachment IDs not found: ${missingIds.join(", ")}`);
        }
        return found;
    }
    static toAttachments(stored) {
        return stored.map((s) => {
            if (s.type === "image") {
                return {
                    type: "image",
                    image: s.imageDataUrl,
                    fileName: s.fileName ?? "image.png",
                    label: s.label,
                    insight: s.insight,
                };
            }
            else if (s.type === "pdf") {
                return {
                    type: "pdf",
                    data: s.data,
                    fileName: s.fileName ?? "document.pdf",
                    size: s.size,
                    label: s.label,
                    insight: s.insight,
                };
            }
            else {
                // Text attachments: csv, json, yaml, css, txt
                return {
                    type: s.type,
                    content: s.content,
                    fileName: s.fileName ?? `file.${s.type}`,
                    mimeType: s.mimeType,
                    size: s.size,
                    label: s.label,
                    insight: s.insight,
                };
            }
        });
    }
}
//# sourceMappingURL=store.js.map