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

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

  const forms = await p.evaluate(() => {
    const formElements = Array.from(document.querySelectorAll('form'));

    return formElements.map(form => {
      const inputs = Array.from(form.querySelectorAll('input, textarea, select'));

      return {
        action: form.action || null,
        method: form.method || 'GET',
        fields: inputs.map(input => ({
          name: input.name || null,
          type: input.type || input.tagName.toLowerCase(),
          placeholder: input.placeholder || '',
          id: input.id || null
        }))
      };
    });
  });

  return {
    success: true,
    action: "get_forms",
    count: forms.length,
    forms
  };
};
