"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.deserializeGetAccountDataResp = exports.serializeGetAccountDataResp = void 0;
const WrappedData_1 = require("./WrappedData");
const TypeIdentifierEnum_1 = require("./enum/TypeIdentifierEnum");
const AJVSchemaEnum_1 = require("./enum/AJVSchemaEnum");
const Helpers_1 = require("./ajv/Helpers");
const cGetAccountDataRespVersion = 1;
function serializeGetAccountDataResp(stream, obj, root = false) {
    if (root) {
        stream.writeUInt16(TypeIdentifierEnum_1.TypeIdentifierEnum.cGetAccountDataResp);
    }
    stream.writeUInt8(cGetAccountDataRespVersion);
    if (obj.data) {
        stream.writeUInt8(1);
        stream.writeUInt16(obj.data.wrappedAccounts.length);
        obj.data.wrappedAccounts.forEach((wrappedAccount) => (0, WrappedData_1.serializeWrappedData)(stream, wrappedAccount));
        stream.writeUInt8(obj.data.lastUpdateNeeded ? 1 : 0);
        stream.writeUInt16(obj.data.wrappedAccounts2.length);
        obj.data.wrappedAccounts2.forEach((wrappedAccount) => (0, WrappedData_1.serializeWrappedData)(stream, wrappedAccount));
        stream.writeBigUInt64(BigInt(obj.data.highestTs));
        stream.writeBigUInt64(BigInt(obj.data.delta));
    }
    else {
        stream.writeUInt8(0);
    }
    if (obj.errors) {
        stream.writeUInt8(1);
        stream.writeUInt16(obj.errors.length);
        obj.errors.forEach((error) => stream.writeString(error));
    }
    else {
        stream.writeUInt8(0);
    }
}
exports.serializeGetAccountDataResp = serializeGetAccountDataResp;
function deserializeGetAccountDataResp(stream) {
    const version = stream.readUInt8();
    if (version > cGetAccountDataRespVersion) {
        throw new Error('GetAccountDataResp version mismatch');
    }
    let data, errors;
    if (stream.readUInt8() === 1) {
        // Check if data is present
        const wrappedAccountsLength = stream.readUInt16();
        const wrappedAccounts = [];
        for (let i = 0; i < wrappedAccountsLength; i++) {
            wrappedAccounts.push((0, WrappedData_1.deserializeWrappedData)(stream));
        }
        const lastUpdateNeeded = stream.readUInt8() === 1;
        const wrappedAccounts2Length = stream.readUInt16();
        const wrappedAccounts2 = [];
        for (let i = 0; i < wrappedAccounts2Length; i++) {
            wrappedAccounts2.push((0, WrappedData_1.deserializeWrappedData)(stream));
        }
        const highestTs = Number(stream.readBigUInt64());
        const delta = Number(stream.readBigUInt64());
        data = { wrappedAccounts, lastUpdateNeeded, wrappedAccounts2, highestTs, delta };
    }
    if (stream.readUInt8() === 1) {
        // Check if errors are present
        const errorsLength = stream.readUInt16();
        errors = [];
        for (let i = 0; i < errorsLength; i++) {
            errors.push(stream.readString());
        }
    }
    const ajvErrors = (0, Helpers_1.verifyPayload)(AJVSchemaEnum_1.AJVSchemaEnum.GetAccountDataResp, {
        ...(data ? { data } : {}),
        ...(errors ? { errors } : {}),
    });
    if (ajvErrors && ajvErrors.length > 0) {
        console.log('The AjvErrors are:', ajvErrors);
        throw new Error('Data validation error');
    }
    return {
        ...(data ? { data } : {}),
        ...(errors ? { errors } : {}),
    };
}
exports.deserializeGetAccountDataResp = deserializeGetAccountDataResp;
//# sourceMappingURL=GetAccountDataResp.js.map