"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 McpCustomWizard_exports = {};
__export(McpCustomWizard_exports, {
  default: () => McpCustomWizard_default
});
module.exports = __toCommonJS(McpCustomWizard_exports);
var import_jsx_runtime = require("react/jsx-runtime");
var import_react = require("react");
var import_ink = require("ink");
const STDIO_STEPS = [
  {
    field: "name",
    label: "Server Name",
    placeholder: "e.g., my-server",
    required: true,
    validate: (v) => v.trim() ? null : "Name is required"
  },
  {
    field: "command",
    label: "Command",
    placeholder: "e.g., npx, uvx, node, python",
    required: true,
    validate: (v) => v.trim() ? null : "Command is required"
  },
  {
    field: "args",
    label: "Arguments (space-separated, optional)",
    placeholder: "e.g., -y @modelcontextprotocol/server-filesystem .",
    required: false
  }
];
const HTTP_STEPS = [
  {
    field: "name",
    label: "Server Name",
    placeholder: "e.g., my-http-server",
    required: true,
    validate: (v) => v.trim() ? null : "Name is required"
  },
  {
    field: "url",
    label: "Server URL",
    placeholder: "e.g., http://localhost:8080",
    required: true,
    validate: (v) => {
      if (!v.trim()) return "URL is required";
      try {
        new URL(v);
        return null;
      } catch {
        return "Invalid URL format";
      }
    }
  }
];
const SSE_STEPS = [
  {
    field: "name",
    label: "Server Name",
    placeholder: "e.g., my-sse-server",
    required: true,
    validate: (v) => v.trim() ? null : "Name is required"
  },
  {
    field: "url",
    label: "SSE Endpoint URL",
    placeholder: "e.g., http://localhost:9000/events",
    required: true,
    validate: (v) => {
      if (!v.trim()) return "URL is required";
      try {
        new URL(v);
        return null;
      } catch {
        return "Invalid URL format";
      }
    }
  }
];
function getStepsForType(serverType) {
  switch (serverType) {
    case "stdio":
      return STDIO_STEPS;
    case "http":
      return HTTP_STEPS;
    case "sse":
      return SSE_STEPS;
  }
}
const McpCustomWizard = (0, import_react.forwardRef)(
  function McpCustomWizard2({ isVisible, serverType, onComplete, onClose }, ref) {
    const steps = getStepsForType(serverType);
    const [currentStep, setCurrentStep] = (0, import_react.useState)(0);
    const [values, setValues] = (0, import_react.useState)({});
    const [currentInput, setCurrentInput] = (0, import_react.useState)("");
    const [error, setError] = (0, import_react.useState)(null);
    (0, import_react.useEffect)(() => {
      if (isVisible) {
        setCurrentStep(0);
        setValues({});
        setCurrentInput("");
        setError(null);
      }
    }, [isVisible, serverType]);
    const currentStepConfig = steps[currentStep];
    const handleNext = (0, import_react.useCallback)(() => {
      if (!currentStepConfig) return;
      const value = currentInput.trim();
      if (currentStepConfig.validate) {
        const validationError = currentStepConfig.validate(value);
        if (validationError) {
          setError(validationError);
          return;
        }
      } else if (currentStepConfig.required && !value) {
        setError(`${currentStepConfig.label} is required`);
        return;
      }
      const newValues = { ...values, [currentStepConfig.field]: value };
      setValues(newValues);
      setError(null);
      setCurrentInput("");
      if (currentStep >= steps.length - 1) {
        const config = {
          serverType,
          name: newValues.name || ""
        };
        if (serverType === "stdio") {
          if (newValues.command) {
            config.command = newValues.command;
          }
          if (newValues.args?.trim()) {
            config.args = newValues.args.split(/\s+/).filter(Boolean);
          }
        } else {
          if (newValues.url) {
            config.url = newValues.url;
          }
        }
        onComplete(config);
      } else {
        setCurrentStep(currentStep + 1);
      }
    }, [
      currentInput,
      currentStep,
      currentStepConfig,
      onComplete,
      serverType,
      steps.length,
      values
    ]);
    const handleBack = (0, import_react.useCallback)(() => {
      if (currentStep > 0) {
        setCurrentStep(currentStep - 1);
        const prevStep = steps[currentStep - 1];
        if (prevStep) {
          setCurrentInput(values[prevStep.field] || "");
        }
        setError(null);
      } else {
        onClose();
      }
    }, [currentStep, onClose, steps, values]);
    (0, import_react.useImperativeHandle)(
      ref,
      () => ({
        handleInput: (input, key) => {
          if (!isVisible) return false;
          if (key.escape) {
            handleBack();
            return true;
          }
          if (key.return) {
            handleNext();
            return true;
          }
          if (key.backspace || key.delete) {
            setCurrentInput((prev) => prev.slice(0, -1));
            setError(null);
            return true;
          }
          if (input && !key.ctrl && !key.meta) {
            setCurrentInput((prev) => prev + input);
            setError(null);
            return true;
          }
          return false;
        }
      }),
      [isVisible, handleBack, handleNext]
    );
    if (!isVisible || !currentStepConfig) return null;
    const serverTypeLabel = serverType.toUpperCase();
    return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
      import_ink.Box,
      {
        flexDirection: "column",
        borderStyle: "round",
        borderColor: "yellowBright",
        paddingX: 1,
        marginTop: 1,
        children: [
          /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Box, { marginBottom: 1, children: [
            /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Text, { bold: true, color: "yellowBright", children: [
              "Add Custom ",
              serverTypeLabel,
              " Server"
            ] }),
            /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Text, { color: "gray", children: [
              " ",
              "(Step ",
              currentStep + 1,
              "/",
              steps.length,
              ")"
            ] })
          ] }),
          /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Box, { flexDirection: "column", children: [
            /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Text, { bold: true, children: [
              currentStepConfig.label,
              ":"
            ] }),
            /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { color: "gray", children: currentStepConfig.placeholder })
          ] }),
          /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Box, { marginTop: 1, children: [
            /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { color: "cyan", children: "> " }),
            /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { children: currentInput }),
            /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { color: "cyan", children: "_" })
          ] }),
          error && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Box, { marginTop: 1, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { color: "red", children: error }) }),
          /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Box, { marginTop: 1, children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Text, { color: "gray", children: [
            "Enter to continue \u2022 Esc to ",
            currentStep > 0 ? "go back" : "cancel"
          ] }) })
        ]
      }
    );
  }
);
var McpCustomWizard_default = McpCustomWizard;
