import { jsx, jsxs } from "react/jsx-runtime";
import { useState, useEffect, forwardRef, useRef, useImperativeHandle } from "react";
import { Box, Text } from "ink";
import { BaseSelector } from "../base/BaseSelector.js";
const PLUGIN_OPTIONS = [
  {
    action: "list",
    label: "Installed Plugins",
    hint: "View, manage, uninstall",
    icon: "\u{1F4E6}"
  },
  {
    action: "marketplace",
    label: "Browse Marketplace",
    hint: "Find and install plugins",
    icon: "\u{1F6D2}"
  },
  {
    action: "back",
    label: "Back",
    hint: "",
    icon: "\u2190"
  }
];
const PluginManager = forwardRef(function PluginManager2({ isVisible, onAction, onClose }, ref) {
  const baseSelectorRef = useRef(null);
  useImperativeHandle(
    ref,
    () => ({
      handleInput: (input, key) => {
        return baseSelectorRef.current?.handleInput(input, key) ?? false;
      }
    }),
    []
  );
  const [selectedIndex, setSelectedIndex] = useState(0);
  useEffect(() => {
    if (isVisible) {
      setSelectedIndex(0);
    }
  }, [isVisible]);
  const formatItem = (option, isSelected) => {
    const isBack = option.action === "back";
    return /* @__PURE__ */ jsxs(Box, { children: [
      /* @__PURE__ */ jsx(Text, { color: isSelected ? "cyan" : "gray", children: isSelected ? "\u25B8 " : "  " }),
      /* @__PURE__ */ jsxs(Text, { color: isBack ? "gray" : isSelected ? "white" : "gray", children: [
        option.icon,
        " "
      ] }),
      /* @__PURE__ */ jsx(Text, { color: isBack ? "gray" : isSelected ? "cyan" : "white", bold: isSelected, children: option.label }),
      option.hint && /* @__PURE__ */ jsxs(Text, { color: "gray", dimColor: true, children: [
        " ",
        "\u2014 ",
        option.hint
      ] })
    ] });
  };
  const handleSelect = (option) => {
    if (option.action === "back") {
      onClose();
    } else {
      onAction(option.action);
    }
  };
  return /* @__PURE__ */ jsx(
    BaseSelector,
    {
      ref: baseSelectorRef,
      items: PLUGIN_OPTIONS,
      isVisible,
      isLoading: false,
      selectedIndex,
      onSelectIndex: setSelectedIndex,
      onSelect: handleSelect,
      onClose,
      formatItem,
      title: "Plugins",
      borderColor: "magenta",
      emptyMessage: "No options available"
    }
  );
});
var PluginManager_default = PluginManager;
export {
  PluginManager_default as default
};
