"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.serializeGetCachedAppDataResp = serializeGetCachedAppDataResp;
exports.deserializeGetCachedAppDataResp = deserializeGetCachedAppDataResp;
const Context_1 = require("../p2p/Context");
const Helpers_1 = require("./ajv/Helpers");
const AJVSchemaEnum_1 = require("./enum/AJVSchemaEnum");
const AppObjEnum_1 = require("./enum/AppObjEnum");
const TypeIdentifierEnum_1 = require("./enum/TypeIdentifierEnum");
const cGetCachedAppDataRespVersion = 1;
function serializeGetCachedAppDataResp(stream, response, root = false) {
    if (root) {
        stream.writeUInt16(TypeIdentifierEnum_1.TypeIdentifierEnum.cGetCachedAppDataResp);
    }
    stream.writeUInt8(cGetCachedAppDataRespVersion);
    if (response.cachedAppData) {
        stream.writeUInt8(1);
        stream.writeString(response.cachedAppData.dataID);
        stream.writeBuffer(Context_1.stateManager.app.binarySerializeObject(AppObjEnum_1.AppObjEnum.CachedAppData, response.cachedAppData.appData));
        stream.writeUInt32(response.cachedAppData.cycle);
    }
    else {
        stream.writeUInt8(0);
    }
}
function deserializeGetCachedAppDataResp(stream) {
    const version = stream.readUInt8();
    if (version > cGetCachedAppDataRespVersion) {
        throw new Error('Unsupported version in deserializeGetCachedAppDataResp');
    }
    let resp = {};
    if (stream.readUInt8() === 1) {
        const dataID = stream.readString();
        const appData = Context_1.stateManager.app.binaryDeserializeObject(AppObjEnum_1.AppObjEnum.CachedAppData, stream.readBuffer());
        const cycle = stream.readUInt32();
        resp = {
            cachedAppData: {
                dataID,
                appData,
                cycle,
            },
        };
    }
    const errors = (0, Helpers_1.verifyPayload)(AJVSchemaEnum_1.AJVSchemaEnum.GetCachedAppDataResp, resp);
    if (errors && errors.length > 0) {
        throw new Error(`AJV: validation error -> ${errors.join(', ')}`);
    }
    return resp;
}
//# sourceMappingURL=GetCachedAppDataResp.js.map