import { Fragment, jsx, jsxs } from "react/jsx-runtime";
import { useState, useEffect, forwardRef, useRef, useImperativeHandle } from "react";
import { Text } from "ink";
import { BaseSelector } from "../base/BaseSelector.js";
const McpRemoveSelector = forwardRef(
  function McpRemoveSelector2({ isVisible, onSelect, onClose, agent }, ref) {
    const baseSelectorRef = useRef(null);
    useImperativeHandle(
      ref,
      () => ({
        handleInput: (input, key) => {
          return baseSelectorRef.current?.handleInput(input, key) ?? false;
        }
      }),
      []
    );
    const [servers, setServers] = useState([]);
    const [isLoading, setIsLoading] = useState(false);
    const [selectedIndex, setSelectedIndex] = useState(0);
    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__ */ jsxs(Fragment, { children: [
      /* @__PURE__ */ jsxs(Text, { children: [
        getStatusIcon(server.status),
        " "
      ] }),
      /* @__PURE__ */ jsx(Text, { color: isSelected ? "cyan" : "gray", bold: isSelected, children: server.name }),
      /* @__PURE__ */ jsxs(
        Text,
        {
          color: server.status === "connected" ? "green" : server.status === "disconnected" ? "gray" : server.status === "auth-required" ? "yellow" : "red",
          children: [
            " ",
            "- ",
            getStatusText(server.status)
          ]
        }
      ),
      server.error && /* @__PURE__ */ jsxs(Text, { color: "gray", children: [
        " ",
        "(",
        server.error.slice(0, 30),
        server.error.length > 30 ? "..." : "",
        ")"
      ] })
    ] });
    const handleSelect = (server) => {
      onSelect(server.name);
    };
    return /* @__PURE__ */ jsx(
      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;
export {
  McpRemoveSelector_default as default
};
