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