import { jsx as _jsx } from "react/jsx-runtime";
import { useDeployCreate2WizardStore } from '../deployCreate2WizardStore.js';
import { getConstructorAbi } from '../../../../util/abi.js';
import { Box } from 'ink';
import { AbiItemForm } from '../../../../components/AbiItemForm.js';
import { getArtifactPathForContract } from '../../../../util/forge/foundryProject.js';
import { useForgeArtifact } from '../../../../queries/forgeArtifact.js';
import { Spinner } from '@inkjs/ui';
import { useEffect } from 'react';
export const ConfigureConstructorArguments = () => {
    const { wizardState, submitConfigureConstructorArguments } = useDeployCreate2WizardStore();
    if (wizardState.stepId !== 'configure-constructor-arguments') {
        throw new Error('Invalid state');
    }
    const path = getArtifactPathForContract(wizardState.foundryProjectPath, wizardState.selectedContract);
    const { data: artifact, isLoading } = useForgeArtifact(path);
    const constructorAbi = artifact === undefined ? undefined : getConstructorAbi(artifact.abi);
    useEffect(() => {
        if (artifact && !constructorAbi) {
            // Some contracts don't have a constructor
            submitConfigureConstructorArguments({ constructorArgs: [] });
        }
    }, [artifact]);
    if (isLoading || !artifact) {
        return (_jsx(Box, { flexDirection: "column", children: _jsx(Spinner, { label: "Loading artifact..." }) }));
    }
    if (!constructorAbi) {
        return null;
    }
    return (_jsx(Box, { flexDirection: "column", children: _jsx(AbiItemForm, { abiItem: constructorAbi, onSubmit: result => {
                // @ts-ignore TODO fix type
                submitConfigureConstructorArguments({ constructorArgs: result });
            } }) }));
};
