import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { bridgeWizardIndexByStepId, useBridgeWizardStore, } from './bridgeWizardStore.js';
import { EnterAmount } from './steps/EnterAmount.js';
import { EnterRecipient } from './steps/EnterRecipient.js';
import { SelectChains } from './steps/SelectChains.js';
import { SelectNetwork } from './steps/SelectNetwork.js';
import BridgeEntrypoint from '../../../commands/bridge.js';
import { useSaveWizardProgress } from '../../../hooks/useSaveWizardProgress.js';
import { toCliFlags } from '../../../util/toCliFlags.js';
import { Box, Text } from 'ink';
import { BackNavigation } from '../../../components/navigation/BackNavigation.js';
const useStepProgress = ({ stepId, }) => {
    const { steps, wizardState } = useBridgeWizardStore();
    const currentIndex = bridgeWizardIndexByStepId[wizardState.stepId];
    const stepIndex = bridgeWizardIndexByStepId[stepId];
    const step = steps[stepIndex];
    if (stepIndex < currentIndex) {
        return {
            status: 'done',
            title: step.title,
            summary: step.getSummary
                ? step.getSummary(wizardState)
                : undefined,
        };
    }
    if (stepIndex === currentIndex) {
        return {
            status: 'current',
            title: step.title,
        };
    }
    return {
        status: 'upcoming',
        title: step.title,
    };
};
const WizardProgressForStep = ({ stepId }) => {
    const { status, title, summary } = useStepProgress({ stepId });
    return (_jsxs(Box, { gap: 1, paddingX: 1, children: [_jsx(Text, { color: status === 'done' ? 'green' : status === 'current' ? 'blue' : 'gray', children: status === 'done' ? '✓' : status === 'current' ? '>' : '○' }), _jsxs(Text, { color: status === 'current' ? 'blue' : 'white', children: [" ", title] }), status === 'done' && summary && _jsx(Text, { color: "yellow", children: summary })] }));
};
const WizardProgress = () => {
    const { steps, wizardState, goToPreviousStep } = useBridgeWizardStore();
    if (wizardState.stepId === 'completed') {
        return (_jsx(Box, { children: _jsx(Text, { children: "Completed" }) }));
    }
    const isFirstStep = wizardState.stepId === steps[0]?.id;
    return (_jsxs(Box, { flexDirection: "column", children: [steps
                .filter(({ id }) => id !== 'completed')
                .map(({ id }) => {
                return _jsx(WizardProgressForStep, { stepId: id }, id);
            }), !isFirstStep && (_jsx(Box, { marginTop: 1, children: _jsx(BackNavigation, { onBack: goToPreviousStep }) }))] }));
};
export const BridgeWizard = ({ isPrepareMode }) => {
    const { wizardState } = useBridgeWizardStore();
    // TODO: update before alpha release, remove private key step entirely from wizard
    useSaveWizardProgress('bridge', wizardState, [
        'completed',
        'enter-private-key',
    ]);
    const stepId = wizardState.stepId;
    if (wizardState.stepId === 'completed') {
        const options = {
            network: wizardState.network,
            chains: wizardState.chains,
            amount: wizardState.amount,
            recipient: wizardState.recipient,
        };
        if (isPrepareMode) {
            console.log(`sup bridge ${toCliFlags(options)}`);
            // TODO: hacky way to quit until we remove pastel
            setTimeout(() => {
                process.exit(0);
            }, 1);
            return null;
        }
        return _jsx(BridgeEntrypoint, { options: options });
    }
    return (_jsxs(Box, { flexDirection: "column", gap: 1, children: [_jsx(Text, { bold: true, color: "blue", children: "\uD83C\uDF09 Bridge Wizard" }), _jsx(WizardProgress, {}), _jsxs(Box, { flexDirection: "column", children: [stepId === 'enter-recipient' && _jsx(EnterRecipient, {}), stepId === 'select-network' && _jsx(SelectNetwork, {}), stepId === 'select-chains' && _jsx(SelectChains, {}), stepId === 'enter-amount' && _jsx(EnterAmount, {})] })] }));
};
