"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.deserializeGetTrieHashesResp = exports.serializeGetTrieHashesResp = void 0;
const TypeIdentifierEnum_1 = require("./enum/TypeIdentifierEnum");
const Helpers_1 = require("./ajv/Helpers");
const AJVSchemaEnum_1 = require("./enum/AJVSchemaEnum");
const cGetTrieHashesRespVersion = 1;
function serializeGetTrieHashesResp(stream, response, root = false) {
    if (root) {
        stream.writeUInt16(TypeIdentifierEnum_1.TypeIdentifierEnum.cGetTrieHashesResp);
    }
    stream.writeUInt8(cGetTrieHashesRespVersion);
    stream.writeString(response.nodeId || '');
    stream.writeUInt32(response.nodeHashes.length);
    for (const { radix, hash } of response.nodeHashes) {
        stream.writeString(radix);
        stream.writeString(hash);
    }
}
exports.serializeGetTrieHashesResp = serializeGetTrieHashesResp;
function deserializeGetTrieHashesResp(stream) {
    const version = stream.readUInt8();
    if (version > cGetTrieHashesRespVersion) {
        throw new Error('Unsupported version in deserializeGetTrieHashesResp');
    }
    const nodeId = stream.readString();
    const length = stream.readUInt32();
    const hashes = [];
    for (let i = 0; i < length; i++) {
        const radix = stream.readString();
        const hash = stream.readString();
        hashes.push({ radix, hash });
    }
    const errors = (0, Helpers_1.verifyPayload)(AJVSchemaEnum_1.AJVSchemaEnum.GetTrieHashesResp, { nodeHashes: hashes, nodeId: nodeId || '' });
    if (errors && errors.length > 0) {
        throw new Error('AJV: GetTrieHashesResp validation error');
    }
    return { nodeHashes: hashes, nodeId: nodeId || '' };
}
exports.deserializeGetTrieHashesResp = deserializeGetTrieHashesResp;
//# sourceMappingURL=GetTrieHashesResp.js.map