"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 McpAddSelector_exports = {};
__export(McpAddSelector_exports, {
  default: () => McpAddSelector_default
});
module.exports = __toCommonJS(McpAddSelector_exports);
var import_jsx_runtime = require("react/jsx-runtime");
var import_react = require("react");
var import_ink = require("ink");
var import_registry = require("@dexto/registry");
var import_BaseSelector = require("../base/BaseSelector.js");
const McpAddSelector = (0, import_react.forwardRef)(
  function McpAddSelector2({ isVisible, onSelect, onClose }, ref) {
    const baseSelectorRef = (0, import_react.useRef)(null);
    (0, import_react.useImperativeHandle)(
      ref,
      () => ({
        handleInput: (input, key) => {
          return baseSelectorRef.current?.handleInput(input, key) ?? false;
        }
      }),
      []
    );
    const [options, setOptions] = (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;
      let cancelled = false;
      setIsLoading(true);
      const fetchEntries = async () => {
        try {
          const entries = await import_registry.serverRegistry.getEntries();
          if (cancelled) return;
          const availablePresets = entries.filter((e) => !e.isInstalled);
          const optionList = availablePresets.map((entry) => ({
            id: entry.id,
            label: entry.name,
            description: entry.description,
            icon: entry.icon || "\u{1F4E6}",
            entry
          }));
          setOptions(optionList);
          setSelectedIndex(0);
        } catch {
          if (!cancelled) {
            setOptions([]);
          }
        } finally {
          if (!cancelled) {
            setIsLoading(false);
          }
        }
      };
      void fetchEntries();
      return () => {
        cancelled = true;
      };
    }, [isVisible]);
    const formatItem = (option, isSelected) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
      /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Text, { children: [
        option.icon,
        " "
      ] }),
      /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { color: isSelected ? "cyan" : "gray", bold: isSelected, children: option.label }),
      /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Text, { color: isSelected ? "white" : "gray", children: [
        " ",
        "- ",
        option.description.slice(0, 40),
        option.description.length > 40 ? "..." : ""
      ] })
    ] });
    const handleSelect = (option) => {
      onSelect({ type: "preset", entry: option.entry });
    };
    return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
      import_BaseSelector.BaseSelector,
      {
        ref: baseSelectorRef,
        items: options,
        isVisible,
        isLoading,
        selectedIndex,
        onSelectIndex: setSelectedIndex,
        onSelect: handleSelect,
        onClose,
        formatItem,
        title: "Add MCP Server (Presets)",
        borderColor: "green",
        loadingMessage: "Loading server presets...",
        emptyMessage: "No server presets available"
      }
    );
  }
);
var McpAddSelector_default = McpAddSelector;
