import puppeteer from 'puppeteer';
const url = 'file://' + process.argv[2];
const out = process.argv[3];
const net = process.argv[4] || '5';
const b = await puppeteer.launch({ headless: 'new', args: ['--no-sandbox'] });
const p = await b.newPage();
await p.setViewport({ width: 1100, height: 900, deviceScaleFactor: 2 });
const errs = [];
p.on('pageerror', e => errs.push(String(e)));
await p.goto(url, { waitUntil: 'networkidle0' });
await new Promise(r => setTimeout(r, 500));

const before = await p.evaluate(() => {
  const svg = document.querySelector('#svgbox svg');
  return { hasSvg: !!svg, zones: svg ? svg.querySelectorAll('.ov-zone[data-net]').length : 0 };
});
console.log('MOUNT', JSON.stringify(before));

await p.evaluate((n) => window.AdomPcb.highlightNet(n), net);
await new Promise(r => setTimeout(r, 300));

const after = await p.evaluate((n) => {
  const svg = document.querySelector('#svgbox svg');
  const hlZones = [...svg.querySelectorAll('.ov-zone.hl')];
  const cs = hlZones.map((z) => {
    const s = getComputedStyle(z);
    return { net: z.getAttribute('data-net'), fill: s.fill, fillOpacity: s.fillOpacity };
  });
  return {
    hlTotal: svg.querySelectorAll('.hl').length,
    hlZones: hlZones.length,
    zoneStyles: cs.slice(0, 4),
    zonesOnNet: svg.querySelectorAll('.ov-zone[data-net="' + n + '"]').length,
  };
}, net);
console.log('AFTER_HL', JSON.stringify(after, null, 1));
console.log('PAGE_ERRORS', JSON.stringify(errs));
await p.screenshot({ path: out });
await b.close();
console.log('shot ->', out);