import { BaseError, ContractFunctionRevertedError, getAddress, zeroAddress, } from 'viem';
import { simulateContract } from '@wagmi/core';
import { CREATEX_ADDRESS, createXABI } from '../../../util/createx/constants.js';
import { supersimNetwork } from '../../../util/chains/networks.js';
// Heuristics for funded accounts on chains
const getFundedAccountForChain = (chainId) => {
    // @ts-expect-error
    if (supersimNetwork.chains.map(c => c.id).includes(chainId)) {
        return '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266';
    }
    return zeroAddress;
};
export const simulationCheckQueryKey = (deterministicAddress, initCode, baseSalt, chainId) => {
    return ['simulationCheck', deterministicAddress, initCode, baseSalt, chainId];
};
export const simulationCheckQueryOptions = (wagmiConfig, { deterministicAddress, initCode, baseSalt, chainId, }) => {
    return {
        queryKey: simulationCheckQueryKey(deterministicAddress, initCode, baseSalt, chainId),
        queryFn: async () => {
            try {
                const simulationResult = await simulateContract(wagmiConfig, {
                    abi: createXABI,
                    account: getFundedAccountForChain(chainId),
                    address: CREATEX_ADDRESS,
                    chainId,
                    functionName: 'deployCreate2',
                    args: [baseSalt, initCode],
                });
                return {
                    isAddressSameAsExpected: getAddress(simulationResult.result) ===
                        getAddress(deterministicAddress),
                };
            }
            catch (err) {
                if (err instanceof BaseError) {
                    const revertError = err.walk(err => err instanceof ContractFunctionRevertedError);
                    if (revertError) {
                        return {
                            isAddressSameAsExpected: true,
                        };
                    }
                }
                throw err;
            }
        },
    };
};
