"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 PromptDeleteSelector_exports = {};
__export(PromptDeleteSelector_exports, {
  default: () => PromptDeleteSelector_default
});
module.exports = __toCommonJS(PromptDeleteSelector_exports);
var import_jsx_runtime = require("react/jsx-runtime");
var import_react = require("react");
var import_ink = require("ink");
var import_BaseSelector = require("../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 = (0, import_react.forwardRef)(
  function PromptDeleteSelector2({ isVisible, onDelete, onClose, agent }, ref) {
    const baseSelectorRef = (0, import_react.useRef)(null);
    const [selectedIndex, setSelectedIndex] = (0, import_react.useState)(0);
    const [deletablePrompts, setDeletablePrompts] = (0, import_react.useState)([]);
    const [isLoading, setIsLoading] = (0, import_react.useState)(true);
    (0, import_react.useImperativeHandle)(
      ref,
      () => ({
        handleInput: (input, key) => {
          return baseSelectorRef.current?.handleInput(input, key) ?? false;
        }
      }),
      []
    );
    (0, import_react.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__ */ (0, import_jsx_runtime.jsxs)(import_ink.Box, { children: [
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { color: isSelected ? "red" : "white", bold: isSelected, children: displayName }),
        item.prompt.title && /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Text, { color: "gray", children: [
          " - ",
          item.prompt.title
        ] }),
        /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Text, { color: sourceColor, children: [
          " (",
          sourceLabel,
          ")"
        ] })
      ] });
    };
    const handleSelect = (item) => {
      onDelete(item);
    };
    const items = (0, import_react.useMemo)(() => {
      return deletablePrompts;
    }, [deletablePrompts]);
    const emptyMessage = "No deletable prompts found.\nMCP prompts cannot be deleted.";
    return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Box, { flexDirection: "column", children: [
      /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
        import_BaseSelector.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__ */ (0, import_jsx_runtime.jsx)(import_ink.Box, { marginTop: 1, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { color: "gray", italic: true, children: "Note: MCP prompts cannot be deleted (they come from servers)" }) })
    ] });
  }
);
var PromptDeleteSelector_default = PromptDeleteSelector;
