"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.serializeCachedAppData = serializeCachedAppData;
exports.deserializeCachedAppData = deserializeCachedAppData;
const Context_1 = require("../p2p/Context");
const Helpers_1 = require("./ajv/Helpers");
const AppObjEnum_1 = require("./enum/AppObjEnum");
const TypeIdentifierEnum_1 = require("./enum/TypeIdentifierEnum");
function serializeCachedAppData(stream, obj, root = false) {
    if (root) {
        stream.writeUInt16(TypeIdentifierEnum_1.TypeIdentifierEnum.cCachedAppData);
    }
    stream.writeUInt32(obj.cycle);
    stream.writeBuffer(Context_1.stateManager.app.binarySerializeObject(AppObjEnum_1.AppObjEnum.CachedAppData, obj.appData));
    stream.writeString(obj.dataID);
}
function deserializeCachedAppData(stream) {
    const cycle = stream.readUInt32();
    const appData = Context_1.stateManager.app.binaryDeserializeObject(AppObjEnum_1.AppObjEnum.CachedAppData, stream.readBuffer());
    const dataID = stream.readString();
    const errors = (0, Helpers_1.verifyPayload)(AppObjEnum_1.AppObjEnum.CachedAppData, { cycle, appData, dataID });
    if (errors && errors.length > 0) {
        throw new Error('AJV: CachedAppData validation failed');
    }
    return {
        cycle,
        appData,
        dataID,
    };
}
//# sourceMappingURL=CachedAppData.js.map