"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 { useCreateMcpServer } from "./useCreateMcpServer";
import { IdentityTransportStep } from "./steps/IdentityTransportStep";
import { EnvironmentAuthStep } from "./steps/EnvironmentAuthStep";
import { ReviewStep, buildMcpServerInput } from "./steps/ReviewStep";
import { createInitialMcpServerWizardData } from "./steps/types";
// ---------------------------------------------------------------------------
// Step definitions
// ---------------------------------------------------------------------------
const STEPS = [
    {
        id: "identity-transport",
        label: "Identity & Transport",
        validate: (data) => {
            if (!data.name.trim())
                return "Name is required";
            if (data.transportType === "http" && !data.httpUrl.trim()) {
                return "HTTP URL is required";
            }
            if (data.transportType === "stdio" && !data.stdioCommand.trim()) {
                return "Stdio command is required";
            }
            return null;
        },
    },
    {
        id: "environment-auth",
        label: "Environment & Auth",
    },
    {
        id: "review",
        label: "Review & Create",
    },
];
// ---------------------------------------------------------------------------
// Component
// ---------------------------------------------------------------------------
/**
 * Multi-step wizard for creating a new MCP server blueprint.
 *
 * Three steps:
 * 1. **Identity & Transport** — name, slug, description, visibility, transport config
 * 2. **Environment & Auth** — env var declarations, OAuth auth configuration (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
 * <McpServerCreationWizard
 *   org="acme"
 *   onComplete={({ org, slug }) => navigate(`/mcp-servers/${org}/${slug}`)}
 *   onCancel={() => navigate("/mcp-servers")}
 * />
 * ```
 */
export function McpServerCreationWizard({ org, initialData, onComplete, onCancel, className, }) {
    const mergedInitialData = useMemo(() => ({ ...createInitialMcpServerWizardData(), ...initialData }), 
    // initialData is consumed once at mount — not reactive by design
    [initialData]);
    const wizard = useWizardState({
        steps: STEPS,
        initialData: mergedInitialData,
    });
    const { create, isCreating, error, clearError } = useCreateMcpServer();
    const handleNext = useCallback(() => {
        if (wizard.isLastStep) {
            handleSubmit();
        }
        else {
            clearError();
            wizard.goNext();
        }
    }, [wizard.isLastStep, wizard.goNext, clearError]);
    const handleSubmit = useCallback(async () => {
        clearError();
        const input = buildMcpServerInput(org, wizard.data);
        try {
            const server = await create(input);
            onComplete({
                org: server.metadata?.org ?? org,
                slug: server.metadata?.slug ?? input.slug ?? input.name,
                name: server.metadata?.name ?? input.name,
            });
        }
        catch {
            // Error is captured by useCreateMcpServer and displayed in ReviewStep
        }
    }, [org, wizard.data, create, clearError, onComplete]);
    const currentStepContent = useMemo(() => {
        switch (wizard.currentStep.id) {
            case "identity-transport":
                return (_jsx(IdentityTransportStep, { data: wizard.data, updateData: wizard.updateData, validationError: wizard.validationError }));
            case "environment-auth":
                return (_jsx(EnvironmentAuthStep, { 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 MCP server", isSubmitting: isCreating, canGoNext: wizard.canGoNext, canGoBack: wizard.canGoBack, onNext: handleNext, onBack: wizard.goBack, onGoToStep: wizard.goToStep, onCancel: onCancel, className: className, children: currentStepContent }));
}
//# sourceMappingURL=McpServerCreationWizard.js.map