"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.deserializeSyncTrieHashesReq = exports.serializeSyncTrieHashesReq = void 0;
const TypeIdentifierEnum_1 = require("./enum/TypeIdentifierEnum");
const Helpers_1 = require("./ajv/Helpers");
const AJVSchemaEnum_1 = require("./enum/AJVSchemaEnum");
const cSyncTrieHashesReqVersion = 1;
function serializeSyncTrieHashesReq(stream, request, root = false) {
    if (root) {
        stream.writeUInt16(TypeIdentifierEnum_1.TypeIdentifierEnum.cSyncTrieHashesReq);
    }
    stream.writeUInt8(cSyncTrieHashesReqVersion);
    stream.writeBigUInt64(BigInt(request.cycle));
    stream.writeUInt32(request.nodeHashes.length);
    for (const nodeHash of request.nodeHashes) {
        stream.writeString(nodeHash.radix);
        stream.writeString(nodeHash.hash);
    }
}
exports.serializeSyncTrieHashesReq = serializeSyncTrieHashesReq;
function deserializeSyncTrieHashesReq(stream) {
    const version = stream.readUInt8();
    if (version > cSyncTrieHashesReqVersion) {
        throw new Error('SyncTrieHashesRequest version mismatch');
    }
    const cycle = Number(stream.readBigUInt64());
    const nodeHashesLength = stream.readUInt32();
    const nodeHashes = [];
    for (let i = 0; i < nodeHashesLength; i++) {
        const radix = stream.readString();
        const hash = stream.readString();
        nodeHashes.push({ radix, hash });
    }
    const errors = (0, Helpers_1.verifyPayload)(AJVSchemaEnum_1.AJVSchemaEnum.SyncTrieHashesReq, { cycle, nodeHashes });
    if (errors && errors.length > 0) {
        throw new Error('AJV: SyncTrieHashesRequest validation failed');
    }
    return {
        cycle,
        nodeHashes,
    };
}
exports.deserializeSyncTrieHashesReq = deserializeSyncTrieHashesReq;
//# sourceMappingURL=SyncTrieHashesReq.js.map