import { jsx, jsxs } from "react/jsx-runtime";
import {
  useState,
  useEffect,
  forwardRef,
  useRef,
  useImperativeHandle,
  useMemo
} from "react";
import { Box, Text } from "ink";
import { BaseSelector } from "../base/BaseSelector.js";
const McpServerActions = forwardRef(
  function McpServerActions2({ isVisible, server, onAction, onClose }, ref) {
    const baseSelectorRef = useRef(null);
    const [selectedIndex, setSelectedIndex] = useState(0);
    useImperativeHandle(
      ref,
      () => ({
        handleInput: (input, key) => {
          return baseSelectorRef.current?.handleInput(input, key) ?? false;
        }
      }),
      []
    );
    useEffect(() => {
      if (isVisible) {
        setSelectedIndex(0);
      }
    }, [isVisible, server]);
    const items = useMemo(() => {
      if (!server) return [];
      const actions = [];
      if (server.enabled) {
        actions.push({
          id: "disable",
          type: "disable",
          label: "Disable server",
          icon: "\u23F8\uFE0F",
          color: "orange"
        });
      } else {
        actions.push({
          id: "enable",
          type: "enable",
          label: "Enable server",
          icon: "\u25B6\uFE0F",
          color: "green"
        });
      }
      if (server.status === "auth-required") {
        actions.push({
          id: "authenticate",
          type: "authenticate",
          label: "Authenticate server",
          icon: "\u{1F510}",
          color: "yellow"
        });
      }
      actions.push({
        id: "delete",
        type: "delete",
        label: "Delete server",
        icon: "\u{1F5D1}\uFE0F",
        color: "red"
      });
      actions.push({
        id: "back",
        type: "back",
        label: "Back to server list",
        icon: "\u2190",
        color: "gray"
      });
      return actions;
    }, [server]);
    const formatItem = (item, isSelected) => {
      return /* @__PURE__ */ jsxs(Box, { children: [
        /* @__PURE__ */ jsxs(Text, { children: [
          item.icon,
          " "
        ] }),
        /* @__PURE__ */ jsx(Text, { color: isSelected ? item.color : "gray", bold: isSelected, children: item.label })
      ] });
    };
    const handleSelect = (item) => {
      if (!server) return;
      onAction({ type: item.type, server });
    };
    if (!server) return null;
    return /* @__PURE__ */ jsx(
      BaseSelector,
      {
        ref: baseSelectorRef,
        items,
        isVisible,
        isLoading: false,
        selectedIndex,
        onSelectIndex: setSelectedIndex,
        onSelect: handleSelect,
        onClose,
        formatItem,
        title: `Server: ${server.name}`,
        borderColor: "magenta",
        emptyMessage: "No actions available"
      }
    );
  }
);
var McpServerActions_default = McpServerActions;
export {
  McpServerActions_default as default
};
