"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const index_esm = require("../../../min-dash/dist/index.esm.js");
const Util = require("./Util.js");
const CompatibilityUtil = require("../util/CompatibilityUtil.js");
function r(m, o) {
  return m.$instanceOf(o);
}
function N(m) {
  return index_esm.find(m.rootElements, function(o) {
    return r(o, "bpmn:Process") || r(o, "bpmn:Collaboration");
  });
}
function tn(m, o) {
  var v = {}, c = [], d = {};
  function u(n, e) {
    return function(i) {
      n(i, e);
    };
  }
  function D(n) {
    v[n.id] = n;
  }
  function I(n) {
    return v[n.id];
  }
  function L(n, e) {
    var i = n.gfx;
    if (i)
      throw new Error(
        o("already rendered {element}", { element: Util.elementToString(n) })
      );
    return m.element(n, d[n.id], e);
  }
  function j(n, e) {
    return m.root(n, d[n.id], e);
  }
  function s(n, e) {
    try {
      var i = d[n.id] && L(n, e);
      return D(n), i;
    } catch (t) {
      p(t.message, { element: n, error: t }), console.error(o("failed to import {element}", { element: Util.elementToString(n) })), console.error(t);
    }
  }
  function p(n, e) {
    m.error(n, e);
  }
  function h(n) {
    var e = n.bpmnElement;
    e ? d[e.id] ? p(
      o("multiple DI elements defined for {element}", {
        element: Util.elementToString(e)
      }),
      { element: e }
    ) : (d[e.id] = n, CompatibilityUtil.ensureCompatDiRef(e)) : p(
      o("no bpmnElement referenced in {element}", {
        element: Util.elementToString(n)
      }),
      { element: n }
    );
  }
  function M(n) {
    q(n.plane);
  }
  function q(n) {
    h(n), index_esm.forEach(n.planeElement, B);
  }
  function B(n) {
    h(n);
  }
  function T(n, e) {
    var i = n.diagrams;
    if (e && i.indexOf(e) === -1)
      throw new Error(o("diagram not part of bpmn:Definitions"));
    if (!e && i && i.length && (e = i[0]), !e)
      throw new Error(o("no diagram to display"));
    d = {}, M(e);
    var t = e.plane;
    if (!t)
      throw new Error(o(
        "no plane for {element}",
        { element: Util.elementToString(e) }
      ));
    var f = t.bpmnElement;
    if (!f)
      if (f = N(n), f)
        p(
          o("correcting missing bpmnElement on {plane} to {rootElement}", {
            plane: Util.elementToString(t),
            rootElement: Util.elementToString(f)
          })
        ), t.bpmnElement = f, h(t);
      else
        throw new Error(o("no process or collaboration to display"));
    var w = j(f, t);
    if (r(f, "bpmn:Process") || r(f, "bpmn:SubProcess"))
      b(f, w);
    else if (r(f, "bpmn:Collaboration"))
      V(f, w), k(n.rootElements, w);
    else
      throw new Error(
        o("unsupported bpmnElement for {plane}: {rootElement}", {
          plane: Util.elementToString(t),
          rootElement: Util.elementToString(f)
        })
      );
    S();
  }
  function S() {
    for (var n; c.length; )
      n = c.shift(), n();
  }
  function b(n, e) {
    R(n, e), F(n.ioSpecification, e), E(n.artifacts, e), D(n);
  }
  function k(n, e) {
    var i = index_esm.filter(n, function(t) {
      return !I(t) && r(t, "bpmn:Process") && t.laneSets;
    });
    i.forEach(u(b, e));
  }
  function z(n, e) {
    s(n, e);
  }
  function H(n, e) {
    index_esm.forEach(n, u(z, e));
  }
  function g(n, e) {
    s(n, e);
  }
  function U(n, e) {
    s(n, e);
  }
  function W(n, e) {
    s(n, e);
  }
  function P(n, e) {
    s(n, e);
  }
  function E(n, e) {
    index_esm.forEach(n, function(i) {
      r(i, "bpmn:Association") ? c.push(function() {
        P(i, e);
      }) : P(i, e);
    });
  }
  function F(n, e) {
    n && (index_esm.forEach(n.dataInputs, u(U, e)), index_esm.forEach(n.dataOutputs, u(W, e)));
  }
  function C(n, e) {
    R(n, e), E(n.artifacts, e);
  }
  function O(n, e) {
    var i = s(n, e);
    r(n, "bpmn:SubProcess") && C(n, i || e), r(n, "bpmn:Activity") && F(n.ioSpecification, e), c.push(function() {
      index_esm.forEach(n.dataInputAssociations, u(g, e)), index_esm.forEach(n.dataOutputAssociations, u(g, e));
    });
  }
  function $(n, e) {
    s(n, e);
  }
  function y(n, e) {
    s(n, e);
  }
  function G(n, e) {
    c.push(function() {
      var i = s(n, e);
      n.childLaneSet && A(n.childLaneSet, i || e), X(n);
    });
  }
  function A(n, e) {
    index_esm.forEach(n.lanes, u(G, e));
  }
  function J(n, e) {
    index_esm.forEach(n, u(A, e));
  }
  function R(n, e) {
    K(n.flowElements, e), n.laneSets && J(n.laneSets, e);
  }
  function K(n, e) {
    index_esm.forEach(n, function(i) {
      r(i, "bpmn:SequenceFlow") ? c.push(function() {
        $(i, e);
      }) : r(i, "bpmn:BoundaryEvent") ? c.unshift(function() {
        O(i, e);
      }) : r(i, "bpmn:FlowNode") ? O(i, e) : r(i, "bpmn:DataObject") || (r(i, "bpmn:DataStoreReference") || r(i, "bpmn:DataObjectReference") ? y(i, e) : p(
        o("unrecognized flowElement {element} in context {context}", {
          element: Util.elementToString(i),
          context: e ? Util.elementToString(e.businessObject) : "null"
        }),
        { element: i, context: e }
      ));
    });
  }
  function Q(n, e) {
    var i = s(n, e), t = n.processRef;
    t && b(t, i || e);
  }
  function V(n, e) {
    index_esm.forEach(n.participants, u(Q, e)), E(n.artifacts, e), c.push(function() {
      H(n.messageFlows, e);
    });
  }
  function X(n) {
    index_esm.forEach(n.flowNodeRef, function(e) {
      var i = e.get("lanes");
      i && i.push(n);
    });
  }
  return {
    handleDeferred: S,
    handleDefinitions: T,
    handleSubProcess: C,
    registerDi: h
  };
}
exports.default = tn;
