const { chromium } = require('playwright');
(async () => {
  const url = 'https://farmer.taild3aaf6.ts.net/fea/primeras-lecturas/';
  const b = await chromium.launch({ args: ['--ignore-certificate-errors'] });
  const p = await b.newPage({ viewport: { width: 1440, height: 1600 }, ignoreHTTPSErrors: true, deviceScaleFactor: 2 });
  await p.goto(url, { waitUntil: 'networkidle' });

  // medidas: ancho del contenedor, ancho del viewport, alineación del título
  const m1 = await p.evaluate(() => {
    const lect = document.querySelector('.fea-lect');
    const title = document.querySelector('.wp-block-post-title');
    const ec = document.querySelector('.entry-content');
    return {
      viewport: window.innerWidth,
      fea_lect_width: lect ? Math.round(lect.getBoundingClientRect().width) : null,
      entry_content_width: ec ? Math.round(ec.getBoundingClientRect().width) : null,
      title_text: title ? title.textContent.trim() : null,
      title_align: title ? getComputedStyle(title).textAlign : null,
      title_centered_box: title ? (Math.abs((title.getBoundingClientRect().left) - (window.innerWidth - title.getBoundingClientRect().right)) < 8) : null,
    };
  });
  console.log('COLAPSADO:', JSON.stringify(m1, null, 2));
  await p.screenshot({ path: '/tmp/lecturas_collapsed.png', fullPage: false });

  // abrir la 1ª sección (1ª lectura) y el primer libro
  await p.evaluate(() => {
    const tops = document.querySelectorAll('.fea-lect > details.fea-lect-top');
    if (tops[0]) tops[0].open = true;
  });
  await p.waitForTimeout(150);
  await p.evaluate(() => {
    const first = document.querySelector('.fea-lect > details.fea-lect-top details');
    if (first) first.open = true;
  });
  await p.waitForTimeout(150);

  const m2 = await p.evaluate(() => {
    const ul = document.querySelector('.fea-lect details details[open] ul');
    return {
      ul_columns: ul ? getComputedStyle(ul).columnWidth + ' / count ' + getComputedStyle(ul).columnCount : null,
      ul_width: ul ? Math.round(ul.getBoundingClientRect().width) : null,
    };
  });
  console.log('EXPANDIDO:', JSON.stringify(m2, null, 2));
  await p.screenshot({ path: '/tmp/lecturas_expanded.png', fullPage: false });

  await b.close();
})();
