const puppeteer = require('puppeteer-core');
const path = require('path');
const fs = require('fs');

const CHROME = 'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe';
const SVG    = fs.readFileSync(path.join(__dirname, 'favicon.svg'), 'utf8');

const sizes = [
  { name: 'favicon-16x16.png',    px: 16  },
  { name: 'favicon-32x32.png',    px: 32  },
  { name: 'apple-touch-icon.png', px: 180 },
];

(async () => {
  const browser = await puppeteer.launch({
    executablePath: CHROME,
    headless: 'new',
    args: ['--no-sandbox'],
  });

  const page = await browser.newPage();

  for (const { name, px } of sizes) {
    await page.setViewport({ width: px, height: px, deviceScaleFactor: 1 });
    await page.setContent(`<!DOCTYPE html>
<html><head><style>
  *{margin:0;padding:0;background:transparent}
  body{width:${px}px;height:${px}px;overflow:hidden}
  img{width:${px}px;height:${px}px}
</style></head><body>
<img src="data:image/svg+xml;base64,${Buffer.from(SVG).toString('base64')}">
</body></html>`);
    await page.waitForNetworkIdle();
    const out = path.join(__dirname, name);
    await page.screenshot({ path: out, clip: { x: 0, y: 0, width: px, height: px }, omitBackground: true });
    console.log(`OK  ${name} (${px}x${px})`);
  }

  await browser.close();
})();
