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";
function isSharedPrompt(prompt) {
  const metadata = prompt.metadata;
  if (metadata?.filePath) {
    return metadata.filePath.includes("/commands/") || metadata.filePath.includes("/.dexto/commands/");
  }
  return false;
}
function getFilePath(prompt) {
  const metadata = prompt.metadata;
  return metadata?.filePath;
}
const PromptDeleteSelector = forwardRef(
  function PromptDeleteSelector2({ isVisible, onDelete, onClose, agent }, ref) {
    const baseSelectorRef = useRef(null);
    const [selectedIndex, setSelectedIndex] = useState(0);
    const [deletablePrompts, setDeletablePrompts] = useState([]);
    const [isLoading, setIsLoading] = useState(true);
    useImperativeHandle(
      ref,
      () => ({
        handleInput: (input, key) => {
          return baseSelectorRef.current?.handleInput(input, key) ?? false;
        }
      }),
      []
    );
    useEffect(() => {
      if (isVisible) {
        setIsLoading(true);
        setSelectedIndex(0);
        agent.listPrompts().then((promptSet) => {
          const prompts = Object.values(promptSet);
          const deletable = [];
          for (const prompt of prompts) {
            if (prompt.source === "mcp") continue;
            const filePath = getFilePath(prompt);
            const isShared = isSharedPrompt(prompt);
            if (prompt.source === "config" && !isShared) {
              deletable.push({
                prompt,
                sourceType: "config",
                filePath
              });
            } else if (prompt.source === "config" && isShared) {
              deletable.push({
                prompt,
                sourceType: "shared",
                filePath
              });
            } else if (prompt.source === "custom") {
              deletable.push({
                prompt,
                sourceType: "config"
                // Treat as config for deletion purposes
              });
            }
          }
          deletable.sort((a, b) => {
            if (a.sourceType !== b.sourceType) {
              return a.sourceType === "config" ? -1 : 1;
            }
            const aName = a.prompt.displayName || a.prompt.name;
            const bName = b.prompt.displayName || b.prompt.name;
            return aName.localeCompare(bName);
          });
          setDeletablePrompts(deletable);
          setIsLoading(false);
        }).catch(() => {
          setDeletablePrompts([]);
          setIsLoading(false);
        });
      }
    }, [isVisible, agent]);
    const formatItem = (item, isSelected) => {
      const displayName = item.prompt.displayName || item.prompt.name;
      const sourceLabel = item.sourceType === "shared" ? "shared" : "config";
      const sourceColor = item.sourceType === "shared" ? "magenta" : "blue";
      return /* @__PURE__ */ jsxs(Box, { children: [
        /* @__PURE__ */ jsx(Text, { color: isSelected ? "red" : "white", bold: isSelected, children: displayName }),
        item.prompt.title && /* @__PURE__ */ jsxs(Text, { color: "gray", children: [
          " - ",
          item.prompt.title
        ] }),
        /* @__PURE__ */ jsxs(Text, { color: sourceColor, children: [
          " (",
          sourceLabel,
          ")"
        ] })
      ] });
    };
    const handleSelect = (item) => {
      onDelete(item);
    };
    const items = useMemo(() => {
      return deletablePrompts;
    }, [deletablePrompts]);
    const emptyMessage = "No deletable prompts found.\nMCP prompts cannot be deleted.";
    return /* @__PURE__ */ jsxs(Box, { flexDirection: "column", children: [
      /* @__PURE__ */ jsx(
        BaseSelector,
        {
          ref: baseSelectorRef,
          items,
          isVisible,
          isLoading,
          selectedIndex,
          onSelectIndex: setSelectedIndex,
          onSelect: handleSelect,
          onClose,
          formatItem,
          title: "Delete Prompt",
          borderColor: "red",
          emptyMessage,
          maxVisibleItems: 10
        }
      ),
      isVisible && !isLoading && items.length > 0 && /* @__PURE__ */ jsx(Box, { marginTop: 1, children: /* @__PURE__ */ jsx(Text, { color: "gray", italic: true, children: "Note: MCP prompts cannot be deleted (they come from servers)" }) })
    ] });
  }
);
var PromptDeleteSelector_default = PromptDeleteSelector;
export {
  PromptDeleteSelector_default as default
};
