import { FunctionType, decodeFromAbi } from '@aztec/foundation/abi';
import { BaseContractInteraction } from './base_contract_interaction.js';
/** A batch of function calls to be sent as a single transaction through a wallet. */
export class BatchCall extends BaseContractInteraction {
    constructor(wallet, calls) {
        super(wallet);
        this.calls = calls;
    }
    /**
     * Create a transaction execution request that represents this batch, encoded and authenticated by the
     * user's wallet, ready to be simulated.
     * @param opts - An optional object containing additional configuration for the transaction.
     * @returns A Promise that resolves to a transaction instance.
     */
    async create(opts) {
        const calls = this.calls;
        const fee = opts?.estimateGas ? await this.getFeeOptionsFromEstimatedGas({ calls, fee: opts?.fee }) : opts?.fee;
        return await this.wallet.createTxExecutionRequest({ calls, fee });
    }
    /**
     * Simulate a transaction and get its return values
     * Differs from prove in a few important ways:
     * 1. It returns the values of the function execution
     * 2. It supports `unconstrained`, `private` and `public` functions
     *
     * @param options - An optional object containing additional configuration for the transaction.
     * @returns The result of the transaction as returned by the contract function.
     */
    async simulate(options = {}) {
        const { calls, unconstrained } = this.calls.reduce((acc, current, index) => {
            if (current.type === FunctionType.UNCONSTRAINED) {
                acc.unconstrained.push([current, index]);
            }
            else {
                acc.calls.push([
                    current,
                    index,
                    current.type === FunctionType.PRIVATE ? acc.privateIndex++ : acc.publicIndex++,
                ]);
            }
            return acc;
        }, { calls: [], unconstrained: [], publicIndex: 0, privateIndex: 0 });
        const txRequest = await this.wallet.createTxExecutionRequest({ calls: calls.map(indexedCall => indexedCall[0]) });
        const unconstrainedCalls = unconstrained.map(async (indexedCall) => {
            const call = indexedCall[0];
            return [
                await this.wallet.simulateUnconstrained(call.name, call.args, call.to, options?.from),
                indexedCall[1],
            ];
        });
        const [unconstrainedResults, simulatedTx] = await Promise.all([
            Promise.all(unconstrainedCalls),
            this.wallet.simulateTx(txRequest, true, options?.from, options?.skipTxValidation),
        ]);
        const results = [];
        unconstrainedResults.forEach(([result, index]) => {
            results[index] = result;
        });
        calls.forEach(([call, callIndex, resultIndex]) => {
            // As account entrypoints are private, for private functions we retrieve the return values from the first nested call
            // since we're interested in the first set of values AFTER the account entrypoint
            // For public functions we retrieve the first values directly from the public output.
            const rawReturnValues = call.type == FunctionType.PRIVATE
                ? simulatedTx.getPrivateReturnValues()?.nested?.[resultIndex].values
                : simulatedTx.getPublicReturnValues()?.[resultIndex].values;
            results[callIndex] = rawReturnValues ? decodeFromAbi(call.returnTypes, rawReturnValues) : [];
        });
        return results;
    }
}
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYmF0Y2hfY2FsbC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9jb250cmFjdC9iYXRjaF9jYWxsLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUNBLE9BQU8sRUFBRSxZQUFZLEVBQUUsYUFBYSxFQUFFLE1BQU0sdUJBQXVCLENBQUM7QUFHcEUsT0FBTyxFQUFFLHVCQUF1QixFQUEwQixNQUFNLGdDQUFnQyxDQUFDO0FBR2pHLHFGQUFxRjtBQUNyRixNQUFNLE9BQU8sU0FBVSxTQUFRLHVCQUF1QjtJQUNwRCxZQUFZLE1BQWMsRUFBWSxLQUFxQjtRQUN6RCxLQUFLLENBQUMsTUFBTSxDQUFDLENBQUM7UUFEc0IsVUFBSyxHQUFMLEtBQUssQ0FBZ0I7SUFFM0QsQ0FBQztJQUVEOzs7OztPQUtHO0lBQ0ksS0FBSyxDQUFDLE1BQU0sQ0FBQyxJQUF3QjtRQUMxQyxNQUFNLEtBQUssR0FBRyxJQUFJLENBQUMsS0FBSyxDQUFDO1FBQ3pCLE1BQU0sR0FBRyxHQUFHLElBQUksRUFBRSxXQUFXLENBQUMsQ0FBQyxDQUFDLE1BQU0sSUFBSSxDQUFDLDZCQUE2QixDQUFDLEVBQUUsS0FBSyxFQUFFLEdBQUcsRUFBRSxJQUFJLEVBQUUsR0FBRyxFQUFFLENBQUMsQ0FBQyxDQUFDLENBQUMsSUFBSSxFQUFFLEdBQUcs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