import type { Doc, InboxAttachment, InboxMessage, Task } from "./schemas.js"; export interface RecordCopyAction { content: string; copyLabel: string; copiedMessage: string; copyErrorMessage: string; } type RecordKind = "task" | "inbox" | "doc"; interface LocalizedRecordCopy { heading: string; copyLabel: string; copiedMessage: string; copyErrorMessage: string; metadata: { id: string; status: string; priority: string; assignee: string; author: string; type: string; attachments: string; }; } const ENGLISH_COPY: Readonly> = { task: { heading: "Task", copyLabel: "Copy task", copiedMessage: "Task copied.", copyErrorMessage: "Task could not be copied.", metadata: { id: "ID", status: "Status", priority: "Priority", assignee: "Assignee", author: "Author", type: "Type", attachments: "Attachments", }, }, inbox: { heading: "Inbox message", copyLabel: "Copy inbox message", copiedMessage: "Inbox message copied.", copyErrorMessage: "Inbox message could not be copied.", metadata: { id: "ID", status: "Status", priority: "Priority", assignee: "Assignee", author: "Author", type: "Type", attachments: "Attachments", }, }, doc: { heading: "Document", copyLabel: "Copy document", copiedMessage: "Document copied.", copyErrorMessage: "Document could not be copied.", metadata: { id: "ID", status: "Status", priority: "Priority", assignee: "Assignee", author: "Author", type: "Type", attachments: "Attachments", }, }, }; const SWEDISH_COPY: Readonly> = { task: { heading: "Uppgift", copyLabel: "Kopiera uppgift", copiedMessage: "Uppgiften har kopierats.", copyErrorMessage: "Uppgiften kunde inte kopieras.", metadata: { id: "ID", status: "Status", priority: "Prioritet", assignee: "Tilldelad", author: "Avsändare", type: "Typ", attachments: "Bilagor", }, }, inbox: { heading: "Inkorgsmeddelande", copyLabel: "Kopiera inkorgsmeddelande", copiedMessage: "Inkorgsmeddelandet har kopierats.", copyErrorMessage: "Inkorgsmeddelandet kunde inte kopieras.", metadata: { id: "ID", status: "Status", priority: "Prioritet", assignee: "Tilldelad", author: "Avsändare", type: "Typ", attachments: "Bilagor", }, }, doc: { heading: "Dokument", copyLabel: "Kopiera dokument", copiedMessage: "Dokumentet har kopierats.", copyErrorMessage: "Dokumentet kunde inte kopieras.", metadata: { id: "ID", status: "Status", priority: "Prioritet", assignee: "Tilldelad", author: "Avsändare", type: "Typ", attachments: "Bilagor", }, }, }; function localizedCopy( kind: RecordKind, projectLanguage: string, ): LocalizedRecordCopy { const isSwedish = projectLanguage === "sv" || projectLanguage.startsWith("sv-"); return isSwedish ? SWEDISH_COPY[kind] : ENGLISH_COPY[kind]; } function singleLine(value: string): string { return value.replace(/\s+/g, " ").trim(); } function escapeLinkLabel(value: string): string { return value .replaceAll("\\", "\\\\") .replaceAll("[", "\\[") .replaceAll("]", "\\]"); } /** * Record copies use raw Markdown bodies rather than rendered HTML so agent * handoffs preserve the user's authored source and never inherit sanitized or * presentation-only markup from the web view. */ function recordMarkdown( heading: string, metadata: Array<[string, string | null]>, body: string, extraSections: string[] = [], ): string { const metadataLines = metadata .filter((entry): entry is [string, string] => entry[1] !== null) .map(([label, value]) => `- ${label}: ${singleLine(value)}`); const sections = [`# ${singleLine(heading)}`, metadataLines.join("\n")]; if (body) sections.push(body); sections.push(...extraSections); return sections.filter(Boolean).join("\n\n"); } function action( kind: RecordKind, content: string, projectLanguage: string, ): RecordCopyAction { const copy = localizedCopy(kind, projectLanguage); return { content, copyLabel: copy.copyLabel, copiedMessage: copy.copiedMessage, copyErrorMessage: copy.copyErrorMessage, }; } export function createTaskRecordCopyAction( task: Task, body: string, projectLanguage: string, ): RecordCopyAction { const copy = localizedCopy("task", projectLanguage); const content = recordMarkdown( task.title, [ [copy.metadata.id, task.id], [copy.metadata.status, task.status], [copy.metadata.priority, task.priority], [copy.metadata.assignee, task.assignee], ], body, ); return action("task", content, projectLanguage); } export function createInboxRecordCopyAction( message: InboxMessage, body: string, projectLanguage: string, attachmentHref: (attachment: InboxAttachment) => string, ): RecordCopyAction { const copy = localizedCopy("inbox", projectLanguage); const attachments = message.attachments.map( (attachment) => `- [${escapeLinkLabel(attachment.name)}](<${attachmentHref(attachment)}>)`, ); const extraSections = attachments.length ? [`## ${copy.metadata.attachments}\n\n${attachments.join("\n")}`] : []; const content = recordMarkdown( copy.heading, [ [copy.metadata.id, message.id], [copy.metadata.status, message.status], [ copy.metadata.author, `${message.author_name} <${message.author_email}>`, ], ], body, extraSections, ); return action("inbox", content, projectLanguage); } export function createDocRecordCopyAction( doc: Doc, body: string, projectLanguage: string, ): RecordCopyAction { const copy = localizedCopy("doc", projectLanguage); const content = recordMarkdown( doc.title, [ [copy.metadata.id, doc.id], [copy.metadata.type, doc.type], [copy.metadata.status, doc.status], ], body, ); return action("doc", content, projectLanguage); }