"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const constants_1 = require("../../constants");
exports.parseMQTT = (msg) => {
    let topic = constants_1.TOPIC.EVENT;
    if (msg.retain) {
        topic = constants_1.TOPIC.RECORD;
    }
    if (msg.cmd === 'subscribe') {
        const names = msg.subscriptions.map((mqttMsg) => mqttMsg.topic);
        return [{
                topic: constants_1.TOPIC.EVENT,
                action: constants_1.EVENT_ACTION.SUBSCRIBE,
                names,
                correlationId: msg.messageId
            }, {
                topic: constants_1.TOPIC.RECORD,
                action: constants_1.RECORD_ACTION.SUBSCRIBE,
                names,
                correlationId: msg.messageId
            }];
    }
    if (msg.cmd === 'unsubscribe') {
        const names = msg.subscriptions.map((mqttMsg) => mqttMsg.topic);
        return [{
                topic: constants_1.TOPIC.EVENT,
                action: constants_1.EVENT_ACTION.UNSUBSCRIBE,
                names,
                correlationId: msg.messageId
            }, {
                topic: constants_1.TOPIC.RECORD,
                action: constants_1.RECORD_ACTION.UNSUBSCRIBE,
                names,
                correlationId: msg.messageId
            }];
    }
    if (msg.cmd === 'publish') {
        if (topic === constants_1.TOPIC.EVENT) {
            return [{
                    topic,
                    action: constants_1.EVENT_ACTION.EMIT,
                    name: msg.topic,
                    parsedData: msg.payload.toString()
                }];
        }
        else if (topic === constants_1.TOPIC.RECORD) {
            return [{
                    topic,
                    action: constants_1.RECORD_ACTION.CREATEANDUPDATE,
                    name: msg.topic,
                    parsedData: JSON.parse(msg.payload.toString()),
                    isWriteAck: msg.qos > 0,
                    version: -1,
                    correlationId: msg.messageId
                }, {
                    topic: constants_1.TOPIC.EVENT,
                    action: constants_1.EVENT_ACTION.EMIT,
                    name: msg.topic,
                    parsedData: msg.payload.toString()
                }];
        }
    }
    return [{
            topic: constants_1.TOPIC.PARSER,
            action: constants_1.PARSER_ACTION.INVALID_MESSAGE
        }];
};
//# sourceMappingURL=message-parser.js.map