const PubSub = {
  handlers: {}
};

PubSub.off = function (eventType, fn) {
  const fns = this.handlers[eventType] || [];

  if (fn) {
    const index = fns.findIndex(_fn => _fn === fn);
    if (index > -1) fns.splice(index, 1);
  } else {
    delete this.handlers[eventType];
  }

  return this;
};

PubSub.on = function (eventType, handler) {
  if (!(eventType in this.handlers)) {
    this.handlers[eventType] = [];
  }

  this.handlers[eventType].push(handler);
  return this;
};

PubSub.emit = function (eventType) {
  // eslint-disable-next-line prefer-rest-params
  const handlerArgs = Array.prototype.slice.call(arguments, 1);
  if (!this.handlers[eventType]) return this;

  for (let i = 0; i < this.handlers[eventType].length; i++) {
    this.handlers[eventType][i].apply(this, handlerArgs);
  }

  return this;
};

export default PubSub;
/*
* 编辑游客
* */

export const EDIT_TOURIST_ITEM = 'EDIT_TOURIST_ITEM';
/*
* 编辑游客
* */

export const ADD_TOURIST_ITEM = 'ADD_TOURIST_ITEM';
/*
* 删除游客
* */

export const DELETE_TOURIST_ITEM = 'DELETE_TOURIST_ITEM';
export const CHOOSE_TOURIST = 'CHOOSE_TOURIST';