yet-another-bpmn-auto-layout 0.0.8 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # bpmn-auto-layout-feat-ivan-tulaev
1
+ # YABAL - Yet Another BPMN Auto Layout
2
2
 
3
3
  Create and layout the graphical representation of a BPMN diagram.
4
4
 
@@ -6,7 +6,7 @@ Create and layout the graphical representation of a BPMN diagram.
6
6
 
7
7
  ## Differences with original repository
8
8
 
9
- * First level lanes are drawn in pools
9
+ * **Lanes** at all levels are drawn in pools
10
10
  * Given a collaboration **ALL** participant's process will be laid out.
11
11
  * **Collapsed** and **expanded sub-processes** has different layout.
12
12
  * Independent graphs of process are laid out on different grid lines.
@@ -30,10 +30,10 @@ console.log(diagramWithLayoutXML);
30
30
 
31
31
  ## Limitations
32
32
 
33
- * The following elements are not laid out:
34
- * Groups
33
+ The following elements are not laid out:
34
+ * Data Associations between processes
35
35
  * Text annotations
36
- * Lanes under first level
36
+ * Groups
37
37
 
38
38
  ## Resources
39
39
 
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";var t=require("bpmn-moddle"),e=require("graph-by-ivan-tulaev"),o=require("min-dash");function s(t){return n(t,"bpmn:SubProcess")||n(t,"bpmn:Task")?{width:100,height:80}:n(t,"bpmn:Gateway")?{width:50,height:50}:n(t,"bpmn:Event")?{width:36,height:36}:n(t,"bpmn:Participant")||n(t,"bpmn:Lane")?{width:400,height:100}:n(t,"bpmn:DataObjectReference")?{width:36,height:50}:n(t,"bpmn:DataStoreReference")?{width:50,height:50}:n(t,"bpmn:TextAnnotation")?{width:100,height:30}:{width:100,height:80}}function n(t,e){return t.$instanceOf(e)}function i(t){if(!t)throw new Error("Element is not defined in getOutgoingElements");const e=(t.outgoing||[]).map(t=>t.targetRef);return[...new Set(e)]}const r=150,d=140;function a(t,e){return new Set(i(t).concat(function(t){const e=new Set;if(t){const o=(t.attachers||[]).sort((t,e)=>(e.outgoing?e.outgoing.length:0)-(t.outgoing?t.outgoing.length:0)).map(t=>(t.outgoing||[]).reverse()).flat().map(t=>t.targetRef).filter((t,e,o)=>o.indexOf(t)===e);for(const t of o)e.add(t)}return[...e]}(t)))}function c(t){return{x:t.x+t.width/2,y:t.y+t.height/2}}function g(t,e,o="r",s="top-left"){if("h"===o&&(o=/left/.test(s)?"l":"r"),"v"===o&&(o=/top/.test(s)?"t":"b"),"t"===o)return{original:t,x:t.x,y:e.y};if("r"===o)return{original:t,x:e.x+e.width,y:t.y};if("b"===o)return{original:t,x:t.x,y:e.y+e.height};if("l"===o)return{original:t,x:e.x,y:t.y};throw new Error("unexpected dockingDirection: <"+o+">")}function h(t,e,o={x:0,y:0}){const[s,n]=e.find(t);return{x:n*r+o.x,y:s*d+o.y}}function l(t,e,o,n){const[i,a,c,g]=e,{width:h,height:l}=s(t);let u=((c||1)-1)*r+h,f=((g||1)-1)*d+l,p=a*r+(r-h)/2+o.x,E=i*d+(d-l)/2+o.y;if("bpmn:Lane"===t.$type){const t=30;u=(c||1)*r+r-t,p=a*r+o.x-75+t,E=i*d+o.y-70,f=(g||1)*d}return{width:u,height:f,x:p,y:E}}class u{constructor(t){this.moddle=t}create(t,e){return this.moddle.create(t,e||{})}createDiBounds(t){return this.create("dc:Bounds",t)}createDiLabel(){return this.create("bpmndi:BPMNLabel",{bounds:this.createDiBounds()})}createDiShape(t,e,s){return this.create("bpmndi:BPMNShape",o.assign({bpmnElement:t,bounds:this.createDiBounds(e)},s))}createDiWaypoints(t){var e=this;return o.map(t,function(t){return e.createDiWaypoint(t)})}createDiWaypoint(t){return this.create("dc:Point",o.pick(t,["x","y"]))}createDiEdge(t,e,s){return this.create("bpmndi:BPMNEdge",o.assign({bpmnElement:t,waypoint:this.createDiWaypoints(e)},s))}createDiPlane(t){return this.create("bpmndi:BPMNPlane",t)}createDiDiagram(t){return this.create("bpmndi:BPMNDiagram",t)}}class f{constructor(){this.isFlipped=!1,this._elements=new Map,this.rows={},this.cols={}}get rowCount(){return Object.keys(this.rows).length}get colCount(){return Object.keys(this.cols).length}get elementsCount(){return this._elements.size}get elements(){return new Set(this._elements.keys())}add(t,e){if(this._elements.has(t))throw new Error(`Cannot add duplicated element ${JSON.stringify(t)}`);if(!e)return void this._addStart(t);const o=this.rowCount-1,s=this.colCount-1,n=e[0]-o,i=e[1]-s;this.addRowCol(!1,o>=0?o:void 0,n),this.addRowCol(!0,s>=0?s:void 0,i),this._elements.set(t,e),this._addElementToRowsCols(t,e)}_addElementToRowsCols(t){const e=this._elements.get(t);this.rows[e[0]]?this.rows[e[0]].add(t):this.rows[e[0]]=new Set([t]),this.cols[e[1]]?this.cols[e[1]].add(t):this.cols[e[1]]=new Set([t])}_removeElementFromRowsCols(t){const e=this._elements.get(t);this.rows[e[0]]&&this.rows[e[0]].delete(t),this.cols[e[1]]&&this.cols[e[1]].delete(t)}_addStart(t){const[e]=this.getGridDimensions();this._elements.set(t,[e,0]),this._addElementToRowsCols(t,[e,0])}move(t,e){if(!this.elements.has(t))throw new Error(`Cannot move not exist element ${JSON.stringify(t)}`);if(!this.isValidPosition(e))throw new Error(`Cannot move element ${JSON.stringify(t)} to invalid position ${e}`);this._removeElementFromRowsCols(t),this._elements.set(t,e),this._addElementToRowsCols(t,e)}removeElement(t){this._elements.has(t)&&(this._removeElementFromRowsCols(t),this._elements.delete(t))}addRowCol(t,e,o=1){const s=t?this.colCount:this.rowCount,n=e>=s-1?e-(s-1)+o:o;for(let e=0;e<n;e++)t?this.cols[s+e]=new Set:this.rows[s+e]=new Set;for(const[s,n]of this._elements.entries()){((t?n[1]:n[0])>e||void 0===e)&&(this._removeElementFromRowsCols(s),n[t?1:0]+=o,this._addElementToRowsCols(s))}}expandRow(t,e,o=1){if(!Number.isInteger(t)||t<0||t>this.rowCount-1)throw new Error(`Can't expand row with index: ${t}. Grid row count is ${this.rowCount}`);const s=this.colCount,n=e>=s-1?e-(s-1)+o:o;for(let t=0;t<n;t++)this.cols[s+t]=new Set;[...this.rows[t]].forEach(t=>{const s=this.find(t);(s[1]>e||void 0===e)&&(this._removeElementFromRowsCols(t),s[1]+=o,this._addElementToRowsCols(t))})}find(t){return this._elements.get(t)}get(t,e){const o=new Set([...this._elements.entries()].filter(([o,s])=>s[0]===t&&s[1]===e).map(t=>t[0]));return o.size?o:null}getElementsInRange({row:t,col:e},{row:o,col:s}){return t>o&&([t,o]=[o,t]),e>s&&([e,s]=[s,e]),[...this._elements.entries()].filter(([,n])=>n[0]>=t&&n[0]<=o&&n[1]>=e&&n[1]<=s).map(t=>t[0])}getGridDimensions(){return[this.rowCount,this.colCount]}shrink(t){const e=[...this._elements.entries()].sort((e,o)=>(t?e[1][0]:e[1][1])-(t?o[1][0]:o[1][1]));let o=0,s=null;for(const n of e){const e=t?n[1][0]:n[1][1];this._removeElementFromRowsCols(n[0]),null===s?o=e:s!==e&&(o=o+e-s-1),s=e;const i=[...n[1]];t?i[0]-=o:i[1]-=o,this._elements.set(n[0],i),this._addElementToRowsCols(n[0])}for(const[e,o]of Object.entries(t?this.rows:this.cols))0===o.size&&(t?delete this.rows[e]:delete this.cols[e])}flip(t){const[e,o]=this.getGridDimensions();for(const[s,n]of this._elements.entries())this._removeElementFromRowsCols(s,n),this._elements.set(s,t?[e-1-n[0],n[1]]:[n[0],o-1-n[1]]),this._addElementToRowsCols(s,t?[e-1-n[0],n[1]]:[n[0],o-1-n[1]]);this.isFlipped=!this.isFlipped}hasElement(t){return this.elements.has(t)}isValidPosition(t){if(!t||!Array.isArray(t))return!1;const[e,o]=t;return Number.isInteger(e)&&Number.isInteger(o)&&e>=0&&o>=0}hasIntermediateElements(t,e,o){if(!this.isValidPosition(t)||!this.isValidPosition(e))return!1;if(o?t[1]!==e[1]:t[0]!==e[0])return!1;const s=o?t[0]:t[1],[n,i]=o?t[0]<=e[0]?[t[0],e[0]]:[e[0],t[0]]:t[1]<=e[1]?[t[1],e[1]]:[e[1],t[1]];return[...this._elements.values()].some(t=>o?t[1]===s&&t[0]>n&&t[0]<i:t[0]===s&&t[1]>n&&t[1]<i)}hasElementAt(t){if(!this.isValidPosition(t))return!1;const[e,o]=t;return!!this.get(e,o)}}class p extends f{constructor(t){super(),this.initialGraph=t,this.graph=new e.Graph}getLanes(){return[...this.elements].filter(t=>"bpmn:Lane"===t.$type)}add(t,e){if("bpmn:Lane"===t.$type)return void super.add(t,e);if(0===this.getLanes().length)return super.add(t,e),this.graph.addNode(t),void this._createNewEdgesFor(t);const o=this.find(t.laneRef);if(!e){const e=o[0],s=o[0]+o[3]-1;for(let o=e;o<=s;o++){const e=new Set(this.rows[o]);if(e.delete(t.laneRef),0===e.size)return super.add(t,[o,0]),this.graph.addNode(t),void this._createNewEdgesFor(t)}return this.addRowCol(!1,o[0]+o[3]-1),super.add(t,[o[0]+o[3],0]),o[3]+=1,this.graph.addNode(t),void this._createNewEdgesFor(t)}const s=e[0]+(e[3]||1)-(o[0]+(o[3]||1));s>0&&(this.addRowCol(!1,o[0]+(o[3]||1)-1,s),o[3]+=s),super.add(t,e),this.graph.addNode(t),this._createNewEdgesFor(t)}removeElement(t){this.graph.deleteNode(t),super.removeElement(t)}_edgeIsExist(t){return[...this.graph.edges].includes(t)}_addEdgeToGrid(t){this._edgeIsExist(t)||this.graph.addEdge(t)}_createNewEdgesFor(t){const e=[...this.initialGraph.getOutgoingEdgesFor(t),...this.initialGraph.getIncomingEdgesFor(t)].filter(t=>this.hasElement(t.source)&&this.hasElement(t.target));for(const t of e)this._addEdgeToGrid(t)}isCrossed(t,e=!1){return[...this._allEdges].some(o=>this.isIntersect(o,t,e))}get _allEdges(){return this.graph.edges}hasAnyCross(t){return[...t||this._allEdges].some(t=>this.getCrossedElementsFor(t).length>0||this.getCrossedElementsFor(t,!0).length>0)}shakeIt(t){const e=[...this.elements].filter(t=>"bpmn:Lane"!==t.$type).sort(t?(o=this,function(t,e){const s=o.find(t),n=o.find(e);return s[0]-n[0]||n[1]-s[1]}):function(t){return function(e,o){const s=t.find(e),n=t.find(o);return s[1]-n[1]||n[0]-s[0]}}(this)).reverse();for(var o;e.length>0;){const o=e.pop(),s=this.getChain(o,!t);for(const t of s){const o=e.indexOf(t);o>=0&&e.splice(o,1)}const[n,i]=this.find([...s][0]);if(!(t?n<=0:i<=0)){for(let e=t?n-1:i-1;e>=0;e--){const n=[...s].every(t=>{const e=this.find(t);return e&&!this.isCrossed(e,!0)&&!this.isCrossed(e,!1)});if(!n)break;const i=[...s].every(o=>{const s=this.find(o),n=t?e:s[0],i=t?s[1]:e;return!this.get(n,i)});if(!i)break;for(const o of s){const s=this.find(o);this.move(o,t?[e,s[1]]:[s[0],e])}const r=this.hasAnyCross(),d=this.getChain(o,!t);if(r||d.size>s.size){for(const o of s){const s=this.find(o);this.move(o,t?[e+1,s[1]]:[s[0],e+1])}break}}this.shrink(t)}}}getChain(t,e,o){const s=o||new Set;if(!t)return s;const n=this.find(t);if(!n)return s;const i=[],r=[...this.get(n[0],n[1])].filter(t=>"bpmn:Lane"!==t.$type);for(const t of r)s.add(t),[...this.getAllExistingEdgesFor(t)].filter(t=>{const o=this.getEdgeDirection(t);return e?"N_S"===o||"S_N"===o:"W_E"===o||"E_W"===o}).forEach(t=>i.push(t));for(const o of i){const n=o.source===t?o.target:o.source;if(!s.has(n)){const t=this.getChain(n,e,s);for(const e of t)s.add(e)}}return s}getExistingOutgoingEdgesFor(t){return this.isFlipped?[...this.graph.getIncomingEdgesFor(t)]:[...this.graph.getOutgoingEdgesFor(t)]}getExistingIncomingEdgesFor(t){return this.isFlipped?[...this.graph.getOutgoingEdgesFor(t)]:[...this.graph.getIncomingEdgesFor(t)]}getAllExistingEdgesFor(t){const e=this.getExistingOutgoingEdgesFor(t),o=this.getExistingIncomingEdgesFor(t);return new Set([...e,...o])}_separateGrid(){const t=e.Graph.mergeGraphs(new Set([this.graph])).getSeparatedGraphs();if(0===t.length)return[new p(this.initialGraph)];const o=[];for(const e of t){const t=new p(this.initialGraph);let s=null,n=null;for(const o of e.nodes){const e=[...this.find(o)];(null===s||s>e[0])&&(s=e[0]),(null===n||n<e[0])&&(n=e[0]),t.add(o,e)}o.push(t)}return o}_mergeGrids(t){const e=new p(t[0].initialGraph);return t.forEach(t=>{let o=e.rowCount;Object.keys(t.rows).forEach(s=>{0===t.rows[s].size?e.addRowCol(!1,o+Number.parseInt(s)-1):t.rows[s].forEach(s=>{const n=[...t.find(s)];n[0]=n[0]+o,e.add(s,n)})})}),e}getSourcePosition(t){const e=this.getEdgeSource(t);return this.find(e)}getTargetPosition(t){const e=this.getEdgeTarget(t);return this.find(e)}getEdgeDirection(t){const e=this.getSourcePosition(t),o=this.getTargetPosition(t);if(!this.isValidPosition(e)||!this.isValidPosition(o))throw new Error(`Invalid position of source or target in ${t.source.id}-${t.target.id} flipped:${this.isFlipped}`);const[s,n]=e,[i,r]=o,d=s-i,a=n-r;return 0===d&&0===a?"NO_DIRECTION":d>0&&0===a?"S_N":d>0&&a<0?"SW_NE":0===d&&a<0?"W_E":d<0&&a<0?"NW_SE":d<0&&0===a?"N_S":s<i&&n>r?"NE_SW":s===i&&n>r?"E_W":s>i&&n>r?"SE_NW":void 0}getPathFor(t){const e=this.getEdgeDirection(t);return"NO_DIRECTION"===e?this._pathForNoDirection():"S_N"===e?this._pathForSouthToNorth(t):"SW_NE"===e?this._pathForSouthWestToNorthEast(t):"W_E"===e?this._pathForWestToEast(t):"NW_SE"===e?this._pathForNorthWestToSouthEast(t):"N_S"===e?this._pathForNorthToSouth(t):"NE_SW"===e?this._pathForNorthEastToSouthWest(t):"E_W"===e?this._pathForEastToWest(t):"SE_NW"===e?this._pathForSouthEastToNorthWest(t):[]}_pathForNoDirection(){return[]}getEdgeSource(t){return this.isFlipped?t.target:t.source}getEdgeTarget(t){return this.isFlipped?t.source:t.target}_pathForSouthToNorth(t){const e=[],[o,s]=this.getSourcePosition(t),[n]=this.getTargetPosition(t);if("bpmn:BoundaryEvent"===this.getEdgeSource(t).$type&&t.id)return e;if(this.hasIntermediateElements(this.getSourcePosition(t),this.getTargetPosition(t),!0))return e;if([...this.getExistingOutgoingEdgesFor(this.getEdgeTarget(t))].map(t=>this.getEdgeTarget(t)).includes(this.getEdgeSource(t)))return e;for(let t=o-1;t>n;t--)e.push({position:[t,s],vCross:!0});return e}_pathForSouthWestToNorthEast(t){const e=[],[o,s]=this.getSourcePosition(t),[n,i]=this.getTargetPosition(t);if("bpmn:BoundaryEvent"!==this.getEdgeSource(t).$type)for(let t=s+1;t<i;t++)e.push({position:[o,t],hCross:!0});e.push({position:[o,i],hCross:!0,vCross:!0});for(let t=o-1;t>n;t--)e.push({position:[t,i],vCross:!0});return e}_pathForWestToEast(t){const e=[],[o,s]=this.getSourcePosition(t),[,n]=this.getTargetPosition(t);for(let t=s+1;t<n;t++)e.push({position:[o,t],hCross:!0});return e}_pathForNorthWestToSouthEast(t){const e=[],[o,s]=this.getSourcePosition(t),[n,i]=this.getTargetPosition(t);for(let t=o+1;t<n;t++)e.push({position:[t,s],vCross:!0});e.push({position:[n,s],vCross:!0,hCross:!0});for(let t=s+1;t<i;t++)e.push({position:[n,t],hCross:!0});return e}_pathForNorthToSouth(t){const e=[],[o,s]=this.getSourcePosition(t),[n]=this.getTargetPosition(t);for(let t=o+1;t<n;t++)e.push({position:[t,s],vCross:!0});return e}_pathForNorthEastToSouthWest(t){const e=[],[o,s]=this.getSourcePosition(t),[n,i]=this.getTargetPosition(t);for(let t=o+1;t<n;t++)e.push({position:[t,s],vCross:!0});e.push({position:[n,s],vCross:!0,hCross:!0});for(let t=s-1;t>i;t--)e.push({position:[n,t],hCross:!0});return e}_pathForEastToWest(t){const e=[],[o,s]=this.getSourcePosition(t),[,n]=this.getTargetPosition(t);for(let t=s-1;t>n;t--)e.push({position:[o,t],hCross:!0});return e}_pathForSouthEastToNorthWest(t){const e=[],[o,s]=this.getSourcePosition(t),[n,i]=this.getTargetPosition(t);if("bpmn:BoundaryEvent"!==this.getEdgeSource(t).$type||!t.id)for(let t=s-1;t>i;t--)e.push({position:[o,t],hCross:!0});"bpmn:BoundaryEvent"===this.getEdgeSource(t).$type&&t.id?e.push({position:[o,i],vCross:!0}):e.push({position:[o,i],hCross:!0,vCross:!0});for(let t=o-1;t>n;t--)e.push({position:[t,i],vCross:!0});return e}getCrossedElementsFor(t,e=!1){const o=[];for(const s of this.getPathFor(t)){const[t,n]=s.position,i=this.get(t,n);i&&(e&&s.vCross||!e&&s.hCross)&&o.push(i)}return o}isIntersect(t,e,o){const[s,n]=e,[i,r]=this.getSourcePosition(t),[d,a]=this.getTargetPosition(t),c=this.getEdgeDirection(t);return"S_N"===c?o&&n===r&&s<i&&s>d:"SW_NE"===c?!!(o&&n===a&&s<=i&&s>d)||!o&&n>r&&n<=a&&s===i:"W_E"===c?!o&&n>r&&n<a&&s===i:"NW_SE"===c?!!(o&&n===r&&s>i&&s<=d)||!o&&n>=r&&n<a&&s===d:"N_S"===c?o&&n===r&&s>i&&s<d:"NE_SW"===c?!!(o&&n===r&&s>i&&s<=d)||!o&&n>a&&n<=r&&s===d:"E_W"===c?!o&&n>a&&n<r&&s===i:"SE_NW"===c?!!(o&&n===a&&s>d&&s<=i)||!o&&n>=a&&n<r&&s===i&&!this.getExistingIncomingEdgesFor(this.getEdgeSource(t)).some(t=>"NW_SE"===this.getEdgeDirection(t)||"W_E"===this.getEdgeDirection(t)):void 0}getBackwardUpOutgoingEdgesFor(t){return this.getExistingOutgoingEdgesFor(t).filter(t=>{const e=this.getEdgeDirection(t);return"S_N"===e||"SE_NW"===e})}getGridCopy(){return this._mergeGrids([this])}removeEdge(t){this.graph.deleteEdge(t)}getGraphSegmentFrom(t){const o=new e.Graph;return this.graph.genericTraversing((t,e,s,n)=>{const i=this.getExistingOutgoingEdgesFor(t).filter(t=>{const e=this.getEdgeTarget(t),o=this.getEdgeSource(t);return!s.has(e)&&e!==o});o.addNode(t);const r=i.map(t=>this.getEdgeTarget(t));for(const t of r)o.addNode(t);for(const t of i)o.addEdge(t);return r},(e,o)=>e.has(t)?void 0:t),o}getSegmentLeftCoordinates(t){const e=new Map;for(const o of t.nodes){const[t,s]=this.find(o);e.has(t)||e.set(t,s),s<e.get(t)&&e.set(t,s)}return e}}function E(t,e,o,s,n){e.hasElement(t)||e.add(t);const i=(e.isFlipped?[...e.initialGraph.getIncomingEdgesFor(t)]:[...e.initialGraph.getOutgoingEdgesFor(t)]).filter(t=>!e.hasElement(e.isFlipped?t.source:t.target)).map(t=>e.isFlipped?t.source:t.target),r=function(t,e,o,s,n,i){const r=e.getExistingOutgoingEdgesFor(t),d=e.getExistingIncomingEdgesFor(t).map(t=>e.getEdgeSource(t)),[,a]=e.find(t),c=[...r].filter(t=>{const n=e.getEdgeTarget(t),i=e.find(n)[1];if(!m(n,o,e))return!1;return(!e.getExistingIncomingEdgesFor(n).map(t=>t.source).find(t=>d.includes(t))||!s)&&i<=a}).map(t=>e.getEdgeTarget(t)).sort(function(t){return function(e,o){const s=t.find(e),n=t.find(o);return s[0]-n[0]||s[1]-n[1]}}(e));for(const t of c)o.splice(o.indexOf(t),1),i.delete(t),e.removeElement(t);return c}(t,e,o,i&&i.length>0,0,s);let d=[...i,...r],a=[];d.forEach(n=>{const i=[...w(t,e,n)];if(e.isFlipped&&"bpmn:BoundaryEvent"===n.$type&&n.attachedToRef!==t){const t=n.attachedToRef;s.has(t)||(e.add(t,i),s.add(t),x(t,e,o,a),a.unshift(n))}e.add(n,i),s.add(n),x(n,e,o,a),function(t,e){let o=e.getBackwardUpOutgoingEdgesFor(t);for(;o.length>0;){const s=o.shift(),n=e.getSourcePosition(s),i=e.getTargetPosition(s),r=e.getEdgeSource(s),d=e.getEdgeTarget(s);if(i[1]>e.find(t)[1])continue;const a=e.getGridCopy();a.removeEdge(s),a._elements.forEach((t,e)=>{const[,o]=t;o<i[1]&&a.removeElement(e)});const c=a.getGraphSegmentFrom(d);if(c.nodes.includes(r))continue;const g=Array.from(a.getSegmentLeftCoordinates(c)),h=g.reduce((t,e)=>void 0===t||e[1]<t[1]?e:t,void 0),l=g.reduce((t,e)=>void 0===t||e[0]<t[0]?e:t,void 0),u=g.reduce((t,e)=>void 0===t||e[0]>t[0]?e:t,void 0),f=n[1]-h[1]+1,p=new Map(g);let E;for(let o=0;o<e.rowCount;o++){const s=(()=>{if(o<l[0])return E=l[0],l[1]-1;if(p.has(o))return E=p.get(o),p.get(o)-1;const s=e.find(t)||[];return o>l[0]&&o<u[0]&&o<s[0]?E:o===s[0]||o>l[0]?(E=s,s[1]):E})();e.expandRow(o,s,f)}}}(n,e),a.unshift(n)});const c=[],g=[];for(const t of a)"bpmn:BoundaryEvent"===t.$type?c.push(t):g.push(t);return c.sort((t,e)=>(t.outgoing?t.outgoing.length:0)-(e.outgoing?e.outgoing.length:0)),a=[...c,...g],a}function m(t,e,o){const s=e.includes(t),n=o.getExistingOutgoingEdgesFor(t);return s&&!n?.length>0}function w(t,e,o){if(e.isFlipped&&"bpmn:BoundaryEvent"===o.$type&&e.hasElement(o.attachedToRef))return e.find(o.attachedToRef);if(o.attachedToRef===t)return e.find(t);const s=e.find(t);if(!s)throw new Error("No source position");const n=[s[0],s[1]+1],i="bpmn:BoundaryEvent"===t.$type&&!e.isFlipped,r=t.laneRef,d=o.laneRef;if(i&&r===d&&(n[0]+=1),r!==d){const[t]=e.find(r),[o,,,s]=e.find(d),i=t-o;n[0]=i>0?o+s-1:o}const a=e.find(t),c=[...e.get(a[0],a[1])],g=[];for(const t of c)[...e.getExistingOutgoingEdgesFor(t)].filter(t=>t.target!==t.source).forEach(t=>g.push(t));for(let t=n[0];t<=e.rowCount;t++){const o=[t,n[1]],s=g.some(s=>e.getTargetPosition(s)[0]===t&&e.getTargetPosition(s)[1]===n[1]||e.isIntersect(s,o,!1)||e.isIntersect(s,o,!0));if(s&&t===e.colCount-1)n[0]=t+1;else if(!s){n[0]=t;break}}return(e.get(n[0],n[1])||e.isCrossed(n,!0))&&e.addRowCol(!0,n[1]-1),e.isCrossed([n[0],n[1]])&&e.addRowCol(!1,n[0]-1),n}function x(t,e,o,s,n){const i=[...[...e.getExistingOutgoingEdgesFor(t)].sort((t,o)=>{const[s,n]=e.getTargetPosition(t),[i,r]=e.getTargetPosition(o);return s-i||n-r}),...[...e.getExistingIncomingEdgesFor(t)].sort((t,o)=>{const[s,n]=e.getSourcePosition(t),[i,r]=e.getSourcePosition(o);return s-i||n-r})].filter(n=>{const{target:i,source:r}=n,d=e.getEdgeDirection(n);return i!==r&&((!s||!s.includes(i))&&((!o||!m(i,o,e))&&(r!==t||"SE_NW"!==d&&"S_N"!==d)))});for(const t of i)y(t,e),"E_W"===e.getEdgeDirection(t)&&"bpmn:BoundaryEvent"===e.getEdgeSource(t).$type&&t.id||_(t,e)}function y(t,e){const o=e.getEdgeDirection(t);if("W_E"===o||"E_W"===o)return;const s=e.getCrossedElementsFor(t,!0).map(t=>[...t]).flat().filter(t=>"bpmn:Lane"!==t.$type);s.length<=0||("S_N"!==o?"SW_NE"!==o&&"NW_SE"!==o?"N_S"!==o?"NE_SW"!==o&&"SE_NW"!==o||F(s,e):C(s,e):F(s,e):C(s,e))}function _(t,e){const o=e.getEdgeDirection(t);if("S_N"===o||"N_S"===o)return;const s=e.getCrossedElementsFor(t,!1).map(t=>[...t]).flat().filter(t=>"bpmn:Lane"!==t.$type);if(0!==s.length){if("SW_NE"===o){const o=function(t,e){const o=e.getSourcePosition(t),s=e.getTargetPosition(t);let n=o[0];for(let t=o[0];t<e.rowCount&&e.getElementsInRange({row:t,col:o[1]+1},{row:t,col:s[1]}).length>0;t++)n++;return n-o[0]}(t,e),[s]=e.getSourcePosition(t);e.addRowCol(!1,s-1,o);const n=e.getElementsInRange({row:e.getSourcePosition(t)[0],col:e.getSourcePosition(t)[1]+1},{row:e.rowCount-1,col:e.colCount-1}).filter(t=>"bpmn:Lane"!==t.$type);for(const t of n){const[s,n]=e.find(t);for(const t of[...e.get(s,n)])e.move(t,[s-o,n])}return}"W_E"!==o?"NW_SE"!==o&&"NE_SW"!==o?"E_W"!==o?"SE_NW"!==o||S(s,e):b(s,e):S(s,e):b(s,e)}}function S(t,e){const[o]=e.find(t[0]);e.addRowCol(!1,o-1);for(const s of t){const[,t]=e.find(s);e.move(s,[o,t])}}function b(t,e){const[o]=e.find(t[0]);e.addRowCol(!1,o);for(const s of t){const[,t]=e.find(s);e.move(s,[o+1,t])}}function C(t,e){const[,o]=e.find(t[0]);e.addRowCol(!0,o);for(const s of t){const[t]=e.find(s);e.move(s,[t,o+1])}}function F(t,e){const[,o]=e.find(t[0]);e.addRowCol(!0,o-1);for(const s of t){const[t]=e.find(s);e.move(s,[t,o])}}function v(t,e,o,s){const{id:n}=t;if(n){const r=function(t,e,o){const{source:s,target:n}=t,r=s.di,l=n.di,u=r.get("bounds"),f=l.get("bounds"),p=c(u),E=c(f),[m,w,x=1]=e.find(s),[y,_]=e.find(n),S=e.getEdgeDirection(t),b=_-w,C=y-m,F=`${C>0?"bottom":"top"}-${b>0?"right":"left"}`,v=`${C>0?"top":"bottom"}-${b>0?"left":"right"}`,{x:N,y:R}=h(s,e,o),{x:T,y:P}=h(n,e,o),D="bpmn:BoundaryEvent"===s.$type;if("NO_DIRECTION"===S)return D?[g(p,u,"b",F),{x:p.x,y:s.attachedToRef.isExpanded?R+(s.attachedToRef.grid.rowCount+1)*d:R+d},{x:T,y:s.attachedToRef.isExpanded?R+(s.attachedToRef.grid.rowCount+1)*d:R+d},{x:N,y:E.y},g(E,f,"l",v)]:[g(p,u,"b",F),{x:p.x,y:R+x*d},{x:T,y:R+x*d},{x:N,y:E.y},g(E,f,"l",v)];if("S_N"===S)return D?[g(p,u,"b",F),{x:p.x,y:s.attachedToRef.isExpanded?R+(s.attachedToRef.grid.rowCount+1)*d:R+d},{x:N,y:s.attachedToRef.isExpanded?R+(s.attachedToRef.grid.rowCount+1)*d:R+d},{x:T,y:E.y},g(E,f,"l",v)]:[...a(n)].includes(s)?[g(p,u,"l",F),{x:N,y:p.y},{x:T,y:E.y},g(E,f,"l",v)]:[g(p,u,"t",F),g(E,f,"b",v)];if("SW_NE"===S)return D?[g(p,u,"b",F),{x:p.x,y:s.attachedToRef.isExpanded?R+(s.attachedToRef.grid.rowCount+1)*d:R+d},{x:E.x,y:s.attachedToRef.isExpanded?R+(s.attachedToRef.grid.rowCount+1)*d:R+d},g(E,f,"b",v)]:[g(p,u,"r"),{x:E.x,y:p.y},g(E,f,"b")];if("W_E"===S){if(D)return[g(p,u,"b",F),{x:p.x,y:s.attachedToRef.isExpanded?R+(s.attachedToRef.grid.rowCount+1)*d:R+d},{x:E.x,y:s.attachedToRef.isExpanded?R+(s.attachedToRef.grid.rowCount+1)*d:R+d},g(E,f,"b",v)];const t=g(p,u,"r",F);s.isExpanded&&(t.y=R+70);const e=g(E,f,"l",v);return n.isExpanded&&(e.y=P+70),[t,e]}if("NW_SE"===S)return[g(p,u,"b"),{x:p.x,y:E.y},g(E,f,"l")];if("N_S"===S){if(D){const t=g(p,u,"b",F),e=g(E,f,"t",v);return s.attachedToRef.isExpanded?[t,{x:p.x,y:s.attachedToRef.isExpanded?R+(s.attachedToRef.grid.rowCount+1)*d:R+d},{x:E.x,y:s.attachedToRef.isExpanded?R+(s.attachedToRef.grid.rowCount+1)*d:R+d},e]:[t,e]}const t=g(p,u,"b",F),e=g(E,f,"t",v);return s.isExpanded||n.isExpanded?[t,{x:p.x,y:s.isExpanded?R+(s.grid.rowCount+1)*d:R+d},{x:E.x,y:s.isExpanded?R+(s.grid.rowCount+1)*d:R+d},e]:[t,e]}if("NE_SW"===S)return[g(p,u,"b"),{x:p.x,y:E.y},g(E,f,"r")];if("E_W"===S){const t=function(t,e,o){const[s,n]=o.find(t),[,i]=o.find(e),r=n<i?n:i,d=n<i?i:n;return[...o.rows[s]].filter(t=>{const[,e]=o.find(t);return e>=r&&e<=d}).reduce((t,e)=>{const[,,,s]=o.find(e);return s>t?s:t},0)}(s,n,e),o=i(n).includes(s);let r=e?e.getExistingOutgoingEdgesFor(n):[];if(r=r.some(t=>"SW_NE"===e.getEdgeDirection(t)),D||o||r)return[g(p,u,"b",F),{x:p.x,y:R+d+(t?t-1:0)*d},{x:E.x,y:R+d+(t?t-1:0)*d},g(E,f,"b",v)];{const t=g(p,u,"l",F);s.isExpanded&&(t.y=R+70);const e=g(E,f,"r",v);return n.isExpanded&&(e.y=P+70),[t,e]}}if("SE_NW"===S){if(D)return[g(p,u,"b",F),{x:p.x,y:s.attachedToRef.isExpanded?R+(s.attachedToRef.grid.rowCount+1)*d:R+d},{x:E.x,y:s.attachedToRef.isExpanded?R+(s.attachedToRef.grid.rowCount+1)*d:R+d},g(E,f,"b",v)];const t=[];for(let o=w-1;o>=_;o--){const s=e?e.get(m,o):null;s&&t.push(s)}return t.length>0?[g(p,u,"b",F),{x:p.x,y:s.isExpanded?R+(s.grid.rowCount+1)*d:R+d},{x:E.x,y:s.isExpanded?R+(s.grid.rowCount+1)*d:R+d},g(E,f,"b",v)]:[g(p,u,"l"),{x:E.x,y:p.y},g(E,f,"b")]}const I=function(t,e,o){const[s,n]=o.find(t),[i,r]=o.find(e),d=i-s;if(r-n>0&&0!==d){if(d>0){let t=0;const e={row:i,col:n};return t+=o.getElementsInRange({row:s,col:n},e).length,t+=o.getElementsInRange(e,{row:i,col:r}).length,!(t>2)&&["v","h"]}{let t=0;const e={row:s,col:r};return t+=o.getElementsInRange({row:s,col:n},e).length,t+=o.getElementsInRange(e,{row:i,col:r}).length,!(t>2)&&["h","v"]}}}(s,n,e);if(I){const t=g(p,u,I[0],F),e=g(E,f,I[1],v);return[t,"h"===I[0]?{x:e.x,y:t.y}:{x:t.x,y:e.y},e]}const W=-Math.sign(C)*d/2;return[g(p,u,"r",F),{x:p.x+75,y:p.y},{x:p.x+75,y:E.y+W},{x:E.x-75,y:E.y+W},{x:E.x-75,y:E.y},g(E,f,"l",v)]}(t,e,s);return o.createDiEdge(t,r,{id:n+"_di"})}}function N(t,e,o,s,i){if(t.di)return[];if("bpmn:BoundaryEvent"===t.$type)return function(t,e,o,s,n){const i=t.attachedToRef,r=s.find(i),d=l(i,r,n);if(!d)throw new Error(`Create DI for ${t.id}. Nо hostBounds`);const a=[];let c=t.$parent.flowElements.filter(e=>e.attachedToRef===t.attachedToRef&&s.hasElement(t));return c=function(t,e){return t.sort((t,o)=>{const s=R(t,e),n=R(o,e);return s[0]-n[0]||s[1]-n[1]}).reverse()}(c,s),c.forEach((s,i,r)=>{const c=l(s,e,n,t.attachedToRef);c.x=d.x+(i+1)*(d.width/(r.length+1))-c.width/2,c.y=d.y+d.height-c.height/2;const g=o.createDiShape(s,c,{id:s.id+"_di"});s.di=g,a.push(g)}),a}(t,e,o,s,i);"bpmn:Lane"===t.$type&&(e[2]=s.colCount);const r=l(t,e,i),d={id:t.id+"_di"};t.isExpanded&&(d.isExpanded=!0),n(t,"bpmn:ExclusiveGateway")&&(d.isMarkerVisible=!0);const a=o.createDiShape(t,r,d);return t.di=a,[a]}function R(t,e){return i(t).reduce((t,o)=>{if(!e.hasElement(o))return t;const s=e.find(o);return t[0]<s[0]||t[1]<s[1]?s:t},[0,0])}class T{constructor(t,e){this.source=t,this.target=e}}class P{constructor(e){this.moddle=new t,this.diFactory=new u(this.moddle),this.maxDebugStep=e,this.currentDebugStep=0}async layoutProcess(t){const e=await this.moddle.fromXML(t),{rootElement:o}=e;this.diagram=o,function(t){const e=t.elementsById;if(e)for(const t of Object.values(e))if("bpmndi:BPMNShape"===t.$type&&!0===t.isExpanded&&(t.bpmnElement.isExpanded=!0),"bpmn:Participant"===t.$type){const e=t.processRef.laneSets||[],o=e[0]?.lanes;o&&o.forEach((t,e)=>{const o=t.flowNodeRef;o&&o.forEach(e=>e.laneRef=t)})}}(e),this.processTrees=this.createNestedSets(e),this.createGridsForProcesses(),this.expandProcessesGrids(),this.expandParticipants();const s=this.getRootProcesses(),n=this.getCollaboration();return s.length>0&&(this.cleanDi(),this.createRootDi(s,n),this.drawParticipants(),this.drawProcesses(),this.drawCollaborationMessageFlows(n)),(await this.moddle.toXML(this.diagram,{format:!0})).xml}expandParticipants(){const t=this.processTrees.map(t=>[...t.nodes]).flat();for(const e of t){const t=(e.laneSets||[])[0]?.lanes;for(const o of t||[]){const t=e.grid.find(o);e.grid.addRowCol(!1,t[0]+t[3]-1,1),t[3]+=1}}}createGridsForProcesses(){const t=this.processTrees.map(t=>[...t.nodes]).flat();t.sort((t,e)=>t.level-e.level);for(const e of t){const t=(e.laneSets||[])[0]?.lanes.length>0;e.grid=this.createGridLayout(e);const o=t?[e.grid]:e.grid._separateGrid()||[e.grid];for(const t of o)t.shrink(!0),t.shrink(!1),t.shakeIt(!0),t.shakeIt(!1);t||(e.grid=e.grid._mergeGrids(o))}}expandProcessesGrids(){const t=this.processTrees.map(t=>[...t.nodes]).flat();t.sort((t,e)=>e.level-t.level);for(const e of t){const t=(e.laneSets||[])[0]?.lanes.length>0,o=t?[e.grid]:e.grid._separateGrid()||[e.grid];for(const t of o)t.shrink(!0),t.shrink(!1),D(t,!1),D(t,!0);t||(e.grid=e.grid._mergeGrids(o))}}drawParticipants(){const t=this.getCollaboration();if(!t||!t[0])return;const e=t[0].participants;let o=0;for(const t of e)o=this.createParticipantDi(t,{x:0,y:o})+70}drawProcesses(){const t=this.processTrees.map(t=>[...t.nodes]).flat();t.sort((t,e)=>t.level-e.level);for(const e of t){const t=this.getParticipantForProcess(e);if(t){const o=this.getElementDi(t),s=this.getProcDi(o);let{x:n,y:i}=o.bounds;n+=75,i+=70,this.generateDi(e,{x:n,y:i},s);continue}if(e.isExpanded&&!this.existingNodes.includes(e))continue;if(e.isExpanded){const t=this.getElementDi(e),o=this.getProcDi(t);let{x:n,y:i}=t.bounds;const{width:r,height:a}=s(e);n+=75-r/4,i+=d-a-a/4,this.generateDi(e,{x:n,y:i},o);continue}const o=this.diagram.diagrams.find(t=>t.plane.bpmnElement===e);this.generateDi(e,{x:0,y:0},o)}}get existingNodes(){return this.processTrees.map(t=>[...t.nodes]).flat().map(t=>[...t.grid.elements]).flat()}drawCollaborationMessageFlows(t){const e=t[0]?t[0].messageFlows:null;if(e)for(const t of e){const{sourceRef:e,targetRef:o}=t,s=this.existingNodes;if(!s.includes(e)||!s.includes(o))continue;const n=e.di.bounds,i=o.di.bounds,r=i.y-n.y,d=[{x:n.x+n.width/2},{x:i.x+i.width/2}];r>0?(d[0].y=n.y+n.height,d[1].y=i.y):(d[0].y=n.y,d[1].y=i.y+i.height);const a=this.diFactory.createDiEdge(t,d);this.diagram.diagrams[0].plane.get("planeElement").push(a)}}getParticipantForProcess(t){const e=this.getCollaboration();if(!e||!e[0])return;const o=this.getCollaboration()[0].participants;return o?o.find(e=>e.processRef===t):void 0}getElementDi(t){return this.diagram.diagrams.map(t=>t.plane.planeElement).flat().find(e=>e.bpmnElement===t)}getProcDi(t){return this.diagram.diagrams.find(e=>e.plane.planeElement.includes(t))}createNestedSets(t){const o=new e.Graph,s=function(t){const e=t.elementsById;return e?Object.values(e).filter(t=>"bpmn:Process"===t.$type||"bpmn:SubProcess"===t.$type):[]}(t);for(const t of s)o.addNode(t);for(const t of s){const e=this.getSubProcesses(t);for(const s of e)o.addEdge({source:t,target:s})}const n=o.getSeparatedGraphs();for(const t of n){const e=t.nodes.filter(e=>0===t.getIncomingEdgesFor(e).size);if(e.length>1)throw new Error("Process tree has more than 1 root elements");if(0===e.length)throw new Error("Process tree has more than 0 root elements");t.rootProcess=e[0]}for(const t of n){const e=(t,e)=>{const o=e.rootProcess;return o.left=0,o.level=0,o},o=(t,e)=>{const{left:o,level:s}=t,n=[...e.getOutgoingEdgesFor(t)].map(t=>t.target).find(t=>void 0===t.left);if(n)return n.level=s+1,n.left=o+1,[n];const i=[...e.getOutgoingEdgesFor(t)].map(t=>t.target).reduce((t,e)=>void 0===t||e.right>t.right?e.right:t,void 0);t.right=i?i+1:t.left+1;const r=[...e.getIncomingEdgesFor(t)].map(t=>t.source).find(t=>void 0===t.right);return r?[r]:void 0};t.genericTraversing(o,e)}return n}getSubProcesses(t){return t.flowElements?t.flowElements.filter(t=>"bpmn:SubProcess"===t.$type):[]}createRootDi(t,e){const o=e&&e.length>0?e[0]:t[0];this.createProcessDi(o)}createProcessDi(t){const e=this.diFactory,o=e.createDiPlane({id:"BPMNPlane_"+t.id,bpmnElement:t}),s=e.createDiDiagram({id:"BPMNDiagram_"+t.id,plane:o});return this.diagram.diagrams.push(s),s}createParticipantDi(t,e){const{colCount:o,rowCount:n}=t.processRef.grid,{width:i,height:a}=s(t),c=o>0?o*r+r:i;let g=a;g=t.processRef.grid.getLanes().length>0?n*d:n>0?n*d+d:a;const h=this.diFactory.createDiShape(t,{width:c,height:g,...e},{id:t.id+"_di"});return this.diagram.diagrams[0].plane.get("planeElement").push(h),h.bounds.y+h.bounds.height}cleanDi(){this.diagram.diagrams=[]}createGridLayout(t){const o=new e.Graph,s=new p(o),i=t.laneSets?t.laneSets[0].lanes:[];for(const t of i)s.add(t),s.find(t)[3]=1;for(const e of t.flowElements||[])n(e,"bpmn:SequenceFlow")||n(e,"bpmn:DataObject")||o.addNode(e);for(const t of o.nodes){"bpmn:BoundaryEvent"===t.$type&&(o.addEdge(new T(t,t.attachedToRef)),o.addEdge(new T(t.attachedToRef,t)));for(const e of t.outgoing||[]){const t=new T(e.sourceRef,e.targetRef);t.id=e.id,o.addEdge(t)}const e=t.dataInputAssociations;for(const t of e||[])for(const e of t.sourceRef||[]){const s=new T(e,t.$parent);s.id=t.id,s.propRef=t.targetRef,o.addEdge(s)}const s=t.dataOutputAssociations;for(const t of s||[]){const e=t.$parent,s=t.targetRef,n=new T(e,s);n.id=t.id,o.addEdge(n)}}return o.genericTraversing((t,e,o,n)=>{if(void 0!==this.maxDebugStep&&this.maxDebugStep<=this.currentDebugStep)return;const i=E(t,s,n,o);return this.currentDebugStep+=1,i},(t,e)=>{if(void 0!==this.maxDebugStep&&this.maxDebugStep<=this.currentDebugStep)return;const o=[...t].find(o=>(s.isFlipped?[...e.getOutgoingEdgesFor(o)]:[...e.getIncomingEdgesFor(o)]).filter(e=>!t.has(s.isFlipped?e.target:e.source)).length>0);if(o)return s.flip(!1),o;const i=e.nodes.filter(o=>{const i=s.isFlipped?e.getOutgoingEdgesFor(o):e.getIncomingEdgesFor(o);return!(t.has(o)||0!==i.size||(r=o,(n(r,"bpmn:IntermediateThrowEvent")||n(r,"bpmn:IntermediateCatchEvent"))&&(void 0===r.incoming||0===r.incoming.length)));var r});if(i.length>0)return function(t,e){const o=[e].flat();let s=[];return o.length>0?o.forEach((e,i)=>{const r=t.filter(t=>n(t,e));if(s=s.concat(r),i===o.length-1&&s.length!==t.length)for(const e of t)s.includes(e)||s.push(e)}):s=t,s}(i,"bpmn:StartEvent")[0];const r=[...t].find(o=>(s.isFlipped?[...e.getIncomingEdgesFor(o)]:[...e.getOutgoingEdgesFor(o)]).filter(e=>!t.has(s.isFlipped?e.source:e.target)).length>0);if(r)return r;const d=e.nodes.find(o=>{if(t.has(o))return!1;return 0===(s.isFlipped?[...e.getOutgoingEdgesFor(o)]:[...e.getIncomingEdgesFor(o)]).filter(t=>o!==(s.isFlipped?t.target:t.source)).length});if(d)return d;const a=e.nodes.find(o=>{if(t.has(o))return!1;return 0===(s.isFlipped?[...e.getIncomingEdgesFor(o)]:[...e.getOutgoingEdgesFor(o)]).filter(t=>t.target!==t.source).length});return a?(s.flip(!1),a):(this.currentDebugStep+=1,e.nodes.find(e=>!t.has(e)))}),s.isFlipped&&s.flip(!1),s}generateDi(t,e,o){const{grid:s}=t,n=this.diFactory,i=(o||this.diagram.diagrams[0]).plane.get("planeElement"),r=[];s._elements.forEach((t,o)=>{const i=N(o,t,n,s,e);r.push(...i)}),s._allEdges.forEach(t=>{const o=v(t,s,n,e);o&&r.push(o)}),this.sortDIsByDefinitionPosition(r,t).forEach(t=>i.push(t))}sortDIsByDefinitionPosition(t,e){return t.sort((t,o)=>{const s=e.flowElements?.findIndex(e=>e.id===t.bpmnElement.id),n=e.flowElements?.findIndex(t=>t.id===o.bpmnElement.id);return s-n})}getRootProcesses(){return this.diagram.get("rootElements").filter(t=>"bpmn:Process"===t.$type)}getCollaboration(){return this.diagram.get("rootElements").filter(t=>"bpmn:Collaboration"===t.$type)}}function D(t,e){const o=new Map,s=new Set;Object.entries(e?t.rows:t.cols).forEach(([t,n])=>{n.forEach(n=>{if(n.isExpanded){s.add(n);const i=o.get(Number.parseInt(t)),[r,d]=n.grid.getGridDimensions();let a=e?r:d;a||(a=1),(void 0===i||a>i)&&o.set(Number.parseInt(t),a)}})}),[...o.entries()].sort(([t],[e])=>e-t).forEach(([o,s])=>{if(e){const e=t.getLanes();for(const n of e){const e=t.find(n);Number.parseInt(o)<=e[0]+e[3]-1&&Number.parseInt(o)>=e[0]&&(e[3]+=Number.parseInt(s))}}t.addRowCol(!e,o,s)}),s.forEach(o=>{const s=t.find(o),[n,i]=o.grid.getGridDimensions();e?s[3]=n<1?2:n+1:s[2]=i<1?2:i+1})}exports.layoutProcess=function(t,e){return new P(e).layoutProcess(t)};//# sourceMappingURL=index.cjs.map
1
+ "use strict";var t=require("bpmn-moddle"),e=require("graph-by-ivan-tulaev"),o=require("min-dash");function s(t){return n(t,"bpmn:SubProcess")||n(t,"bpmn:Task")?{width:100,height:80}:n(t,"bpmn:Gateway")?{width:50,height:50}:n(t,"bpmn:Event")?{width:36,height:36}:n(t,"bpmn:Participant")||n(t,"bpmn:Lane")?{width:400,height:100}:n(t,"bpmn:DataObjectReference")?{width:36,height:50}:n(t,"bpmn:DataStoreReference")?{width:50,height:50}:n(t,"bpmn:TextAnnotation")?{width:100,height:30}:{width:100,height:80}}function n(t,e){return t.$instanceOf(e)}function i(t){if(!t)throw new Error("Element is not defined in getOutgoingElements");const e=(t.outgoing||[]).map(t=>t.targetRef);return[...new Set(e)]}const r=150,d=140;function a(t,e){return new Set(i(t).concat(function(t){const e=new Set;if(t){const o=(t.attachers||[]).sort((t,e)=>(e.outgoing?e.outgoing.length:0)-(t.outgoing?t.outgoing.length:0)).map(t=>(t.outgoing||[]).reverse()).flat().map(t=>t.targetRef).filter((t,e,o)=>o.indexOf(t)===e);for(const t of o)e.add(t)}return[...e]}(t)))}function c(t){return{x:t.x+t.width/2,y:t.y+t.height/2}}function g(t,e,o="r",s="top-left"){if("h"===o&&(o=/left/.test(s)?"l":"r"),"v"===o&&(o=/top/.test(s)?"t":"b"),"t"===o)return{original:t,x:t.x,y:e.y};if("r"===o)return{original:t,x:e.x+e.width,y:t.y};if("b"===o)return{original:t,x:t.x,y:e.y+e.height};if("l"===o)return{original:t,x:e.x,y:t.y};throw new Error("unexpected dockingDirection: <"+o+">")}function h(t,e,o={x:0,y:0}){const[s,n]=e.find(t);return{x:n*r+o.x,y:s*d+o.y}}function l(t,e,o,n){const[i,a,c,g]=e,{width:h,height:l}=s(t);let u=((c||1)-1)*r+h,f=((g||1)-1)*d+l,p=a*r+(r-h)/2+o.x,E=i*d+(d-l)/2+o.y;if("bpmn:Lane"===t.$type){const t=30;u=(c||1)*r+r+(n-1)*t,p=a*r+o.x-75+t,E=i*d+o.y-70,f=(g||1)*d}return{width:u,height:f,x:p,y:E}}class u{constructor(t){this.moddle=t}create(t,e){return this.moddle.create(t,e||{})}createDiBounds(t){return this.create("dc:Bounds",t)}createDiLabel(){return this.create("bpmndi:BPMNLabel",{bounds:this.createDiBounds()})}createDiShape(t,e,s){return this.create("bpmndi:BPMNShape",o.assign({bpmnElement:t,bounds:this.createDiBounds(e)},s))}createDiWaypoints(t){var e=this;return o.map(t,function(t){return e.createDiWaypoint(t)})}createDiWaypoint(t){return this.create("dc:Point",o.pick(t,["x","y"]))}createDiEdge(t,e,s){return this.create("bpmndi:BPMNEdge",o.assign({bpmnElement:t,waypoint:this.createDiWaypoints(e)},s))}createDiPlane(t){return this.create("bpmndi:BPMNPlane",t)}createDiDiagram(t){return this.create("bpmndi:BPMNDiagram",t)}}class f{constructor(){this.isFlipped=!1,this._elements=new Map,this.rows={},this.cols={}}get rowCount(){return Object.keys(this.rows).length}get colCount(){return Object.keys(this.cols).length}get elementsCount(){return this._elements.size}get elements(){return new Set(this._elements.keys())}add(t,e){if(this._elements.has(t))throw new Error(`Cannot add duplicated element ${JSON.stringify(t)}`);if(!e)return void this._addStart(t);const o=this.rowCount-1,s=this.colCount-1,n=e[0]-o,i=e[1]-s;this.addRowCol(!1,o>=0?o:void 0,n),this.addRowCol(!0,s>=0?s:void 0,i),this._elements.set(t,e),this._addElementToRowsCols(t,e)}_addElementToRowsCols(t){const e=this._elements.get(t);this.rows[e[0]]?this.rows[e[0]].add(t):this.rows[e[0]]=new Set([t]),this.cols[e[1]]?this.cols[e[1]].add(t):this.cols[e[1]]=new Set([t])}_removeElementFromRowsCols(t){const e=this._elements.get(t);this.rows[e[0]]&&this.rows[e[0]].delete(t),this.cols[e[1]]&&this.cols[e[1]].delete(t)}_addStart(t){const[e]=this.getGridDimensions();this._elements.set(t,[e,0]),this._addElementToRowsCols(t,[e,0])}move(t,e){if(!this.elements.has(t))throw new Error(`Cannot move not exist element ${JSON.stringify(t)}`);if(!this.isValidPosition(e))throw new Error(`Cannot move element ${JSON.stringify(t)} to invalid position ${e}`);this._removeElementFromRowsCols(t),this._elements.set(t,e),this._addElementToRowsCols(t,e)}removeElement(t){this._elements.has(t)&&(this._removeElementFromRowsCols(t),this._elements.delete(t))}addRowCol(t,e,o=1){const s=t?this.colCount:this.rowCount,n=e>=s-1?e-(s-1)+o:o;for(let e=0;e<n;e++)t?this.cols[s+e]=new Set:this.rows[s+e]=new Set;for(const[s,n]of this._elements.entries()){((t?n[1]:n[0])>e||void 0===e)&&(this._removeElementFromRowsCols(s),n[t?1:0]+=o,this._addElementToRowsCols(s))}}expandRow(t,e,o=1){if(!Number.isInteger(t)||t<0||t>this.rowCount-1)throw new Error(`Can't expand row with index: ${t}. Grid row count is ${this.rowCount}`);const s=this.colCount,n=e>=s-1?e-(s-1)+o:o;for(let t=0;t<n;t++)this.cols[s+t]=new Set;[...this.rows[t]].forEach(t=>{const s=this.find(t);(s[1]>e||void 0===e)&&(this._removeElementFromRowsCols(t),s[1]+=o,this._addElementToRowsCols(t))})}find(t){return this._elements.get(t)}get(t,e){const o=new Set([...this._elements.entries()].filter(([o,s])=>s[0]===t&&s[1]===e).map(t=>t[0]));return o.size?o:null}getElementsInRange({row:t,col:e},{row:o,col:s}){return t>o&&([t,o]=[o,t]),e>s&&([e,s]=[s,e]),[...this._elements.entries()].filter(([,n])=>n[0]>=t&&n[0]<=o&&n[1]>=e&&n[1]<=s).map(t=>t[0])}getGridDimensions(){return[this.rowCount,this.colCount]}shrink(t){const e=[...this._elements.entries()].sort((e,o)=>(t?e[1][0]:e[1][1])-(t?o[1][0]:o[1][1]));let o=0,s=null;for(const n of e){const e=t?n[1][0]:n[1][1];this._removeElementFromRowsCols(n[0]),null===s?o=e:s!==e&&(o=o+e-s-1),s=e;const i=[...n[1]];t?i[0]-=o:i[1]-=o,this._elements.set(n[0],i),this._addElementToRowsCols(n[0])}for(const[e,o]of Object.entries(t?this.rows:this.cols))0===o.size&&(t?delete this.rows[e]:delete this.cols[e])}flip(t){const[e,o]=this.getGridDimensions();for(const[s,n]of this._elements.entries())this._removeElementFromRowsCols(s,n),this._elements.set(s,t?[e-1-n[0],n[1]]:[n[0],o-1-n[1]]),this._addElementToRowsCols(s,t?[e-1-n[0],n[1]]:[n[0],o-1-n[1]]);this.isFlipped=!this.isFlipped}hasElement(t){return this.elements.has(t)}isValidPosition(t){if(!t||!Array.isArray(t))return!1;const[e,o]=t;return Number.isInteger(e)&&Number.isInteger(o)&&e>=0&&o>=0}hasIntermediateElements(t,e,o){if(!this.isValidPosition(t)||!this.isValidPosition(e))return!1;if(o?t[1]!==e[1]:t[0]!==e[0])return!1;const s=o?t[0]:t[1],[n,i]=o?t[0]<=e[0]?[t[0],e[0]]:[e[0],t[0]]:t[1]<=e[1]?[t[1],e[1]]:[e[1],t[1]];return[...this._elements.values()].some(t=>o?t[1]===s&&t[0]>n&&t[0]<i:t[0]===s&&t[1]>n&&t[1]<i)}hasElementAt(t){if(!this.isValidPosition(t))return!1;const[e,o]=t;return!!this.get(e,o)}}class p extends f{constructor(t){super(),this.initialGraph=t,this.graph=new e.Graph}getLanes(){return[...this.elements].filter(t=>"bpmn:Lane"===t.$type)}add(t,e){if("bpmn:Lane"===t.$type)return void super.add(t,e);if(0===this.getLanes().length)return super.add(t,e),this.graph.addNode(t),void this._createNewEdgesFor(t);const o=this.find(t.laneRef);if(!e){const e=o[0],s=o[0]+o[3]-1;for(let o=e;o<=s;o++){const e=new Set(this.rows[o]);if(e.delete(t.laneRef),0===e.size)return super.add(t,[o,0]),this.graph.addNode(t),void this._createNewEdgesFor(t)}return this.addRowCol(!1,o[0]+o[3]-1),super.add(t,[o[0]+o[3],0]),o[3]+=1,this.graph.addNode(t),void this._createNewEdgesFor(t)}const s=e[0]+(e[3]||1)-(o[0]+(o[3]||1));s>0&&(this.addRowCol(!1,o[0]+(o[3]||1)-1,s),o[3]+=s),super.add(t,e),this.graph.addNode(t),this._createNewEdgesFor(t)}removeElement(t){this.graph.deleteNode(t),super.removeElement(t)}_edgeIsExist(t){return[...this.graph.edges].includes(t)}_addEdgeToGrid(t){this._edgeIsExist(t)||this.graph.addEdge(t)}_createNewEdgesFor(t){const e=[...this.initialGraph.getOutgoingEdgesFor(t),...this.initialGraph.getIncomingEdgesFor(t)].filter(t=>this.hasElement(t.source)&&this.hasElement(t.target));for(const t of e)this._addEdgeToGrid(t)}isCrossed(t,e=!1){return[...this._allEdges].some(o=>this.isIntersect(o,t,e))}get _allEdges(){return this.graph.edges}hasAnyCross(t){return[...t||this._allEdges].some(t=>this.getCrossedElementsFor(t).length>0||this.getCrossedElementsFor(t,!0).length>0)}shakeIt(t){const e=[...this.elements].filter(t=>"bpmn:Lane"!==t.$type).sort(t?(o=this,function(t,e){const s=o.find(t),n=o.find(e);return s[0]-n[0]||n[1]-s[1]}):function(t){return function(e,o){const s=t.find(e),n=t.find(o);return s[1]-n[1]||n[0]-s[0]}}(this)).reverse();for(var o;e.length>0;){const o=e.pop(),s=this.getChain(o,!t);for(const t of s){const o=e.indexOf(t);o>=0&&e.splice(o,1)}const[n,i]=this.find([...s][0]);if(!(t?n<=0:i<=0)){for(let e=t?n-1:i-1;e>=0;e--){const n=[...s].every(t=>{const e=this.find(t);return e&&!this.isCrossed(e,!0)&&!this.isCrossed(e,!1)});if(!n)break;const i=[...s].every(o=>{const s=this.find(o),n=t?e:s[0],i=t?s[1]:e;return!this.get(n,i)});if(!i)break;for(const o of s){const s=this.find(o);this.move(o,t?[e,s[1]]:[s[0],e])}const r=this.hasAnyCross(),d=this.getChain(o,!t);if(r||d.size>s.size){for(const o of s){const s=this.find(o);this.move(o,t?[e+1,s[1]]:[s[0],e+1])}break}}this.shrink(t)}}}getChain(t,e,o){const s=o||new Set;if(!t)return s;const n=this.find(t);if(!n)return s;const i=[],r=[...this.get(n[0],n[1])].filter(t=>"bpmn:Lane"!==t.$type);for(const t of r)s.add(t),[...this.getAllExistingEdgesFor(t)].filter(t=>{const o=this.getEdgeDirection(t);return e?"N_S"===o||"S_N"===o:"W_E"===o||"E_W"===o}).forEach(t=>i.push(t));for(const o of i){const n=o.source===t?o.target:o.source;if(!s.has(n)){const t=this.getChain(n,e,s);for(const e of t)s.add(e)}}return s}getExistingOutgoingEdgesFor(t){return this.isFlipped?[...this.graph.getIncomingEdgesFor(t)]:[...this.graph.getOutgoingEdgesFor(t)]}getExistingIncomingEdgesFor(t){return this.isFlipped?[...this.graph.getOutgoingEdgesFor(t)]:[...this.graph.getIncomingEdgesFor(t)]}getAllExistingEdgesFor(t){const e=this.getExistingOutgoingEdgesFor(t),o=this.getExistingIncomingEdgesFor(t);return new Set([...e,...o])}_separateGrid(){const t=e.Graph.mergeGraphs(new Set([this.graph])).getSeparatedGraphs();if(0===t.length)return[new p(this.initialGraph)];const o=[];for(const e of t){const t=new p(this.initialGraph);let s=null,n=null;for(const o of e.nodes){const e=[...this.find(o)];(null===s||s>e[0])&&(s=e[0]),(null===n||n<e[0])&&(n=e[0]),t.add(o,e)}o.push(t)}return o}_mergeGrids(t){const e=new p(t[0].initialGraph);return t.forEach(t=>{let o=e.rowCount;Object.keys(t.rows).forEach(s=>{0===t.rows[s].size?e.addRowCol(!1,o+Number.parseInt(s)-1):t.rows[s].forEach(s=>{const n=[...t.find(s)];n[0]=n[0]+o,e.add(s,n)})})}),e}getSourcePosition(t){const e=this.getEdgeSource(t);return this.find(e)}getTargetPosition(t){const e=this.getEdgeTarget(t);return this.find(e)}getEdgeDirection(t){const e=this.getSourcePosition(t),o=this.getTargetPosition(t);if(!this.isValidPosition(e)||!this.isValidPosition(o))throw new Error(`Invalid position of source or target in ${t.source.id}-${t.target.id} flipped:${this.isFlipped}`);const[s,n]=e,[i,r]=o,d=s-i,a=n-r;return 0===d&&0===a?"NO_DIRECTION":d>0&&0===a?"S_N":d>0&&a<0?"SW_NE":0===d&&a<0?"W_E":d<0&&a<0?"NW_SE":d<0&&0===a?"N_S":s<i&&n>r?"NE_SW":s===i&&n>r?"E_W":s>i&&n>r?"SE_NW":void 0}getPathFor(t){const e=this.getEdgeDirection(t);return"NO_DIRECTION"===e?this._pathForNoDirection():"S_N"===e?this._pathForSouthToNorth(t):"SW_NE"===e?this._pathForSouthWestToNorthEast(t):"W_E"===e?this._pathForWestToEast(t):"NW_SE"===e?this._pathForNorthWestToSouthEast(t):"N_S"===e?this._pathForNorthToSouth(t):"NE_SW"===e?this._pathForNorthEastToSouthWest(t):"E_W"===e?this._pathForEastToWest(t):"SE_NW"===e?this._pathForSouthEastToNorthWest(t):[]}_pathForNoDirection(){return[]}getEdgeSource(t){return this.isFlipped?t.target:t.source}getEdgeTarget(t){return this.isFlipped?t.source:t.target}_pathForSouthToNorth(t){const e=[],[o,s]=this.getSourcePosition(t),[n]=this.getTargetPosition(t);if("bpmn:BoundaryEvent"===this.getEdgeSource(t).$type&&t.id)return e;if(this.hasIntermediateElements(this.getSourcePosition(t),this.getTargetPosition(t),!0))return e;if([...this.getExistingOutgoingEdgesFor(this.getEdgeTarget(t))].map(t=>this.getEdgeTarget(t)).includes(this.getEdgeSource(t)))return e;for(let t=o-1;t>n;t--)e.push({position:[t,s],vCross:!0});return e}_pathForSouthWestToNorthEast(t){const e=[],[o,s]=this.getSourcePosition(t),[n,i]=this.getTargetPosition(t);if("bpmn:BoundaryEvent"!==this.getEdgeSource(t).$type)for(let t=s+1;t<i;t++)e.push({position:[o,t],hCross:!0});e.push({position:[o,i],hCross:!0,vCross:!0});for(let t=o-1;t>n;t--)e.push({position:[t,i],vCross:!0});return e}_pathForWestToEast(t){const e=[],[o,s]=this.getSourcePosition(t),[,n]=this.getTargetPosition(t);for(let t=s+1;t<n;t++)e.push({position:[o,t],hCross:!0});return e}_pathForNorthWestToSouthEast(t){const e=[],[o,s]=this.getSourcePosition(t),[n,i]=this.getTargetPosition(t);for(let t=o+1;t<n;t++)e.push({position:[t,s],vCross:!0});e.push({position:[n,s],vCross:!0,hCross:!0});for(let t=s+1;t<i;t++)e.push({position:[n,t],hCross:!0});return e}_pathForNorthToSouth(t){const e=[],[o,s]=this.getSourcePosition(t),[n]=this.getTargetPosition(t);for(let t=o+1;t<n;t++)e.push({position:[t,s],vCross:!0});return e}_pathForNorthEastToSouthWest(t){const e=[],[o,s]=this.getSourcePosition(t),[n,i]=this.getTargetPosition(t);for(let t=o+1;t<n;t++)e.push({position:[t,s],vCross:!0});e.push({position:[n,s],vCross:!0,hCross:!0});for(let t=s-1;t>i;t--)e.push({position:[n,t],hCross:!0});return e}_pathForEastToWest(t){const e=[],[o,s]=this.getSourcePosition(t),[,n]=this.getTargetPosition(t);for(let t=s-1;t>n;t--)e.push({position:[o,t],hCross:!0});return e}_pathForSouthEastToNorthWest(t){const e=[],[o,s]=this.getSourcePosition(t),[n,i]=this.getTargetPosition(t);if("bpmn:BoundaryEvent"!==this.getEdgeSource(t).$type||!t.id)for(let t=s-1;t>i;t--)e.push({position:[o,t],hCross:!0});"bpmn:BoundaryEvent"===this.getEdgeSource(t).$type&&t.id?e.push({position:[o,i],vCross:!0}):e.push({position:[o,i],hCross:!0,vCross:!0});for(let t=o-1;t>n;t--)e.push({position:[t,i],vCross:!0});return e}getCrossedElementsFor(t,e=!1){const o=[];for(const s of this.getPathFor(t)){const[t,n]=s.position,i=this.get(t,n);i&&(e&&s.vCross||!e&&s.hCross)&&o.push(i)}return o}isIntersect(t,e,o){const[s,n]=e,[i,r]=this.getSourcePosition(t),[d,a]=this.getTargetPosition(t),c=this.getEdgeDirection(t);return"S_N"===c?o&&n===r&&s<i&&s>d:"SW_NE"===c?!!(o&&n===a&&s<=i&&s>d)||!o&&n>r&&n<=a&&s===i:"W_E"===c?!o&&n>r&&n<a&&s===i:"NW_SE"===c?!!(o&&n===r&&s>i&&s<=d)||!o&&n>=r&&n<a&&s===d:"N_S"===c?o&&n===r&&s>i&&s<d:"NE_SW"===c?!!(o&&n===r&&s>i&&s<=d)||!o&&n>a&&n<=r&&s===d:"E_W"===c?!o&&n>a&&n<r&&s===i:"SE_NW"===c?!!(o&&n===a&&s>d&&s<=i)||!o&&n>=a&&n<r&&s===i&&!this.getExistingIncomingEdgesFor(this.getEdgeSource(t)).some(t=>"NW_SE"===this.getEdgeDirection(t)||"W_E"===this.getEdgeDirection(t)):void 0}getBackwardUpOutgoingEdgesFor(t){return this.getExistingOutgoingEdgesFor(t).filter(t=>{const e=this.getEdgeDirection(t);return"S_N"===e||"SE_NW"===e})}getGridCopy(){return this._mergeGrids([this])}removeEdge(t){this.graph.deleteEdge(t)}getGraphSegmentFrom(t){const o=new e.Graph;return this.graph.genericTraversing((t,e,s,n)=>{const i=this.getExistingOutgoingEdgesFor(t).filter(t=>{const e=this.getEdgeTarget(t),o=this.getEdgeSource(t);return!s.has(e)&&e!==o});o.addNode(t);const r=i.map(t=>this.getEdgeTarget(t));for(const t of r)o.addNode(t);for(const t of i)o.addEdge(t);return r},(e,o)=>e.has(t)?void 0:t),o}getSegmentLeftCoordinates(t){const e=new Map;for(const o of t.nodes){const[t,s]=this.find(o);e.has(t)||e.set(t,s),s<e.get(t)&&e.set(t,s)}return e}}function E(t,e,o,s,n){e.hasElement(t)||e.add(t);const i=(e.isFlipped?[...e.initialGraph.getIncomingEdgesFor(t)]:[...e.initialGraph.getOutgoingEdgesFor(t)]).filter(t=>!e.hasElement(e.isFlipped?t.source:t.target)).map(t=>e.isFlipped?t.source:t.target),r=function(t,e,o,s,n,i){const r=e.getExistingOutgoingEdgesFor(t),d=e.getExistingIncomingEdgesFor(t).map(t=>e.getEdgeSource(t)),[,a]=e.find(t),c=[...r].filter(t=>{const n=e.getEdgeTarget(t),i=e.find(n)[1];if(!m(n,o,e))return!1;return(!e.getExistingIncomingEdgesFor(n).map(t=>t.source).find(t=>d.includes(t))||!s)&&i<=a}).map(t=>e.getEdgeTarget(t)).sort(function(t){return function(e,o){const s=t.find(e),n=t.find(o);return s[0]-n[0]||s[1]-n[1]}}(e));for(const t of c)o.splice(o.indexOf(t),1),i.delete(t),e.removeElement(t);return c}(t,e,o,i&&i.length>0,0,s);let d=[...i,...r],a=[];d.forEach(n=>{const i=[...w(t,e,n)];if(e.isFlipped&&"bpmn:BoundaryEvent"===n.$type&&n.attachedToRef!==t){const t=n.attachedToRef;s.has(t)||(e.add(t,i),s.add(t),x(t,e,o,a),a.unshift(n))}e.add(n,i),s.add(n),x(n,e,o,a),function(t,e){let o=e.getBackwardUpOutgoingEdgesFor(t);for(;o.length>0;){const s=o.shift(),n=e.getSourcePosition(s),i=e.getTargetPosition(s),r=e.getEdgeSource(s),d=e.getEdgeTarget(s);if(i[1]>e.find(t)[1])continue;const a=e.getGridCopy();a.removeEdge(s),a._elements.forEach((t,e)=>{const[,o]=t;o<i[1]&&a.removeElement(e)});const c=a.getGraphSegmentFrom(d);if(c.nodes.includes(r))continue;const g=Array.from(a.getSegmentLeftCoordinates(c)),h=g.reduce((t,e)=>void 0===t||e[1]<t[1]?e:t,void 0),l=g.reduce((t,e)=>void 0===t||e[0]<t[0]?e:t,void 0),u=g.reduce((t,e)=>void 0===t||e[0]>t[0]?e:t,void 0),f=n[1]-h[1]+1,p=new Map(g);let E;for(let o=0;o<e.rowCount;o++){const s=(()=>{if(o<l[0])return E=l[0],l[1]-1;if(p.has(o))return E=p.get(o),p.get(o)-1;const s=e.find(t)||[];return o>l[0]&&o<u[0]&&o<s[0]?E:o===s[0]||o>l[0]?(E=s,s[1]):E})();e.expandRow(o,s,f)}}}(n,e),a.unshift(n)});const c=[],g=[];for(const t of a)"bpmn:BoundaryEvent"===t.$type?c.push(t):g.push(t);return c.sort((t,e)=>(t.outgoing?t.outgoing.length:0)-(e.outgoing?e.outgoing.length:0)),a=[...c,...g],a}function m(t,e,o){const s=e.includes(t),n=o.getExistingOutgoingEdgesFor(t);return s&&!n?.length>0}function w(t,e,o){if(e.isFlipped&&"bpmn:BoundaryEvent"===o.$type&&e.hasElement(o.attachedToRef))return e.find(o.attachedToRef);if(o.attachedToRef===t)return e.find(t);const s=e.find(t);if(!s)throw new Error("No source position");const n=[s[0],s[1]+1],i="bpmn:BoundaryEvent"===t.$type&&!e.isFlipped,r=t.laneRef,d=o.laneRef;if(i&&r===d&&(n[0]+=1),r!==d){const[t]=e.find(r),[o,,,s]=e.find(d),i=t-o;n[0]=i>0?o+s-1:o}const a=e.find(t),c=[...e.get(a[0],a[1])],g=[];for(const t of c)[...e.getExistingOutgoingEdgesFor(t)].filter(t=>t.target!==t.source).forEach(t=>g.push(t));for(let t=n[0];t<=e.rowCount;t++){const o=[t,n[1]],s=g.some(s=>e.getTargetPosition(s)[0]===t&&e.getTargetPosition(s)[1]===n[1]||e.isIntersect(s,o,!1)||e.isIntersect(s,o,!0));if(s&&t===e.colCount-1)n[0]=t+1;else if(!s){n[0]=t;break}}return(e.get(n[0],n[1])||e.isCrossed(n,!0))&&e.addRowCol(!0,n[1]-1),e.isCrossed([n[0],n[1]])&&e.addRowCol(!1,n[0]-1),n}function x(t,e,o,s,n){const i=[...[...e.getExistingOutgoingEdgesFor(t)].sort((t,o)=>{const[s,n]=e.getTargetPosition(t),[i,r]=e.getTargetPosition(o);return s-i||n-r}),...[...e.getExistingIncomingEdgesFor(t)].sort((t,o)=>{const[s,n]=e.getSourcePosition(t),[i,r]=e.getSourcePosition(o);return s-i||n-r})].filter(n=>{const{target:i,source:r}=n,d=e.getEdgeDirection(n);return i!==r&&((!s||!s.includes(i))&&((!o||!m(i,o,e))&&(r!==t||"SE_NW"!==d&&"S_N"!==d)))});for(const t of i)y(t,e),"E_W"===e.getEdgeDirection(t)&&"bpmn:BoundaryEvent"===e.getEdgeSource(t).$type&&t.id||S(t,e)}function y(t,e){const o=e.getEdgeDirection(t);if("W_E"===o||"E_W"===o)return;const s=e.getCrossedElementsFor(t,!0).map(t=>[...t]).flat().filter(t=>"bpmn:Lane"!==t.$type);s.length<=0||("S_N"!==o?"SW_NE"!==o&&"NW_SE"!==o?"N_S"!==o?"NE_SW"!==o&&"SE_NW"!==o||C(s,e):b(s,e):C(s,e):b(s,e))}function S(t,e){const o=e.getEdgeDirection(t);if("S_N"===o||"N_S"===o)return;const s=e.getCrossedElementsFor(t,!1).map(t=>[...t]).flat().filter(t=>"bpmn:Lane"!==t.$type);if(0!==s.length){if("SW_NE"===o){const o=function(t,e){const o=e.getSourcePosition(t),s=e.getTargetPosition(t);let n=o[0];for(let t=o[0];t<e.rowCount&&e.getElementsInRange({row:t,col:o[1]+1},{row:t,col:s[1]}).length>0;t++)n++;return n-o[0]}(t,e),[s]=e.getSourcePosition(t);e.addRowCol(!1,s-1,o);const n=e.getElementsInRange({row:e.getSourcePosition(t)[0],col:e.getSourcePosition(t)[1]+1},{row:e.rowCount-1,col:e.colCount-1}).filter(t=>"bpmn:Lane"!==t.$type);for(const t of n){const[s,n]=e.find(t);for(const t of[...e.get(s,n)])e.move(t,[s-o,n])}return}"W_E"!==o?"NW_SE"!==o&&"NE_SW"!==o?"E_W"!==o?"SE_NW"!==o||v(s,e):_(s,e):v(s,e):_(s,e)}}function v(t,e){const[o]=e.find(t[0]);e.addRowCol(!1,o-1);for(const s of t){const[,t]=e.find(s);e.move(s,[o,t])}}function _(t,e){const[o]=e.find(t[0]);e.addRowCol(!1,o);for(const s of t){const[,t]=e.find(s);e.move(s,[o+1,t])}}function b(t,e){const[,o]=e.find(t[0]);e.addRowCol(!0,o);for(const s of t){const[t]=e.find(s);e.move(s,[t,o+1])}}function C(t,e){const[,o]=e.find(t[0]);e.addRowCol(!0,o-1);for(const s of t){const[t]=e.find(s);e.move(s,[t,o])}}function F(t,e,o,s){const{id:n}=t;if(n){const r=function(t,e,o){const{source:s,target:n}=t,r=s.di,l=n.di,u=r.get("bounds"),f=l.get("bounds"),p=c(u),E=c(f),[m,w,x=1]=e.find(s),[y,S]=e.find(n),v=e.getEdgeDirection(t),_=S-w,b=y-m,C=`${b>0?"bottom":"top"}-${_>0?"right":"left"}`,F=`${b>0?"top":"bottom"}-${_>0?"left":"right"}`,{x:N,y:R}=h(s,e,o),{x:T,y:P}=h(n,e,o),D="bpmn:BoundaryEvent"===s.$type;if("NO_DIRECTION"===v)return D?[g(p,u,"b",C),{x:p.x,y:s.attachedToRef.isExpanded?R+(s.attachedToRef.grid.rowCount+1)*d:R+d},{x:T,y:s.attachedToRef.isExpanded?R+(s.attachedToRef.grid.rowCount+1)*d:R+d},{x:N,y:E.y},g(E,f,"l",F)]:[g(p,u,"b",C),{x:p.x,y:R+x*d},{x:T,y:R+x*d},{x:N,y:E.y},g(E,f,"l",F)];if("S_N"===v)return D?[g(p,u,"b",C),{x:p.x,y:s.attachedToRef.isExpanded?R+(s.attachedToRef.grid.rowCount+1)*d:R+d},{x:N,y:s.attachedToRef.isExpanded?R+(s.attachedToRef.grid.rowCount+1)*d:R+d},{x:T,y:E.y},g(E,f,"l",F)]:[...a(n)].includes(s)?[g(p,u,"l",C),{x:N,y:p.y},{x:T,y:E.y},g(E,f,"l",F)]:[g(p,u,"t",C),g(E,f,"b",F)];if("SW_NE"===v)return D?[g(p,u,"b",C),{x:p.x,y:s.attachedToRef.isExpanded?R+(s.attachedToRef.grid.rowCount+1)*d:R+d},{x:E.x,y:s.attachedToRef.isExpanded?R+(s.attachedToRef.grid.rowCount+1)*d:R+d},g(E,f,"b",F)]:[g(p,u,"r"),{x:E.x,y:p.y},g(E,f,"b")];if("W_E"===v){if(D)return[g(p,u,"b",C),{x:p.x,y:s.attachedToRef.isExpanded?R+(s.attachedToRef.grid.rowCount+1)*d:R+d},{x:E.x,y:s.attachedToRef.isExpanded?R+(s.attachedToRef.grid.rowCount+1)*d:R+d},g(E,f,"b",F)];const t=g(p,u,"r",C);s.isExpanded&&(t.y=R+70);const e=g(E,f,"l",F);return n.isExpanded&&(e.y=P+70),[t,e]}if("NW_SE"===v)return[g(p,u,"b"),{x:p.x,y:E.y},g(E,f,"l")];if("N_S"===v){if(D){const t=g(p,u,"b",C),e=g(E,f,"t",F);return s.attachedToRef.isExpanded?[t,{x:p.x,y:s.attachedToRef.isExpanded?R+(s.attachedToRef.grid.rowCount+1)*d:R+d},{x:E.x,y:s.attachedToRef.isExpanded?R+(s.attachedToRef.grid.rowCount+1)*d:R+d},e]:[t,e]}const t=g(p,u,"b",C),e=g(E,f,"t",F);return s.isExpanded||n.isExpanded?[t,{x:p.x,y:s.isExpanded?R+(s.grid.rowCount+1)*d:R+d},{x:E.x,y:s.isExpanded?R+(s.grid.rowCount+1)*d:R+d},e]:[t,e]}if("NE_SW"===v)return[g(p,u,"b"),{x:p.x,y:E.y},g(E,f,"r")];if("E_W"===v){const t=function(t,e,o){const[s,n]=o.find(t),[,i]=o.find(e),r=n<i?n:i,d=n<i?i:n;return[...o.rows[s]].filter(t=>{const[,e]=o.find(t);return e>=r&&e<=d}).reduce((t,e)=>{const[,,,s]=o.find(e);return s>t?s:t},0)}(s,n,e),o=i(n).includes(s);let r=e?e.getExistingOutgoingEdgesFor(n):[];if(r=r.some(t=>"SW_NE"===e.getEdgeDirection(t)),D||o||r)return[g(p,u,"b",C),{x:p.x,y:R+d+(t?t-1:0)*d},{x:E.x,y:R+d+(t?t-1:0)*d},g(E,f,"b",F)];{const t=g(p,u,"l",C);s.isExpanded&&(t.y=R+70);const e=g(E,f,"r",F);return n.isExpanded&&(e.y=P+70),[t,e]}}if("SE_NW"===v){if(D)return[g(p,u,"b",C),{x:p.x,y:s.attachedToRef.isExpanded?R+(s.attachedToRef.grid.rowCount+1)*d:R+d},{x:E.x,y:s.attachedToRef.isExpanded?R+(s.attachedToRef.grid.rowCount+1)*d:R+d},g(E,f,"b",F)];const t=[];for(let o=w-1;o>=S;o--){const s=e?e.get(m,o):null;s&&t.push(s)}return t.length>0?[g(p,u,"b",C),{x:p.x,y:s.isExpanded?R+(s.grid.rowCount+1)*d:R+d},{x:E.x,y:s.isExpanded?R+(s.grid.rowCount+1)*d:R+d},g(E,f,"b",F)]:[g(p,u,"l"),{x:E.x,y:p.y},g(E,f,"b")]}const I=function(t,e,o){const[s,n]=o.find(t),[i,r]=o.find(e),d=i-s;if(r-n>0&&0!==d){if(d>0){let t=0;const e={row:i,col:n};return t+=o.getElementsInRange({row:s,col:n},e).length,t+=o.getElementsInRange(e,{row:i,col:r}).length,!(t>2)&&["v","h"]}{let t=0;const e={row:s,col:r};return t+=o.getElementsInRange({row:s,col:n},e).length,t+=o.getElementsInRange(e,{row:i,col:r}).length,!(t>2)&&["h","v"]}}}(s,n,e);if(I){const t=g(p,u,I[0],C),e=g(E,f,I[1],F);return[t,"h"===I[0]?{x:e.x,y:t.y}:{x:t.x,y:e.y},e]}const W=-Math.sign(b)*d/2;return[g(p,u,"r",C),{x:p.x+75,y:p.y},{x:p.x+75,y:E.y+W},{x:E.x-75,y:E.y+W},{x:E.x-75,y:E.y},g(E,f,"l",F)]}(t,e,s);return o.createDiEdge(t,r,{id:n+"_di"})}}function N(t,e,o,s,i,r){if(t.di)return[];if("bpmn:BoundaryEvent"===t.$type)return function(t,e,o,s,n){const i=t.attachedToRef,r=s.find(i),d=l(i,r,n);if(!d)throw new Error(`Create DI for ${t.id}. Nо hostBounds`);const a=[];let c=t.$parent.flowElements.filter(e=>e.attachedToRef===t.attachedToRef&&s.hasElement(t));return c=function(t,e){return t.sort((t,o)=>{const s=R(t,e),n=R(o,e);return s[0]-n[0]||s[1]-n[1]}).reverse()}(c,s),c.forEach((t,s,i)=>{const r=l(t,e,n);r.x=d.x+(s+1)*(d.width/(i.length+1))-r.width/2,r.y=d.y+d.height-r.height/2;const c=o.createDiShape(t,r,{id:t.id+"_di"});t.di=c,a.push(c)}),a}(t,e,o,s,i);"bpmn:Lane"===t.$type&&(e[2]=s.colCount);const d=l(t,e,i,r),a={id:t.id+"_di"};t.isExpanded&&(a.isExpanded=!0),n(t,"bpmn:ExclusiveGateway")&&(a.isMarkerVisible=!0);const c=o.createDiShape(t,d,a);return t.di=c,[c]}function R(t,e){return i(t).reduce((t,o)=>{if(!e.hasElement(o))return t;const s=e.find(o);return t[0]<s[0]||t[1]<s[1]?s:t},[0,0])}class T{constructor(t,e){this.source=t,this.target=e}}class P{constructor(e){this.moddle=new t,this.diFactory=new u(this.moddle),this.maxDebugStep=e,this.currentDebugStep=0}async layoutProcess(t){const e=await this.moddle.fromXML(t),{rootElement:o}=e;this.diagram=o,function(t,e){const o=t.elementsById;if(o)for(const t of Object.values(o))if("bpmndi:BPMNShape"===t.$type&&!0===t.isExpanded&&(t.bpmnElement.isExpanded=!0),"bpmn:Participant"===t.$type){const o=[...e(t.processRef)].filter(([,t])=>t.right-t.left===1).map(([t])=>t);o&&o.forEach((t,e)=>{const o=t.flowNodeRef;o&&o.forEach(e=>e.laneRef=t)})}}(e,this.getLanesNestedSet),this.processTrees=this.createNestedSets(e),this.createGridsForProcesses(),this.expandProcessesGrids(),this.expandParticipants();const s=this.getRootProcesses(),n=this.getCollaboration();return s.length>0&&(this.cleanDi(),this.createRootDi(s,n),this.drawParticipants(),this.drawProcesses(),this.drawCollaborationMessageFlows(n)),(await this.moddle.toXML(this.diagram,{format:!0})).xml}expandParticipants(){const t=this.processTrees.map(t=>[...t.nodes]).flat();for(const e of t){const t=e.grid.getLanes();for(const o of t||[]){const t=e.grid.find(o);e.grid.addRowCol(!1,t[0]+t[3]-1,1),t[3]+=1}}}createGridsForProcesses(){const t=this.processTrees.map(t=>[...t.nodes]).flat();t.sort((t,e)=>t.level-e.level);for(const e of t){const t=(e.laneSets||[])[0]?.lanes.length>0;e.grid=this.createGridLayout(e);const o=t?[e.grid]:e.grid._separateGrid()||[e.grid];for(const t of o)t.shrink(!0),t.shrink(!1),t.shakeIt(!0),t.shakeIt(!1);t||(e.grid=e.grid._mergeGrids(o))}}expandProcessesGrids(){const t=this.processTrees.map(t=>[...t.nodes]).flat();t.sort((t,e)=>e.level-t.level);for(const e of t){const t=(e.laneSets||[])[0]?.lanes.length>0,o=t?[e.grid]:e.grid._separateGrid()||[e.grid];for(const t of o)t.shrink(!0),t.shrink(!1),D(t,!1),D(t,!0);t||(e.grid=e.grid._mergeGrids(o))}}drawParticipants(){const t=this.getCollaboration();if(!t||!t[0])return;const e=t[0].participants;let o=0;for(const t of e)o=this.createParticipantDi(t,{x:0,y:o})+70}drawProcesses(){const t=this.processTrees.map(t=>[...t.nodes]).flat();t.sort((t,e)=>t.level-e.level);for(const e of t){const t=this.getParticipantForProcess(e);if(t){const o=this.getElementDi(t),s=this.getProcDi(o);let{x:n,y:i}=o.bounds;n+=75,i+=70,this.generateDi(e,{x:n,y:i},s);continue}if(e.isExpanded&&!this.existingNodes.includes(e))continue;if(e.isExpanded){const t=this.getElementDi(e),o=this.getProcDi(t);let{x:n,y:i}=t.bounds;const{width:r,height:a}=s(e);n+=75-r/4,i+=d-a-a/4,this.generateDi(e,{x:n,y:i},o);continue}const o=this.diagram.diagrams.find(t=>t.plane.bpmnElement===e);this.generateDi(e,{x:0,y:0},o)}}get existingNodes(){return this.processTrees.map(t=>[...t.nodes]).flat().map(t=>[...t.grid.elements]).flat()}drawCollaborationMessageFlows(t){const e=t[0]?t[0].messageFlows:null;if(e)for(const t of e){const{sourceRef:e,targetRef:o}=t,s=this.existingNodes;if(!s.includes(e)||!s.includes(o))continue;const n=e.di.bounds,i=o.di.bounds,r=i.y-n.y,d=[{x:n.x+n.width/2},{x:i.x+i.width/2}];r>0?(d[0].y=n.y+n.height,d[1].y=i.y):(d[0].y=n.y,d[1].y=i.y+i.height);const a=this.diFactory.createDiEdge(t,d);this.diagram.diagrams[0].plane.get("planeElement").push(a)}}getParticipantForProcess(t){const e=this.getCollaboration();if(!e||!e[0])return;const o=this.getCollaboration()[0].participants;return o?o.find(e=>e.processRef===t):void 0}getElementDi(t){return this.diagram.diagrams.map(t=>t.plane.planeElement).flat().find(e=>e.bpmnElement===t)}getProcDi(t){return this.diagram.diagrams.find(e=>e.plane.planeElement.includes(t))}createNestedSets(t){const o=new e.Graph,s=function(t){const e=t.elementsById;return e?Object.values(e).filter(t=>"bpmn:Process"===t.$type||"bpmn:SubProcess"===t.$type):[]}(t);for(const t of s)o.addNode(t);for(const t of s){const e=this.getSubProcesses(t);for(const s of e)o.addEdge({source:t,target:s})}const n=o.getSeparatedGraphs();for(const t of n){const e=t.nodes.filter(e=>0===t.getIncomingEdgesFor(e).size);if(e.length>1)throw new Error("Process tree has more than 1 root elements");if(0===e.length)throw new Error("Process tree has more than 0 root elements");t.rootProcess=e[0]}for(const t of n){const e=(t,e)=>{const o=e.rootProcess;return o.left=0,o.level=0,o},o=(t,e)=>{const{left:o,level:s}=t,n=[...e.getOutgoingEdgesFor(t)].map(t=>t.target).find(t=>void 0===t.left);if(n)return n.level=s+1,n.left=o+1,[n];const i=[...e.getOutgoingEdgesFor(t)].map(t=>t.target).reduce((t,e)=>void 0===t||e.right>t.right?e.right:t,void 0);t.right=i?i+1:t.left+1;const r=[...e.getIncomingEdgesFor(t)].map(t=>t.source).find(t=>void 0===t.right);return r?[r]:void 0};t.genericTraversing(o,e)}return n}getSubProcesses(t){return t.flowElements?t.flowElements.filter(t=>"bpmn:SubProcess"===t.$type):[]}createRootDi(t,e){const o=e&&e.length>0?e[0]:t[0];this.createProcessDi(o)}createProcessDi(t){const e=this.diFactory,o=e.createDiPlane({id:"BPMNPlane_"+t.id,bpmnElement:t}),s=e.createDiDiagram({id:"BPMNDiagram_"+t.id,plane:o});return this.diagram.diagrams.push(s),s}createParticipantDi(t,e){const{colCount:o,rowCount:n}=t.processRef.grid,{width:i,height:a}=s(t);let c=o>0?o*r+r:i,g=a;if(t.processRef.grid.getLanes().length>0){g=n*d;c+=[...this.getLanesNestedSet(t.processRef)].reduce((t,[,e])=>t<e.level?e.level:t,0)*30}else g=n>0?n*d+d:a;const h=this.diFactory.createDiShape(t,{width:c,height:g,...e},{id:t.id+"_di"});return this.diagram.diagrams[0].plane.get("planeElement").push(h),h.bounds.y+h.bounds.height}cleanDi(){this.diagram.diagrams=[]}getLanesNestedSet(t){const e=new Map,o=[...t.laneSets?t.laneSets[0].lanes:[]].reverse();for(o.forEach(t=>{e.set(t,{level:0})});o.length>0;){const t=o.pop(),s=e.get(t);if(void 0===s.left){const t=[...e.values()].reduce((t,e)=>e.right>t||void 0===t?e.right:t,void 0);s.left=void 0!==t?t+1:0}if(void 0===s.right){o.push(t);const n=(t.childLaneSet?.lanes||[]).filter(t=>void 0===e.get(t)?.level);if(0===n.length){let t=[...e.values()].reduce((t,e)=>e.right>t||void 0===t?e.right:t,void 0);void 0===t&&(t=[...e.values()].reduce((t,e)=>e.left>t||void 0===t?e.left:t,void 0)),s.left>t&&(t=s.left),s.right=t+1}[...n].reverse().forEach((t,n,i)=>{o.push(t);const r=n===i.length-1?{level:s.level+1,left:s.left+1}:{level:s.level+1};e.set(t,r)})}}return e}createGridLayout(t){const o=new e.Graph,s=new p(o),i=[...this.getLanesNestedSet(t)].filter(([,t])=>t.right-t.left===1).sort(([,t],[,e])=>t.left-e.left).map(([t])=>t);for(const t of i)s.add(t),s.find(t)[3]=1;for(const e of t.flowElements||[])n(e,"bpmn:SequenceFlow")||n(e,"bpmn:DataObject")||o.addNode(e);for(const t of o.nodes){"bpmn:BoundaryEvent"===t.$type&&(o.addEdge(new T(t,t.attachedToRef)),o.addEdge(new T(t.attachedToRef,t)));for(const e of t.outgoing||[]){const t=new T(e.sourceRef,e.targetRef);t.id=e.id,o.addEdge(t)}const e=t.dataInputAssociations;for(const t of e||[])for(const e of t.sourceRef||[]){const s=new T(e,t.$parent);s.id=t.id,s.propRef=t.targetRef,o.addEdge(s)}const s=t.dataOutputAssociations;for(const t of s||[]){const e=t.$parent,s=t.targetRef,n=new T(e,s);n.id=t.id,o.addEdge(n)}}return o.genericTraversing((t,e,o,n)=>{if(void 0!==this.maxDebugStep&&this.maxDebugStep<=this.currentDebugStep)return;const i=E(t,s,n,o);return this.currentDebugStep+=1,i},(t,e)=>{if(void 0!==this.maxDebugStep&&this.maxDebugStep<=this.currentDebugStep)return;const o=[...t].find(o=>(s.isFlipped?[...e.getOutgoingEdgesFor(o)]:[...e.getIncomingEdgesFor(o)]).filter(e=>!t.has(s.isFlipped?e.target:e.source)).length>0);if(o)return s.flip(!1),o;const i=e.nodes.filter(o=>{const i=s.isFlipped?e.getOutgoingEdgesFor(o):e.getIncomingEdgesFor(o);return!(t.has(o)||0!==i.size||(r=o,(n(r,"bpmn:IntermediateThrowEvent")||n(r,"bpmn:IntermediateCatchEvent"))&&(void 0===r.incoming||0===r.incoming.length)));var r});if(i.length>0)return function(t,e){const o=[e].flat();let s=[];return o.length>0?o.forEach((e,i)=>{const r=t.filter(t=>n(t,e));if(s=s.concat(r),i===o.length-1&&s.length!==t.length)for(const e of t)s.includes(e)||s.push(e)}):s=t,s}(i,"bpmn:StartEvent")[0];const r=[...t].find(o=>(s.isFlipped?[...e.getIncomingEdgesFor(o)]:[...e.getOutgoingEdgesFor(o)]).filter(e=>!t.has(s.isFlipped?e.source:e.target)).length>0);if(r)return r;const d=e.nodes.find(o=>{if(t.has(o))return!1;return 0===(s.isFlipped?[...e.getOutgoingEdgesFor(o)]:[...e.getIncomingEdgesFor(o)]).filter(t=>o!==(s.isFlipped?t.target:t.source)).length});if(d)return d;const a=e.nodes.find(o=>{if(t.has(o))return!1;return 0===(s.isFlipped?[...e.getIncomingEdgesFor(o)]:[...e.getOutgoingEdgesFor(o)]).filter(t=>t.target!==t.source).length});return a?(s.flip(!1),a):(this.currentDebugStep+=1,e.nodes.find(e=>!t.has(e)))}),s.isFlipped&&s.flip(!1),s}generateDi(t,e,o){const{grid:s}=t,n=this.diFactory,i=(o||this.diagram.diagrams[0]).plane.get("planeElement"),r=[],d=[...this.getLanesNestedSet(t)].sort(([,t],[,e])=>e.level-t.level),a=d.reduce((t,[,e])=>e.level>t?e.level:t,0);d.forEach(([t,o])=>{const i={...e},c=30*o.level;i.x+=c;const g=d.filter(([t,e])=>e.left>o.left&&e.right<o.right&&e.right-e.left===1);0===g.length&&g.push([t,o]);const[h,l]=g.reduce((t,[e])=>{const o=s.find(e);return void 0===t||o[0]<t[0]?o:t},void 0);let u=s.colCount>0?s.colCount+1:2;const f=a-o.level;const p=N(t,[h,l,u,g.reduce((t,[e])=>t+(s.find(e)[3]||2),0)],n,s,i,f);r.push(...p)});const c=30*[...this.getLanesNestedSet(t).values()].reduce((t,e)=>e.level>t?e.level:t,0);[...s._elements].filter(([t])=>"bpmn:Lane"!==t.$type).forEach(([t,o])=>{const i={...e};i.x+=c;const d=N(t,o,n,s,i);r.push(...d)}),s._allEdges.forEach(t=>{const o={...e};o.x+=c;const i=F(t,s,n,o);i&&r.push(i)}),this.sortDIsByDefinitionPosition(r,t).forEach(t=>i.push(t))}sortDIsByDefinitionPosition(t,e){return t.sort((t,o)=>{const s=e.flowElements?.findIndex(e=>e.id===t.bpmnElement.id),n=e.flowElements?.findIndex(t=>t.id===o.bpmnElement.id);return s-n})}getRootProcesses(){return this.diagram.get("rootElements").filter(t=>"bpmn:Process"===t.$type)}getCollaboration(){return this.diagram.get("rootElements").filter(t=>"bpmn:Collaboration"===t.$type)}}function D(t,e){const o=new Map,s=new Set;Object.entries(e?t.rows:t.cols).forEach(([t,n])=>{n.forEach(n=>{if(n.isExpanded){s.add(n);const i=o.get(Number.parseInt(t)),[r,d]=n.grid.getGridDimensions();let a=e?r:d;a||(a=1),(void 0===i||a>i)&&o.set(Number.parseInt(t),a)}})}),[...o.entries()].sort(([t],[e])=>e-t).forEach(([o,s])=>{if(e){const e=t.getLanes();for(const n of e){const e=t.find(n);Number.parseInt(o)<=e[0]+e[3]-1&&Number.parseInt(o)>=e[0]&&(e[3]+=Number.parseInt(s))}}t.addRowCol(!e,o,s)}),s.forEach(o=>{const s=t.find(o),[n,i]=o.grid.getGridDimensions();e?s[3]=n<1?2:n+1:s[2]=i<1?2:i+1})}exports.layoutProcess=function(t,e){return new P(e).layoutProcess(t)};//# sourceMappingURL=index.cjs.map