"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const index_esm = require("../../../../min-dash/dist/index.esm.js");
const index_esm$1 = require("../../../../min-dom/dist/index.esm.js");
const Elements = require("../../util/Elements.js");
const IdGenerator = require("../../util/IdGenerator.js");
var S = new IdGenerator.default("ov"), Z = 500;
function s(t, e, n, i) {
  this._eventBus = e, this._canvas = n, this._elementRegistry = i, this._ids = S, this._overlayDefaults = index_esm.assign({
    // 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 (index_esm.isString(t) && (t = { id: t }), index_esm.isString(t.element) && (t.element = this._elementRegistry.get(t.element)), t.element) {
    var e = this._getOverlayContainer(t.element, true);
    return e ? t.type ? index_esm.filter(e.overlays, index_esm.matchPattern({ type: t.type })) : e.overlays.slice() : [];
  } else
    return t.type ? index_esm.filter(this._overlays, index_esm.matchPattern({ type: t.type })) : t.id ? this._overlays[t.id] : null;
};
s.prototype.add = function(t, e, n) {
  if (index_esm.isObject(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 = index_esm.assign({}, 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) || [];
  index_esm.isArray(e) || (e = [e]);
  var n = this;
  index_esm.forEach(e, function(i) {
    var o = n._getOverlayContainer(i.element, true);
    if (i && (index_esm$1.remove(i.html), index_esm$1.remove(i.htmlContainer), delete i.htmlContainer, delete i.element, delete n._overlays[i.id]), o) {
      var a = o.overlays.indexOf(i);
      a !== -1 && o.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 = [], index_esm$1.clear(this._overlayRoot);
};
s.prototype._updateOverlayContainer = function(t) {
  var e = t.element, n = t.html, i = e.x, o = e.y;
  if (e.waypoints) {
    var a = Elements.getBBox(e);
    i = a.x, o = a.y;
  }
  R(n, i, o), index_esm$1.attr(t.html, "data-container-id", e.id);
};
s.prototype._updateOverlay = function(t) {
  var e = t.position, n = t.htmlContainer, i = t.element, o = e.left, a = e.top;
  if (e.right !== void 0) {
    var r;
    i.waypoints ? r = Elements.getBBox(i).width : r = i.width, o = e.right * -1 + r;
  }
  if (e.bottom !== void 0) {
    var l;
    i.waypoints ? l = Elements.getBBox(i).height : l = i.height, a = e.bottom * -1 + l;
  }
  R(n, o || 0, a || 0), this._updateOverlayVisibilty(t, this._canvas.viewbox());
};
s.prototype._createOverlayContainer = function(t) {
  var e = index_esm$1.domify('<div class="djs-overlays" />');
  index_esm$1.assignStyle(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 = index_esm.find(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, o, a;
  i.get && i.constructor.prototype.jquery && (i = i.get(0)), index_esm.isString(i) && (i = index_esm$1.domify(i)), a = this._getOverlayContainer(n), o = index_esm$1.domify('<div class="djs-overlay" data-overlay-id="' + e + '">'), index_esm$1.assignStyle(o, { position: "absolute" }), o.appendChild(i), t.type && index_esm$1.classes(o).add("djs-overlay-" + t.type);
  var r = this._canvas.findRoot(n), l = this._canvas.getRootElement();
  d(o, r === l), t.htmlContainer = o, a.overlays.push(t), a.html.appendChild(o), 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), o = n && n.minZoom, a = n && n.maxZoom, r = t.htmlContainer, l = this._canvas.getRootElement(), f = true;
  (i !== l || n && (index_esm.isDefined(o) && o > e.scale || index_esm.isDefined(a) && a < e.scale)) && (f = false), d(r, f), this._updateOverlayScale(t, e);
};
s.prototype._updateOverlayScale = function(t, e) {
  var n = t.scale, i, o, a = t.htmlContainer, r, l = "";
  n !== true && (n === false ? (i = 1, o = 1) : (i = n.min, o = n.max), index_esm.isDefined(i) && e.scale < i && (r = (1 / e.scale || 1) * i), index_esm.isDefined(o) && e.scale > o && (r = (1 / e.scale || 1) * o)), index_esm.isDefined(r) && (l = "scale(" + r + "," + r + ")"), w(a, l);
};
s.prototype._updateOverlaysVisibilty = function(t) {
  var e = this;
  index_esm.forEach(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 o = i.element, a = e.get({ element: o });
    index_esm.forEach(a, function(f) {
      e.remove(f.id);
    });
    var r = e._getOverlayContainer(o);
    if (r) {
      index_esm$1.remove(r.html);
      var l = e._overlayContainers.indexOf(r);
      l !== -1 && e._overlayContainers.splice(l, 1);
    }
  }), t.on("element.changed", Z, function(i) {
    var o = i.element, a = e._getOverlayContainer(o, true);
    a && (index_esm.forEach(a.overlays, function(r) {
      e._updateOverlay(r);
    }), e._updateOverlayContainer(a));
  }), t.on("element.marker.update", function(i) {
    var o = e._getOverlayContainer(i.element, true);
    o && index_esm$1.classes(o.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 = index_esm$1.domify(
    '<div class="djs-overlay-container" />'
  );
  return index_esm$1.assignStyle(e, {
    position: "absolute",
    width: 0,
    height: 0
  }), t.insertBefore(e, t.firstChild), e;
}
function R(t, e, n) {
  index_esm$1.assignStyle(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;
  });
}
exports.default = s;
