import t from "../../../../inherits-browser/dist/index.es.mjs";
import n$1 from "../../../../diagram-js/lib/command/CommandInterceptor.mjs";
import { is as u } from "../../util/ModelUtil.mjs";
import { classes as R, domify as G } from "../../../../min-dom/dist/index.esm.mjs";
import { getPlaneIdFromShape as u$1 } from "../../util/DrilldownUtil.mjs";
var v = 250, m = '<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 16 16"><path fill-rule="evenodd" d="M4.81801948,3.50735931 L10.4996894,9.1896894 L10.5,4 L12,4 L12,12 L4,12 L4,10.5 L9.6896894,10.4996894 L3.75735931,4.56801948 C3.46446609,4.27512627 3.46446609,3.80025253 3.75735931,3.50735931 C4.05025253,3.21446609 4.52512627,3.21446609 4.81801948,3.50735931 Z"/></svg>', w = "bjs-drilldown-empty";
function n(r, t2, l, s) {
  n$1.call(this, t2), this._canvas = r, this._eventBus = t2, this._elementRegistry = l, this._overlays = s;
  var e = this;
  this.executed("shape.toggleCollapse", v, function(a) {
    var o = a.shape;
    e.canDrillDown(o) ? e.addOverlay(o) : e.removeOverlay(o);
  }, true), this.reverted("shape.toggleCollapse", v, function(a) {
    var o = a.shape;
    e.canDrillDown(o) ? e.addOverlay(o) : e.removeOverlay(o);
  }, true), this.executed(
    ["shape.create", "shape.move", "shape.delete"],
    v,
    function(a) {
      var o = a.oldParent, d = a.newParent || a.parent, i = a.shape;
      e.canDrillDown(i) && e.addOverlay(i), e.updateDrilldownOverlay(o), e.updateDrilldownOverlay(d), e.updateDrilldownOverlay(i);
    },
    true
  ), this.reverted(
    ["shape.create", "shape.move", "shape.delete"],
    v,
    function(a) {
      var o = a.oldParent, d = a.newParent || a.parent, i = a.shape;
      e.canDrillDown(i) && e.addOverlay(i), e.updateDrilldownOverlay(o), e.updateDrilldownOverlay(d), e.updateDrilldownOverlay(i);
    },
    true
  ), t2.on("import.render.complete", function() {
    l.filter(function(a) {
      return e.canDrillDown(a);
    }).map(function(a) {
      e.addOverlay(a);
    });
  });
}
t(n, n$1);
n.prototype.updateDrilldownOverlay = function(r) {
  var t2 = this._canvas;
  if (r) {
    var l = t2.findRoot(r);
    l && this.updateOverlayVisibility(l);
  }
};
n.prototype.canDrillDown = function(r) {
  var t2 = this._canvas;
  return u(r, "bpmn:SubProcess") && t2.findRoot(u$1(r));
};
n.prototype.updateOverlayVisibility = function(r) {
  var t2 = this._overlays, l = r.businessObject, s = t2.get({ element: l.id, type: "drilldown" })[0];
  if (s) {
    var e = l && l.flowElements && l.flowElements.length;
    R(s.html).toggle(w, !e);
  }
};
n.prototype.addOverlay = function(r) {
  var t2 = this._canvas, l = this._overlays, s = l.get({ element: r, type: "drilldown" });
  s.length && this.removeOverlay(r);
  var e = G('<button class="bjs-drilldown">' + m + "</button>");
  e.addEventListener("click", function() {
    t2.setRootElement(t2.findRoot(u$1(r)));
  }), l.add(r, "drilldown", {
    position: {
      bottom: -7,
      right: -8
    },
    html: e
  }), this.updateOverlayVisibility(r);
};
n.prototype.removeOverlay = function(r) {
  var t2 = this._overlays;
  t2.remove({
    element: r,
    type: "drilldown"
  });
};
n.$inject = [
  "canvas",
  "eventBus",
  "elementRegistry",
  "overlays"
];
export {
  n as default
};
