// scripts/i18n-audit.ts — repère les valeurs de fr.json identiques à en.json
// (candidat "anglais non traduit"), hors allowlist. Sortie : rapport + exit 1
// si des non-traduits hors allowlist subsistent (utilisable en CI plus tard).
import en from "../messages/en.json";
import fr from "../messages/fr.json";
import {
  ALLOWED_IDENTICAL_KEYS,
  ALLOWED_IDENTICAL_VALUES,
} from "../src/i18n/untranslated-allowlist";

type Flat = Record<string, string>;
function flatten(obj: unknown, prefix = "", out: Flat = {}): Flat {
  if (obj && typeof obj === "object") {
    for (const [k, v] of Object.entries(obj as Record<string, unknown>)) {
      flatten(v, prefix ? `${prefix}.${k}` : k, out);
    }
  } else if (typeof obj === "string") {
    out[prefix] = obj;
  }
  return out;
}

const enFlat = flatten(en);
const frFlat = flatten(fr);
const suspects: string[] = [];
for (const [key, enVal] of Object.entries(enFlat)) {
  const frVal = frFlat[key];
  if (frVal === undefined) continue; // parité gérée par catalogs.test.ts
  if (frVal !== enVal) continue; // traduit
  if (ALLOWED_IDENTICAL_VALUES.has(frVal.trim())) continue;
  if (ALLOWED_IDENTICAL_KEYS.has(key)) continue;
  suspects.push(`${key} = ${JSON.stringify(frVal)}`);
}
if (suspects.length) {
  console.log(
    `\n${suspects.length} valeur(s) FR non traduite(s) (hors allowlist) :`,
  );
  for (const s of suspects) console.log(`  ${s}`);
  process.exit(1);
}
console.log("Aucune valeur FR non traduite (hors allowlist).");
