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

// lib/launcher/index.ts
var launcher_exports = {};
__export(launcher_exports, {
  launchInspector: () => launchInspector
});
module.exports = __toCommonJS(launcher_exports);

// ../node_modules/tsup/assets/cjs_shims.js
var getImportMetaUrl = () => typeof document === "undefined" ? new URL(`file:${__filename}`).href : document.currentScript && document.currentScript.tagName.toUpperCase() === "SCRIPT" ? document.currentScript.src : new URL("main.js", document.baseURI).href;
var importMetaUrl = /* @__PURE__ */ getImportMetaUrl();

// lib/launcher/index.ts
var import_child_process = require("child_process");
var import_url = require("url");
var import_path = __toESM(require("path"), 1);
async function waitForServer(port, timeout = 3e4) {
  const start = Date.now();
  while (Date.now() - start < timeout) {
    try {
      const res = await fetch(`http://localhost:${port}/health`);
      if (res.ok) return;
    } catch {
    }
    await new Promise((r) => setTimeout(r, 100));
  }
  throw new Error(`Inspector failed to start within ${timeout}ms`);
}
var INSPECTOR_PORT = 6274;
async function launchInspector(options = {}) {
  const __dirname = import_path.default.dirname((0, import_url.fileURLToPath)(importMetaUrl));
  const binPath = import_path.default.resolve(__dirname, "../../../bin/start.js");
  const env = {
    ...Object.fromEntries(
      Object.entries(process.env).filter(([, v]) => v !== void 0)
    )
  };
  if (options.server) {
    const config = {
      mcpServers: {
        [options.server.name]: {
          url: options.server.url,
          ...options.server.headers && { headers: options.server.headers },
          ...options.server.useOAuth && { useOAuth: true }
        }
      }
    };
    env.MCP_CONFIG_DATA = JSON.stringify(config);
    env.MCP_AUTO_CONNECT_SERVER = options.server.name;
  }
  if (options.defaultTab) {
    const tab = options.defaultTab === "chat" ? "chat-v2" : options.defaultTab;
    env.MCP_INITIAL_TAB = tab;
  }
  if (options.verbose) {
    env.VERBOSE_LOGS = "true";
  }
  if (options.cspMode) {
    env.MCP_CSP_MODE = options.cspMode;
  }
  const child = (0, import_child_process.spawn)("node", [binPath], {
    env,
    stdio: "inherit",
    detached: false
  });
  await new Promise((resolve, reject) => {
    let resolved = false;
    child.on("error", (err) => {
      if (!resolved) {
        resolved = true;
        reject(new Error(`Failed to spawn inspector process: ${err.message}`));
      }
    });
    waitForServer(INSPECTOR_PORT).then(() => {
      if (!resolved) {
        resolved = true;
        resolve();
      }
    }).catch((err) => {
      if (!resolved) {
        resolved = true;
        reject(err);
      }
    });
  });
  const tabHash = options.defaultTab ? `#${options.defaultTab === "chat" ? "chat-v2" : options.defaultTab}` : "";
  const url = `http://localhost:${INSPECTOR_PORT}${tabHash}`;
  return {
    url,
    process: child,
    stop: async () => {
      child.kill("SIGTERM");
      await new Promise((resolve) => {
        const timeout = setTimeout(() => {
          child.kill("SIGKILL");
          resolve();
        }, 5e3);
        child.on("exit", () => {
          clearTimeout(timeout);
          resolve();
        });
      });
    }
  };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  launchInspector
});
//# sourceMappingURL=index.cjs.map