'use strict';

var has = Object.prototype.hasOwnProperty;
class Events {
}
class EE {
  constructor(fn, context, once = false) {
    this.fn = fn;
    this.context = context;
    this.once = once;
  }
}
function addListener(emitter, event, fn, context, once) {
  if (typeof fn !== "function") {
    throw new TypeError("The listener must be a function");
  }
  var listener = new EE(fn, context || emitter, once);
  var evt = event;
  if (!emitter._events[evt]) {
    emitter._events[evt] = listener;
    emitter._eventsCount++;
  } else if (!emitter._events[evt].fn)
    emitter._events[evt].push(listener);
  else
    emitter._events[evt] = [emitter._events[evt], listener];
  return emitter;
}
function clearEvent(emitter, evt) {
  if (--emitter._eventsCount === 0)
    emitter._events = new Events();
  else
    delete emitter._events[evt];
}
class EventEmitter {
  constructor() {
    this._events = new Events();
    this._eventsCount = 0;
  }
  /**
   * Return an array listing the events for which the emitter has registered
   * listeners.
   *
   * @returns {Array}
   * @public
   */
  eventNames() {
    var names = [];
    var events;
    var name;
    if (this._eventsCount === 0)
      return names;
    for (name in events = this._events) {
      if (has.call(events, name))
        names.push(name);
    }
    if (Object.getOwnPropertySymbols) {
      return names.concat(Object.getOwnPropertySymbols(events));
    }
    return names;
  }
  /**
   * Return the listeners registered for a given event.
   *
   * @param {(String|Symbol)} event The event name.
   * @returns {Array} The registered listeners.
   * @public
   */
  listeners(event) {
    var evt = event;
    var handlers = this._events[evt];
    if (!handlers)
      return [];
    if (handlers.fn)
      return [handlers.fn];
    var l = handlers.length;
    var ee = new Array(l);
    for (var i = 0; i < l; i++) {
      ee[i] = handlers[i].fn;
    }
    return ee;
  }
  /**
   * Return the number of listeners listening to a given event.
   *
   * @param {(String|Symbol)} event The event name.
   * @returns {Number} The number of listeners.
   * @public
   */
  listenerCount(event) {
    var evt = event;
    var listeners = this._events[evt];
    if (!listeners)
      return 0;
    if (listeners.fn)
      return 1;
    return listeners.length;
  }
  /**
   * Calls each of the listeners registered for a given event.
   *
   * @param {(String|Symbol)} event The event name.
   * @returns {Boolean} `true` if the event had listeners, else `false`.
   * @public
   */
  emit(event, a1, a2, a3, a4, a5) {
    var evt = event;
    if (!this._events[evt])
      return false;
    var listeners = this._events[evt];
    var len = arguments.length;
    var args;
    var i;
    if (listeners.fn) {
      if (listeners.once)
        this.removeListener(event, listeners.fn, void 0, true);
      switch (len) {
        case 1:
          listeners.fn.call(listeners.context);
          break;
        case 2:
          listeners.fn.call(listeners.context, a1);
          break;
        case 3:
          listeners.fn.call(listeners.context, a1, a2);
          break;
        case 4:
          listeners.fn.call(listeners.context, a1, a2, a3);
          break;
        case 5:
          listeners.fn.call(listeners.context, a1, a2, a3, a4);
          break;
        case 6:
          listeners.fn.call(listeners.context, a1, a2, a3, a4, a5);
          break;
        default:
          for (i = 1, args = new Array(len - 1); i < len; i++) {
            args[i - 1] = arguments[i];
          }
          listeners.fn.apply(listeners.context, args);
      }
    } else {
      var length = listeners.length;
      var j;
      for (i = 0; i < length; i++) {
        if (listeners[i].once)
          this.removeListener(event, listeners[i].fn, void 0, true);
        switch (len) {
          case 1:
            listeners[i].fn.call(listeners[i].context);
            break;
          case 2:
            listeners[i].fn.call(listeners[i].context, a1);
            break;
          case 3:
            listeners[i].fn.call(listeners[i].context, a1, a2);
            break;
          case 4:
            listeners[i].fn.call(listeners[i].context, a1, a2, a3);
            break;
          default:
            if (!args) {
              for (j = 1, args = new Array(len - 1); j < len; j++) {
                args[j - 1] = arguments[j];
              }
            }
            listeners[i].fn.apply(listeners[i].context, args);
        }
      }
    }
    return true;
  }
  /**
   * Add a listener for a given event.
   *
   * @param {(String|Symbol)} event The event name.
   * @param {Function} fn The listener function.
   * @param {*} [context=this] The context to invoke the listener with.
   * @returns {EventEmitter} `this`.
   * @public
   */
  on(event, fn, context) {
    addListener(this, event, fn, context, false);
    return this;
  }
  /**
   * Add a one-time listener for a given event.
   *
   * @param {(String|Symbol)} event The event name.
   * @param {Function} fn The listener function.
   * @param {*} [context=this] The context to invoke the listener with.
   * @returns {EventEmitter} `this`.
   * @public
   */
  once(event, fn, context) {
    addListener(this, event, fn, context, true);
    return this;
  }
  /**
   * Remove the listeners of a given event.
   *
   * @param {(String|Symbol)} event The event name.
   * @param {Function} fn Only remove the listeners that match this function.
   * @param {*} context Only remove the listeners that have this context.
   * @param {Boolean} once Only remove one-time listeners.
   * @returns {EventEmitter} `this`.
   * @public
   */
  removeListener(event, fn, context, once) {
    var evt = event;
    if (!this._events[evt])
      return this;
    if (!fn) {
      clearEvent(this, evt);
      return this;
    }
    var listeners = this._events[evt];
    if (listeners.fn) {
      if (listeners.fn === fn && (!once || listeners.once) && (!context || listeners.context === context)) {
        clearEvent(this, evt);
      }
    } else {
      var events = [];
      for (var i = 0, length = listeners.length; i < length; i++) {
        if (listeners[i].fn !== fn || once && !listeners[i].once || context && listeners[i].context !== context) {
          events.push(listeners[i]);
        }
      }
      if (events.length)
        this._events[evt] = events.length === 1 ? events[0] : events;
      else
        clearEvent(this, evt);
    }
    return this;
  }
  /**
   * Remove all listeners, or those of the specified event.
   *
   * @param {(String|Symbol)} [event] The event name.
   * @returns {EventEmitter} `this`.
   * @public
   */
  removeAllListeners(event) {
    var evt;
    if (event) {
      evt = event;
      if (this._events[evt])
        clearEvent(this, evt);
    } else {
      this._events = new Events();
      this._eventsCount = 0;
    }
    return this;
  }
  //
  // Alias methods names because people roll like that.
  //
  off(event, fn, context, once) {
    return this.removeListener(event, fn, context, once);
  }
  addListener(event, fn, context) {
    addListener(this, event, fn, context, true);
    return this;
  }
}

exports.EventEmitter = EventEmitter;
//# sourceMappingURL=event_emitter.js.map
