import { forEach as o, find as _, filter as v } from "../../../min-dash/dist/index.esm.mjs";
import { elementToString as t } from "./Util.mjs";
import { ensureCompatDiRef as p } from "../util/CompatibilityUtil.mjs";
function r(m, o2) {
  return m.$instanceOf(o2);
}
function N(m) {
  return _(m.rootElements, function(o2) {
    return r(o2, "bpmn:Process") || r(o2, "bpmn:Collaboration");
  });
}
function tn(m, o$1) {
  var v$1 = {}, c = [], d = {};
  function u(n, e) {
    return function(i) {
      n(i, e);
    };
  }
  function D(n) {
    v$1[n.id] = n;
  }
  function I(n) {
    return v$1[n.id];
  }
  function L(n, e) {
    var i = n.gfx;
    if (i)
      throw new Error(
        o$1("already rendered {element}", { element: t(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$1) {
      p$1(t$1.message, { element: n, error: t$1 }), console.error(o$1("failed to import {element}", { element: t(n) })), console.error(t$1);
    }
  }
  function p$1(n, e) {
    m.error(n, e);
  }
  function h(n) {
    var e = n.bpmnElement;
    e ? d[e.id] ? p$1(
      o$1("multiple DI elements defined for {element}", {
        element: t(e)
      }),
      { element: e }
    ) : (d[e.id] = n, p(e)) : p$1(
      o$1("no bpmnElement referenced in {element}", {
        element: t(n)
      }),
      { element: n }
    );
  }
  function M(n) {
    q(n.plane);
  }
  function q(n) {
    h(n), o(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$1("diagram not part of bpmn:Definitions"));
    if (!e && i && i.length && (e = i[0]), !e)
      throw new Error(o$1("no diagram to display"));
    d = {}, M(e);
    var t$1 = e.plane;
    if (!t$1)
      throw new Error(o$1(
        "no plane for {element}",
        { element: t(e) }
      ));
    var f = t$1.bpmnElement;
    if (!f)
      if (f = N(n), f)
        p$1(
          o$1("correcting missing bpmnElement on {plane} to {rootElement}", {
            plane: t(t$1),
            rootElement: t(f)
          })
        ), t$1.bpmnElement = f, h(t$1);
      else
        throw new Error(o$1("no process or collaboration to display"));
    var w = j(f, t$1);
    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$1("unsupported bpmnElement for {plane}: {rootElement}", {
          plane: t(t$1),
          rootElement: t(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 = v(n, function(t2) {
      return !I(t2) && r(t2, "bpmn:Process") && t2.laneSets;
    });
    i.forEach(u(b, e));
  }
  function z(n, e) {
    s(n, e);
  }
  function H(n, e) {
    o(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) {
    o(n, function(i) {
      r(i, "bpmn:Association") ? c.push(function() {
        P(i, e);
      }) : P(i, e);
    });
  }
  function F(n, e) {
    n && (o(n.dataInputs, u(U, e)), o(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() {
      o(n.dataInputAssociations, u(g, e)), o(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) {
    o(n.lanes, u(G, e));
  }
  function J(n, e) {
    o(n, u(A, e));
  }
  function R(n, e) {
    K(n.flowElements, e), n.laneSets && J(n.laneSets, e);
  }
  function K(n, e) {
    o(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$1(
        o$1("unrecognized flowElement {element} in context {context}", {
          element: t(i),
          context: e ? t(e.businessObject) : "null"
        }),
        { element: i, context: e }
      ));
    });
  }
  function Q(n, e) {
    var i = s(n, e), t2 = n.processRef;
    t2 && b(t2, i || e);
  }
  function V(n, e) {
    o(n.participants, u(Q, e)), E(n.artifacts, e), c.push(function() {
      H(n.messageFlows, e);
    });
  }
  function X(n) {
    o(n.flowNodeRef, function(e) {
      var i = e.get("lanes");
      i && i.push(n);
    });
  }
  return {
    handleDeferred: S,
    handleDefinitions: T,
    handleSubProcess: C,
    registerDi: h
  };
}
export {
  tn as default
};
