"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.deserializeGlobalAccountReportResp = exports.serializeGlobalAccountReportResp = void 0;
const TypeIdentifierEnum_1 = require("./enum/TypeIdentifierEnum");
const Helpers_1 = require("./ajv/Helpers");
const AJVSchemaEnum_1 = require("./enum/AJVSchemaEnum");
const cGlobalAccountReportRespVersion = 1;
function serializeGlobalAccountReportResp(stream, obj, root = false) {
    if (root) {
        stream.writeUInt16(TypeIdentifierEnum_1.TypeIdentifierEnum.cGlobalAccountReportResp);
    }
    stream.writeUInt8(cGlobalAccountReportRespVersion);
    if ('error' in obj) {
        stream.writeUInt8(0); // Error indicator
        stream.writeString(obj.error);
    }
    else {
        stream.writeUInt8(1); // Success indicator
        stream.writeString(obj.combinedHash);
        stream.writeUInt32(obj.accounts.length);
        obj.accounts.forEach((account) => {
            stream.writeString(account.id);
            stream.writeString(account.hash);
            stream.writeBigUInt64(BigInt(account.timestamp));
        });
        stream.writeUInt8(obj.ready ? 1 : 0);
    }
}
exports.serializeGlobalAccountReportResp = serializeGlobalAccountReportResp;
function deserializeGlobalAccountReportResp(stream) {
    const version = stream.readUInt8();
    if (version > cGlobalAccountReportRespVersion) {
        throw new Error('GlobalAccountReportRespSerializable version mismatch');
    }
    const responseType = stream.readUInt8();
    if (responseType === 0) {
        // Error response
        return { error: stream.readString() };
    }
    else {
        // Success response
        const combinedHash = stream.readString();
        const accountsLength = stream.readUInt32();
        const accounts = [];
        for (let i = 0; i < accountsLength; i++) {
            accounts.push({
                id: stream.readString(),
                hash: stream.readString(),
                timestamp: Number(stream.readBigUInt64()),
            });
        }
        const ready = stream.readUInt8() === 1;
        const errors = (0, Helpers_1.verifyPayload)(AJVSchemaEnum_1.AJVSchemaEnum.GlobalAccountReportResp, { combinedHash, accounts, ready });
        if (errors && errors.length > 0) {
            throw new Error('Data validation error');
        }
        return { combinedHash, accounts, ready };
    }
}
exports.deserializeGlobalAccountReportResp = deserializeGlobalAccountReportResp;
//# sourceMappingURL=GlobalAccountReportResp.js.map