/** * MITHOME-129: exact-match content revision on localized fields, in place. * * Used by data migrations that correct published copy (e.g. service claims * aligned with the real product range). Rules: * - a value changes only when its current Hungarian text is EXACTLY the * expected old text — anything an editor already rewrote is left alone; * - English is replaced only when it is empty or still the old AI draft * (`enWas`) — a human English edit is kept and reported instead; * - if English changed on a page whose translation was approved, the page * goes back to "ai-draft": the new English has not been reviewed yet. * Works for localized text leaves and whole-array localized string lists * ({ value } rows), where it can also insert a new item after an anchor. */ import { applyAll, isPlainObject, type LocalizedPath } from './localize-paths' export type TextRevision = { from: string; to: string; enWas?: string; enTo: string } export type ListInsertion = { afterHu: string; hu: string; afterEn: string; en: string } export type RevisionReport = { changed: number; enKept: string[] } type Row = { value?: unknown; id?: unknown } const isEmpty = (value: unknown) => value === undefined || value === null || value === '' function reviseText(value: Record, revisions: readonly TextRevision[], report: RevisionReport) { const revision = revisions.find((r) => r.from === value.hu) if (!revision) return { hu: false, en: false } value.hu = revision.to if (isEmpty(value.en) || value.en === revision.enWas) { value.en = revision.enTo return { hu: true, en: true } } report.enKept.push(revision.from) return { hu: true, en: false } } function reviseRows(rows: Row[], match: (text: string) => string | undefined): number { let count = 0 for (const row of rows) { const next = typeof row.value === 'string' ? match(row.value) : undefined if (next !== undefined) { row.value = next count += 1 } } return count } function insertRow(rows: Row[], after: string, text: string, newId: () => string): boolean { if (rows.some((row) => row.value === text)) return false const index = rows.findIndex((row) => row.value === after) if (index === -1) return false rows.splice(index + 1, 0, { value: text, id: newId() }) return true } export function reviseContent( doc: Record, paths: readonly LocalizedPath[], revisions: readonly TextRevision[], insertions: readonly ListInsertion[], newId: () => string, ): RevisionReport { const report: RevisionReport = { changed: 0, enKept: [] } let enChanged = false applyAll(doc, paths, (value) => { if (!isPlainObject(value)) return { value, changed: false } if (typeof value.hu === 'string') { const result = reviseText(value, revisions, report) enChanged ||= result.en if (result.hu) report.changed += 1 return { value, changed: result.hu } } if (!Array.isArray(value.hu)) return { value, changed: false } const hu = value.hu as Row[] const en = Array.isArray(value.en) ? (value.en as Row[]) : undefined const huMatched = revisions.filter((r) => hu.some((row) => row.value === r.from)) let count = reviseRows(hu, (text) => revisions.find((r) => r.from === text)?.to) const enCount = en ? reviseRows(en, (text) => revisions.find((r) => r.enWas !== undefined && r.enWas === text)?.enTo) : 0 // A Hungarian row changed but its English row was not the old AI draft. if (en && enCount < huMatched.length) report.enKept.push(...huMatched.map((r) => r.from).slice(enCount)) for (const insertion of insertions) { if (insertRow(hu, insertion.afterHu, insertion.hu, newId)) count += 1 if (en && insertRow(en, insertion.afterEn, insertion.en, newId)) enChanged = true } enChanged ||= enCount > 0 report.changed += count return { value, changed: count > 0 || enCount > 0 } }) if (enChanged && doc.enTranslationStatus === 'approved') doc.enTranslationStatus = 'ai-draft' return report }