// 「贈り物」の説明文を、語彙表が実際に組み立てた形で全部並べる。
// 入会画面のツールチップと同じ文字列なので、ここが正しければ画面も正しい。
import fs from 'fs';
import { spawn } from 'child_process';

const CHROME = 'C:/Users/ADMINI~1/AppData/Local/Temp/claude/c--Users-Administrator/70d0da47-d68c-407f-8f8d-40faa36e11a5/scratchpad/lowmoor';
const EXE = 'C:/Program Files/Google/Chrome/Application/chrome.exe';
const PORT = 9499, profile = 'C:/Users/ADMINI~1/AppData/Local/Temp/lm-gift';
fs.rmSync(profile, { recursive: true, force: true });
const chrome = spawn(EXE, ['--headless=new', '--disable-gpu', '--no-sandbox', '--no-first-run',
  '--remote-debugging-port=' + PORT, '--user-data-dir=' + profile, '--window-size=1200,900', 'about:blank'], { stdio: 'ignore' });
const sleep = ms => new Promise(r => setTimeout(r, ms));
let list = null;
for (let i = 0; i < 40; i++) { try { list = await (await fetch(`http://127.0.0.1:${PORT}/json/list`)).json(); if (list.length) break; } catch { } await sleep(300); }
const ws = new WebSocket(list.find(t => t.type === 'page').webSocketDebuggerUrl);
await new Promise(r => ws.addEventListener('open', r));
let id = 0; const pend = new Map();
ws.addEventListener('message', e => { const m = JSON.parse(e.data); if (m.id && pend.has(m.id)) { pend.get(m.id)(m.result); pend.delete(m.id); } });
const send = (m, p = {}) => new Promise(res => { const i = ++id; pend.set(i, res); ws.send(JSON.stringify({ id: i, method: m, params: p })); });
const ev = async x => (await send('Runtime.evaluate', { expression: x, returnByValue: true, awaitPromise: true })).result?.value;

await send('Page.enable'); await send('Runtime.enable');
await send('Page.navigate', { url: 'file:///' + process.cwd().replace(/\\/g, '/') + '/build/index-test.html' });
await sleep(3500);

for (const plain of [false, true]) {
  const r = await ev(`(()=>{
    plainWords = ${plain}; refreshMechText();
    const out = {folk:{}, trade:{}, origin:{}, procs:{}, levers:[]};
    for (const n of FOLK)    out.folk[n]   = giftNote(n);
    for (const n of TRADES)  out.trade[n]  = giftNote(n);
    for (const n of ORIGINS) out.origin[n] = giftNote(n);
    for (const s of ["sight","crit","travel","mend","undead","rarity","ward","fury"])
      out.procs[s] = mechLine("procs:"+s, 1.15);
    // 代表的なレバーを一通り
    for (const [k,v] of [["carry",2],["carry",1],["hp",6],["mp",4],["crit",0.03],
                         ["wound",1],["wound",3],["luck",0.5],["rarity",0.9],
                         ["startGold",40],["startSpell",1],["startSpell",2],
                         ["travel",0.92],["ill",0.8],["procs",1.15],["attack",1.1],
                         ["hpKnit",1.3],["mpWell",1.25],["xp",1.04],["gold",1.04]])
      out.levers.push(k+"("+v+") = "+mechLine(k,v));
    return out;
  })()`);
  console.log('\\n########## ' + (plain ? 'Simplicity ON（平明）' : 'Simplicity OFF（世界の言葉）') + ' ##########');
  console.log('--- 学派ごとの発動 ---');
  for (const [k, v] of Object.entries(r.procs)) console.log('  ' + k.padEnd(8), v);
  console.log('--- レバー ---');
  r.levers.forEach(l => console.log('  ' + l));
  console.log('--- 種族の贈り物 ---');
  for (const [k, v] of Object.entries(r.folk)) console.log('  ' + k.padEnd(24), v);
  console.log('--- 職業の贈り物 ---');
  for (const [k, v] of Object.entries(r.trade)) console.log('  ' + k.padEnd(24), v);
  console.log('--- 出自の贈り物 ---');
  for (const [k, v] of Object.entries(r.origin)) console.log('  ' + k.padEnd(24), v);
}
ws.close(); chrome.kill();
