var _a, _L1EventPayload_fromIncomingBodyPlaintextAndContractAddress;
import { __classPrivateFieldGet } from "tslib";
import { AztecAddress } from '@aztec/circuits.js';
import { EventSelector } from '@aztec/foundation/abi';
import { Fr } from '@aztec/foundation/fields';
import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize';
import { EncryptedLogPayload } from './encrypted_log_payload.js';
import { Event } from './payload.js';
/**
 * A class which wraps event data which is pushed on L1.
 */
export class L1EventPayload {
    constructor(
    /**
     * A event as emitted from Noir contract. Can be used along with private key to compute nullifier.
     */
    event, 
    /**
     * Address of the contract this tx is interacting with.
     */
    contractAddress, 
    /**
     * Type identifier for the underlying event, required to determine how to compute its hash and nullifier.
     */
    eventTypeId) {
        this.event = event;
        this.contractAddress = contractAddress;
        this.eventTypeId = eventTypeId;
    }
    static decryptAsIncoming(log, sk) {
        const decryptedLog = EncryptedLogPayload.decryptAsIncoming(log, sk);
        if (!decryptedLog) {
            return undefined;
        }
        return __classPrivateFieldGet(this, _a, "m", _L1EventPayload_fromIncomingBodyPlaintextAndContractAddress).call(this, decryptedLog.incomingBodyPlaintext, decryptedLog.contractAddress);
    }
    /**
     * Serializes the L1EventPayload object into a Buffer.
     * @returns Buffer representation of the L1EventPayload object.
     */
    toIncomingBodyPlaintext() {
        const fields = [this.eventTypeId.toField(), ...this.event.items];
        return serializeToBuffer(fields);
    }
    /**
     * Create a random L1EventPayload object (useful for testing purposes).
     * @param contract - The address of a contract the event was emitted from.
     * @returns A random L1EventPayload object.
     */
    static random(contract = AztecAddress.random()) {
        return new _a(Event.random(), contract, EventSelector.random());
    }
    equals(other) {
        return (this.event.equals(other.event) &&
            this.contractAddress.equals(other.contractAddress) &&
            this.eventTypeId.equals(other.eventTypeId));
    }
}
_a = L1EventPayload, _L1EventPayload_fromIncomingBodyPlaintextAndContractAddress = function _L1EventPayload_fromIncomingBodyPlaintextAndContractAddress(plaintext, contractAddress) {
    let payload;
    try {
        const reader = BufferReader.asReader(plaintext);
        const fields = reader.readArray(plaintext.length / Fr.SIZE_IN_BYTES, Fr);
        const eventTypeId = EventSelector.fromField(fields[0]);
        const event = new Event(fields.slice(1));
        payload = new _a(event, contractAddress, eventTypeId);
    }
    catch (e) {
        return undefined;
    }
    return payload;
};
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibDFfZXZlbnRfcGF5bG9hZC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uL3NyYy9sb2dzL2wxX3BheWxvYWQvbDFfZXZlbnRfcGF5bG9hZC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOztBQUFBLE9BQU8sRUFBRSxZQUFZLEVBQW1CLE1BQU0sb0JBQW9CLENBQUM7QUFDbkUsT0FBTyxFQUFFLGFBQWEsRUFBRSxNQUFNLHVCQUF1QixDQUFDO0FBQ3RELE9BQU8sRUFBVyxFQUFFLEVBQUUsTUFBTSwwQkFBMEIsQ0FBQztBQUN2RCxPQUFPLEVBQUUsWUFBWSxFQUFFLGlCQUFpQixFQUFFLE1BQU0sNkJBQTZCLENBQUM7QUFFOUUsT0FBTyxFQUFFLG1CQUFtQixFQUFFLE1BQU0sNEJBQTRCLENBQUM7QUFDakUsT0FBTyxFQUFFLEtBQUssRUFBRSxNQUFNLGNBQWMsQ0FBQztBQUVyQzs7R0FFRztBQUNILE1BQU0sT0FBTyxjQUFjO0lBQ3pCO0lBQ0U7O09BRUc7SUFDSSxLQUFZO0lBQ25COztPQUVHO0lBQ0ksZUFBNkI7SUFDcEM7O09BRUc7SUFDSSxXQUEwQjtRQVIxQixVQUFLLEdBQUwsS0FBSyxDQUFPO1FBSVosb0JBQWUsR0FBZixlQUFlLENBQWM7UUFJN0IsZ0JBQVcsR0FBWCxXQUFXLENBQWU7SUFDaEMsQ0FBQztJQXVCSixNQUFNLENBQUMsaUJBQWlCLENBQUMsR0FBZSxFQUFFLEVBQU07UUFDOUMsTUFBTSxZQUFZLEdBQUcsbUJB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