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 { IBehaviour } from '../api';
import { Defaults } from '../support/Defaults';
import { Label } from "../renderable/label";
import { MessageType } from "../support/MessageType";
import EventStream from "../event/EventStream";
import { EventType } from "../event/EventType";
import { MultiTargetRenderer } from "../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.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.withChannelId(EventType.PUBLISH, this.channelId);
                var attributes = { text: value };
                var type = MessageType.UTTERANCE;
                EventStream.emit(evType, { type: type, payload: value, attributes: attributes });
                element.value = "";
                var newElement = { type: Label.TYPE, text: value, position: "right", channelId: this.channelId };
                this.renderer.render(newElement).forEach(function (e) {
                    _this.renderer instanceof MultiTargetRenderer ?
                        _this.renderer.appendContent(e, _this.channelId) :
                        _this.renderer.appendContent(e);
                });
                if (this.callback !== undefined) {
                    this.callback();
                }
            }
        }
    };
    return KeyboardBehaviour;
}(IBehaviour));
export { KeyboardBehaviour };
//# sourceMappingURL=KeyboardBehaviour.js.map