import { prisma } from '../utils/index.js';
function uniqueIds(ids) {
    return Array.from(new Set((ids ?? []).map((id) => id.trim()).filter(Boolean)));
}
function formatAttachmentMarkdown(attachment) {
    const prefix = attachment.mimeType.startsWith('image/') ? '!' : '';
    return `${prefix}[${attachment.originalName}](${attachment.storagePath})`;
}
export async function buildAttachmentMarkdownContext(attachmentIds, existingContent = '') {
    const ids = uniqueIds(attachmentIds);
    if (ids.length === 0)
        return '';
    const attachments = await prisma.attachment.findMany({
        where: { id: { in: ids } },
    });
    const attachmentById = new Map(attachments.map((attachment) => [attachment.id, attachment]));
    const lines = [];
    for (const id of ids) {
        const attachment = attachmentById.get(id);
        if (!attachment)
            continue;
        if (existingContent.includes(attachment.storagePath))
            continue;
        lines.push(formatAttachmentMarkdown(attachment));
    }
    if (lines.length === 0)
        return '';
    return ['Attachments:', ...lines].join('\n');
}
export async function appendAttachmentMarkdownContext(content, attachmentIds) {
    const attachmentContext = await buildAttachmentMarkdownContext(attachmentIds, content);
    return [content.trim(), attachmentContext].filter(Boolean).join('\n\n');
}
//# sourceMappingURL=attachment-context.js.map