import type { Message, MessageArea } from "@/app/lib/types"; export interface ImportPreviewRow { code: string; area: MessageArea; action: "create" | "update"; currentEn?: string; incomingEn?: string; } const key = (m: Message) => `${m.area}|${m.code.toLowerCase()}`; const english = (content: string): string | undefined => { try { const v = JSON.parse(content)?.en_US; return typeof v === "string" ? v : undefined; } catch { return undefined; } }; /** Matches on area + case-insensitive code, ignoring ids (as the backend import does). */ export function buildImportPreview(rows: Message[], existing: Message[]): ImportPreviewRow[] { const byKey = new Map(); for (const m of existing) if (!byKey.has(key(m))) byKey.set(key(m), m); // first wins, like the backend return rows.map((r) => { const cur = byKey.get(key(r)); return cur ? { code: r.code, area: r.area, action: "update", currentEn: english(cur.content), incomingEn: english(r.content) } : { code: r.code, area: r.area, action: "create", incomingEn: english(r.content) }; }); }