"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 McpRemoveSelector_exports = {};
__export(McpRemoveSelector_exports, {
  default: () => McpRemoveSelector_default
});
module.exports = __toCommonJS(McpRemoveSelector_exports);
var import_jsx_runtime = require("react/jsx-runtime");
var import_react = require("react");
var import_ink = require("ink");
var import_BaseSelector = require("../base/BaseSelector.js");
const McpRemoveSelector = (0, import_react.forwardRef)(
  function McpRemoveSelector2({ isVisible, onSelect, onClose, agent }, ref) {
    const baseSelectorRef = (0, import_react.useRef)(null);
    (0, import_react.useImperativeHandle)(
      ref,
      () => ({
        handleInput: (input, key) => {
          return baseSelectorRef.current?.handleInput(input, key) ?? false;
        }
      }),
      []
    );
    const [servers, setServers] = (0, import_react.useState)([]);
    const [isLoading, setIsLoading] = (0, import_react.useState)(false);
    const [selectedIndex, setSelectedIndex] = (0, import_react.useState)(0);
    (0, import_react.useEffect)(() => {
      if (!isVisible) return;
      setIsLoading(true);
      try {
        const serverList = agent.getMcpServersWithStatus();
        serverList.sort((a, b) => a.name.localeCompare(b.name));
        setServers(serverList);
        setSelectedIndex(0);
      } finally {
        setIsLoading(false);
      }
    }, [isVisible, agent]);
    const getStatusIcon = (status) => {
      switch (status) {
        case "connected":
          return "\u{1F50C}";
        case "disconnected":
          return "\u23F8\uFE0F";
        case "auth-required":
          return "\u{1F510}";
        case "error":
          return "!";
      }
    };
    const getStatusText = (status) => {
      switch (status) {
        case "connected":
          return "Connected";
        case "disconnected":
          return "Disabled";
        case "auth-required":
          return "Auth required";
        case "error":
          return "Failed";
      }
    };
    const formatItem = (server, isSelected) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
      /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Text, { children: [
        getStatusIcon(server.status),
        " "
      ] }),
      /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { color: isSelected ? "cyan" : "gray", bold: isSelected, children: server.name }),
      /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
        import_ink.Text,
        {
          color: server.status === "connected" ? "green" : server.status === "disconnected" ? "gray" : server.status === "auth-required" ? "yellow" : "red",
          children: [
            " ",
            "- ",
            getStatusText(server.status)
          ]
        }
      ),
      server.error && /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Text, { color: "gray", children: [
        " ",
        "(",
        server.error.slice(0, 30),
        server.error.length > 30 ? "..." : "",
        ")"
      ] })
    ] });
    const handleSelect = (server) => {
      onSelect(server.name);
    };
    return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
      import_BaseSelector.BaseSelector,
      {
        ref: baseSelectorRef,
        items: servers,
        isVisible,
        isLoading,
        selectedIndex,
        onSelectIndex: setSelectedIndex,
        onSelect: handleSelect,
        onClose,
        formatItem,
        title: "Remove MCP Server",
        borderColor: "red",
        loadingMessage: "Loading servers...",
        emptyMessage: "No MCP servers installed"
      }
    );
  }
);
var McpRemoveSelector_default = McpRemoveSelector;
