"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 PluginList_exports = {};
__export(PluginList_exports, {
  default: () => PluginList_default
});
module.exports = __toCommonJS(PluginList_exports);
var import_jsx_runtime = require("react/jsx-runtime");
var import_react = require("react");
var import_ink = require("ink");
var import_agent_management = require("@dexto/agent-management");
var import_BaseSelector = require("../base/BaseSelector.js");
const PluginList = (0, import_react.forwardRef)(function PluginList2({ isVisible, onPluginSelect, 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 [plugins, setPlugins] = (0, import_react.useState)([]);
  const [selectedIndex, setSelectedIndex] = (0, import_react.useState)(0);
  const [isLoading, setIsLoading] = (0, import_react.useState)(true);
  (0, import_react.useEffect)(() => {
    if (isVisible) {
      setIsLoading(true);
      try {
        const installedPlugins = (0, import_agent_management.listInstalledPlugins)();
        setPlugins(installedPlugins);
      } catch {
        setPlugins([]);
      } finally {
        setIsLoading(false);
        setSelectedIndex(0);
      }
    }
  }, [isVisible]);
  const items = (0, import_react.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__ */ (0, import_jsx_runtime.jsxs)(import_ink.Box, { children: [
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { color: isSelected ? "cyan" : "gray", children: isSelected ? "\u25B8 " : "  " }),
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { color: "gray", children: "\u2190 " }),
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.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__ */ (0, import_jsx_runtime.jsxs)(import_ink.Box, { flexDirection: "column", children: [
      /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Box, { children: [
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { color: isSelected ? "cyan" : "gray", children: isSelected ? "\u25B8 " : "  " }),
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { color: isSelected ? "white" : "gray", children: "\u{1F4E6} " }),
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { color: isSelected ? "cyan" : "white", bold: isSelected, children: plugin.name }),
        /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Text, { color: "gray", dimColor: true, children: [
          "@",
          version
        ] }),
        scopeBadge && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { color: "yellow", dimColor: true, children: scopeBadge })
      ] }),
      isSelected && plugin.description && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Box, { marginLeft: 4, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { color: "gray", children: plugin.description }) }),
      isSelected && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Box, { marginLeft: 4, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.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__ */ (0, import_jsx_runtime.jsx)(
    import_BaseSelector.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;
