import { BaseError, ContractFunctionRevertedError, getAddress, } from 'viem';
import { simulateContract } from '@wagmi/core';
import { CREATEX_ADDRESS, createXABI } from '../contracts/createx/constants.js';
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,
                    // Just a random address
                    account: '0x000000000000000000000000000000000000dead',
                    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;
            }
        },
    };
};
