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";
const setupWizardBackgroundUrl = new URL("../../images/Parhelia-Onboarding-BG.jpg", import.meta.url).href;
const setupWizardBackdropClassName = "fixed inset-0 z-50 flex items-center justify-center overflow-hidden bg-cover bg-center bg-no-repeat p-4 backdrop-blur-sm";
const setupWizardBackdropStyle = {
    backgroundImage: `url("${setupWizardBackgroundUrl}")`,
};
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 skipLicenseStep = () => {
        setLicenseConfigured(true);
        setCurrentStep("provider");
    };
    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, onSkip: skipLicenseStep }));
            case "license-activation":
                return (_jsx(LicenseActivationStep, { email: licenseEmail, mode: licenseMode, onBack: handleBack, onActivated: skipLicenseStep }));
            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 }));
        }
    };
    if (licensingDisabled === null) {
        return (_jsx("div", { className: setupWizardBackdropClassName, style: setupWizardBackdropStyle, children: _jsx("div", { className: "relative z-10 w-full max-w-2xl", children: _jsx("div", { className: "overflow-hidden rounded-2xl border-none bg-white shadow-2xl", children: _jsxs("div", { className: "text-neutral-grey-50 flex flex-col items-center justify-center gap-3 p-12", children: [_jsx(RefreshCw, { className: "text-feedback-blue h-10 w-10 animate-spin" }), _jsx("p", { className: "text-sm font-medium", children: "Loading\u2026" })] }) }) }) }));
    }
    return (_jsx("div", { className: setupWizardBackdropClassName, style: setupWizardBackdropStyle, children: _jsxs("div", { className: "relative z-10 w-full max-w-2xl", children: [_jsx("div", { className: "overflow-hidden rounded-2xl border-none bg-white shadow-2xl", children: _jsx("div", { className: "relative flex flex-col", 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
                            ? "w-8 bg-white"
                            : "bg-neutral-grey-10 w-1.5"}` }, step))) })] }) }));
}
//# sourceMappingURL=SetupWizard.js.map