"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 PromptAddWizard_exports = {};
__export(PromptAddWizard_exports, {
  default: () => PromptAddWizard_default
});
module.exports = __toCommonJS(PromptAddWizard_exports);
var import_jsx_runtime = require("react/jsx-runtime");
var import_react = require("react");
var import_ink = require("ink");
const PROMPT_NAME_REGEX = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
const WIZARD_STEPS = [
  {
    field: "name",
    label: "Prompt Name",
    placeholder: "e.g., my-prompt (kebab-case)",
    required: true,
    validate: (v) => {
      if (!v.trim()) return "Name is required";
      if (!PROMPT_NAME_REGEX.test(v.trim())) {
        return "Name must be kebab-case (lowercase letters, numbers, hyphens)";
      }
      return null;
    }
  },
  {
    field: "title",
    label: "Title (optional)",
    placeholder: "e.g., My Custom Prompt",
    required: false
  },
  {
    field: "description",
    label: "Description (optional)",
    placeholder: "e.g., Helps with specific task",
    required: false
  },
  {
    field: "argumentHint",
    label: "Arguments (optional)",
    placeholder: "e.g., [style] [length?] - use ? for optional",
    required: false
  },
  {
    field: "content",
    label: "Prompt Content",
    placeholder: "Use $1, $2 for args, $ARGUMENTS for remaining",
    required: true,
    multiline: true,
    validate: (v) => v.trim() ? null : "Content is required"
  }
];
const PromptAddWizard = (0, import_react.forwardRef)(
  function PromptAddWizard2({ isVisible, scope, onComplete, onClose }, ref) {
    const [currentStep, setCurrentStep] = (0, import_react.useState)(0);
    const [values, setValues] = (0, import_react.useState)({});
    const [currentInput, setCurrentInput] = (0, import_react.useState)("");
    const [error, setError] = (0, import_react.useState)(null);
    (0, import_react.useEffect)(() => {
      if (isVisible) {
        setCurrentStep(0);
        setValues({});
        setCurrentInput("");
        setError(null);
      }
    }, [isVisible, scope]);
    const currentStepConfig = WIZARD_STEPS[currentStep];
    const handleNext = (0, import_react.useCallback)(() => {
      if (!currentStepConfig) return;
      const value = currentInput.trim();
      if (currentStepConfig.validate) {
        const validationError = currentStepConfig.validate(value);
        if (validationError) {
          setError(validationError);
          return;
        }
      } else if (currentStepConfig.required && !value) {
        setError(`${currentStepConfig.label} is required`);
        return;
      }
      const newValues = { ...values, [currentStepConfig.field]: value };
      setValues(newValues);
      setError(null);
      setCurrentInput("");
      if (currentStep >= WIZARD_STEPS.length - 1) {
        const data = {
          name: newValues.name || "",
          content: newValues.content || ""
        };
        if (newValues.title?.trim()) {
          data.title = newValues.title.trim();
        }
        if (newValues.description?.trim()) {
          data.description = newValues.description.trim();
        }
        if (newValues.argumentHint?.trim()) {
          data.argumentHint = newValues.argumentHint.trim();
        }
        onComplete(data);
      } else {
        setCurrentStep(currentStep + 1);
      }
    }, [currentInput, currentStep, currentStepConfig, onComplete, values]);
    const handleBack = (0, import_react.useCallback)(() => {
      if (currentStep > 0) {
        setCurrentStep(currentStep - 1);
        const prevStep = WIZARD_STEPS[currentStep - 1];
        if (prevStep) {
          setCurrentInput(values[prevStep.field] || "");
        }
        setError(null);
      } else {
        onClose();
      }
    }, [currentStep, onClose, values]);
    (0, import_react.useImperativeHandle)(
      ref,
      () => ({
        handleInput: (input, key) => {
          if (!isVisible) return false;
          if (key.escape) {
            handleBack();
            return true;
          }
          if (key.return) {
            if (currentStepConfig?.multiline && key.shift) {
              setCurrentInput((prev) => prev + "\n");
              return true;
            }
            handleNext();
            return true;
          }
          if (key.backspace || key.delete) {
            setCurrentInput((prev) => prev.slice(0, -1));
            setError(null);
            return true;
          }
          if (input && !key.ctrl && !key.meta) {
            setCurrentInput((prev) => prev + input);
            setError(null);
            return true;
          }
          return false;
        }
      }),
      [isVisible, handleBack, handleNext, currentStepConfig]
    );
    if (!isVisible || !currentStepConfig) return null;
    const scopeLabel = scope === "agent" ? "Agent" : "Shared";
    return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
      import_ink.Box,
      {
        flexDirection: "column",
        borderStyle: "round",
        borderColor: "yellowBright",
        paddingX: 1,
        marginTop: 1,
        children: [
          /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Box, { marginBottom: 1, children: [
            /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Text, { bold: true, color: "yellowBright", children: [
              "Add ",
              scopeLabel,
              " Prompt"
            ] }),
            /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Text, { color: "gray", children: [
              " ",
              "(Step ",
              currentStep + 1,
              "/",
              WIZARD_STEPS.length,
              ")"
            ] })
          ] }),
          /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Box, { flexDirection: "column", children: [
            /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Text, { bold: true, children: [
              currentStepConfig.label,
              ":"
            ] }),
            /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { color: "gray", children: currentStepConfig.placeholder })
          ] }),
          /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Box, { marginTop: 1, flexDirection: "column", children: [
            /* @__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: currentInput }),
              /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { color: "cyan", children: "_" })
            ] }),
            currentStepConfig.multiline && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { color: "gray", italic: true, children: "(Shift+Enter for newline)" })
          ] }),
          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 }) }),
          /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Box, { marginTop: 1, children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Text, { color: "gray", children: [
            "Enter to continue \u2022 Esc to ",
            currentStep > 0 ? "go back" : "cancel"
          ] }) })
        ]
      }
    );
  }
);
var PromptAddWizard_default = PromptAddWizard;
