"use strict";
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 __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
var api_1 = require("../api");
var Defaults_1 = require("../support/Defaults");
var MessageType_1 = require("../support/MessageType");
var label_1 = require("../renderable/label");
var EventStream_1 = require("../event/EventStream");
var EventType_1 = require("../event/EventType");
var MultiTargetRenderer_1 = require("../renderer/MultiTargetRenderer");
/**
 * IBehaviour implementation which listens for a mouse click event in order to publish
 * a text message to the outgoing text channel.
 */
var MouseBehaviour = /** @class */ (function (_super) {
    __extends(MouseBehaviour, _super);
    function MouseBehaviour(renderer, button, textArea, callback) {
        var _this = _super.call(this, button || Defaults_1.Defaults.invoker(), { type: 'click', handler: function () { return _this.handler(); } }) || this;
        _this.textArea = textArea || Defaults_1.Defaults.textbox();
        _this.renderer = renderer;
        _this.callback = callback;
        _this.subscription = null;
        return _this;
    }
    MouseBehaviour.prototype.handler = function () {
        var _this = this;
        var value = this.textArea.value;
        if (this.isValueValid(value)) {
            var evType = EventType_1.EventType.withChannelId(EventType_1.EventType.PUBLISH, this.channelId);
            var attributes = { text: value };
            var type = MessageType_1.MessageType.UTTERANCE;
            EventStream_1.default.emit(evType, { type: type, payload: value, attributes: attributes });
            this.textArea.value = "";
            var newElement = { type: label_1.Label.TYPE, text: value, position: "right", channelId: this.channelId };
            this.renderer.render(newElement).forEach(function (e) {
                _this.renderer instanceof MultiTargetRenderer_1.MultiTargetRenderer ?
                    _this.renderer.appendContent(e, _this.channelId) :
                    _this.renderer.appendContent(e);
            });
            if (this.callback !== undefined) {
                this.callback();
            }
        }
    };
    return MouseBehaviour;
}(api_1.IBehaviour));
exports.MouseBehaviour = MouseBehaviour;
//# sourceMappingURL=MouseBehaviour.js.map