"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SocketMessage = void 0;
var crypto = require("crypto");
function isNumber(value) {
    return ((value != null) && !isNaN(Number(value.toString())));
}
var SocketMessage = /** @class */ (function () {
    function SocketMessage() {
    }
    SocketMessage.fromjson = function (json) {
        var result = new SocketMessage();
        var obj = JSON.parse(json);
        result.command = obj.command;
        result.id = obj.id;
        result.replyto = obj.replyto;
        result.count = 1;
        result.index = 0;
        result.data = obj.data;
        if (isNumber(obj.count)) {
            result.count = obj.count;
        }
        if (isNumber(obj.index)) {
            result.index = obj.index;
        }
        if (result.id === null || result.id === undefined || result.id === "") {
            result.id = crypto.randomBytes(16).toString("hex");
        }
        return result;
    };
    SocketMessage.fromcommand = function (command) {
        var result = new SocketMessage();
        result.command = command;
        result.count = 1;
        result.index = 0;
        result.id = crypto.randomBytes(16).toString("hex");
        return result;
    };
    SocketMessage.frommessage = function (msg, data, count, index) {
        var result = new SocketMessage();
        result.id = msg.id;
        result.replyto = msg.replyto;
        result.command = msg.command;
        result.count = count;
        result.index = index;
        result.data = data;
        return result;
    };
    SocketMessage.prototype.tojson = function () {
        return JSON.stringify(this);
    };
    return SocketMessage;
}());
exports.SocketMessage = SocketMessage;
//# sourceMappingURL=SocketMessage.js.map