"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.serializeGetTxTimestampResp = serializeGetTxTimestampResp;
exports.deserializeGetTxTimestampResp = deserializeGetTxTimestampResp;
const TypeIdentifierEnum_1 = require("./enum/TypeIdentifierEnum");
const Helpers_1 = require("./ajv/Helpers");
const AJVSchemaEnum_1 = require("./enum/AJVSchemaEnum");
const cGetTxTimestampRespVersion = 1;
function serializeGetTxTimestampResp(stream, obj, root = false) {
    const errors = (0, Helpers_1.verifyPayload)(AJVSchemaEnum_1.AJVSchemaEnum.GetTxTimestampResp, obj);
    if (errors && errors.length > 0) {
        throw new Error('Data validation error');
    }
    if (root) {
        stream.writeUInt16(TypeIdentifierEnum_1.TypeIdentifierEnum.cGetTxTimestampResp);
    }
    stream.writeUInt8(cGetTxTimestampRespVersion);
    stream.writeString(obj.txId);
    stream.writeString(obj.cycleMarker);
    stream.writeUInt32(obj.cycleCounter);
    stream.writeBigUInt64(BigInt(obj.timestamp));
    if (obj.sign) {
        stream.writeUInt8(1);
        stream.writeString(obj.sign.owner);
        stream.writeString(obj.sign.sig);
    }
    else {
        stream.writeUInt8(0);
    }
    if (obj.isResponse !== undefined) {
        stream.writeUInt8(1);
        stream.writeUInt8(obj.isResponse ? 1 : 0);
    }
    else {
        stream.writeUInt8(0);
    }
}
function deserializeGetTxTimestampResp(stream) {
    const version = stream.readUInt8();
    if (version > cGetTxTimestampRespVersion) {
        throw new Error('GetTxTimestampRespVersion : Unsupported version');
    }
    const txId = stream.readString();
    const cycleMarker = stream.readString();
    const cycleCounter = stream.readUInt32();
    const timestamp = Number(stream.readBigUInt64());
    let sign = undefined;
    if (stream.readUInt8() === 1) {
        const owner = stream.readString();
        const sig = stream.readString();
        sign = { owner, sig };
    }
    let isResponse;
    if (stream.readUInt8() === 1) {
        isResponse = stream.readUInt8() === 1 ? true : false;
    }
    let result;
    if (sign != undefined) {
        result = {
            txId,
            cycleMarker,
            cycleCounter,
            timestamp,
            sign,
            ...(typeof isResponse !== 'undefined' && { isResponse }),
        };
    }
    else {
        result = {
            txId,
            cycleMarker,
            cycleCounter,
            timestamp,
            ...(typeof isResponse !== 'undefined' && { isResponse }),
        };
    }
    const errors = (0, Helpers_1.verifyPayload)(AJVSchemaEnum_1.AJVSchemaEnum.GetTxTimestampResp, result);
    if (errors && errors.length > 0) {
        throw new Error('Data validation error');
    }
    return result;
}
//# sourceMappingURL=GetTxTimestampResp.js.map