"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.serializeLostReportReq = serializeLostReportReq;
exports.deserializeLostReportReq = deserializeLostReportReq;
const Helpers_1 = require("./ajv/Helpers");
const AJVSchemaEnum_1 = require("./enum/AJVSchemaEnum");
const TypeIdentifierEnum_1 = require("./enum/TypeIdentifierEnum");
const cLostReportReqVersion = 1;
function serializeLostReportReq(stream, obj, root = false) {
    if (root) {
        stream.writeUInt16(TypeIdentifierEnum_1.TypeIdentifierEnum.cLostReportReq);
    }
    stream.writeUInt8(cLostReportReqVersion);
    stream.writeString(obj.target);
    stream.writeString(obj.checker);
    stream.writeString(obj.reporter);
    stream.writeUInt32(obj.cycle);
    stream.writeBigUInt64(BigInt(obj.timestamp));
    stream.writeString(obj.requestId);
    stream.writeString(obj.sign.owner);
    stream.writeString(obj.sign.sig);
    if (obj.killother) {
        stream.writeUInt8(1);
        stream.writeUInt8(obj.killother ? 1 : 0);
    }
    else
        stream.writeUInt8(0);
}
function deserializeLostReportReq(stream) {
    const version = stream.readUInt8();
    if (version > cLostReportReqVersion) {
        throw new Error('cLostReportReq version mismatch');
    }
    const obj = {
        target: stream.readString(),
        checker: stream.readString(),
        reporter: stream.readString(),
        cycle: stream.readUInt32(),
        timestamp: Number(stream.readBigUInt64()),
        requestId: stream.readString(),
        sign: {
            owner: stream.readString(),
            sig: stream.readString(),
        },
    };
    if (stream.readUInt8() === 1) {
        // Check if killother is present
        obj.killother = stream.readUInt8() === 1;
    }
    const errors = (0, Helpers_1.verifyPayload)(AJVSchemaEnum_1.AJVSchemaEnum.LostReportReq, obj);
    if (errors && errors.length > 0) {
        throw new Error(`AJV: validation error -> ${errors.join(', ')}`);
    }
    return obj;
}
//# sourceMappingURL=LostReportReq.js.map