"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const LayoutUtil = require("../../../../diagram-js/lib/layout/LayoutUtil.js");
const ModelUtil = require("../../util/ModelUtil.js");
var DEFAULT_POSITION = {
  x: 180,
  y: 160
};
function SubprocessCompatibility(eventBus, moddle) {
  this._eventBus = eventBus;
  this._moddle = moddle;
  var self = this;
  eventBus.on("import.render.start", 1500, function(e, context) {
    self.handleImport(context.definitions);
  });
}
SubprocessCompatibility.prototype.handleImport = function(definitions) {
  if (!definitions.diagrams) {
    return;
  }
  var self = this;
  this._definitions = definitions;
  this._processToDiagramMap = {};
  definitions.diagrams.forEach(function(diagram) {
    if (!diagram.plane || !diagram.plane.bpmnElement) {
      return;
    }
    self._processToDiagramMap[diagram.plane.bpmnElement.id] = diagram;
  });
  var newDiagrams = [];
  definitions.diagrams.forEach(function(diagram) {
    var createdDiagrams = self.createNewDiagrams(diagram.plane);
    Array.prototype.push.apply(newDiagrams, createdDiagrams);
  });
  newDiagrams.forEach(function(diagram) {
    self.movePlaneElementsToOrigin(diagram.plane);
  });
};
SubprocessCompatibility.prototype.createNewDiagrams = function(plane) {
  var self = this;
  var collapsedElements = [];
  var elementsToMove = [];
  plane.get("planeElement").forEach(function(diElement) {
    var bo = diElement.bpmnElement;
    if (!bo) {
      return;
    }
    var parent = bo.$parent;
    if (ModelUtil.is(bo, "bpmn:SubProcess") && !diElement.isExpanded) {
      collapsedElements.push(bo);
    }
    if (shouldMoveToPlane(bo, plane)) {
      elementsToMove.push({ diElement, parent });
    }
  });
  var newDiagrams = [];
  collapsedElements.forEach(function(element) {
    if (!self._processToDiagramMap[element.id]) {
      var diagram = self.createDiagram(element);
      self._processToDiagramMap[element.id] = diagram;
      newDiagrams.push(diagram);
    }
  });
  elementsToMove.forEach(function(element) {
    var diElement = element.diElement;
    var parent = element.parent;
    while (parent && collapsedElements.indexOf(parent) === -1) {
      parent = parent.$parent;
    }
    if (!parent) {
      return;
    }
    var diagram = self._processToDiagramMap[parent.id];
    self.moveToDiPlane(diElement, diagram.plane);
  });
  return newDiagrams;
};
SubprocessCompatibility.prototype.movePlaneElementsToOrigin = function(plane) {
  var elements = plane.get("planeElement");
  var planeBounds = getPlaneBounds(plane);
  var offset = {
    x: planeBounds.x - DEFAULT_POSITION.x,
    y: planeBounds.y - DEFAULT_POSITION.y
  };
  elements.forEach(function(diElement) {
    if (diElement.waypoint) {
      diElement.waypoint.forEach(function(waypoint) {
        waypoint.x = waypoint.x - offset.x;
        waypoint.y = waypoint.y - offset.y;
      });
    } else if (diElement.bounds) {
      diElement.bounds.x = diElement.bounds.x - offset.x;
      diElement.bounds.y = diElement.bounds.y - offset.y;
    }
  });
};
SubprocessCompatibility.prototype.moveToDiPlane = function(diElement, newPlane) {
  var containingDiagram = findRootDiagram(diElement);
  var parentPlaneElement = containingDiagram.plane.get("planeElement");
  parentPlaneElement.splice(parentPlaneElement.indexOf(diElement), 1);
  newPlane.get("planeElement").push(diElement);
};
SubprocessCompatibility.prototype.createDiagram = function(bo) {
  var plane = this._moddle.create("bpmndi:BPMNPlane", { bpmnElement: bo });
  var diagram = this._moddle.create("bpmndi:BPMNDiagram", {
    plane
  });
  plane.$parent = diagram;
  plane.bpmnElement = bo;
  diagram.$parent = this._definitions;
  this._definitions.diagrams.push(diagram);
  return diagram;
};
SubprocessCompatibility.$inject = ["eventBus", "moddle"];
function findRootDiagram(element) {
  if (ModelUtil.is(element, "bpmndi:BPMNDiagram")) {
    return element;
  } else {
    return findRootDiagram(element.$parent);
  }
}
function getPlaneBounds(plane) {
  var planeTrbl = {
    top: Infinity,
    right: -Infinity,
    bottom: -Infinity,
    left: Infinity
  };
  plane.planeElement.forEach(function(element) {
    if (!element.bounds) {
      return;
    }
    var trbl = LayoutUtil.asTRBL(element.bounds);
    planeTrbl.top = Math.min(trbl.top, planeTrbl.top);
    planeTrbl.left = Math.min(trbl.left, planeTrbl.left);
  });
  return LayoutUtil.asBounds(planeTrbl);
}
function shouldMoveToPlane(bo, plane) {
  var parent = bo.$parent;
  if (!ModelUtil.is(parent, "bpmn:SubProcess") || parent === plane.bpmnElement) {
    return false;
  }
  if (ModelUtil.isAny(bo, ["bpmn:DataInputAssociation", "bpmn:DataOutputAssociation"])) {
    return false;
  }
  return true;
}
exports.default = SubprocessCompatibility;
