import { ConversationQueueType, MqttSensorQueue, } from "@leftshiftone/gaia-sdk/dist";
import EventStream from "../event/EventStream";
import { Subscription } from "./Subscription";
import { InteractionSubscription } from "./InteractionSubscription";
import { ContentCentricRenderer } from "../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 === ConversationQueueType.INTERACTION && !r) {
            console.error("Renderer is not set. Using fallback ContentCentricRenderer");
            r = new ContentCentricRenderer();
        }
        var subscription = type === ConversationQueueType.INTERACTION ?
            new InteractionSubscription(header, callback, this.mqttSensorQueue, r) :
            new 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(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(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(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(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.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 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;
}());
export { Connection };
//# sourceMappingURL=Connection.js.map