"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.cWrappedDataVersion = void 0;
exports.serializeWrappedData = serializeWrappedData;
exports.deserializeWrappedData = deserializeWrappedData;
const lib_types_1 = require("@shardeum-foundation/lib-types");
const Context_1 = require("../p2p/Context");
const AppObjEnum_1 = require("./enum/AppObjEnum");
const TypeIdentifierEnum_1 = require("./enum/TypeIdentifierEnum");
exports.cWrappedDataVersion = 1;
function serializeWrappedData(stream, obj, root = false) {
    if (root) {
        stream.writeUInt16(TypeIdentifierEnum_1.TypeIdentifierEnum.cWrappedData);
    }
    stream.writeUInt8(exports.cWrappedDataVersion);
    stream.writeString(obj.accountId);
    stream.writeString(obj.stateId);
    stream.writeBuffer(Context_1.stateManager.app.binarySerializeObject(AppObjEnum_1.AppObjEnum.AppData, obj.data));
    stream.writeBigUInt64(BigInt(obj.timestamp));
    if (obj.syncData !== undefined) {
        stream.writeUInt8(1);
        stream.writeString(lib_types_1.Utils.safeStringify(obj.syncData));
    }
    else {
        stream.writeUInt8(0);
    }
}
function deserializeWrappedData(stream) {
    const version = stream.readUInt8();
    if (version > exports.cWrappedDataVersion) {
        throw new Error(`WrappedData version mismatch`);
    }
    return {
        accountId: stream.readString(),
        stateId: stream.readString(),
        data: Context_1.stateManager.app.binaryDeserializeObject(AppObjEnum_1.AppObjEnum.AppData, stream.readBuffer()),
        timestamp: Number(stream.readBigUInt64()),
        syncData: stream.readUInt8() === 1 ? lib_types_1.Utils.safeJsonParse(stream.readString()) : undefined,
    };
}
//# sourceMappingURL=WrappedData.js.map