import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
/**
 * @license
 * Copyright 2025 Vybestack LLC
 * SPDX-License-Identifier: Apache-2.0
 */
import { useState, useCallback } from 'react';
import { Box, Text } from 'ink';
import { Colors } from '../../colors.js';
import { RadioButtonSelect } from '../shared/RadioButtonSelect.js';
import { WizardStep } from './types.js';
import { getNextStep, getPreviousStep } from './utils.js';
import { ProviderSelectStep } from './ProviderSelectStep.js';
import { BaseUrlConfigStep } from './BaseUrlConfigStep.js';
import { ModelSelectStep } from './ModelSelectStep.js';
import { AuthenticationStep } from './AuthenticationStep.js';
import { AdvancedParamsStep } from './AdvancedParamsStep.js';
import { ProfileSaveStep } from './ProfileSaveStep.js';
import { ProfileSuccessSummary } from './ProfileSuccessSummary.js';
export const ProfileCreateWizard = ({ onClose, onLoadProfile, availableProviders, }) => {
    const [state, setState] = useState({
        currentStep: WizardStep.PROVIDER_SELECT,
        stepHistory: [WizardStep.PROVIDER_SELECT],
        config: {
            provider: null,
            model: null,
            auth: {
                type: null,
            },
        },
        validationErrors: {},
        skipValidation: false,
    });
    const [showCancelConfirm, setShowCancelConfirm] = useState(false);
    const navigateToStep = useCallback((nextStep) => {
        setState((prev) => ({
            ...prev,
            currentStep: nextStep,
            stepHistory: [...prev.stepHistory, nextStep],
        }));
    }, []);
    const goBack = useCallback(() => {
        setState((prev) => {
            const prevStep = getPreviousStep(prev);
            return {
                ...prev,
                currentStep: prevStep,
                stepHistory: prev.stepHistory.slice(0, -1),
            };
        });
    }, []);
    const handleContinue = useCallback(() => {
        const nextStep = getNextStep(state.currentStep, state);
        navigateToStep(nextStep);
    }, [state, navigateToStep]);
    const handleCancel = useCallback(() => {
        // If no config entered yet (still on first step with no provider), exit immediately
        if (state.currentStep === WizardStep.PROVIDER_SELECT &&
            !state.config.provider) {
            onClose();
            return;
        }
        // Show confirmation dialog
        setShowCancelConfirm(true);
    }, [state.currentStep, state.config.provider, onClose]);
    const handleCancelConfirm = useCallback(() => {
        onClose();
    }, [onClose]);
    const handleCancelResume = useCallback(() => {
        setShowCancelConfirm(false);
    }, []);
    const handleCancelDialogSelect = useCallback((value) => {
        if (value === 'confirm') {
            handleCancelConfirm();
        }
        else {
            handleCancelResume();
        }
    }, [handleCancelConfirm, handleCancelResume]);
    const updateConfig = useCallback((key, value) => {
        setState((prev) => ({
            ...prev,
            config: {
                ...prev.config,
                [key]: value,
            },
        }));
    }, []);
    const handleUpdateProvider = useCallback((provider) => {
        // Navigate based on the new provider value
        setState((prev) => {
            const newConfig = { ...prev.config, provider };
            const tempState = { ...prev, config: newConfig };
            const nextStep = getNextStep(WizardStep.PROVIDER_SELECT, tempState);
            return {
                ...prev,
                config: newConfig,
                currentStep: nextStep,
                stepHistory: [...prev.stepHistory, nextStep],
            };
        });
    }, []);
    const handleUpdateBaseUrl = useCallback((baseUrl) => updateConfig('baseUrl', baseUrl), [updateConfig]);
    const handleUpdateModel = useCallback((model) => updateConfig('model', model), [updateConfig]);
    const handleUpdateAuth = useCallback((auth) => updateConfig('auth', auth), [updateConfig]);
    const handleUpdateParams = useCallback((params) => updateConfig('params', params), [updateConfig]);
    const handleUpdateProfileName = useCallback((name) => {
        setState((prev) => ({ ...prev, profileName: name }));
    }, []);
    // Render current step
    const renderStep = () => {
        switch (state.currentStep) {
            case WizardStep.PROVIDER_SELECT:
                return (_jsx(ProviderSelectStep, { state: state, onUpdateProvider: handleUpdateProvider, onCancel: handleCancel, availableProviders: availableProviders }));
            case WizardStep.BASE_URL_CONFIG:
                return (_jsx(BaseUrlConfigStep, { state: state, onUpdateBaseUrl: handleUpdateBaseUrl, onContinue: handleContinue, onBack: goBack }));
            case WizardStep.MODEL_SELECT:
                return (_jsx(ModelSelectStep, { state: state, onUpdateModel: handleUpdateModel, onContinue: handleContinue, onBack: goBack, onCancel: handleCancel }));
            case WizardStep.AUTHENTICATION:
                return (_jsx(AuthenticationStep, { state: state, onUpdateAuth: handleUpdateAuth, onContinue: handleContinue, onBack: goBack, onCancel: handleCancel }));
            case WizardStep.ADVANCED_PARAMS:
                return (_jsx(AdvancedParamsStep, { state: state, onUpdateParams: handleUpdateParams, onContinue: handleContinue, onBack: goBack, onCancel: handleCancel }));
            case WizardStep.SAVE_PROFILE:
                return (_jsx(ProfileSaveStep, { state: state, onUpdateProfileName: handleUpdateProfileName, onContinue: handleContinue, onBack: goBack, onCancel: handleCancel }));
            case WizardStep.SUCCESS_SUMMARY:
                return (_jsx(ProfileSuccessSummary, { state: state, onClose: onClose, onLoadProfile: onLoadProfile }));
            default:
                return _jsx(Text, { color: Colors.Foreground, children: "Unknown step" });
        }
    };
    // Render cancel confirmation dialog if needed
    if (showCancelConfirm) {
        return (_jsxs(Box, { borderStyle: "round", borderColor: Colors.Gray, flexDirection: "column", padding: 1, children: [_jsx(Text, { bold: true, color: Colors.AccentYellow, children: "Cancel Profile Creation?" }), _jsx(Text, { color: Colors.Foreground, children: " " }), _jsx(Text, { color: Colors.Foreground, children: "Your configuration will be lost:" }), state.config.provider && (_jsxs(Text, { color: Colors.Gray, children: [" \u2022 Provider: ", state.config.provider] })), state.config.model && (_jsxs(Text, { color: Colors.Gray, children: [" \u2022 Model: ", state.config.model] })), state.config.baseUrl && (_jsxs(Text, { color: Colors.Gray, children: [" \u2022 Base URL: ", state.config.baseUrl] })), _jsx(Text, { color: Colors.Foreground, children: " " }), _jsx(Text, { color: Colors.Foreground, children: "Are you sure you want to cancel?" }), _jsx(Text, { color: Colors.Foreground, children: " " }), _jsx(RadioButtonSelect, { items: [
                        {
                            label: 'No, continue editing',
                            value: 'resume',
                            key: 'resume',
                        },
                        {
                            label: 'Yes, discard and exit',
                            value: 'confirm',
                            key: 'confirm',
                        },
                    ], onSelect: handleCancelDialogSelect, isFocused: true })] }));
    }
    return (_jsx(Box, { borderStyle: "round", borderColor: Colors.Gray, flexDirection: "column", padding: 1, children: renderStep() }));
};
//# sourceMappingURL=index.js.map