import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Box, Text } from 'ink';
import { useEffect } from 'react';
import { zodVerifyContractParams } from '../actions/verifyContract.js';
import { Badge, Spinner } from '@inkjs/ui';
import { useStandardJsonInputQuery } from '../actions/verify/getStandardJsonInputQuery.js';
import { useMappingChainByIdentifier } from '../queries/chainByIdentifier.js';
import { useMutation } from '@tanstack/react-query';
import { verifyOnBlockscoutMutation, verifyOnBlockscoutMutationKey, } from '../actions/verify/verifyOnBlockscoutMutation.js';
import { getBlockExplorerAddressLink } from '../util/blockExplorer.js';
import { useForgeArtifact } from '../queries/forgeArtifact.js';
const zodVerifyContractCommandParams = zodVerifyContractParams;
const VerifyCommand = ({ options, }) => {
    const { data: chainByIdentifier, isLoading: isChainByIdentifierLoading } = useMappingChainByIdentifier();
    const { data: forgeArtifact, isLoading: isForgeArtifactLoading } = useForgeArtifact(options.forgeArtifactPath);
    if (isChainByIdentifierLoading || isForgeArtifactLoading) {
        return _jsx(Spinner, {});
    }
    if (!chainByIdentifier) {
        return _jsx(Text, { children: "Error loading chains" });
    }
    if (!forgeArtifact) {
        return _jsx(Text, { children: "Error loading forge artifact" });
    }
    const chains = options.chains.map(chain => chainByIdentifier[`${options.network}/${chain}`]);
    return (_jsx(VerifyCommandInner, { chains: chains, forgeArtifactPath: options.forgeArtifactPath, forgeArtifact: forgeArtifact, contractAddress: options.contractAddress }));
};
export const VerifyCommandInner = ({ chains, forgeArtifactPath, contractAddress, forgeArtifact, index, }) => {
    const { data: standardJsonInput, isLoading: isStandardJsonInputLoading } = useStandardJsonInputQuery(forgeArtifactPath);
    if (isStandardJsonInputLoading) {
        return _jsx(Spinner, {});
    }
    if (!standardJsonInput) {
        return _jsx(Text, { children: "Error generating standard JSON input" });
    }
    // TODO: support file with multiple contracts in single sol file & when there's multiple .sol files in a single artifact
    const contractName = Object.values(forgeArtifact.metadata.settings.compilationTarget)[0];
    if (!contractName) {
        return _jsx(Text, { children: "Error getting contract name" });
    }
    return (_jsxs(Box, { flexDirection: "column", children: [_jsx(Box, { marginBottom: 1, children: _jsxs(Text, { bold: true, children: ["Verify", index !== undefined ? ` ${index + 1}` : '', ":", ' ', _jsx(Text, { color: "cyan", children: forgeArtifactPath })] }) }), _jsxs(Box, { flexDirection: "row", paddingLeft: 2, children: [_jsx(Box, { flexDirection: "column", marginRight: 2, children: chains.map(chain => (_jsxs(Text, { bold: true, color: "blue", children: [chain.name, ":"] }, chain.id))) }), _jsx(Box, { flexDirection: "column", children: chains.map(chain => (_jsx(VerifyForChain, { chain: chain, address: contractAddress, standardJsonInput: standardJsonInput, contractName: contractName }, chain.id))) })] })] }));
};
const VerifyForChain = ({ chain, address, standardJsonInput, contractName, }) => {
    const { isPending, error, mutate } = useMutation({
        mutationKey: verifyOnBlockscoutMutationKey(address, chain, standardJsonInput),
        mutationFn: () => verifyOnBlockscoutMutation(address, chain, standardJsonInput, contractName),
    });
    useEffect(() => {
        mutate();
    }, []);
    if (isPending) {
        return _jsx(Spinner, { label: "Verifying contract..." });
    }
    if (error) {
        return (_jsxs(Box, { gap: 1, children: [_jsx(Badge, { color: "red", children: "Failed" }), _jsxs(Text, { children: ["Error: ", error.message] })] }));
    }
    return (_jsxs(Box, { gap: 1, children: [_jsx(Badge, { color: "green", children: "Verified" }), _jsx(Text, { children: "Contract successfully verified" }), _jsx(Text, { children: getBlockExplorerAddressLink(chain, address) })] }));
};
export default VerifyCommand;
export const options = zodVerifyContractCommandParams;
