#!/usr/bin/env node
/** Ergänzt fehlende billing/orders-Keys in de-extended aus de. */
const fs = require('fs');
const path = require('path');

const LOCALES_DIR = path.join(__dirname, '../frontend/src/i18n/locales');

function flatten(obj, prefix = '', out = {}) {
  for (const [key, value] of Object.entries(obj || {})) {
    const nextKey = prefix ? `${prefix}.${key}` : key;
    if (value && typeof value === 'object' && !Array.isArray(value)) flatten(value, nextKey, out);
    else if (typeof value === 'string') out[nextKey] = value;
  }
  return out;
}

function deepMerge(base, override) {
  if (!base || typeof base !== 'object' || Array.isArray(base)) return override ?? base;
  const result = { ...base };
  for (const [key, value] of Object.entries(override || {})) {
    if (
      value && typeof value === 'object' && !Array.isArray(value) &&
      result[key] && typeof result[key] === 'object' && !Array.isArray(result[key])
    ) {
      result[key] = deepMerge(result[key], value);
    } else {
      result[key] = value;
    }
  }
  return result;
}

function setByPath(obj, dotPath, value) {
  const parts = dotPath.split('.');
  let cur = obj;
  for (let i = 0; i < parts.length - 1; i++) {
    if (!cur[parts[i]] || typeof cur[parts[i]] !== 'object') cur[parts[i]] = {};
    cur = cur[parts[i]];
  }
  cur[parts[parts.length - 1]] = value;
}

const de = JSON.parse(fs.readFileSync(path.join(LOCALES_DIR, 'de.json'), 'utf8'));
const ext = JSON.parse(fs.readFileSync(path.join(LOCALES_DIR, 'de-extended.json'), 'utf8'));
const deFlat = flatten(de);
const merged = flatten(deepMerge(JSON.parse(JSON.stringify(de)), ext));

const NAMESPACES = ['billing', 'orders'];
let added = 0;
for (const key of Object.keys(deFlat)) {
  if (!NAMESPACES.some((ns) => key.startsWith(`${ns}.`))) continue;
  if (!(key in flatten(ext)) || merged[key] === deFlat[key]) {
    setByPath(ext, key, deFlat[key]);
    merged[key] = deFlat[key];
    added++;
  }
}

fs.writeFileSync(
  path.join(LOCALES_DIR, 'de-extended.json'),
  `${JSON.stringify(ext, null, 2)}\n`,
  'utf8'
);
const stillDe = Object.keys(deFlat).filter((k) => merged[k] === deFlat[k]).length;
console.log(`de-extended: added ${added} keys, stillDe=${stillDe}`);
