"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 label_1 = require("../renderable/label");
var MessageType_1 = require("../support/MessageType");
var EventStream_1 = require("../event/EventStream");
var EventType_1 = require("../event/EventType");
var MultiTargetRenderer_1 = require("../renderer/MultiTargetRenderer");
/**
 * IBehaviour implementation which listens for a keyup event with code 13 in order to publish
 * a text message to the outgoing text channel.
 */
var KeyboardBehaviour = /** @class */ (function (_super) {
    __extends(KeyboardBehaviour, _super);
    function KeyboardBehaviour(renderer, target, callback) {
        var _this = _super.call(this, target || Defaults_1.Defaults.textbox(), { type: 'keyup', handler: function (event) { return _this.handler(event); } }) || this;
        _this.renderer = renderer;
        _this.callback = callback;
        _this.subscription = null;
        return _this;
    }
    KeyboardBehaviour.prototype.handler = function (ev) {
        var _this = this;
        if (ev.key === "Enter") {
            var element = this.target;
            var value = element.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 });
                element.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 KeyboardBehaviour;
}(api_1.IBehaviour));
exports.KeyboardBehaviour = KeyboardBehaviour;
//# sourceMappingURL=KeyboardBehaviour.js.map