import { jsx, jsxs } from "react/jsx-runtime";
import { useState, useEffect, forwardRef, useImperativeHandle, useCallback } from "react";
import { Box, Text } from "ink";
import { addMarketplace } from "@dexto/agent-management";
import { logger } from "@dexto/core";
const MarketplaceAddPrompt = forwardRef(
  function MarketplaceAddPrompt2({ isVisible, onComplete, onClose }, ref) {
    const [input, setInput] = useState("");
    const [error, setError] = useState(null);
    const [isAdding, setIsAdding] = useState(false);
    useEffect(() => {
      if (isVisible) {
        setInput("");
        setError(null);
        setIsAdding(false);
      }
    }, [isVisible]);
    const handleAdd = useCallback(async () => {
      const source = input.trim();
      if (!source) {
        setError("Please enter a marketplace source");
        return;
      }
      setError(null);
      setIsAdding(true);
      try {
        const result = await addMarketplace(source);
        onComplete(result.name, result.pluginCount);
      } catch (err) {
        const errorMessage = err instanceof Error ? err.message : String(err);
        setError(errorMessage);
        logger.error(`MarketplaceAddPrompt.handleAdd failed: ${errorMessage}`);
      } finally {
        setIsAdding(false);
      }
    }, [input, onComplete]);
    useImperativeHandle(
      ref,
      () => ({
        handleInput: (inputStr, key) => {
          if (key.escape) {
            onClose();
            return true;
          }
          if (key.return) {
            if (!isAdding) {
              handleAdd();
            }
            return true;
          }
          if (key.backspace || key.delete) {
            setInput((prev) => prev.slice(0, -1));
            setError(null);
            return true;
          }
          if (inputStr && !key.ctrl && !key.meta) {
            setInput((prev) => prev + inputStr);
            setError(null);
            return true;
          }
          return false;
        }
      }),
      [handleAdd, isAdding, onClose]
    );
    if (!isVisible) return null;
    return /* @__PURE__ */ jsxs(
      Box,
      {
        flexDirection: "column",
        borderStyle: "round",
        borderColor: "green",
        paddingX: 1,
        marginTop: 1,
        children: [
          /* @__PURE__ */ jsx(Box, { marginBottom: 1, children: /* @__PURE__ */ jsx(Text, { bold: true, color: "green", children: "Add Marketplace" }) }),
          /* @__PURE__ */ jsx(Box, { marginBottom: 1, children: /* @__PURE__ */ jsx(Text, { color: "gray", children: "Enter marketplace source:" }) }),
          /* @__PURE__ */ jsxs(Box, { marginBottom: 1, flexDirection: "column", children: [
            /* @__PURE__ */ jsx(Text, { color: "gray", dimColor: true, children: "- GitHub: owner/repo (e.g., anthropics/claude-plugins-official)" }),
            /* @__PURE__ */ jsx(Text, { color: "gray", dimColor: true, children: "- Git URL: https://github.com/user/repo.git" }),
            /* @__PURE__ */ jsx(Text, { color: "gray", dimColor: true, children: "- Local: /path/to/marketplace or ~/marketplace" })
          ] }),
          /* @__PURE__ */ jsxs(Box, { children: [
            /* @__PURE__ */ jsx(Text, { color: "cyan", children: "> " }),
            /* @__PURE__ */ jsx(Text, { children: input }),
            /* @__PURE__ */ jsx(Text, { color: "cyan", children: "_" })
          ] }),
          error && /* @__PURE__ */ jsx(Box, { marginTop: 1, children: /* @__PURE__ */ jsx(Text, { color: "red", children: error }) }),
          isAdding && /* @__PURE__ */ jsx(Box, { marginTop: 1, children: /* @__PURE__ */ jsx(Text, { color: "yellow", children: "Adding marketplace..." }) }),
          /* @__PURE__ */ jsx(Box, { marginTop: 1, children: /* @__PURE__ */ jsx(Text, { color: "gray", dimColor: true, children: "Press Enter to add, Escape to cancel" }) })
        ]
      }
    );
  }
);
var MarketplaceAddPrompt_default = MarketplaceAddPrompt;
export {
  MarketplaceAddPrompt_default as default
};
