import { domify as domify$1, classes } from "../../../../min-dom/dist/index.esm.mjs";
import { find } from "../../../../min-dash/dist/index.esm.mjs";
import { escapeHTML } from "../../../../diagram-js/lib/util/EscapeUtil.mjs";
import { getBusinessObject, is } from "../../util/ModelUtil.mjs";
import { getPlaneIdFromShape } from "../../util/DrilldownUtil.mjs";
var OPEN_CLASS = "bjs-breadcrumbs-shown";
function DrilldownBreadcrumbs(eventBus, elementRegistry, overlays, canvas) {
  var breadcrumbs = domify$1('<ul class="bjs-breadcrumbs"></ul>');
  var container = canvas.getContainer();
  var containerClasses = classes(container);
  container.appendChild(breadcrumbs);
  var boParents = [];
  eventBus.on("element.changed", function(e) {
    var shape = e.element, bo = getBusinessObject(shape);
    var isPresent = find(boParents, function(el) {
      return el === bo;
    });
    if (!isPresent) {
      return;
    }
    updateBreadcrumbs();
  });
  function updateBreadcrumbs(element) {
    if (element) {
      boParents = getBoParentChain(element);
    }
    var path = boParents.map(function(parent) {
      var title = escapeHTML(parent.name || parent.id);
      var link = domify$1('<li><span class="bjs-crumb"><a title="' + title + '">' + title + "</a></span></li>");
      var parentPlane = canvas.findRoot(getPlaneIdFromShape(parent)) || canvas.findRoot(parent.id);
      if (!parentPlane && is(parent, "bpmn:Process")) {
        var participant = elementRegistry.find(function(element2) {
          var bo = getBusinessObject(element2);
          return bo && bo.processRef && bo.processRef === parent;
        });
        parentPlane = canvas.findRoot(participant.id);
      }
      link.addEventListener("click", function() {
        canvas.setRootElement(parentPlane);
      });
      return link;
    });
    breadcrumbs.innerHTML = "";
    var visible = path.length > 1;
    containerClasses.toggle(OPEN_CLASS, visible);
    path.forEach(function(el) {
      breadcrumbs.appendChild(el);
    });
  }
  eventBus.on("root.set", function(event) {
    updateBreadcrumbs(event.element);
  });
}
DrilldownBreadcrumbs.$inject = ["eventBus", "elementRegistry", "overlays", "canvas"];
function getBoParentChain(child) {
  var bo = getBusinessObject(child);
  var parents = [];
  for (var element = bo; element; element = element.$parent) {
    if (is(element, "bpmn:SubProcess") || is(element, "bpmn:Process")) {
      parents.push(element);
    }
  }
  return parents.reverse();
}
export {
  DrilldownBreadcrumbs as default
};
