"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 WebRTCRecorder_1 = require("../audio/recorder/WebRTCRecorder");
var MessageType_1 = require("../support/MessageType");
var EventStream_1 = require("../event/EventStream");
var BufferedAudioPlayer_1 = require("../audio/player/BufferedAudioPlayer");
var EventType_1 = require("../event/EventType");
/**
 * IBehaviour implementation that listens for a onmouseup/down on the given recordButton.
 *
 * onmouseup starts the recording, onmousedown stops the recording and publishes an audio message
 * to the outbound audio channel.
 */
var VoiceBehaviour = /** @class */ (function (_super) {
    __extends(VoiceBehaviour, _super);
    function VoiceBehaviour(recordButton, recorder, callback) {
        if (recorder === void 0) { recorder = WebRTCRecorder_1.WebRTCRecorder.instance(); }
        if (callback === void 0) { callback = function () { return undefined; }; }
        var _this = _super.call(this, recordButton, [
            { type: AudioEventType.ON_MOUSE_DOWN, handler: function () { return _this.startRecordingOnMouseDown(); } },
            { type: AudioEventType.ON_MOUSE_UP, handler: function () { return _this.subscription && _this.stopRecordingOnMouseUp(_this.subscription); } }
        ]) || this;
        _this.recorder = recorder;
        _this.callback = callback;
        return _this;
    }
    /**
     *
     * @inheritDoc
     */
    VoiceBehaviour.prototype.bind = function (gateway) {
        this.handleAudio(gateway);
        _super.prototype.bind.call(this, gateway);
    };
    /**
     * Binds {@link AudioRecorder#startRecording} to {@link this#recordButton} when an "mousedown" event occurs
     */
    VoiceBehaviour.prototype.startRecordingOnMouseDown = function () {
        this.recorder.startRecording();
        this.callback(AudioEventType.ON_MOUSE_DOWN);
    };
    /**
     * Binds {@link AudioRecorder#stopRecording} to {@link this#recordButton} when an "mouseup" event occurs
     */
    VoiceBehaviour.prototype.stopRecordingOnMouseUp = function (gateway) {
        var _this = this;
        this.recorder.stopRecording().then(function (result) {
            return EventStream_1.default.emitEvent({
                type: EventType_1.EventType.withChannelId(EventType_1.EventType.PUBLISH, _this.channelId),
                payload: { type: MessageType_1.MessageType.AUDIO, payload: {}, attributes: { text: result } }
            });
        });
    };
    VoiceBehaviour.prototype.handleAudio = function (msg) {
        BufferedAudioPlayer_1.BufferedAudioPlayer.instance().play(msg);
    };
    return VoiceBehaviour;
}(api_1.IBehaviour));
exports.VoiceBehaviour = VoiceBehaviour;
var AudioEventType;
(function (AudioEventType) {
    AudioEventType["ON_MOUSE_DOWN"] = "mousedown";
    AudioEventType["ON_MOUSE_UP"] = "mouseup";
})(AudioEventType || (AudioEventType = {}));
//# sourceMappingURL=VoiceBehaviour.js.map