import { MAX_UNENCRYPTED_LOGS_PER_CALL } from '@aztec/circuits.js';
import { sha256Trunc } from '@aztec/foundation/crypto';
import { BufferReader, prefixBufferWithLength } from '@aztec/foundation/serialize';
import { z } from 'zod';
import { UnencryptedL2Log } from './unencrypted_l2_log.js';
/**
 * Data container of logs emitted in 1 function invocation (corresponds to 1 kernel iteration).
 */
export class UnencryptedFunctionL2Logs {
    constructor(
    /** An array of logs. */
    logs) {
        this.logs = logs;
    }
    /**
     * Serializes all function logs into a buffer.
     * @returns A buffer containing the serialized logs.
     * @remarks Each log is prefixed with 4 bytes for its length, then all the serialized logs are concatenated and
     *          the resulting buffer is prefixed with 4 bytes for its total length.
     */
    toBuffer() {
        const serializedLogs = this.logs.map(log => prefixBufferWithLength(log.toBuffer()));
        return prefixBufferWithLength(Buffer.concat(serializedLogs));
    }
    /**
     * Get the total length of all serialized data
     * @returns Total length of serialized data.
     */
    getSerializedLength() {
        // adding 4 for the resulting buffer length.
        return this.getKernelLength() + 4;
    }
    /**
     * Get the total length of all chargable data (raw log data + 4 for each log)
     * TODO: Rename this? getChargableLength? getDALength?
     * @returns Total length of data.
     */
    getKernelLength() {
        // Adding 4 to each log's length to account for the size stored in the serialized buffer
        return this.logs.reduce((acc, log) => acc + log.length + 4, 0);
    }
    /**
     * Calculates hash of serialized logs.
     * @returns Buffer containing 248 bits of information of sha256 hash.
     */
    hash() {
        // Truncated SHA hash of the concatenation of the hash of each inner log
        // Changed in resolving #5017 to mimic logs hashing in kernels
        const preimage = Buffer.concat(this.logs.map(l => l.hash()));
        return sha256Trunc(preimage);
    }
    static get schema() {
        return z
            .object({ logs: z.array(UnencryptedL2Log.schema) })
            .transform(({ logs }) => new UnencryptedFunctionL2Logs(logs));
    }
    /**
     * Creates an empty L2Logs object with no logs.
     * @returns A new FunctionL2Logs object with no logs.
     */
    static empty() {
        return new UnencryptedFunctionL2Logs([]);
    }
    /**
     * Deserializes logs from a buffer.
     * @param buf - The buffer containing the serialized logs.
     * @param isLengthPrefixed - Whether the buffer is prefixed with 4 bytes for its total length.
     * @returns Deserialized instance of `FunctionL2Logs`.
     */
    static fromBuffer(buf, isLengthPrefixed = true) {
        const reader = new BufferReader(buf, 0);
        // If the buffer is length prefixed use the length to read the array. Otherwise, the entire buffer is consumed.
        const logsBufLength = isLengthPrefixed ? reader.readNumber() : -1;
        const logs = reader.readBufferArray(logsBufLength);
        return new UnencryptedFunctionL2Logs(logs.map(UnencryptedL2Log.fromBuffer));
    }
    /**
     * Creates a new L2Logs object with `numLogs` logs.
     * @param numLogs - The number of logs to create.
     * @returns A new UnencryptedFunctionL2Logs object.
     */
    static random(numLogs) {
        if (numLogs > MAX_UNENCRYPTED_LOGS_PER_CALL) {
            throw new Error(`Trying to create ${numLogs} logs for one call (max: ${MAX_UNENCRYPTED_LOGS_PER_CALL})`);
        }
        const logs = [];
        for (let i = 0; i < numLogs; i++) {
            logs.push(UnencryptedL2Log.random());
        }
        return new UnencryptedFunctionL2Logs(logs);
    }
}
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZnVuY3Rpb25fbDJfbG9ncy5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9sb2dzL2Z1bmN0aW9uX2wyX2xvZ3MudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxFQUFFLDZCQUE2QixFQUFFLE1BQU0sb0JBQW9CLENBQUM7QUFDbkUsT0FBTyxFQUFFLFdBQVcsRUFBRSxNQUFNLDBCQUEwQixDQUFDO0FBQ3ZELE9BQU8sRUFBRSxZQUFZLEVBQUUsc0JBQXNCLEVBQUUsTUFBTSw2QkFBNkIsQ0FBQztBQUVuRixPQUFPLEVBQUUsQ0FBQyxFQUFFLE1BQU0sS0FBSyxDQUFDO0FBRXhCLE9BQU8sRUFBRSxnQkFBZ0IsRUFBRSxNQUFNLHlCQUF5QixDQUFDO0FBRTNEOztHQUVHO0FBQ0g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