const browser = require('../browser');

module.exports = async ({ selector = 'a' }) => {
  await browser.start();
  const p = await browser.ensurePage();

  const links = await p.evaluate((sel) => {
    const elements = Array.from(document.querySelectorAll(sel));

    return elements.map(el => ({
      text: (el.innerText || el.textContent || '').trim(),
      href: el.href || el.getAttribute('href') || ''
    })).filter(link => link.href && link.href !== '');
  }, selector);

  return {
    success: true,
    action: "get_links",
    count: links.length,
    links
  };
};
