"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 widgetContract_exports = {};
__export(widgetContract_exports, {
  InferredWidgetContractSchema: () => InferredWidgetContractSchema,
  WidgetContractSchema: () => WidgetContractSchema,
  cacheInferredContract: () => cacheInferredContract,
  lookupContract: () => lookupContract,
  validateContract: () => validateContract
});
module.exports = __toCommonJS(widgetContract_exports);
var import_mcpBundle = require("playwright-core/lib/mcpBundle");
var import_curatedWidgets = require("./curatedWidgets");
const ClickStepSchema = import_mcpBundle.z.object({
  kind: import_mcpBundle.z.literal("click"),
  target: import_mcpBundle.z.enum(["trigger", "option"]),
  matchBy: import_mcpBundle.z.enum(["accessibleName", "role"]).optional(),
  param: import_mcpBundle.z.string().optional()
});
const FillStepSchema = import_mcpBundle.z.discriminatedUnion("target", [
  import_mcpBundle.z.object({ kind: import_mcpBundle.z.literal("fill"), target: import_mcpBundle.z.literal("activeInput"), param: import_mcpBundle.z.string() }),
  import_mcpBundle.z.object({ kind: import_mcpBundle.z.literal("fill"), target: import_mcpBundle.z.literal("inputByLabel"), label: import_mcpBundle.z.string(), param: import_mcpBundle.z.string() })
]);
const WaitForStepSchema = import_mcpBundle.z.object({
  kind: import_mcpBundle.z.literal("waitFor"),
  location: import_mcpBundle.z.enum(["inDom", "portal", "frame"]),
  signal: import_mcpBundle.z.enum(["visible", "present"])
});
const SelectOptionStepSchema = import_mcpBundle.z.object({
  kind: import_mcpBundle.z.literal("selectOption"),
  by: import_mcpBundle.z.enum(["accessibleName", "index"]),
  param: import_mcpBundle.z.string()
});
const EnterFrameStepSchema = import_mcpBundle.z.object({
  kind: import_mcpBundle.z.literal("enterFrame"),
  framePath: import_mcpBundle.z.array(import_mcpBundle.z.string())
});
const ExitFrameStepSchema = import_mcpBundle.z.object({ kind: import_mcpBundle.z.literal("exitFrame") });
const HandoffStepSchema = import_mcpBundle.z.object({ kind: import_mcpBundle.z.literal("handoff") });
const ContractStepSchema = import_mcpBundle.z.union([
  ClickStepSchema,
  FillStepSchema,
  WaitForStepSchema,
  SelectOptionStepSchema,
  EnterFrameStepSchema,
  ExitFrameStepSchema,
  HandoffStepSchema
]);
const WidgetContractSchema = import_mcpBundle.z.object({
  source: import_mcpBundle.z.enum(["curated", "inferred", "unknown"]),
  confidence: import_mcpBundle.z.enum(["high", "medium", "low"]),
  widgetIdentification: import_mcpBundle.z.string().max(100),
  parameters: import_mcpBundle.z.array(import_mcpBundle.z.string()),
  steps: import_mcpBundle.z.array(ContractStepSchema)
});
const InferredWidgetContractSchema = import_mcpBundle.z.object({
  source: import_mcpBundle.z.enum(["inferred", "unknown"]),
  confidence: import_mcpBundle.z.enum(["high", "medium", "low"]),
  widgetIdentification: import_mcpBundle.z.string().max(100),
  parameters: import_mcpBundle.z.array(import_mcpBundle.z.string()),
  steps: import_mcpBundle.z.array(ContractStepSchema)
});
function validateContract(contract) {
  const params = new Set(contract.parameters);
  let enterFrameCount = 0;
  let sawWaitFor = false;
  for (const step of contract.steps) {
    const stepParam = step.param;
    if (stepParam !== void 0 && !params.has(stepParam))
      throw new Error(`step param '${stepParam}' not declared in parameters[]`);
    if (step.kind === "enterFrame") {
      if (step.framePath.length === 0)
        throw new Error("EnterFrameStep.framePath must be non-empty");
      enterFrameCount++;
      if (enterFrameCount > 1)
        throw new Error("at most one EnterFrameStep per contract (provisional limit)");
    }
    if (step.kind === "waitFor") sawWaitFor = true;
    if (step.kind === "handoff" && !sawWaitFor) {
      console.warn("widgetContract: HandoffStep not preceded by WaitForStep \u2014 agent may act on pre-widget-open DOM state");
    }
  }
}
function lookupContract(fingerprint, inferredCache) {
  const curated = import_curatedWidgets.CURATED[fingerprint];
  if (curated) return { status: "found", contract: curated };
  const inferred = inferredCache.get(fingerprint);
  if (inferred) return { status: "found", contract: inferred };
  return { status: "needs_inference_marker" };
}
function cacheInferredContract(fingerprint, contract, inferredCache) {
  if (Object.prototype.hasOwnProperty.call(import_curatedWidgets.CURATED, fingerprint))
    return { ok: false, error: "fingerprint collides with curated entry; cannot overwrite" };
  if (contract.source === "curated")
    return { ok: false, error: "source: 'curated' is reserved for the curated library; use 'inferred' or 'unknown'" };
  try {
    validateContract(contract);
  } catch (e) {
    return { ok: false, error: e.message };
  }
  inferredCache.set(fingerprint, contract);
  return { ok: true };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  InferredWidgetContractSchema,
  WidgetContractSchema,
  cacheInferredContract,
  lookupContract,
  validateContract
});
