import { asBounds as B, asTRBL as m$1 } from "../../../../diagram-js/lib/layout/LayoutUtil.mjs";
import { is as u$1, isAny as c } from "../../util/ModelUtil.mjs";
var u = {
  x: 180,
  y: 160
};
function s(n, a) {
  this._eventBus = n, this._moddle = a;
  var e = this;
  n.on("import.render.start", 1500, function(r, o) {
    e.handleImport(o.definitions);
  });
}
s.prototype.handleImport = function(n) {
  if (n.diagrams) {
    var a = this;
    this._definitions = n, this._processToDiagramMap = {}, n.diagrams.forEach(function(r) {
      !r.plane || !r.plane.bpmnElement || (a._processToDiagramMap[r.plane.bpmnElement.id] = r);
    });
    var e = [];
    n.diagrams.forEach(function(r) {
      var o = a.createNewDiagrams(r.plane);
      Array.prototype.push.apply(e, o);
    }), e.forEach(function(r) {
      a.movePlaneElementsToOrigin(r.plane);
    });
  }
};
s.prototype.createNewDiagrams = function(n) {
  var a = this, e = [], r = [];
  n.get("planeElement").forEach(function(t) {
    var i = t.bpmnElement;
    if (i) {
      var p = i.$parent;
      u$1(i, "bpmn:SubProcess") && !t.isExpanded && e.push(i), g(i, n) && r.push({ diElement: t, parent: p });
    }
  });
  var o = [];
  return e.forEach(function(t) {
    if (!a._processToDiagramMap[t.id]) {
      var i = a.createDiagram(t);
      a._processToDiagramMap[t.id] = i, o.push(i);
    }
  }), r.forEach(function(t) {
    for (var i = t.diElement, p = t.parent; p && e.indexOf(p) === -1; )
      p = p.$parent;
    if (p) {
      var l = a._processToDiagramMap[p.id];
      a.moveToDiPlane(i, l.plane);
    }
  }), o;
};
s.prototype.movePlaneElementsToOrigin = function(n) {
  var a = n.get("planeElement"), e = v(n), r = {
    x: e.x - u.x,
    y: e.y - u.y
  };
  a.forEach(function(o) {
    o.waypoint ? o.waypoint.forEach(function(t) {
      t.x = t.x - r.x, t.y = t.y - r.y;
    }) : o.bounds && (o.bounds.x = o.bounds.x - r.x, o.bounds.y = o.bounds.y - r.y);
  });
};
s.prototype.moveToDiPlane = function(n, a) {
  var e = m(n), r = e.plane.get("planeElement");
  r.splice(r.indexOf(n), 1), a.get("planeElement").push(n);
};
s.prototype.createDiagram = function(n) {
  var a = this._moddle.create("bpmndi:BPMNPlane", { bpmnElement: n }), e = this._moddle.create("bpmndi:BPMNDiagram", {
    plane: a
  });
  return a.$parent = e, a.bpmnElement = n, e.$parent = this._definitions, this._definitions.diagrams.push(e), e;
};
s.$inject = ["eventBus", "moddle"];
function m(n) {
  return u$1(n, "bpmndi:BPMNDiagram") ? n : m(n.$parent);
}
function v(n) {
  var a = {
    top: 1 / 0,
    right: -1 / 0,
    bottom: -1 / 0,
    left: 1 / 0
  };
  return n.planeElement.forEach(function(e) {
    if (e.bounds) {
      var r = m$1(e.bounds);
      a.top = Math.min(r.top, a.top), a.left = Math.min(r.left, a.left);
    }
  }), B(a);
}
function g(n, a) {
  var e = n.$parent;
  return !(!u$1(e, "bpmn:SubProcess") || e === a.bpmnElement || c(n, ["bpmn:DataInputAssociation", "bpmn:DataOutputAssociation"]));
}
export {
  s as default
};
