import { encodeAbiParameters, stringToBytes, } from 'viem';
export function preparedParamForEncoding(input, value) {
    const { type } = input;
    if (type.startsWith('uint') || type.startsWith('int')) {
        return BigInt(value);
    }
    if (type === 'boolean') {
        return value === 'true';
    }
    if (type.startsWith('bytes')) {
        return stringToBytes(value);
    }
    return value;
}
export function getConstructorAbi(abi) {
    return abi.find(abi => abi.type === 'constructor');
}
export function getEncodedConstructorArgs(abi, args) {
    let encodedConstructorArgs;
    const constructorAbi = abi.find(abi => abi.type === 'constructor');
    if (args?.length && constructorAbi) {
        const constructorInputTypes = constructorAbi.inputs.map(input => ({
            type: input.type,
            name: input.name,
        }));
        if (args.length !== constructorInputTypes.length) {
            throw new Error(`Constructor input types length mismatch: ${args.length} !== ${constructorInputTypes.length}`);
        }
        if (args.length && args.length === constructorInputTypes.length) {
            const preparedArgs = args.map((arg, i) => preparedParamForEncoding(constructorInputTypes[i], arg));
            encodedConstructorArgs = encodeAbiParameters(constructorInputTypes, preparedArgs);
        }
    }
    return encodedConstructorArgs;
}
export function getEncodedInitializationArgs(abi, args) {
    let encodedInitializationArgs;
    const initializationAbi = abi.find(abi => abi.type === 'function' && abi.name === 'initialize');
    if (initializationAbi) {
        const initializationInputTypes = initializationAbi.inputs.map(input => ({
            type: input.type,
            name: input.name,
        }));
        if (args?.length && args?.length == initializationInputTypes.length) {
            const preparedArgs = args.map((arg, i) => preparedParamForEncoding(initializationInputTypes[i], arg));
            encodedInitializationArgs = encodeAbiParameters(initializationInputTypes, preparedArgs);
        }
    }
    return encodedInitializationArgs;
}
