import { assign as q } from "../../../../../node_modules/min-dash/dist/index.esm.mjs";
import { is as u } from "../../../../../node_modules/bpmn-js/lib/util/ModelUtil.mjs";
import { getExternalLabelBounds as s } from "../../../../../node_modules/bpmn-js/lib/util/LabelUtil.mjs";
import { getMid as P } from "../../../../../node_modules/diagram-js/lib/layout/LayoutUtil.mjs";
import { isExpanded as e } from "../../../../../node_modules/bpmn-js/lib/util/DiUtil.mjs";
import { getLabel as p } from "../../../../../node_modules/bpmn-js/lib/features/label-editing/LabelUtil.mjs";
import { elementToString as t } from "../../../../../node_modules/bpmn-js/lib/import/Util.mjs";
function f(t2, n, r) {
  return q(
    {
      id: t2.id,
      type: t2.$type,
      businessObject: t2,
      di: n
    },
    r
  );
}
function M(t2, n, r) {
  var e2 = t2.waypoint;
  return !e2 || e2.length < 2 ? [P(n), P(r)] : e2.map(function(a) {
    return { x: a.x, y: a.y };
  });
}
function g(t$1, n, r, e2) {
  return new Error(
    t$1(
      "element {element} referenced by {referenced}#{property} not yet drawn",
      {
        element: t(r),
        referenced: t(n),
        property: e2
      }
    )
  );
}
function h(t2, n, r, e2, a, i) {
  this._eventBus = t2, this._canvas = n, this._elementFactory = r, this._elementRegistry = e2, this._translate = a, this._textRenderer = i;
}
h.$inject = [
  "eventBus",
  "canvas",
  "elementFactory",
  "elementRegistry",
  "translate",
  "textRenderer"
];
h.prototype.add = function(t$1, n, r) {
  var e$1, a = this._translate, i, p$1;
  if (u(n, "bpmndi:BPMNPlane")) {
    var v = u(t$1, "bpmn:SubProcess") ? { id: t$1.id + "_plane" } : {};
    e$1 = this._elementFactory.createRoot(f(t$1, n, v)), this._canvas.addRootElement(e$1);
  } else if (u(n, "bpmndi:BPMNShape")) {
    var m = !e(t$1, n), w = S(t$1);
    i = r && (r.hidden || r.collapsed);
    var d = n.bounds;
    e$1 = this._elementFactory.createShape(
      f(t$1, n, {
        collapsed: m,
        hidden: i,
        x: Math.round(d.x),
        y: Math.round(d.y),
        width: Math.round(d.width),
        height: Math.round(d.height),
        isFrame: w
      })
    ), u(t$1, "bpmn:BoundaryEvent") && this._attachBoundary(t$1, e$1), u(t$1, "bpmn:Lane") && (p$1 = 0), u(t$1, "bpmn:DataStoreReference") && (b(r, P(d)) || (r = this._canvas.findRoot(r))), this._canvas.addShape(e$1, r, p$1);
  } else if (u(n, "bpmndi:BPMNEdge")) {
    var l = this._getSource(t$1), y = this._getTarget(t$1);
    i = r && (r.hidden || r.collapsed), e$1 = this._elementFactory.createConnection(
      f(t$1, n, {
        hidden: i,
        source: l,
        target: y,
        waypoints: M(n, l, y)
      })
    ), u(t$1, "bpmn:DataAssociation") && (r = this._canvas.findRoot(r)), this._canvas.addConnection(e$1, r, p$1);
  } else
    throw new Error(
      a("unknown di {di} for element {semantic}", {
        di: t(n),
        semantic: t(t$1)
      })
    );
  let _ = p(e$1);
  return _ && _ != "undefined" && this.addLabel(t$1, n, e$1), this._eventBus.fire("bpmnElement.added", { element: e$1 }), e$1;
};
h.prototype._attachBoundary = function(t$1, n) {
  var r = this._translate, e2 = t$1.attachedToRef;
  if (!e2)
    throw new Error(
      r("missing {semantic}#attachedToRef", {
        semantic: t(t$1)
      })
    );
  var a = this._elementRegistry.get(e2.id), i = a && a.attachers;
  if (!a)
    throw g(
      r,
      t$1,
      e2,
      "attachedToRef"
    );
  n.host = a, i || (a.attachers = i = []), i.indexOf(n) === -1 && i.push(n);
};
h.prototype.addLabel = function(t2, n, r) {
  var e2, a, i;
  return e2 = s(n, r), a = p(r), a && a != "undefined" && (e2 = this._textRenderer.getExternalLabelBounds(e2, a)), i = this._elementFactory.createLabel(
    f(t2, n, {
      id: t2.id + "_label",
      labelTarget: r,
      type: "label",
      hidden: r.hidden || !p(r),
      x: Math.round(e2.x),
      y: Math.round(e2.y),
      width: Math.round(e2.width),
      height: Math.round(e2.height)
    })
  ), this._canvas.addShape(i, r.parent);
};
h.prototype._getEnd = function(t$1, n) {
  var r, e2, a = t$1.$type, i = this._translate;
  if (e2 = t$1[n + "Ref"], n === "source" && a === "bpmn:DataInputAssociation" && (e2 = e2 && e2[0]), (n === "source" && a === "bpmn:DataOutputAssociation" || n === "target" && a === "bpmn:DataInputAssociation") && (e2 = t$1.$parent), r = e2 && this._getElement(e2), r)
    return r;
  throw e2 ? g(i, t$1, e2, n + "Ref") : new Error(
    i("{semantic}#{side} Ref not specified", {
      semantic: t(t$1),
      side: n
    })
  );
};
h.prototype._getSource = function(t2) {
  return this._getEnd(t2, "source");
};
h.prototype._getTarget = function(t2) {
  return this._getEnd(t2, "target");
};
h.prototype._getElement = function(t2) {
  return this._elementRegistry.get(t2.id);
};
function b(t2, n) {
  var r = n.x, e2 = n.y;
  return r >= t2.x && r <= t2.x + t2.width && e2 >= t2.y && e2 <= t2.y + t2.height;
}
function S(t2) {
  return u(t2, "bpmn:Group");
}
export {
  h as default
};
