var _EntrypointPayload_packedArguments, _EntrypointPayload_functionCalls, _EntrypointPayload_nonce, _EntrypointPayload_generatorIndex, _FeeEntrypointPayload_isFeePayer;
import { __classPrivateFieldGet, __classPrivateFieldSet } from "tslib";
import { FunctionCall, PackedValues } from '@aztec/circuit-types';
import { Fr, GeneratorIndex } from '@aztec/circuits.js';
import { FunctionType } from '@aztec/foundation/abi';
import { padArrayEnd } from '@aztec/foundation/collection';
import { poseidon2HashWithSeparator } from '@aztec/foundation/crypto';
// These must match the values defined in:
// - noir-projects/aztec-nr/aztec/src/entrypoint/app.nr
const APP_MAX_CALLS = 4;
// - and noir-projects/aztec-nr/aztec/src/entrypoint/fee.nr
const FEE_MAX_CALLS = 2;
/* eslint-enable camelcase */
/** Assembles an entrypoint payload */
export class EntrypointPayload {
    constructor(functionCalls, generatorIndex, nonce = Fr.random()) {
        _EntrypointPayload_packedArguments.set(this, []);
        _EntrypointPayload_functionCalls.set(this, []);
        _EntrypointPayload_nonce.set(this, void 0);
        _EntrypointPayload_generatorIndex.set(this, void 0);
        for (const call of functionCalls) {
            __classPrivateFieldGet(this, _EntrypointPayload_packedArguments, "f").push(PackedValues.fromValues(call.args));
        }
        /* eslint-disable camelcase */
        __classPrivateFieldSet(this, _EntrypointPayload_functionCalls, functionCalls.map((call, index) => ({
            args_hash: __classPrivateFieldGet(this, _EntrypointPayload_packedArguments, "f")[index].hash,
            function_selector: call.selector.toField(),
            target_address: call.to.toField(),
            is_public: call.type == FunctionType.PUBLIC,
            is_static: call.isStatic,
        })), "f");
        /* eslint-enable camelcase */
        __classPrivateFieldSet(this, _EntrypointPayload_generatorIndex, generatorIndex, "f");
        __classPrivateFieldSet(this, _EntrypointPayload_nonce, nonce, "f");
    }
    /* eslint-disable camelcase */
    /**
     * The function calls to execute. This uses snake_case naming so that it is compatible with Noir encoding
     * @internal
     */
    get function_calls() {
        return __classPrivateFieldGet(this, _EntrypointPayload_functionCalls, "f");
    }
    /* eslint-enable camelcase */
    /**
     * The nonce
     * @internal
     */
    get nonce() {
        return __classPrivateFieldGet(this, _EntrypointPayload_nonce, "f");
    }
    /**
     * The packed arguments for the function calls
     */
    get packedArguments() {
        return __classPrivateFieldGet(this, _EntrypointPayload_packedArguments, "f");
    }
    /**
     * Hashes the payload
     * @returns The hash of the payload
     */
    hash() {
        return poseidon2HashWithSeparator(this.toFields(), __classPrivateFieldGet(this, _EntrypointPayload_generatorIndex, "f"));
    }
    /** Serializes the function calls to an array of fields. */
    functionCallsToFields() {
        return __classPrivateFieldGet(this, _EntrypointPayload_functionCalls, "f").flatMap(call => [
            call.args_hash,
            call.function_selector,
            call.target_address,
            new Fr(call.is_public),
            new Fr(call.is_static),
        ]);
    }
    /**
     * Creates an execution payload for a dapp from a set of function calls
     * @param functionCalls - The function calls to execute
     * @returns The execution payload
     */
    static fromFunctionCalls(functionCalls) {
        return new AppEntrypointPayload(functionCalls, 0);
    }
    /**
     * Creates an execution payload for the app-portion of a transaction from a set of function calls
     * @param functionCalls - The function calls to execute
     * @param nonce - The nonce for the payload, used to emit a nullifier identifying the call
     * @returns The execution payload
     */
    static fromAppExecution(functionCalls, nonce = Fr.random()) {
        if (functionCalls.length > APP_MAX_CALLS) {
            throw new Error(`Expected at most ${APP_MAX_CALLS} function calls, got ${functionCalls.length}`);
        }
        const paddedCalls = padArrayEnd(functionCalls, FunctionCall.empty(), APP_MAX_CALLS);
        return new AppEntrypointPayload(paddedCalls, GeneratorIndex.SIGNATURE_PAYLOAD, nonce);
    }
    /**
     * Creates an execution payload to pay the fee for a transaction
     * @param sender - The address sending this payload
     * @param feeOpts - The fee payment options
     * @returns The execution payload
     */
    static async fromFeeOptions(sender, feeOpts) {
        const calls = (await feeOpts?.paymentMethod.getFunctionCalls(feeOpts?.gasSettings)) ?? [];
        const feePayer = await feeOpts?.paymentMethod.getFeePayer(feeOpts?.gasSettings);
        const isFeePayer = !!feePayer && feePayer.equals(sender);
        const paddedCalls = padArrayEnd(calls, FunctionCall.empty(), FEE_MAX_CALLS);
        return new FeeEntrypointPayload(paddedCalls, GeneratorIndex.FEE_PAYLOAD, isFeePayer);
    }
}
_EntrypointPayload_packedArguments = new WeakMap(), _EntrypointPayload_functionCalls = new WeakMap(), _EntrypointPayload_nonce = new WeakMap(), _EntrypointPayload_generatorIndex = new WeakMap();
/** Entrypoint payload for app phase execution. */
class AppEntrypointPayload extends EntrypointPayload {
    toFields() {
        return [...this.functionCallsToFields(), this.nonce];
    }
}
/** Entrypoint payload for fee payment to be run during setup phase. */
class FeeEntrypointPayload extends EntrypointPayload {
    constructor(functionCalls, generatorIndex, isFeePayer) {
        super(functionCalls, generatorIndex);
        _FeeEntrypointPayload_isFeePayer.set(this, void 0);
        __classPrivateFieldSet(this, _FeeEntrypointPayload_isFeePayer, isFeePayer, "f");
    }
    toFields() {
        return [...this.functionCallsToFields(), this.nonce, new Fr(__classPrivateFieldGet(this, _FeeEntrypointPayload_isFeePayer, "f"))];
    }
    /* eslint-disable camelcase */
    /** Whether the sender should be appointed as fee payer. */
    get is_fee_payer() {
        return __classPrivateFieldGet(this, _FeeEntrypointPayload_isFeePayer, "f");
    }
}
_FeeEntrypointPayload_isFeePayer = new WeakMap();
/**
 * Computes a hash of a combined payload.
 * @param appPayload - An app payload.
 * @param feePayload - A fee payload.
 * @returns A hash of a combined payload.
 */
export function computeCombinedPayloadHash(appPayload, feePayload) {
    return poseidon2HashWithSeparator([appPayload.hash(), feePayload.hash()], GeneratorIndex.COMBINED_PAYLOAD);
}
//# sourceMappingURL=data:application/json;base64, 