import { assign as q, isString as S$1, filter as v, matchPattern as I, isObject as E, isArray as p, forEach as o$1, find as _, isDefined as w$1 } from "../../../../min-dash/dist/index.esm.mjs";
import { domify as G, assignStyle as B, remove as I$1, clear as k$1, attr as F, classes as R$1 } from "../../../../min-dom/dist/index.esm.mjs";
import { getBBox as g } from "../../util/Elements.mjs";
import o from "../../util/IdGenerator.mjs";
var S = new o("ov"), Z = 500;
function s(t, e, n, i) {
  this._eventBus = e, this._canvas = n, this._elementRegistry = i, this._ids = S, this._overlayDefaults = q({
    // no show constraints
    show: null,
    // always scale
    scale: true
  }, t && t.defaults), this._overlays = {}, this._overlayContainers = [], this._overlayRoot = k(n.getContainer()), this._init();
}
s.$inject = [
  "config.overlays",
  "eventBus",
  "canvas",
  "elementRegistry"
];
s.prototype.get = function(t) {
  if (S$1(t) && (t = { id: t }), S$1(t.element) && (t.element = this._elementRegistry.get(t.element)), t.element) {
    var e = this._getOverlayContainer(t.element, true);
    return e ? t.type ? v(e.overlays, I({ type: t.type })) : e.overlays.slice() : [];
  } else
    return t.type ? v(this._overlays, I({ type: t.type })) : t.id ? this._overlays[t.id] : null;
};
s.prototype.add = function(t, e, n) {
  if (E(e) && (n = e, e = null), t.id || (t = this._elementRegistry.get(t)), !n.position)
    throw new Error("must specifiy overlay position");
  if (!n.html)
    throw new Error("must specifiy overlay html");
  if (!t)
    throw new Error("invalid element specified");
  var i = this._ids.next();
  return n = q({}, this._overlayDefaults, n, {
    id: i,
    type: e,
    element: t,
    html: n.html
  }), this._addOverlay(n), i;
};
s.prototype.remove = function(t) {
  var e = this.get(t) || [];
  p(e) || (e = [e]);
  var n = this;
  o$1(e, function(i) {
    var o2 = n._getOverlayContainer(i.element, true);
    if (i && (I$1(i.html), I$1(i.htmlContainer), delete i.htmlContainer, delete i.element, delete n._overlays[i.id]), o2) {
      var a = o2.overlays.indexOf(i);
      a !== -1 && o2.overlays.splice(a, 1);
    }
  });
};
s.prototype.show = function() {
  d(this._overlayRoot);
};
s.prototype.hide = function() {
  d(this._overlayRoot, false);
};
s.prototype.clear = function() {
  this._overlays = {}, this._overlayContainers = [], k$1(this._overlayRoot);
};
s.prototype._updateOverlayContainer = function(t) {
  var e = t.element, n = t.html, i = e.x, o2 = e.y;
  if (e.waypoints) {
    var a = g(e);
    i = a.x, o2 = a.y;
  }
  R(n, i, o2), F(t.html, "data-container-id", e.id);
};
s.prototype._updateOverlay = function(t) {
  var e = t.position, n = t.htmlContainer, i = t.element, o2 = e.left, a = e.top;
  if (e.right !== void 0) {
    var r;
    i.waypoints ? r = g(i).width : r = i.width, o2 = e.right * -1 + r;
  }
  if (e.bottom !== void 0) {
    var l;
    i.waypoints ? l = g(i).height : l = i.height, a = e.bottom * -1 + l;
  }
  R(n, o2 || 0, a || 0), this._updateOverlayVisibilty(t, this._canvas.viewbox());
};
s.prototype._createOverlayContainer = function(t) {
  var e = G('<div class="djs-overlays" />');
  B(e, { position: "absolute" }), this._overlayRoot.appendChild(e);
  var n = {
    html: e,
    element: t,
    overlays: []
  };
  return this._updateOverlayContainer(n), this._overlayContainers.push(n), n;
};
s.prototype._updateRoot = function(t) {
  var e = t.scale || 1, n = "matrix(" + [
    e,
    0,
    0,
    e,
    -1 * t.x * e,
    -1 * t.y * e
  ].join(",") + ")";
  w(this._overlayRoot, n);
};
s.prototype._getOverlayContainer = function(t, e) {
  var n = _(this._overlayContainers, function(i) {
    return i.element === t;
  });
  return !n && !e ? this._createOverlayContainer(t) : n;
};
s.prototype._addOverlay = function(t) {
  var e = t.id, n = t.element, i = t.html, o2, a;
  i.get && i.constructor.prototype.jquery && (i = i.get(0)), S$1(i) && (i = G(i)), a = this._getOverlayContainer(n), o2 = G('<div class="djs-overlay" data-overlay-id="' + e + '">'), B(o2, { position: "absolute" }), o2.appendChild(i), t.type && R$1(o2).add("djs-overlay-" + t.type);
  var r = this._canvas.findRoot(n), l = this._canvas.getRootElement();
  d(o2, r === l), t.htmlContainer = o2, a.overlays.push(t), a.html.appendChild(o2), this._overlays[e] = t, this._updateOverlay(t), this._updateOverlayVisibilty(t, this._canvas.viewbox());
};
s.prototype._updateOverlayVisibilty = function(t, e) {
  var n = t.show, i = this._canvas.findRoot(t.element), o2 = n && n.minZoom, a = n && n.maxZoom, r = t.htmlContainer, l = this._canvas.getRootElement(), f = true;
  (i !== l || n && (w$1(o2) && o2 > e.scale || w$1(a) && a < e.scale)) && (f = false), d(r, f), this._updateOverlayScale(t, e);
};
s.prototype._updateOverlayScale = function(t, e) {
  var n = t.scale, i, o2, a = t.htmlContainer, r, l = "";
  n !== true && (n === false ? (i = 1, o2 = 1) : (i = n.min, o2 = n.max), w$1(i) && e.scale < i && (r = (1 / e.scale || 1) * i), w$1(o2) && e.scale > o2 && (r = (1 / e.scale || 1) * o2)), w$1(r) && (l = "scale(" + r + "," + r + ")"), w(a, l);
};
s.prototype._updateOverlaysVisibilty = function(t) {
  var e = this;
  o$1(this._overlays, function(n) {
    e._updateOverlayVisibilty(n, t);
  });
};
s.prototype._init = function() {
  var t = this._eventBus, e = this;
  function n(i) {
    e._updateRoot(i), e._updateOverlaysVisibilty(i), e.show();
  }
  t.on("canvas.viewbox.changing", function(i) {
    e.hide();
  }), t.on("canvas.viewbox.changed", function(i) {
    n(i.viewbox);
  }), t.on(["shape.remove", "connection.remove"], function(i) {
    var o2 = i.element, a = e.get({ element: o2 });
    o$1(a, function(f) {
      e.remove(f.id);
    });
    var r = e._getOverlayContainer(o2);
    if (r) {
      I$1(r.html);
      var l = e._overlayContainers.indexOf(r);
      l !== -1 && e._overlayContainers.splice(l, 1);
    }
  }), t.on("element.changed", Z, function(i) {
    var o2 = i.element, a = e._getOverlayContainer(o2, true);
    a && (o$1(a.overlays, function(r) {
      e._updateOverlay(r);
    }), e._updateOverlayContainer(a));
  }), t.on("element.marker.update", function(i) {
    var o2 = e._getOverlayContainer(i.element, true);
    o2 && R$1(o2.html)[i.add ? "add" : "remove"](i.marker);
  }), t.on("root.set", function() {
    e._updateOverlaysVisibilty(e._canvas.viewbox());
  }), t.on("diagram.clear", this.clear, this);
};
function k(t) {
  var e = G(
    '<div class="djs-overlay-container" />'
  );
  return B(e, {
    position: "absolute",
    width: 0,
    height: 0
  }), t.insertBefore(e, t.firstChild), e;
}
function R(t, e, n) {
  B(t, { left: e + "px", top: n + "px" });
}
function d(t, e) {
  t.style.display = e === false ? "none" : "";
}
function w(t, e) {
  t.style["transform-origin"] = "top left", ["", "-ms-", "-webkit-"].forEach(function(n) {
    t.style[n + "transform"] = e;
  });
}
export {
  s as default
};
