function EventEmitter() {
  let instance;

  let listeners = {};

  function on(eventName, callback) {
    if (!listeners[eventName]) {
      listeners[eventName] = [];
    }

    if (listeners[eventName].indexOf(callback) !== -1) return;

    listeners[eventName].push(callback);
  }

  function dispatchEvent(eventName, payload) {
    if (!listeners[eventName]) return;
    listeners[eventName].forEach((listener) => listener(payload));
  }

  function off(eventName, callback) {
    if (!listeners[eventName]) return;
    let index = listeners[eventName].indexOf(callback);
    listeners[eventName].splice(index, 1);
  }

  instance = {
    on,
    off,
    dispatchEvent,
  };

  return instance;
}

export default EventEmitter;
