"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 possibleAssertions_exports = {};
__export(possibleAssertions_exports, {
  buildFullCaptureAssertions: () => buildFullCaptureAssertions,
  buildPossibleAssertions: () => buildPossibleAssertions
});
module.exports = __toCommonJS(possibleAssertions_exports);
const INTERACTIVE_ROLES = /* @__PURE__ */ new Set([
  "button",
  "link",
  "textbox",
  "combobox",
  "tab",
  "menuitem",
  "switch",
  "checkbox",
  "radio",
  "option",
  "searchbox",
  "dialog",
  "alertdialog"
]);
const LIVE_REGION_ROLES = /* @__PURE__ */ new Set([
  "alert",
  "status",
  "log",
  "marquee",
  "timer"
]);
function escapeForSingleQuote(s) {
  return s.replace(/\\/g, "\\\\").replace(/'/g, "\\'").replace(/[\r\n]+/g, " ");
}
function truncateForDisplay(s, max = 80) {
  if (s.length <= max) return s;
  return s.slice(0, max - 1) + "\u2026";
}
function buildPossibleAssertions(delta) {
  const results = [];
  for (const el of delta.elementsAdded) {
    if (!el.accessibleName.trim()) {
      continue;
    }
    const escapedName = escapeForSingleQuote(el.accessibleName);
    const code = `await expect(page.getByRole('${el.role}', { name: '${escapedName}' })).toBeVisible();`;
    let rationale;
    let tier;
    const displayName = truncateForDisplay(el.accessibleName);
    if (INTERACTIVE_ROLES.has(el.role)) {
      rationale = `Element added to DOM after action: ${el.role} "${displayName}"`;
      tier = "MEDIUM";
    } else if (el.role === "heading") {
      rationale = `Heading appeared after action: "${displayName}"`;
      tier = "MEDIUM";
    } else if (LIVE_REGION_ROLES.has(el.role)) {
      rationale = `${el.role} live region appeared: "${displayName}"`;
      tier = "LOW";
    } else {
      rationale = `${el.role} appeared after action: "${displayName}"`;
      tier = "LOW";
    }
    results.push({ code, rationale, tier });
  }
  for (const el of delta.elementsRemoved) {
    if (!el.accessibleName.trim()) continue;
    const escapedName = escapeForSingleQuote(el.accessibleName);
    const code = `await expect(page.getByRole('${el.role}', { name: '${escapedName}' })).not.toBeVisible();`;
    const tier = INTERACTIVE_ROLES.has(el.role) ? "MEDIUM" : "LOW";
    results.push({
      code,
      rationale: `Element removed from DOM after action: ${el.role} "${truncateForDisplay(el.accessibleName)}"`,
      tier
    });
  }
  for (const tc of delta.textChanges) {
    if (!tc.before.trim() && !tc.after.trim()) continue;
    if (!tc.accessibleName.trim()) continue;
    const escapedAccessibleName = escapeForSingleQuote(tc.accessibleName);
    const escapedAfter = escapeForSingleQuote(tc.after);
    const code = `await expect(page.getByRole('${tc.role}', { name: '${escapedAccessibleName}' })).toHaveText('${escapedAfter}');`;
    results.push({
      code,
      rationale: `Text changed: "${truncateForDisplay(tc.before)}" \u2192 "${truncateForDisplay(tc.after)}"`,
      tier: "HIGH"
    });
  }
  for (const rc of delta.repeatingCountChanges) {
    if (rc.before === rc.after) continue;
    if (!rc.accessibleNameTemplate.trim()) continue;
    const regexExpr = templateToRegex(rc.accessibleNameTemplate);
    const code = `await expect(page.getByRole('${rc.role}', { name: ${regexExpr} })).toHaveCount(${rc.after});`;
    results.push({
      code,
      rationale: `Repeating element count changed: ${rc.before} \u2192 ${rc.after}`,
      tier: "HIGH"
    });
  }
  const tierOrder = { HIGH: 0, MEDIUM: 1, LOW: 2 };
  results.sort((a, b) => tierOrder[a.tier] - tierOrder[b.tier]);
  return results;
}
function buildFullCaptureAssertions(blueprint) {
  const out = [];
  if (blueprint.url && blueprint.url.trim()) {
    const escapedUrl = escapeForSingleQuote(blueprint.url);
    out.push({
      code: `await expect(page).toHaveURL('${escapedUrl}');`,
      rationale: `Page URL after action: ${blueprint.url}`,
      tier: "HIGH"
    });
  }
  const heading = findFirstHeading(blueprint);
  if (heading) {
    const escapedName = escapeForSingleQuote(heading);
    out.push({
      code: `await expect(page.getByRole('heading', { name: '${escapedName}' })).toBeVisible();`,
      rationale: `Heading visible on destination page: "${heading}"`,
      tier: "LOW"
    });
  }
  return out;
}
function findFirstHeading(blueprint) {
  for (const section of blueprint.sections) {
    for (const el of section.elements) {
      if (el.role === "heading" && el.accessibleName.trim())
        return el.accessibleName;
    }
  }
  return null;
}
function templateToRegex(template) {
  let pattern = template.replace(/[.+*?^$()|[\]\\\/]/g, "\\$&");
  pattern = pattern.replace(/\{[a-zA-Z0-9_]+\}/g, ".+");
  return `new RegExp('${escapeForSingleQuote(`^${pattern}$`)}', 'i')`;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  buildFullCaptureAssertions,
  buildPossibleAssertions
});
