"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);

// tools/browser/live.ts
var live_exports = {};
__export(live_exports, {
  LIVE_PRESETS: () => LIVE_PRESETS,
  buildEmbedCode: () => buildEmbedCode,
  buildLiveIframe: () => buildLiveIframe,
  buildLiveUrl: () => buildLiveUrl,
  resolvePreset: () => resolvePreset
});
module.exports = __toCommonJS(live_exports);
var LIVE_PRESETS = {
  /** Read-only monitoring (no interaction) */
  readonly: { interactive: false },
  /** Interactive control (human-in-the-loop) */
  interactive: { interactive: true },
  /** Watch-only without controls */
  monitoring: { interactive: false, showControls: false }
};
function buildLiveUrl(debugUrl, options = {}) {
  if (!debugUrl) {
    throw new Error(
      "debugUrl is required. Ensure your backend returns debugUrl in the task response. Contact support@morphllm.com if you need help."
    );
  }
  const normalized = normalizeLiveUrl(debugUrl);
  const url = new URL(normalized);
  if (options.interactive !== void 0) {
    url.searchParams.set("interactive", String(options.interactive));
  }
  if (options.theme) {
    url.searchParams.set("theme", options.theme);
  }
  if (options.showControls !== void 0) {
    url.searchParams.set("showControls", String(options.showControls));
  }
  if (options.pageId) {
    url.searchParams.set("pageId", options.pageId);
  }
  if (options.pageIndex) {
    url.searchParams.set("pageIndex", options.pageIndex);
  }
  return url.toString();
}
function cdpToHttps(wsUrl) {
  return wsUrl.replace(/^wss:\/\//, "https://").replace(/^ws:\/\//, "http://");
}
function normalizeLiveUrl(debugUrl) {
  const trimmed = debugUrl.trim();
  if (!trimmed) return trimmed;
  if (trimmed.startsWith("wss://") || trimmed.startsWith("ws://")) {
    return `https://live.browser-use.com?wss=${encodeURIComponent(cdpToHttps(trimmed))}`;
  }
  let url;
  try {
    url = new URL(trimmed);
  } catch {
    return trimmed;
  }
  const wssParam = url.searchParams.get("wss");
  if (wssParam && (wssParam.startsWith("wss://") || wssParam.startsWith("ws://"))) {
    url.searchParams.set("wss", cdpToHttps(wssParam));
  }
  return url.toString();
}
function buildLiveIframe(debugUrl, options = {}) {
  const {
    width = "100%",
    height = "600px",
    style = "",
    className = "",
    ...sessionOptions
  } = options;
  const src = buildLiveUrl(debugUrl, sessionOptions);
  const widthStr = typeof width === "number" ? `${width}px` : width;
  const heightStr = typeof height === "number" ? `${height}px` : height;
  const baseStyle = `width: ${widthStr}; height: ${heightStr}; border: none;`;
  const fullStyle = style ? `${baseStyle} ${style}` : baseStyle;
  const attributes = [
    `src="${src}"`,
    `style="${fullStyle}"`
  ];
  if (className) {
    attributes.push(`class="${className}"`);
  }
  return `<iframe ${attributes.join(" ")}></iframe>`;
}
function buildEmbedCode(debugUrl, options = {}) {
  const iframe = buildLiveIframe(debugUrl, options);
  return `<!-- Embed Morph Live Session -->
${iframe}`;
}
function resolvePreset(optionsOrPreset) {
  if (!optionsOrPreset) {
    return {};
  }
  if (typeof optionsOrPreset === "string") {
    const preset = LIVE_PRESETS[optionsOrPreset];
    if (!preset) {
      throw new Error(
        `Unknown preset: ${optionsOrPreset}. Available presets: ${Object.keys(LIVE_PRESETS).join(", ")}`
      );
    }
    return preset;
  }
  return optionsOrPreset;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  LIVE_PRESETS,
  buildEmbedCode,
  buildLiveIframe,
  buildLiveUrl,
  resolvePreset
});
//# sourceMappingURL=live.cjs.map