"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 McpServerList_exports = {};
__export(McpServerList_exports, {
  default: () => McpServerList_default
});
module.exports = __toCommonJS(McpServerList_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");
function getStatusIcon(status) {
  switch (status) {
    case "connected":
      return "\u{1F7E2}";
    case "disconnected":
      return "\u26AA";
    case "auth-required":
      return "\u{1F510}";
    case "error":
      return "\u{1F534}";
  }
}
function getStatusText(status) {
  switch (status) {
    case "connected":
      return "connected";
    case "disconnected":
      return "disabled";
    case "auth-required":
      return "auth required";
    case "error":
      return "failed";
  }
}
const McpServerList = (0, import_react.forwardRef)(function McpServerList2({ isVisible, onAction, onClose, agent }, ref) {
  const baseSelectorRef = (0, import_react.useRef)(null);
  const [selectedIndex, setSelectedIndex] = (0, import_react.useState)(0);
  const [servers, setServers] = (0, import_react.useState)([]);
  const [isLoading, setIsLoading] = (0, import_react.useState)(true);
  (0, import_react.useImperativeHandle)(
    ref,
    () => ({
      handleInput: (input, key) => {
        return baseSelectorRef.current?.handleInput(input, key) ?? false;
      }
    }),
    []
  );
  (0, import_react.useEffect)(() => {
    if (isVisible) {
      setIsLoading(true);
      setSelectedIndex(0);
      const serverList = agent.getMcpServersWithStatus();
      serverList.sort((a, b) => {
        const order = {
          connected: 0,
          disconnected: 1,
          "auth-required": 2,
          error: 3
        };
        return order[a.status] - order[b.status];
      });
      setServers(serverList);
      setIsLoading(false);
    }
  }, [isVisible, agent]);
  const items = (0, import_react.useMemo)(() => {
    const list = servers.map((server) => ({
      id: server.name,
      isAddNew: false,
      server
    }));
    list.push({
      id: "__add_new__",
      isAddNew: true
    });
    return list;
  }, [servers]);
  const formatItem = (item, isSelected) => {
    if (item.isAddNew) {
      return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Box, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { color: isSelected ? "green" : "gray", bold: isSelected, children: "+ Add new server" }) });
    }
    const server = item.server;
    const statusIcon = getStatusIcon(server.status);
    const statusText = getStatusText(server.status);
    return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Box, { children: [
      /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Text, { children: [
        statusIcon,
        " "
      ] }),
      /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { color: isSelected ? "cyan" : "white", bold: isSelected, children: server.name }),
      /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Text, { color: "gray", children: [
        " (",
        server.type,
        ") "
      ] }),
      /* @__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: [
            "[",
            statusText,
            "]"
          ]
        }
      )
    ] });
  };
  const handleSelect = (item) => {
    if (item.isAddNew) {
      onAction({ type: "add-new" });
    } else if (item.server) {
      onAction({ type: "select-server", server: item.server });
    }
  };
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
    import_BaseSelector.BaseSelector,
    {
      ref: baseSelectorRef,
      items,
      isVisible,
      isLoading,
      selectedIndex,
      onSelectIndex: setSelectedIndex,
      onSelect: handleSelect,
      onClose,
      formatItem,
      title: "MCP Servers",
      borderColor: "magenta",
      emptyMessage: "No servers configured"
    }
  );
});
var McpServerList_default = McpServerList;
