import { jsx, jsxs } from "react/jsx-runtime";
import {
  useState,
  useEffect,
  forwardRef,
  useRef,
  useImperativeHandle,
  useMemo
} from "react";
import { Box, Text } from "ink";
import { listInstalledPlugins } from "@dexto/agent-management";
import { BaseSelector } from "../base/BaseSelector.js";
const PluginList = forwardRef(function PluginList2({ isVisible, onPluginSelect, onClose }, ref) {
  const baseSelectorRef = useRef(null);
  useImperativeHandle(
    ref,
    () => ({
      handleInput: (input, key) => {
        return baseSelectorRef.current?.handleInput(input, key) ?? false;
      }
    }),
    []
  );
  const [plugins, setPlugins] = useState([]);
  const [selectedIndex, setSelectedIndex] = useState(0);
  const [isLoading, setIsLoading] = useState(true);
  useEffect(() => {
    if (isVisible) {
      setIsLoading(true);
      try {
        const installedPlugins = listInstalledPlugins();
        setPlugins(installedPlugins);
      } catch {
        setPlugins([]);
      } finally {
        setIsLoading(false);
        setSelectedIndex(0);
      }
    }
  }, [isVisible]);
  const items = useMemo(() => {
    const list = [{ type: "back" }];
    list.push(...plugins.map((plugin) => ({ type: "plugin", plugin })));
    return list;
  }, [plugins]);
  const formatItem = (item, isSelected) => {
    if (item.type === "back") {
      return /* @__PURE__ */ jsxs(Box, { children: [
        /* @__PURE__ */ jsx(Text, { color: isSelected ? "cyan" : "gray", children: isSelected ? "\u25B8 " : "  " }),
        /* @__PURE__ */ jsx(Text, { color: "gray", children: "\u2190 " }),
        /* @__PURE__ */ jsx(Text, { color: isSelected ? "white" : "gray", children: "Back to menu" })
      ] });
    }
    const plugin = item.plugin;
    const version = plugin.version || "unknown";
    const scopeBadge = plugin.scope ? ` [${plugin.scope}]` : "";
    return /* @__PURE__ */ jsxs(Box, { flexDirection: "column", children: [
      /* @__PURE__ */ jsxs(Box, { children: [
        /* @__PURE__ */ jsx(Text, { color: isSelected ? "cyan" : "gray", children: isSelected ? "\u25B8 " : "  " }),
        /* @__PURE__ */ jsx(Text, { color: isSelected ? "white" : "gray", children: "\u{1F4E6} " }),
        /* @__PURE__ */ jsx(Text, { color: isSelected ? "cyan" : "white", bold: isSelected, children: plugin.name }),
        /* @__PURE__ */ jsxs(Text, { color: "gray", dimColor: true, children: [
          "@",
          version
        ] }),
        scopeBadge && /* @__PURE__ */ jsx(Text, { color: "yellow", dimColor: true, children: scopeBadge })
      ] }),
      isSelected && plugin.description && /* @__PURE__ */ jsx(Box, { marginLeft: 4, children: /* @__PURE__ */ jsx(Text, { color: "gray", children: plugin.description }) }),
      isSelected && /* @__PURE__ */ jsx(Box, { marginLeft: 4, children: /* @__PURE__ */ jsx(Text, { color: "gray", dimColor: true, children: plugin.path }) })
    ] });
  };
  const handleSelect = (item) => {
    if (item.type === "back") {
      onClose();
    } else {
      onPluginSelect(item.plugin);
    }
  };
  const pluginCount = plugins.length;
  const title = pluginCount > 0 ? `Installed Plugins (${pluginCount})` : "Installed Plugins";
  return /* @__PURE__ */ jsx(
    BaseSelector,
    {
      ref: baseSelectorRef,
      items,
      isVisible,
      isLoading,
      selectedIndex,
      onSelectIndex: setSelectedIndex,
      onSelect: handleSelect,
      onClose,
      formatItem,
      title,
      borderColor: "cyan",
      emptyMessage: "No plugins installed. Browse the marketplace to find plugins."
    }
  );
});
var PluginList_default = PluginList;
export {
  PluginList_default as default
};
