"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.EventDispatcher = void 0;
exports.createDispatch = createDispatch;

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }

var EventDispatcher = /*#__PURE__*/function () {
  function EventDispatcher() {
    _classCallCheck(this, EventDispatcher);

    this.listeners = {};
  }

  _createClass(EventDispatcher, [{
    key: "on",
    value: function on(event, cb) {
      if (!this.listeners[event]) {
        this.listeners[event] = new Set();
      }

      this.listeners[event].add(cb);
    }
  }, {
    key: "off",
    value: function off(event, cb) {
      if (!this.listeners[event]) {
        return;
      }

      if (this.listeners[event].has(cb)) {
        this.listeners[event]["delete"](cb);
      }
    }
  }, {
    key: "emit",
    value: function emit(event, data) {
      if (!this.listeners[event]) {
        return;
      }

      this.listeners[event].forEach(function (cb) {
        return cb(data);
      });
    }
  }, {
    key: "destroy",
    value: function destroy() {
      this.listeners = {};
    }
  }]);

  return EventDispatcher;
}();
/**
 * Creates a dispatch function that can be called inside ProseMirror Plugin
 * to notify listeners about that plugin's state change.
 */


exports.EventDispatcher = EventDispatcher;

function createDispatch(eventDispatcher) {
  return function (eventName, data) {
    if (!eventName) {
      throw new Error('event name is required!');
    }

    var event = typeof eventName === 'string' ? eventName : eventName.key;
    eventDispatcher.emit(event, data);
  };
}