"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var dist_1 = require("@leftshiftone/gaia-sdk/dist");
var KeyboardBehaviour_1 = require("../behaviour/KeyboardBehaviour");
var MouseBehaviour_1 = require("../behaviour/MouseBehaviour");
var EventStream_1 = require("../event/EventStream");
var EventType_1 = require("../event/EventType");
var MultiTargetRenderer_1 = require("../renderer/MultiTargetRenderer");
var MessageType_1 = require("../support/MessageType");
var Subscription = /** @class */ (function () {
    function Subscription(type, header, callback, mqttSensorQueue, renderer) {
        var _this = this;
        this.behaviourBind = [];
        /**
         * Sends the given message
         *
         * @param payload the payload
         */
        this.publish = function (payload) {
            return _this.mqttSensorQueue.publish(_this.type, _this.header, payload.payload, payload.attributes, payload.type);
        };
        this.getTopic = function () { return _this.mqttSensorQueue.getTopic(_this.type, _this.header); };
        this.type = type;
        this.header = header;
        this.callback = callback;
        this.renderer = renderer;
        this.mqttSensorQueue = mqttSensorQueue;
        this.mqttSensorQueue.subscribe(type, header, callback);
        if (this.type === dist_1.ConversationQueueType.INTERACTION) {
            var ev = EventType_1.EventType.withChannelId(EventType_1.EventType.PUBLISH, this.header.channelId);
            EventStream_1.default.addListener(ev, function (e) { return _this.publish(e[0]); });
        }
    }
    /**
     * Unsubscribes from the queue
     */
    Subscription.prototype.unsubscribe = function () {
        this.behaviourBind.forEach(function (binding) { return binding.unbind(); });
        this.mqttSensorQueue.unsubscribe(this.getTopic());
    };
    /**
     * Initial request to make the system aware that the user is listening.
     */
    Subscription.prototype.reception = function (attributes) {
        if (this.behaviourBind.length === 0) {
            this.bind(new KeyboardBehaviour_1.KeyboardBehaviour(this.renderer));
            this.bind(new MouseBehaviour_1.MouseBehaviour(this.renderer));
        }
        this.mqttSensorQueue.publishConvInteraction(this.header, { attributes: attributes, payload: {}, type: MessageType_1.MessageType.RECEPTION });
    };
    Subscription.prototype.onMessage = function (message) {
        var _this = this;
        if (this.type === dist_1.ConversationQueueType.INTERACTION) {
            var spec = message;
            if (spec.type !== "reception" && spec.elements) {
                spec = Object.assign(spec, { position: 'left', channelId: this.header.channelId });
                this.renderer.render(spec).forEach(function (element) {
                    _this.renderer instanceof MultiTargetRenderer_1.MultiTargetRenderer ?
                        _this.renderer.appendContent(element, _this.header.channelId) :
                        _this.renderer.appendContent(element);
                });
            }
        }
        this.mqttSensorQueue.callback(this.getTopic(), message);
    };
    /**
     * Binds the given IBehaviour to the Subscription.
     *
     * @param behaviour
     */
    Subscription.prototype.bind = function (behaviour) {
        behaviour.init(this.header.channelId);
        behaviour.bind(this);
        this.behaviourBind.push(behaviour);
    };
    return Subscription;
}());
exports.Subscription = Subscription;
//# sourceMappingURL=Subscription.js.map