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