import { jsx, jsxs } from "react/jsx-runtime";
import { useState, useEffect, forwardRef, useImperativeHandle, useCallback } from "react";
import { Box, Text } from "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 = forwardRef(
  function PromptAddWizard2({ isVisible, scope, onComplete, onClose }, ref) {
    const [currentStep, setCurrentStep] = useState(0);
    const [values, setValues] = useState({});
    const [currentInput, setCurrentInput] = useState("");
    const [error, setError] = useState(null);
    useEffect(() => {
      if (isVisible) {
        setCurrentStep(0);
        setValues({});
        setCurrentInput("");
        setError(null);
      }
    }, [isVisible, scope]);
    const currentStepConfig = WIZARD_STEPS[currentStep];
    const handleNext = 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 = 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]);
    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__ */ jsxs(
      Box,
      {
        flexDirection: "column",
        borderStyle: "round",
        borderColor: "yellowBright",
        paddingX: 1,
        marginTop: 1,
        children: [
          /* @__PURE__ */ jsxs(Box, { marginBottom: 1, children: [
            /* @__PURE__ */ jsxs(Text, { bold: true, color: "yellowBright", children: [
              "Add ",
              scopeLabel,
              " Prompt"
            ] }),
            /* @__PURE__ */ jsxs(Text, { color: "gray", children: [
              " ",
              "(Step ",
              currentStep + 1,
              "/",
              WIZARD_STEPS.length,
              ")"
            ] })
          ] }),
          /* @__PURE__ */ jsxs(Box, { flexDirection: "column", children: [
            /* @__PURE__ */ jsxs(Text, { bold: true, children: [
              currentStepConfig.label,
              ":"
            ] }),
            /* @__PURE__ */ jsx(Text, { color: "gray", children: currentStepConfig.placeholder })
          ] }),
          /* @__PURE__ */ jsxs(Box, { marginTop: 1, flexDirection: "column", children: [
            /* @__PURE__ */ jsxs(Box, { children: [
              /* @__PURE__ */ jsx(Text, { color: "cyan", children: "> " }),
              /* @__PURE__ */ jsx(Text, { children: currentInput }),
              /* @__PURE__ */ jsx(Text, { color: "cyan", children: "_" })
            ] }),
            currentStepConfig.multiline && /* @__PURE__ */ jsx(Text, { color: "gray", italic: true, children: "(Shift+Enter for newline)" })
          ] }),
          error && /* @__PURE__ */ jsx(Box, { marginTop: 1, children: /* @__PURE__ */ jsx(Text, { color: "red", children: error }) }),
          /* @__PURE__ */ jsx(Box, { marginTop: 1, children: /* @__PURE__ */ jsxs(Text, { color: "gray", children: [
            "Enter to continue \u2022 Esc to ",
            currentStep > 0 ? "go back" : "cancel"
          ] }) })
        ]
      }
    );
  }
);
var PromptAddWizard_default = PromptAddWizard;
export {
  PromptAddWizard_default as default
};
