"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var dist_1 = require("@leftshiftone/gaia-sdk/dist");
var EventStream_1 = require("../event/EventStream");
var Subscription_1 = require("./Subscription");
var InteractionSubscription_1 = require("./InteractionSubscription");
var ContentCentricRenderer_1 = require("../renderer/ContentCentricRenderer");
var Connection = /** @class */ (function () {
    function Connection(options, listener) {
        this.subscriptions = new Map();
        this.mqttSensorQueue = this.initMqttSensorQueue(options);
        this.listener = listener;
    }
    /**
     * Subscribes to the selected queue.
     *
     * @param type the queue type
     * @param header the destination header
     * @param callback the callback function
     * @param renderer the renderer which is only used when the type is set to interaction
     */
    Connection.prototype.subscribe = function (type, header, callback, renderer) {
        var r = renderer;
        if (type === dist_1.ConversationQueueType.INTERACTION && !r) {
            console.error("Renderer is not set. Using fallback ContentCentricRenderer");
            r = new ContentCentricRenderer_1.ContentCentricRenderer();
        }
        var subscription = type === dist_1.ConversationQueueType.INTERACTION ?
            new InteractionSubscription_1.InteractionSubscription(header, callback, this.mqttSensorQueue, r) :
            new Subscription_1.Subscription(type, header, callback, this.mqttSensorQueue);
        this.setSubscription(subscription);
        return subscription;
    };
    /**
     * Subscribes to the context queue.
     *
     * @param header the destination header
     * @param callback the callback function
     */
    Connection.prototype.subscribeContext = function (header, callback) {
        var subscription = new Subscription_1.Subscription(dist_1.ConversationQueueType.CONTEXT, header, callback, this.mqttSensorQueue);
        this.setSubscription(subscription);
        return subscription;
    };
    /**
     * Subscribes to the notification queue.
     *
     * @param header the destination header
     * @param callback the callback function
     */
    Connection.prototype.subscribeNotification = function (header, callback) {
        var subscription = new Subscription_1.Subscription(dist_1.ConversationQueueType.NOTIFICATION, header, callback, this.mqttSensorQueue);
        this.setSubscription(subscription);
        return subscription;
    };
    /**
     * Subscribes to the logging queue.
     *
     * @param header the destination header
     * @param callback the callback function
     */
    Connection.prototype.subscribeLogging = function (header, callback) {
        var subscription = new Subscription_1.Subscription(dist_1.ConversationQueueType.LOGGING, header, callback, this.mqttSensorQueue);
        this.setSubscription(subscription);
        return subscription;
    };
    /**
     * Subscribes to the interaction queue.
     *
     * @param header the destination header
     * @param callback the callback function
     * @param renderer the renderer
     * @param interactionInterceptor intercepts the interaction
     */
    Connection.prototype.subscribeInteraction = function (header, callback, renderer, interactionInterceptor) {
        var subscription = new InteractionSubscription_1.InteractionSubscription(header, callback, this.mqttSensorQueue, renderer, interactionInterceptor);
        this.setSubscription(subscription);
        return subscription;
    };
    Connection.prototype.setSubscription = function (subscription) {
        this.subscriptions.set(subscription.getTopic(), subscription);
    };
    /**
     * disconnect the mqtt connection
     */
    Connection.prototype.disconnect = function () {
        var _this = this;
        EventStream_1.default.removeAllListeners();
        this.subscriptions.forEach(function (subscription) { return subscription.unsubscribe(); });
        this.mqttSensorQueue.end(false, function () { return _this.listener.onDisconnected(); });
    };
    Connection.prototype.initMqttSensorQueue = function (options) {
        var _this = this;
        var mqttSensorQueue = new dist_1.MqttSensorQueue(options);
        mqttSensorQueue.on('packetsend', function (packet) { return _this.listener.onPacketSend(packet); });
        mqttSensorQueue.on('message', function (topic, message) { return _this.onMessage(topic, message); });
        mqttSensorQueue.on('connect', function () { return _this.listener.onConnected(); });
        mqttSensorQueue.on('offline', function () { return _this.listener.onConnectionLost(); });
        return mqttSensorQueue;
    };
    Connection.prototype.onMessage = function (topic, message) {
        var payload = JSON.parse(message);
        this.listener.onMessage(payload);
        this.subscriptions.get(topic).onMessage(payload);
    };
    return Connection;
}());
exports.Connection = Connection;
//# sourceMappingURL=Connection.js.map