import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { preVerificationCheckQueryOptions } from '../queries/preVerificationCheckQuery.js';
import { simulationCheckQueryOptions } from '../queries/simulationCheckQuery.js';
import { useGasEstimation } from '../../../hooks/useGasEstimation.js';
import { CREATEX_ADDRESS, createXABI } from '../../../util/createx/constants.js';
import { useQuery } from '@tanstack/react-query';
import { Badge, Spinner } from '@inkjs/ui';
import { Text, Box } from 'ink';
import { encodeFunctionData, zeroAddress } from 'viem';
import { useConfig } from 'wagmi';
import { getBlockExplorerAddressLink } from '../../../util/blockExplorer.js';
import { PrivateKeyExecution } from './PrivateKeyExecution.js';
import { ExternalSignerExecution } from './ExternalSignerExecution.js';
import { GasEstimation } from './GasEstimation.js';
import { supersimL1 } from '@eth-optimism/viem/chains';
import { privateKeyToAccount } from 'viem/accounts';
export const DeployStatus = ({ chain, initCode, baseSalt, deterministicAddress, executionOption, }) => {
    const wagmiConfig = useConfig();
    const { data: preVerificationCheckData, isLoading: isPreVerificationCheckLoading, error: preVerificationCheckError, } = useQuery({
        ...preVerificationCheckQueryOptions(wagmiConfig, {
            deterministicAddress,
            initCode,
            baseSalt,
            chainId: chain.id,
        }),
    });
    const { data: simulationData, isLoading: isSimulationLoading, error: simulationError, } = useQuery({
        ...simulationCheckQueryOptions(wagmiConfig, {
            deterministicAddress,
            initCode,
            baseSalt,
            chainId: chain.id,
        }),
    });
    const { data: gasEstimation, isLoading: isGasEstimationLoading } = useGasEstimation({
        chainId: chain.id,
        to: CREATEX_ADDRESS,
        account: 
        // TODO: fix - hacky way to get the account for the chain
        chain.sourceId === supersimL1.id
            ? privateKeyToAccount('0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80')
            : executionOption?.type === 'privateKey'
                ? privateKeyToAccount(executionOption.privateKey)
                : zeroAddress,
        data: encodeFunctionData({
            abi: createXABI,
            functionName: 'deployCreate2',
            args: [baseSalt, initCode],
        }),
    });
    if (preVerificationCheckError) {
        return (_jsxs(Box, { gap: 1, children: [_jsx(Badge, { color: "red", children: "Error" }), _jsxs(Text, { children: ["Pre-verification check failed:", ' ', preVerificationCheckError.message.split('\n')[0]] })] }));
    }
    if (isPreVerificationCheckLoading || !preVerificationCheckData) {
        return _jsx(Spinner, { label: "Checking if contract is already deployed" });
    }
    if (preVerificationCheckData.isAlreadyDeployed) {
        return (_jsxs(Box, { gap: 1, children: [_jsx(Badge, { color: "green", children: "Deployed" }), _jsx(Text, { children: "Contract is already deployed" }), _jsx(Text, { children: getBlockExplorerAddressLink(chain, deterministicAddress) })] }));
    }
    if (simulationError) {
        return (_jsxs(Box, { gap: 1, children: [_jsx(Badge, { color: "red", children: "Error" }), _jsxs(Text, { children: ["Simulation check failed: ", simulationError.message.split('\n')[0]] })] }));
    }
    if (isSimulationLoading || !simulationData) {
        return (_jsx(Spinner, { label: "Simulating deployment to check for address mismatch" }));
    }
    if (!simulationData.isAddressSameAsExpected) {
        return (_jsxs(Box, { gap: 1, children: [_jsx(Badge, { color: "red", children: "Failed" }), _jsx(Text, { children: "Address mismatch" })] }));
    }
    if (!executionOption) {
        return (_jsxs(Box, { gap: 1, children: [_jsx(Badge, { color: "blue", children: "Ready" }), _jsx(Text, { children: "Estimated fees" }), isGasEstimationLoading || !gasEstimation ? (_jsx(Spinner, {})) : (_jsx(GasEstimation, { gasEstimation: gasEstimation }))] }));
    }
    if (executionOption.type === 'privateKey' ||
        executionOption.type === 'sponsoredSender') {
        return (_jsx(PrivateKeyExecution, { chain: chain, initCode: initCode, baseSalt: baseSalt, deterministicAddress: deterministicAddress }));
    }
    return (_jsx(ExternalSignerExecution, { chain: chain, initCode: initCode, baseSalt: baseSalt, deterministicAddress: deterministicAddress }));
};
