"use client";
import { jsx as _jsx } from "react/jsx-runtime";
import { useCallback, useMemo } from "react";
import { useWizardState } from "../resource-creation/useWizardState";
import { WizardShell } from "../resource-creation/WizardShell";
import { useCreateAgent } from "./useCreateAgent";
import { IdentityStep } from "./steps/IdentityStep";
import { CapabilitiesStep } from "./steps/CapabilitiesStep";
import { ReviewStep, buildAgentInput } from "./steps/ReviewStep";
import { createInitialWizardData } from "./steps/types";
// ---------------------------------------------------------------------------
// Step definitions
// ---------------------------------------------------------------------------
const STEPS = [
    {
        id: "identity",
        label: "Identity & Instructions",
        validate: (data) => {
            if (!data.name.trim())
                return "Name is required";
            return null;
        },
    },
    {
        id: "capabilities",
        label: "Capabilities",
    },
    {
        id: "review",
        label: "Review & Create",
    },
];
// ---------------------------------------------------------------------------
// Component
// ---------------------------------------------------------------------------
/**
 * Multi-step wizard for creating a new agent blueprint.
 *
 * Three condensed steps:
 * 1. **Identity & Instructions** — name, slug, description, visibility, system prompt
 * 2. **Capabilities** — MCP servers + tools, skills, env var declarations (all optional)
 * 3. **Review & Create** — YAML preview + submission
 *
 * Uses the shared `WizardShell` layout and `useWizardState` hook for
 * navigation. Form data accumulates in the wizard state; on the final
 * step the user reviews and confirms creation.
 *
 * This component is an SDK-first, embeddable wizard with zero Console
 * dependencies. Platform builders can mount it anywhere.
 *
 * @example
 * ```tsx
 * <AgentCreationWizard
 *   org="acme"
 *   onComplete={({ org, slug }) => navigate(`/agents/${org}/${slug}`)}
 *   onCancel={() => navigate("/agents")}
 * />
 * ```
 */
export function AgentCreationWizard({ org, initialData, onComplete, onCancel, className, }) {
    const mergedInitialData = useMemo(() => ({ ...createInitialWizardData(), ...initialData }), 
    // initialData is consumed once at mount — not reactive by design
    [initialData]);
    const wizard = useWizardState({
        steps: STEPS,
        initialData: mergedInitialData,
    });
    const { create, isCreating, error, clearError } = useCreateAgent();
    const handleNext = useCallback(() => {
        if (wizard.isLastStep) {
            handleSubmit();
        }
        else {
            clearError();
            wizard.goNext();
        }
    }, [wizard.isLastStep, wizard.goNext, clearError]);
    const handleSubmit = useCallback(async () => {
        clearError();
        const input = buildAgentInput(org, wizard.data);
        try {
            const agent = await create(input);
            onComplete({
                org: agent.metadata?.org ?? org,
                slug: agent.metadata?.slug ?? input.slug ?? input.name,
                name: agent.metadata?.name ?? input.name,
            });
        }
        catch {
            // Error is captured by useCreateAgent and displayed in ReviewStep
        }
    }, [org, wizard.data, create, clearError, onComplete]);
    const currentStepContent = useMemo(() => {
        switch (wizard.currentStep.id) {
            case "identity":
                return (_jsx(IdentityStep, { data: wizard.data, updateData: wizard.updateData, validationError: wizard.validationError }));
            case "capabilities":
                return (_jsx(CapabilitiesStep, { org: org, data: wizard.data, updateData: wizard.updateData }));
            case "review":
                return (_jsx(ReviewStep, { org: org, data: wizard.data, isCreating: isCreating, error: error }));
            default:
                return null;
        }
    }, [
        wizard.currentStep.id,
        wizard.data,
        wizard.updateData,
        wizard.validationError,
        org,
        isCreating,
        error,
    ]);
    return (_jsx(WizardShell, { steps: STEPS, currentStepIndex: wizard.currentStepIndex, submitLabel: "Create agent", isSubmitting: isCreating, canGoNext: wizard.canGoNext, canGoBack: wizard.canGoBack, onNext: handleNext, onBack: wizard.goBack, onGoToStep: wizard.goToStep, onCancel: onCancel, className: className, children: currentStepContent }));
}
//# sourceMappingURL=AgentCreationWizard.js.map