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 './EnterFoundryProjectPath.js';
import { SelectContract } from './SelectContract.js';
import { ConfigureConstructorArguments } from './ConfigureConstructorArguments.js';
import { ConfigureSalt } from './ConfigureSalt.js';
import { SelectNetwork } from './SelectNetwork.js';
import { SelectChains } from './SelectChains.js';
import { getArtifactPathForContract } from '../forge/foundryProject.js';
import { useSaveWizardProgress } from '../hooks/useSaveWizardProgress.js';
import { DeployCreate2Command } from '../deploy-create2/DeployCreate2Command.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 } = useDeployCreate2WizardStore();
    if (wizardState.stepId === 'completed') {
        return (_jsx(Box, { children: _jsx(Text, { children: "Completed" }) }));
    }
    return (_jsx(Box, { flexDirection: "column", children: steps
            .filter(({ id }) => id !== 'completed')
            .map(({ id }) => {
            return _jsx(WizardProgressForStep, { stepId: id }, id);
        }) }));
};
export const DeployCreate2Wizard = () => {
    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,
        };
        console.log(Object.entries(options)
            .map(([key, value]) => `--${key.replace(/[A-Z]/g, letter => `-${letter.toLowerCase()}`)} ${value}`)
            .join(' '));
        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, {})] }));
};
