"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const index_esm = require("../../../../min-dom/dist/index.esm.js");
const index_esm$1 = require("../../../../min-dash/dist/index.esm.js");
const EscapeUtil = require("../../../../diagram-js/lib/util/EscapeUtil.js");
const ModelUtil = require("../../util/ModelUtil.js");
const DrilldownUtil = require("../../util/DrilldownUtil.js");
var OPEN_CLASS = "bjs-breadcrumbs-shown";
function DrilldownBreadcrumbs(eventBus, elementRegistry, overlays, canvas) {
  var breadcrumbs = index_esm.domify('<ul class="bjs-breadcrumbs"></ul>');
  var container = canvas.getContainer();
  var containerClasses = index_esm.classes(container);
  container.appendChild(breadcrumbs);
  var boParents = [];
  eventBus.on("element.changed", function(e) {
    var shape = e.element, bo = ModelUtil.getBusinessObject(shape);
    var isPresent = index_esm$1.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 = EscapeUtil.escapeHTML(parent.name || parent.id);
      var link = index_esm.domify('<li><span class="bjs-crumb"><a title="' + title + '">' + title + "</a></span></li>");
      var parentPlane = canvas.findRoot(DrilldownUtil.getPlaneIdFromShape(parent)) || canvas.findRoot(parent.id);
      if (!parentPlane && ModelUtil.is(parent, "bpmn:Process")) {
        var participant = elementRegistry.find(function(element2) {
          var bo = ModelUtil.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 = ModelUtil.getBusinessObject(child);
  var parents = [];
  for (var element = bo; element; element = element.$parent) {
    if (ModelUtil.is(element, "bpmn:SubProcess") || ModelUtil.is(element, "bpmn:Process")) {
      parents.push(element);
    }
  }
  return parents.reverse();
}
exports.default = DrilldownBreadcrumbs;
