"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.deserializeRadixAndChildHashes = exports.serializeRadixAndChildHashes = exports.deserializeGetTrieAccountHashesResp = exports.serializeGetTrieAccountHashesResp = exports.cRadixAndChildHashesVersion = exports.cGetTrieAccountHashesRespVersion = void 0;
const TypeIdentifierEnum_1 = require("./enum/TypeIdentifierEnum");
const Helpers_1 = require("./ajv/Helpers");
const AJVSchemaEnum_1 = require("./enum/AJVSchemaEnum");
exports.cGetTrieAccountHashesRespVersion = 1;
exports.cRadixAndChildHashesVersion = 1;
function serializeGetTrieAccountHashesResp(stream, req, root = false) {
    if (root) {
        stream.writeUInt16(TypeIdentifierEnum_1.TypeIdentifierEnum.cGetAccountTrieHashesResp);
    }
    stream.writeUInt8(exports.cGetTrieAccountHashesRespVersion);
    stream.writeUInt16(req.nodeChildHashes.length);
    for (let i = 0; i < req.nodeChildHashes.length; i++) {
        // eslint-disable-next-line security/detect-object-injection
        serializeRadixAndChildHashes(stream, req.nodeChildHashes[i]);
    }
    stream.writeUInt32(req.stats.matched);
    stream.writeUInt32(req.stats.visisted);
    stream.writeUInt32(req.stats.empty);
    stream.writeUInt32(req.stats.childCount);
}
exports.serializeGetTrieAccountHashesResp = serializeGetTrieAccountHashesResp;
function deserializeGetTrieAccountHashesResp(stream) {
    const version = stream.readUInt8();
    if (version !== exports.cGetTrieAccountHashesRespVersion) {
        throw new Error(`Unsupported version for GetAccountTrieHashesResp: ${version}`);
    }
    const nodeChildHashesLength = stream.readUInt16();
    const nodeChildHashes = [];
    for (let i = 0; i < nodeChildHashesLength; i++) {
        nodeChildHashes.push(deserializeRadixAndChildHashes(stream));
    }
    const stats = {
        matched: stream.readUInt32(),
        visisted: stream.readUInt32(),
        empty: stream.readUInt32(),
        childCount: stream.readUInt32(),
    };
    const errors = (0, Helpers_1.verifyPayload)(AJVSchemaEnum_1.AJVSchemaEnum.GetTrieAccountHashesResp, {
        nodeChildHashes,
        stats,
    });
    if (errors && errors.length > 0) {
        throw new Error('Data validation error');
    }
    return {
        nodeChildHashes,
        stats,
    };
}
exports.deserializeGetTrieAccountHashesResp = deserializeGetTrieAccountHashesResp;
function serializeRadixAndChildHashes(stream, req, root = false) {
    if (root) {
        stream.writeUInt16(TypeIdentifierEnum_1.TypeIdentifierEnum.cRadixAndChildHashes);
    }
    stream.writeUInt8(exports.cRadixAndChildHashesVersion);
    stream.writeString(req.radix);
    stream.writeUInt16(req.childAccounts.length);
    for (let i = 0; i < req.childAccounts.length; i++) {
        // eslint-disable-next-line security/detect-object-injection
        stream.writeString(req.childAccounts[i].accountID);
        // eslint-disable-next-line security/detect-object-injection
        stream.writeString(req.childAccounts[i].hash);
    }
}
exports.serializeRadixAndChildHashes = serializeRadixAndChildHashes;
function deserializeRadixAndChildHashes(stream) {
    const version = stream.readUInt8();
    if (version !== exports.cRadixAndChildHashesVersion) {
        throw new Error(`Unsupported version for RadixAndChildHashes: ${version}`);
    }
    const radix = stream.readString();
    const childAccountsLength = stream.readUInt16();
    const childAccounts = [];
    for (let i = 0; i < childAccountsLength; i++) {
        childAccounts.push({
            accountID: stream.readString(),
            hash: stream.readString(),
        });
    }
    return {
        radix,
        childAccounts,
    };
}
exports.deserializeRadixAndChildHashes = deserializeRadixAndChildHashes;
//# sourceMappingURL=GetTrieAccountHashesResp.js.map