import { isArray as p, isFunction as F, isNumber as P, bind as U, assign as q } from "../../../min-dash/dist/index.esm.mjs";
var a = "__fn", c = 1e3, d = Array.prototype.slice;
function s() {
  this._listeners = {}, this.on("diagram.destroy", 1, this._destroy, this);
}
s.prototype.on = function(e, t, r, n) {
  if (e = p(e) ? e : [e], F(t) && (n = r, r = t, t = c), !P(t))
    throw new Error("priority must be a number");
  var i = r;
  n && (i = U(r, n), i[a] = r[a] || r);
  var o = this;
  e.forEach(function(f) {
    o._addListener(f, {
      priority: t,
      callback: i,
      next: null
    });
  });
};
s.prototype.once = function(e, t, r, n) {
  var i = this;
  if (F(t) && (n = r, r = t, t = c), !P(t))
    throw new Error("priority must be a number");
  function o() {
    o.__isTomb = true;
    var f = r.apply(n, arguments);
    return i.off(e, o), f;
  }
  o[a] = r, this.on(e, t, o);
};
s.prototype.off = function(e, t) {
  e = p(e) ? e : [e];
  var r = this;
  e.forEach(function(n) {
    r._removeListener(n, t);
  });
};
s.prototype.createEvent = function(e) {
  var t = new u();
  return t.init(e), t;
};
s.prototype.fire = function(e, t) {
  var r, n, i, o;
  if (o = d.call(arguments), typeof e == "object" && (t = e, e = t.type), !e)
    throw new Error("no event type specified");
  if (n = this._listeners[e], !!n) {
    t instanceof u ? r = t : r = this.createEvent(t), o[0] = r;
    var f = r.type;
    e !== f && (r.type = e);
    try {
      i = this._invokeListeners(r, o, n);
    } finally {
      e !== f && (r.type = f);
    }
    return i === void 0 && r.defaultPrevented && (i = false), i;
  }
};
s.prototype.handleError = function(e) {
  return this.fire("error", { error: e }) === false;
};
s.prototype._destroy = function() {
  this._listeners = {};
};
s.prototype._invokeListeners = function(e, t, r) {
  for (var n; r && !e.cancelBubble; )
    n = this._invokeListener(e, t, r), r = r.next;
  return n;
};
s.prototype._invokeListener = function(e, t, r) {
  var n;
  if (r.callback.__isTomb)
    return n;
  try {
    n = v(r.callback, t), n !== void 0 && (e.returnValue = n, e.stopPropagation()), n === false && e.preventDefault();
  } catch (i) {
    if (!this.handleError(i))
      throw console.error("unhandled error in event listener", i), i;
  }
  return n;
};
s.prototype._addListener = function(e, t) {
  var r = this._getListeners(e), n;
  if (!r) {
    this._setListeners(e, t);
    return;
  }
  for (; r; ) {
    if (r.priority < t.priority) {
      t.next = r, n ? n.next = t : this._setListeners(e, t);
      return;
    }
    n = r, r = r.next;
  }
  n.next = t;
};
s.prototype._getListeners = function(e) {
  return this._listeners[e];
};
s.prototype._setListeners = function(e, t) {
  this._listeners[e] = t;
};
s.prototype._removeListener = function(e, t) {
  var r = this._getListeners(e), n, i, o;
  if (!t) {
    this._setListeners(e, null);
    return;
  }
  for (; r; )
    n = r.next, o = r.callback, (o === t || o[a] === t) && (i ? i.next = n : this._setListeners(e, n)), i = r, r = n;
};
function u() {
}
u.prototype.stopPropagation = function() {
  this.cancelBubble = true;
};
u.prototype.preventDefault = function() {
  this.defaultPrevented = true;
};
u.prototype.init = function(e) {
  q(this, e || {});
};
function v(e, t) {
  return e.apply(null, t);
}
export {
  s as default
};
