import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { deployCreate2WizardIndexByStepId, useDeployCreate2WizardStore, } from './deployCreate2WizardStore.js';
import { Box, Text } from 'ink';
import { EnterFoundryProjectPath } from './steps/EnterFoundryProjectPath.js';
import { SelectContract } from './steps/SelectContract.js';
import { ConfigureConstructorArguments } from './steps/ConfigureConstructorArguments.js';
import { ConfigureSalt } from './steps/ConfigureSalt.js';
import { SelectNetwork } from './steps/SelectNetwork.js';
import { SelectChains } from './steps/SelectChains.js';
import { ShouldVerifyContract } from './steps/ShouldVerifyContract.js';
import { getArtifactPathForContract } from '../../../util/forge/foundryProject.js';
import { useSaveWizardProgress } from '../../../hooks/useSaveWizardProgress.js';
import { DeployCreate2Command } from '../DeployCreate2Command.js';
import { toCliFlags } from '../../../util/toCliFlags.js';
import { BackNavigation } from '../../../components/navigation/BackNavigation.js';
const useStepProgress = ({ stepId, }) => {
    const { steps, wizardState } = useDeployCreate2WizardStore();
    const currentIndex = deployCreate2WizardIndexByStepId[wizardState.stepId];
    const stepIndex = deployCreate2WizardIndexByStepId[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 } = useDeployCreate2WizardStore();
    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 DeployCreate2Wizard = ({ isPrepareMode, }) => {
    const { wizardState } = useDeployCreate2WizardStore();
    useSaveWizardProgress('deployCreate2', wizardState, ['completed']);
    const stepId = wizardState.stepId;
    if (stepId === 'completed') {
        const options = {
            chains: wizardState.chainNames,
            salt: wizardState.salt,
            forgeArtifactPath: getArtifactPathForContract(wizardState.foundryProjectPath, wizardState.selectedContract),
            constructorArgs: wizardState.constructorArgs.join(','),
            network: wizardState.network,
            verify: wizardState.shouldVerifyContract,
        };
        if (isPrepareMode) {
            console.log(`sup deploy create2 ${toCliFlags(options)}`);
            // TODO: hacky way to quit until we remove pastel
            setTimeout(() => {
                process.exit(0);
            }, 1);
            return null;
        }
        return _jsx(DeployCreate2Command, { options: options });
    }
    return (_jsxs(Box, { flexDirection: "column", gap: 1, children: [_jsx(Text, { bold: true, color: "blue", children: "\uD83D\uDE80 Deploy Create2 Wizard" }), _jsx(WizardProgress, {}), stepId === 'enter-foundry-project-path' && _jsx(EnterFoundryProjectPath, {}), stepId === 'select-contract' && _jsx(SelectContract, {}), stepId === 'configure-constructor-arguments' && (_jsx(ConfigureConstructorArguments, {})), stepId === 'configure-salt' && _jsx(ConfigureSalt, {}), stepId === 'select-network' && _jsx(SelectNetwork, {}), stepId === 'select-chains' && _jsx(SelectChains, {}), stepId === 'should-verify-contract' && _jsx(ShouldVerifyContract, {})] }));
};
