// 抽出済みなのに訳が無いものを、全抽出器ぶん集めて一覧にする。
import fs from 'fs';
const tm = JSON.parse(fs.readFileSync('tm.json', 'utf8'));
const ov = JSON.parse(fs.readFileSync('overrides.json', 'utf8'));
const have = s => ov[s] !== undefined || tm[s] !== undefined;

const FILES = [
  ['out-label-safe.json', 'text'],
  ['out-sentence.json', 'text'],
  ['out-fragment-safe.json', 'text'],
  ['out-html-items.json', 'text'],
  ['out2-plain-go.json', 'text'],
  ['out3-go.json', 'text'],
  ['out4-tpl.json', 'text'],
];

const todo = new Map();   // 原文 -> {n, from:Set}
for (const [f, field] of FILES) {
  if (!fs.existsSync(f)) { console.log('  (無い) ' + f); continue; }
  let data = JSON.parse(fs.readFileSync(f, 'utf8'));
  if (!Array.isArray(data)) data = Object.keys(data);
  let n = 0;
  for (const it of data) {
    const s = typeof it === 'string' ? it : (it[field] ?? it.en ?? it.cooked);
    if (typeof s !== 'string' || !s.trim()) continue;
    n++;
    if (have(s)) continue;
    if (!todo.has(s)) todo.set(s, { n: 0, from: new Set() });
    const t = todo.get(s); t.n++; t.from.add(f.replace(/\.json$/, ''));
  }
  console.log(`${f.padEnd(24)} ${n} 件`);
}

// 訳す値打ちのないものを落とす（update.mjs と同じ考え方）
const junk = s =>
  s.includes('${') ||
  /^[A-Za-z0-9_\-]{40,}$/.test(s) || s.includes('eyJ') ||
  /^[#.[]/.test(s) || /:not\(|::|\[data-/.test(s) ||
  /="/.test(s) || /^&|&utm_|&amp;/.test(s) ||
  (!/\s/.test(s) && s.length > 20) ||
  !/[A-Za-z]{2}/.test(s) ||
  (/^[MmLlHhVvCcSsQqTtAaZz][\d\s.,-]/.test(s) && /^[MmLlHhVvCcSsQqTtAaZz\d\s.,-]+$/.test(s)) ||
  ((s.match(/[\d.]/g) || []).length > (s.match(/[A-Za-z]/g) || []).length) ||
  /^https?:|^www\.|creativecommons|^[a-z]+\.[a-z]{2,4}\//i.test(s);

const list = [...todo.entries()].filter(([s]) => !junk(s));
console.log(`\n未訳: ${todo.size} 種、うち訳す値打ちがあるもの ${list.length} 種`);
list.sort((a, b) => a[0].length - b[0].length);
fs.writeFileSync('gap-todo.json', JSON.stringify(list.map(([s]) => s), null, 1));
console.log('\n--- 短いもの 60 件 ---');
list.slice(0, 60).forEach(([s, v]) => console.log(`  ${JSON.stringify(s).slice(0, 70).padEnd(72)} [${[...v.from].join(',')}]`));
console.log('\n--- 長いもの 15 件 ---');
list.slice(-15).forEach(([s, v]) => console.log(`  ${s.slice(0, 100)}`));
