import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useState, useMemo, useEffect } from "react";
import { RefreshCw } from "lucide-react";
import { WelcomeStep } from "./setup-wizard/steps/WelcomeStep";
import { CreateSettingsStep } from "./setup-wizard/steps/CreateSettingsStep";
import { LicenseEmailStep, } from "./setup-wizard/steps/LicenseEmailStep";
import { LicenseActivationStep } from "./setup-wizard/steps/LicenseActivationStep";
import { ProviderStep } from "./setup-wizard/steps/ProviderStep";
import { ModelSelectionStep } from "./setup-wizard/steps/ModelSelectionStep";
import { CompleteStep } from "./setup-wizard/steps/CompleteStep";
import { getLicenseStatus } from "../licensing/licenseService";
export function SetupWizard({ onComplete, settingsExist }) {
    const [currentStep, setCurrentStep] = useState("welcome");
    const [licenseEmail, setLicenseEmail] = useState("");
    const [licenseMode, setLicenseMode] = useState("trial_form");
    const [licenseConfigured, setLicenseConfigured] = useState(false);
    const [providerId, setProviderId] = useState();
    const [chatModelId, setChatModelId] = useState();
    const [imageModelId, setImageModelId] = useState();
    const [embeddingsModelId, setEmbeddingsModelId] = useState();
    /** null = still loading license-status; when true, wizard skips license steps (matches LicenseContext / About). */
    const [licensingDisabled, setLicensingDisabled] = useState(null);
    useEffect(() => {
        let cancelled = false;
        getLicenseStatus()
            .then((status) => {
            if (!cancelled)
                setLicensingDisabled(!!status.licensingDisabled);
        })
            .catch(() => {
            if (!cancelled)
                setLicensingDisabled(false);
        });
        return () => {
            cancelled = true;
        };
    }, []);
    const includeLicenseSteps = licensingDisabled === false;
    const steps = useMemo(() => {
        const s = ["welcome"];
        if (!settingsExist)
            s.push("create-settings");
        if (includeLicenseSteps) {
            s.push("license-email", "license-activation");
        }
        s.push("provider", "chat-model", "image-model", "embeddings-model", "complete");
        return s;
    }, [settingsExist, includeLicenseSteps]);
    const handleBack = () => {
        const currentIndex = steps.indexOf(currentStep);
        if (currentIndex > 0) {
            const prevStep = steps[currentIndex - 1];
            if (prevStep) {
                setCurrentStep(prevStep);
            }
        }
    };
    const renderStep = () => {
        switch (currentStep) {
            case "welcome":
                return _jsx(WelcomeStep, { onNext: () => {
                        const welcomeIndex = steps.indexOf("welcome");
                        const nextStep = steps[welcomeIndex + 1];
                        if (nextStep) {
                            setCurrentStep(nextStep);
                        }
                    } });
            case "create-settings":
                return (_jsx(CreateSettingsStep, { onNext: () => setCurrentStep(includeLicenseSteps ? "license-email" : "provider"), onBack: handleBack }));
            case "license-email":
                return (_jsx(LicenseEmailStep, { onNext: ({ email, mode }) => {
                        setLicenseEmail(email);
                        setLicenseMode(mode);
                        setCurrentStep("license-activation");
                    }, onBack: handleBack }));
            case "license-activation":
                return (_jsx(LicenseActivationStep, { email: licenseEmail, mode: licenseMode, onBack: handleBack, onActivated: () => {
                        setLicenseConfigured(true);
                        setCurrentStep("provider");
                    } }));
            case "provider":
                return (_jsx(ProviderStep, { onNext: (id) => {
                        setProviderId(id);
                        setCurrentStep("chat-model");
                    }, onSkip: () => setCurrentStep("chat-model"), onBack: handleBack }));
            case "chat-model":
                return (_jsx(ModelSelectionStep, { category: "chat", providerId: providerId, onNext: (id) => {
                        setChatModelId(id);
                        setCurrentStep("image-model");
                    }, onSkip: () => setCurrentStep("image-model"), onBack: handleBack }));
            case "image-model":
                return (_jsx(ModelSelectionStep, { category: "image", providerId: providerId, onNext: (id) => {
                        setImageModelId(id);
                        setCurrentStep("embeddings-model");
                    }, onSkip: () => setCurrentStep("embeddings-model"), onBack: handleBack }));
            case "embeddings-model":
                return (_jsx(ModelSelectionStep, { category: "embeddings", providerId: providerId, onNext: (id) => {
                        setEmbeddingsModelId(id);
                        setCurrentStep("complete");
                    }, onSkip: () => setCurrentStep("complete"), onBack: handleBack }));
            case "complete":
                return (_jsx(CompleteStep, { licenseConfigured: licenseConfigured || licensingDisabled === true, chatModelId: chatModelId, imageModelId: imageModelId, embeddingsModelId: embeddingsModelId, onFinish: onComplete, onBack: handleBack }));
        }
    };
    const getStepProgress = () => {
        const index = steps.indexOf(currentStep);
        return ((index + 1) / steps.length) * 100;
    };
    if (licensingDisabled === null) {
        return (_jsx("div", { className: "fixed inset-0 flex items-center justify-center bg-gray-900/40 backdrop-blur-sm z-50 p-4", children: _jsx("div", { className: "w-full max-w-2xl", children: _jsx("div", { className: "shadow-2xl border-none overflow-hidden bg-white rounded-2xl", children: _jsxs("div", { className: "min-h-[520px] flex flex-col items-center justify-center gap-3 text-gray-600", children: [_jsx(RefreshCw, { className: "h-10 w-10 animate-spin text-blue-600" }), _jsx("p", { className: "text-sm font-medium", children: "Loading\u2026" })] }) }) }) }));
    }
    return (_jsx("div", { className: "fixed inset-0 flex items-center justify-center bg-gray-900/40 backdrop-blur-sm z-50 p-4", children: _jsxs("div", { className: "w-full max-w-2xl", children: [_jsxs("div", { className: "shadow-2xl border-none overflow-hidden bg-white rounded-2xl", children: [_jsx("div", { className: "h-1 w-full bg-gray-100", children: _jsx("div", { className: "h-full bg-blue-600 transition-all duration-700 ease-out", style: { width: `${getStepProgress()}%` } }) }), _jsx("div", { className: "min-h-[520px] flex flex-col relative", children: renderStep() }, currentStep)] }), _jsx("div", { className: "mt-8 flex justify-center gap-3", children: steps.map((step, idx) => (_jsx("div", { className: `h-1.5 rounded-full transition-all duration-300 ${currentStep === step ? "bg-blue-600 w-8" : "bg-gray-300 w-1.5"}` }, step))) })] }) }));
}
//# sourceMappingURL=SetupWizard.js.map