import _defineProperty from "@babel/runtime/helpers/defineProperty";
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-explicit-any

// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-explicit-any

// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export class EventDispatcher {
  constructor() {
    _defineProperty(this, "listeners", {});
  }
  on(event, cb) {
    if (!this.listeners[event]) {
      this.listeners[event] = new Set();
    }
    this.listeners[event].add(cb);
  }
  has(event, cb) {
    if (!this.listeners[event]) {
      return false;
    }
    return this.listeners[event].has(cb);
  }
  off(event, cb) {
    if (!this.listeners[event]) {
      return;
    }
    if (this.listeners[event].has(cb)) {
      this.listeners[event].delete(cb);
    }
  }
  emit(event, data) {
    if (!this.listeners[event]) {
      return;
    }
    this.listeners[event].forEach(cb => cb(data));
  }
  destroy() {
    this.listeners = {};
  }
}
function getEventFromEventName(eventName) {
  return typeof eventName === 'string' ? eventName : eventName.key;
}

/**
 * Creates a dispatch function that can be called inside ProseMirror Plugin
 * to notify listeners about that plugin's state change.
 */
// eslint-disable-next-line @atlaskit/volt-strict-mode/no-multiple-exports
export function createDispatch(eventDispatcher) {
  return (eventName, data) => {
    if (!eventName) {
      throw new Error('event name is required!');
    }
    eventDispatcher.emit(getEventFromEventName(eventName), data);
  };
}