import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

const __dirname = path.dirname(fileURLToPath(import.meta.url));
const root = path.join(__dirname, '..');

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

function extractKeysFromDir(dir) {
  const keys = new Set();
  const re = /\bt\s*\(\s*['"`]([^'"`]+)['"`]/g;
  function walk(d) {
    for (const ent of fs.readdirSync(d, { withFileTypes: true })) {
      const p = path.join(d, ent.name);
      if (ent.isDirectory()) walk(p);
      else if (ent.name.endsWith('.vue')) {
        const src = fs.readFileSync(p, 'utf8');
        let m;
        while ((m = re.exec(src))) keys.add(m[1]);
      }
    }
  }
  walk(dir);
  return keys;
}

const zhMod = await import('../src/i18n/zh-CN.ts');
const zh = flatten(zhMod.default);
const dirs = ['src/views/desktop', 'src/components/desktop'].map((d) => path.join(root, d));
const used = new Set();
for (const d of dirs) extractKeysFromDir(d).forEach((k) => used.add(k));

const missing = [...used].filter((k) => !(k in zh)).sort();
console.log(`Missing keys (${missing.length}):`);
missing.forEach((k) => console.log(`  ${k}`));
