"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var exportTool_exports = {};
__export(exportTool_exports, {
  createExportZipHandler: () => createExportZipHandler,
  exportZipMcpTool: () => exportZipMcpTool,
  exportZipSchema: () => exportZipSchema
});
module.exports = __toCommonJS(exportTool_exports);
var import_path = __toESM(require("path"));
var import_fs = __toESM(require("fs"));
var import_mcpBundle = require("playwright-core/lib/mcpBundle");
var import_skyRampExport = require("../test/skyRampExport");
var import_tool = require("../sdk/tool");
const exportZipSchema = {
  name: "skyramp_export_zip",
  title: "Export Skyramp zip",
  description: [
    "Export the recorded browser interactions as a Skyramp zip (JSONL + HAR).",
    "Pass outputPath as the absolute path for the zip \u2014 use the same directory and base name as the test file, replacing .spec.ts with .zip. If the path already exists, a unique suffix is appended automatically (e.g. _1, _2) \u2014 the actual path written is returned in the result.",
    "BEFORE calling this tool, output a <thinking> block that: (1) lists every user-requested step and confirms it was completed, (2) confirms no step was skipped or hallucinated.",
    "Only call this tool when all interactions are fully complete. Do NOT ask the user for confirmation \u2014 call it automatically.",
    "Only the last complete attempt is exported \u2014 retries from the start URL are deduplicated automatically.",
    "Do NOT reuse zip files from previous sessions \u2014 always record fresh."
  ].join(" "),
  inputSchema: import_mcpBundle.z.object({
    outputPath: import_mcpBundle.z.string().describe("Absolute path where the zip should be written, e.g. /path/to/box_notes.zip. Use the same directory and base name as the test file, replacing .spec.ts with .zip.")
  }),
  type: "readOnly"
};
function createExportZipHandler(ctx) {
  return async (params) => {
    if (!ctx.trackedActions.length) {
      return {
        content: [{ type: "text", text: "### Error\nNo browser interactions recorded. At minimum, call browser_navigate to open the target URL, then use browser_click or browser_type to record at least one user action before exporting." }],
        isError: true
      };
    }
    let outputZip = import_path.default.resolve(params.outputPath);
    if (ctx.outputDir) {
      const resolvedOutputDir = import_path.default.resolve(ctx.outputDir);
      if (!outputZip.startsWith(resolvedOutputDir + import_path.default.sep) && outputZip !== resolvedOutputDir) {
        return {
          content: [{ type: "text", text: `### Error
Output path must be within the configured output directory: ${resolvedOutputDir}
Requested path: ${outputZip}` }],
          isError: true
        };
      }
    }
    if (import_fs.default.existsSync(outputZip)) {
      const ext = import_path.default.extname(outputZip);
      const base = outputZip.slice(0, -ext.length);
      let counter = 1;
      while (import_fs.default.existsSync(`${base}_${counter}${ext}`))
        counter++;
      outputZip = `${base}_${counter}${ext}`;
    }
    const { jsonl: jsonlContent, actionCount, skipped } = (0, import_skyRampExport.buildJsonlContent)(ctx.trackedActions, "chromium", ctx.harPath);
    await (0, import_skyRampExport.writeSkyrampZip)(outputZip, jsonlContent, ctx.harPath);
    const skippedNote = skipped.length ? `
Skipped (not exportable): ${[...new Set(skipped)].join(", ")}` : "";
    return {
      content: [{
        type: "text",
        text: `### Result
Skyramp zip written to ${outputZip}

Contains:
- skyramp_playwright.txt (${actionCount} actions)${skippedNote}`
      }]
    };
  };
}
function exportZipMcpTool() {
  return (0, import_tool.toMcpTool)(exportZipSchema);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  createExportZipHandler,
  exportZipMcpTool,
  exportZipSchema
});
