var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
import { Subscription } from "./Subscription";
import { ConversationQueueType } from "@leftshiftone/gaia-sdk/dist";
import { EventType } from "../event/EventType";
import EventStream from "../event/EventStream";
import { KeyboardBehaviour } from "../behaviour/KeyboardBehaviour";
import { MouseBehaviour } from "../behaviour/MouseBehaviour";
import { MessageType } from "../support/MessageType";
import { DefaultInteractionInterceptor } from "./interceptor/DefaultInteractionInterceptor";
var InteractionSubscription = /** @class */ (function (_super) {
    __extends(InteractionSubscription, _super);
    function InteractionSubscription(header, callback, mqttSensorQueue, renderer, interactionInterceptor) {
        if (interactionInterceptor === void 0) { interactionInterceptor = new DefaultInteractionInterceptor(); }
        var _this = _super.call(this, ConversationQueueType.INTERACTION, header, callback, mqttSensorQueue) || this;
        _this.renderer = renderer;
        _this.renderer.init(header.channelId);
        _this.interactionInterceptor = interactionInterceptor;
        var ev = EventType.withChannelId(EventType.PUBLISH, _this.header.channelId);
        EventStream.addListener(ev, function (e) { return _this.publish(_this.interactionInterceptor.execute(new ConvInteractionAdapter(e[0])).toIPayloadEvent()); });
        return _this;
    }
    /**
     * Initial request to make the system aware that the user is listening.
     */
    InteractionSubscription.prototype.reception = function (attributes) {
        if (this.behaviourBind.length === 0) {
            this.bind(new KeyboardBehaviour(this.renderer));
            this.bind(new MouseBehaviour(this.renderer));
        }
        this.mqttSensorQueue.publishConvInteraction(this.header, this.interactionInterceptor.execute({
            attributes: attributes,
            payload: {},
            type: MessageType.RECEPTION
        }));
    };
    InteractionSubscription.prototype.onMessage = function (message) {
        var _this = this;
        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.appendContent(element);
            });
        }
        _super.prototype.onMessage.call(this, message);
    };
    return InteractionSubscription;
}(Subscription));
export { InteractionSubscription };
var ConvInteractionAdapter = /** @class */ (function () {
    function ConvInteractionAdapter(eventPayload) {
        this.eventPayload = eventPayload;
        this.attributes = this.eventPayload.attributes;
        this.payload = this.eventPayload.payload;
        this.type = MessageType[this.eventPayload.type];
    }
    ConvInteractionAdapter.prototype.toIPayloadEvent = function () {
        return this.eventPayload;
    };
    return ConvInteractionAdapter;
}());
export { ConvInteractionAdapter };
//# sourceMappingURL=InteractionSubscription.js.map