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 PluginActions = forwardRef(function PluginActions2({ isVisible, plugin, 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, plugin]);
  const items = useMemo(() => {
    if (!plugin) return [];
    return [
      {
        id: "back",
        type: "back",
        label: "Back to list",
        icon: "\u2190",
        color: "gray"
      },
      {
        id: "uninstall",
        type: "uninstall",
        label: "Uninstall",
        icon: "\u{1F5D1}",
        color: "red"
      }
    ];
  }, [plugin]);
  const formatItem = (item, isSelected) => {
    const isBack = item.type === "back";
    return /* @__PURE__ */ jsxs(Box, { children: [
      /* @__PURE__ */ jsx(Text, { color: isSelected ? "cyan" : "gray", children: isSelected ? "\u25B8 " : "  " }),
      /* @__PURE__ */ jsxs(Text, { color: isBack ? "gray" : isSelected ? item.color : "gray", children: [
        item.icon,
        " "
      ] }),
      /* @__PURE__ */ jsx(
        Text,
        {
          color: isBack ? isSelected ? "white" : "gray" : isSelected ? item.color : "white",
          bold: isSelected && !isBack,
          children: item.label
        }
      )
    ] });
  };
  const handleSelect = (item) => {
    if (!plugin) return;
    onAction({ type: item.type, plugin });
  };
  if (!plugin) return null;
  const version = plugin.version || "unknown";
  const scopeBadge = plugin.scope ? ` [${plugin.scope}]` : "";
  const title = `\u{1F4E6} ${plugin.name}@${version}${scopeBadge}`;
  return /* @__PURE__ */ jsx(
    BaseSelector,
    {
      ref: baseSelectorRef,
      items,
      isVisible,
      isLoading: false,
      selectedIndex,
      onSelectIndex: setSelectedIndex,
      onSelect: handleSelect,
      onClose,
      formatItem,
      title,
      borderColor: "magenta",
      emptyMessage: "No actions available"
    }
  );
});
var PluginActions_default = PluginActions;
export {
  PluginActions_default as default
};
