"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var extensionFrames_exports = {};
__export(extensionFrames_exports, {
  enumerateExtensionFrames: () => enumerateExtensionFrames
});
module.exports = __toCommonJS(extensionFrames_exports);
const EXTENSION_URL_PREFIX = "chrome-extension://";
function urlMatchOf(url) {
  try {
    const parsed = new URL(url);
    const pathname = parsed.pathname.replace(/^\/+/, "");
    if (pathname.startsWith("popup/"))
      return "popup/";
    return pathname || url;
  } catch {
    return url;
  }
}
function seqToSuffix(seq) {
  let n = seq;
  let out = "";
  while (n > 0) {
    const r = (n - 1) % 26;
    out = String.fromCharCode(97 + r) + out;
    n = Math.floor((n - 1) / 26);
  }
  return out || "a";
}
function exprFor(entry) {
  const escape = (s) => s.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
  switch (entry.strategy) {
    case "testid":
      return `getByTestId('${escape(entry.payload)}')`;
    case "role-name":
      return `getByRole('${escape(entry.secondaryRole || "button")}', { name: '${escape(entry.payload)}' })`;
    case "role":
      return `getByRole('${escape(entry.secondaryRole || entry.payload)}')`;
    case "text":
      return `getByText('${escape(entry.payload)}')`;
  }
}
function locatorFor(frame, entry) {
  switch (entry.strategy) {
    case "testid":
      return frame.getByTestId(entry.payload);
    case "role-name":
      return frame.getByRole(
        entry.secondaryRole || "button",
        { name: entry.payload }
      );
    case "role":
      return frame.getByRole(
        entry.secondaryRole || entry.payload
      );
    case "text":
      return frame.getByText(entry.payload);
  }
}
async function enumerateExtensionFrames(page) {
  const lines = [];
  const refMap = /* @__PURE__ */ new Map();
  const extFrames = page.frames().filter((f) => f.url().startsWith(EXTENSION_URL_PREFIX));
  if (!extFrames.length)
    return { lines, refMap };
  for (let i = 0; i < extFrames.length; i++) {
    const frame = extFrames[i];
    const frameIdx = i + 1;
    const url = frame.url();
    let entries = [];
    try {
      entries = await frame.evaluate(walkerSource);
    } catch {
      continue;
    }
    if (!entries.length)
      continue;
    const urlMatch = urlMatchOf(url);
    lines.push(`- extension iframe "${urlMatch}" [frameUrl=${url}]:`);
    for (const entry of entries) {
      const ref = `ef${frameIdx}-${seqToSuffix(entry.seq)}`;
      const expr = exprFor(entry);
      const locator = locatorFor(frame, entry);
      const nameDisplay = entry.strategy === "role-name" || entry.strategy === "text" ? ` "${entry.payload}"` : "";
      lines.push(`  - ${entry.role}${nameDisplay} [ref=${ref}]`);
      refMap.set(ref, { frame, urlMatch, expr, locator });
    }
  }
  return { lines, refMap };
}
const walkerSource = () => {
  const entries = [];
  let seq = 1;
  const visible = (el) => {
    const style = el.ownerDocument?.defaultView?.getComputedStyle(el) || null;
    if (!style) return true;
    if (style.visibility === "hidden" || style.display === "none") return false;
    const rect = el.getBoundingClientRect?.();
    return !rect || rect.width > 0 && rect.height > 0;
  };
  const accessibleName = (el) => {
    const aria = (el.getAttribute("aria-label") || "").trim();
    if (aria) return aria;
    const text = (el.textContent || "").replace(/\s+/g, " ").trim();
    if (text.length && text.length < 80) return text;
    const ph = (el.getAttribute("placeholder") || "").trim();
    if (ph) return ph;
    const title = (el.getAttribute("title") || "").trim();
    if (title) return title;
    return "";
  };
  const push = (role, el) => {
    if (!visible(el)) return;
    const testid = el.getAttribute("data-testid") || el.getAttribute("data-test-id");
    if (testid) {
      entries.push({
        seq: seq++,
        role,
        name: testid,
        strategy: "testid",
        payload: testid
      });
      return;
    }
    const name = accessibleName(el);
    if (name) {
      entries.push({
        seq: seq++,
        role,
        name,
        strategy: "role-name",
        payload: name,
        secondaryRole: role
      });
      return;
    }
    entries.push({
      seq: seq++,
      role,
      name: "",
      strategy: "role",
      payload: role
    });
  };
  const BUTTONS = 'button, [role="button"], input[type="button"], input[type="submit"]';
  const TEXTBOXES = 'input[type="text"], input[type="email"], input[type="password"], input:not([type]), textarea';
  const LINKS = 'a[href], [role="link"]';
  const CHECKBOXES = 'input[type="checkbox"], [role="checkbox"]';
  const RADIOS = 'input[type="radio"], [role="radio"]';
  const TABS = '[role="tab"]';
  const HEADINGS = 'h1, h2, h3, [role="heading"]';
  for (const el of document.querySelectorAll(BUTTONS)) push("button", el);
  for (const el of document.querySelectorAll(TEXTBOXES)) push("textbox", el);
  for (const el of document.querySelectorAll(LINKS)) push("link", el);
  for (const el of document.querySelectorAll(CHECKBOXES)) push("checkbox", el);
  for (const el of document.querySelectorAll(RADIOS)) push("radio", el);
  for (const el of document.querySelectorAll(TABS)) push("tab", el);
  for (const el of document.querySelectorAll(HEADINGS)) push("heading", el);
  return entries.slice(0, 100);
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  enumerateExtensionFrames
});
