"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const index_esm = require("../../../min-dash/dist/index.esm.js");
const BpmnTreeWalker = require("./BpmnTreeWalker.js");
const ModelUtil = require("../util/ModelUtil.js");
function y(e, r, t) {
  var o, a, s, u, v, l = [];
  function i(n, f) {
    var d = {
      root: function(p, c) {
        return o.add(p, c);
      },
      element: function(p, c, I) {
        return o.add(p, c, I);
      },
      error: function(p, c) {
        l.push({ message: p, context: c });
      }
    }, R = new BpmnTreeWalker.default(d, s);
    f = f || n.diagrams && n.diagrams[0];
    var h = C(n, f);
    if (!h)
      throw new Error(s("no diagram to display"));
    index_esm.forEach(h, function(p) {
      R.handleDefinitions(n, p);
    });
    var w = f.plane.bpmnElement.id;
    u.setRootElement(
      u.findRoot(w + "_plane") || u.findRoot(w)
    );
  }
  return new Promise(function(n, f) {
    try {
      return o = e.get("bpmnImporter"), a = e.get("eventBus"), s = e.get("translate"), u = e.get("canvas"), a.fire("import.render.start", { definitions: r }), i(r, t), a.fire("import.render.complete", {
        error: v,
        warnings: l
      }), n({ warnings: l });
    } catch (d) {
      return d.warnings = l, f(d);
    }
  });
}
function C(e, r) {
  if (r) {
    var t = r.plane.bpmnElement, o = t;
    !ModelUtil.is(t, "bpmn:Process") && !ModelUtil.is(t, "bpmn:Collaboration") && (o = P(t));
    var a;
    ModelUtil.is(o, "bpmn:Collaboration") ? a = o : a = index_esm.find(e.rootElements, function(i) {
      if (ModelUtil.is(i, "bpmn:Collaboration"))
        return index_esm.find(i.participants, function(n) {
          return n.processRef === o;
        });
    });
    var s = [o];
    a && (s = index_esm.map(a.participants, function(i) {
      return i.processRef;
    }), s.push(a));
    var u = g(s), v = [r], l = [t];
    return index_esm.forEach(e.diagrams, function(i) {
      var n = i.plane.bpmnElement;
      u.indexOf(n) !== -1 && l.indexOf(n) === -1 && (v.push(i), l.push(n));
    }), v;
  }
}
function g(e) {
  var r = [];
  return index_esm.forEach(e, function(t) {
    t && (r.push(t), r = r.concat(g(t.flowElements)));
  }), r;
}
function P(e) {
  for (var r = e; r; ) {
    if (ModelUtil.is(r, "bpmn:Process"))
      return r;
    r = r.$parent;
  }
}
exports.importBpmnDiagram = y;
