"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var EventEmitter = /** @class */ (function () {
    function EventEmitter() {
        this.listeners = {};
    }
    EventEmitter.prototype.addListener = function (event, listener) {
        if (this.listeners[event] === undefined) {
            this.listeners[event] = [];
        }
        this.listeners[event].push(listener);
    };
    EventEmitter.prototype.on = function (event, listener) {
        this.addListener(event, {
            cb: listener,
        });
    };
    EventEmitter.prototype.once = function (event, listener) {
        var _this = this;
        var returnPromise = new Promise(function (resolve) {
            return _this.addListener(event, {
                cb: resolve,
                once: true,
            });
        });
        if (listener) {
            returnPromise.then(listener);
        }
        return returnPromise;
    };
    EventEmitter.prototype.emit = function (event, data) {
        if (!this.listeners[event]) {
            return;
        }
        this.listeners[event] = this.listeners[event].filter(function (listener) {
            listener.cb(data);
            return !listener.once;
        });
    };
    EventEmitter.prototype.off = function (event, listener) {
        if (!this.listeners[event]) {
            return;
        }
        var tIndex = this.listeners[event].findIndex(function (l) { return l.cb === listener; });
        this.listeners[event].splice(tIndex, 1);
    };
    EventEmitter.prototype.getListeners = function (event) {
        if (!this.listeners[event]) {
            return [];
        }
        return this.listeners[event].map(function (listener) { return listener.cb; });
    };
    return EventEmitter;
}());
exports.EventEmitter = EventEmitter;
