刘光辉
8 小时以前 cf11e63b659afcc9792ab3e395f1b45d844431a8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
import { bpmnStart, bpmnTask, typeConfluence, typeTrigger } from '../config/variableName';
import { NodeUtils } from './nodeUtil';
import { changeTypeByTaskShape, hasGatewayType, typeConfig } from '../config';
import { DEFAULT_CONNECT, DEFAULT_DISTANCE } from '../config/constants';
export class BPMNTreeBuilder {
    _allElement;
    _connectMap;
    constructor(allElement) {
        this._allElement = allElement;
        this._connectMap = new Map();
    }
    addUniqueChild(parent, child) {
        if (!parent.children.some(c => c.id === child.id))
            parent.children.push(child);
    }
    findStartElement(obj) {
        for (const key in obj) {
            if (obj.hasOwnProperty(key)) {
                const item = obj[key];
                if (item.type === bpmnStart)
                    return item;
            }
        }
        return null;
    }
    constructTree(treeType) {
        let startNode = this.findStartElement(this._allElement);
        if (!startNode)
            throw new Error('开始节点未找到');
        const rootNode = {
            id: startNode.id,
            name: startNode.businessObject.name,
            type: startNode.type,
            wnType: startNode.wnType,
            children: [],
            virtualWidth: 320,
            virtualHeight: DEFAULT_DISTANCE,
            level: 0,
            x: startNode.x,
            y: startNode.y,
            width: startNode.width,
            height: startNode.height,
        };
        let connections = {};
        let newConnectsMap = new Map();
        for (let key in this._allElement) {
            let element = this._allElement[key];
            if (element.incoming?.length > 1) {
                let connectMap = new Map();
                element.incoming.map((item) => {
                    if (!connectMap.has(item.id)) {
                        let connectId = item.id;
                        let parentELement = item.source;
                        if (parentELement.outgoing?.length > 1) {
                            parentELement.outgoing.map((connect) => {
                                if (!connectMap.has(connect.id)) {
                                    connectMap.set(connectId, parentELement.outgoing.length || 0);
                                }
                            });
                        }
                    }
                });
                if (treeType != 1) {
                    if (connectMap.size > 0) {
                        if (connectMap.size === element.incoming?.length) {
                            let minKey = null;
                            let minValue = Infinity;
                            for (let [key, value] of connectMap) {
                                if (value < minValue) {
                                    minValue = value;
                                    minKey = key;
                                }
                            }
                            if (minKey !== null) {
                                connectMap.delete(minKey);
                            }
                        }
                    }
                    connectMap.forEach((value, key) => {
                        newConnectsMap.set(key, value);
                    });
                }
            }
        }
        for (let key in this._allElement) {
            let element = this._allElement[key];
            if (element.type === 'bpmn:SequenceFlow' && !newConnectsMap.has(element.id)) {
                let sourceId = element.source.id;
                let targetId = element.target.id;
                if (!connections[sourceId])
                    connections[sourceId] = [];
                let targetElement = element.target;
                let childNode = {
                    id: targetId,
                    name: targetElement.name,
                    type: targetElement.type,
                    wnType: targetElement.wnType,
                    children: [],
                    virtualWidth: 320,
                    virtualHeight: DEFAULT_DISTANCE,
                    x: targetElement.x,
                    y: targetElement.y,
                    width: targetElement.width,
                    height: targetElement.height,
                };
                if (!connections[sourceId].some(child => child.id === childNode.id))
                    connections[sourceId].push(childNode);
            }
        }
        let queue = [rootNode];
        let processedNodes = new Map();
        while (queue.length > 0) {
            let current = queue.shift();
            if (current && connections[current.id]) {
                connections[current.id].forEach(child => {
                    child.parentData = current;
                    if (!processedNodes.has(child.id)) {
                        this.addUniqueChild(current, child);
                        queue.push(child);
                        processedNodes.set(child.id, child);
                    }
                    else
                        this.addUniqueChild(current, processedNodes.get(child.id));
                });
            }
        }
        this._connectMap = newConnectsMap;
        return rootNode;
    }
    calculateVirtualWidth(root, elementRegistry) {
        let stack = [];
        stack.push({ node: root, totalWidth: 0 });
        let parentChildMapping = new Map();
        while (stack.length > 0) {
            let current = stack[stack.length - 1];
            let { node, totalWidth } = current;
            if (node.children.length === 0) {
                node.virtualWidth = 320;
                stack.pop();
                this.updateParent(stack, node.virtualWidth, 'horizontal');
                continue;
            }
            let children = node.children;
            let unprocessedChildren = parentChildMapping.get(node);
            if (!unprocessedChildren) {
                unprocessedChildren = [...children];
                parentChildMapping.set(node, unprocessedChildren);
            }
            if (unprocessedChildren.length > 0) {
                let child = unprocessedChildren.pop();
                stack.push({ node: child, totalWidth: 0 });
            }
            else {
                let finalWidth = totalWidth;
                let hasMergeChild = children.some(child => child.wnType === typeConfluence);
                let hasTrigger = children.some(child => child.wnType === typeTrigger);
                if (hasMergeChild && !hasTrigger)
                    finalWidth = 320;
                else {
                    let newElement = elementRegistry?.get(node.id);
                    finalWidth = children.reduce((sum, child) => {
                        let virtualWidth = child.virtualWidth ?? 0;
                        if (child.isGateway)
                            virtualWidth = 320;
                        return sum + virtualWidth;
                    }, 0);
                    let hasTrigger = newElement.outgoing?.some(o => o.target.wnType === typeTrigger);
                    let isNotTriggerChildren = children?.some(o => o.wnType != typeTrigger);
                    if (hasTrigger && !isNotTriggerChildren)
                        finalWidth += 320;
                }
                if (hasGatewayType.has(node.parentData?.wnType) && !hasTrigger) {
                    let elements = new Map();
                    let number = 0;
                    function getChildrenMaxWidth(targetElement) {
                        elements.set(targetElement.id, targetElement.virtualWidth);
                        if (number < targetElement.virtualWidth && targetElement.wnType != typeConfluence)
                            number = targetElement.virtualWidth;
                        if (targetElement.id !== node.parentData?.id + '_confluence')
                            findPath(targetElement, false);
                    }
                    function findPath(currentElement, isRoot, id) {
                        currentElement?.children.forEach(targetElement => {
                            if (isRoot) {
                                if (targetElement.id === id)
                                    getChildrenMaxWidth(targetElement);
                            }
                            else
                                getChildrenMaxWidth(targetElement);
                        });
                    }
                    findPath(node.parentData, true, node.id);
                    finalWidth = number;
                }
                if (elementRegistry) {
                    let currentElement = elementRegistry.get(node.id);
                    if (currentElement.incoming?.length > 1)
                        node.isGateway = true;
                }
                node.virtualWidth = finalWidth;
                stack.pop();
                this.updateParent(stack, finalWidth, 'horizontal');
            }
        }
        return root.virtualWidth;
    }
    calculateVirtualHeight(root, elementRegistry) {
        let stack = [];
        stack.push({ node: root, totalHeight: 0 });
        let parentChildMapping = new Map();
        while (stack.length > 0) {
            let current = stack[stack.length - 1];
            let { node, totalHeight } = current;
            if (node.children.length === 0) {
                node.virtualHeight = 118;
                stack.pop();
                this.updateParent(stack, node.virtualHeight, 'vertical');
                continue;
            }
            let children = node.children;
            let unprocessedChildren = parentChildMapping.get(node);
            if (!unprocessedChildren) {
                unprocessedChildren = [...children];
                parentChildMapping.set(node, unprocessedChildren);
            }
            if (unprocessedChildren.length > 0) {
                let child = unprocessedChildren.pop();
                stack.push({ node: child, totalHeight: 0 });
            }
            else {
                let finalHeight = totalHeight;
                let hasMergeChild = children.some(child => child.wnType === typeConfluence);
                if (hasMergeChild)
                    finalHeight = 208;
                else
                    finalHeight = children.reduce((sum, child) => {
                        let virtualHeight = child.virtualHeight ?? 0;
                        if (child.isGateway)
                            virtualHeight = 208;
                        return sum + virtualHeight;
                    }, 0);
                if (hasGatewayType.has(node.parentData?.wnType)) {
                    let elements = new Map();
                    let number = 0;
                    function getChildrenMaxHeight(targetElement) {
                        elements.set(targetElement.id, targetElement.virtualHeight);
                        if (number < targetElement.virtualHeight && targetElement.wnType != typeConfluence)
                            number = targetElement.virtualHeight;
                        if (targetElement.id !== node.parentData?.id + '_confluence')
                            findPath(targetElement, false);
                    }
                    function findPath(currentElement, isRoot, id) {
                        currentElement?.children.forEach(targetElement => {
                            if (isRoot) {
                                if (targetElement.id === id)
                                    getChildrenMaxHeight(targetElement);
                            }
                            else
                                getChildrenMaxHeight(targetElement);
                        });
                    }
                    findPath(node.parentData, true, node.id);
                    finalHeight = number;
                }
                if (elementRegistry) {
                    let currentElement = elementRegistry.get(node.id);
                    if (currentElement.incoming?.length > 1)
                        node.isGateway = true;
                }
                node.virtualHeight = finalHeight;
                stack.pop();
                this.updateParent(stack, finalHeight, 'vertical');
            }
        }
        return root.virtualHeight;
    }
    updateParent(stack, number, type) {
        if (stack.length > 0) {
            const parent = stack[stack.length - 1];
            type === 'horizontal' ? (parent['totalWidth'] += number) : (parent['totalHeight'] += number);
        }
    }
    findNodeById(root, id) {
        if (root.id === id)
            return root;
        for (const child of root.children) {
            const foundNode = this.findNodeById(child, id);
            if (foundNode)
                return foundNode;
        }
        return undefined;
    }
    isGateway(element) {
        const gatewayTypes = ['bpmn:ExclusiveGateway', 'bpmn:ParallelGateway', 'bpmn:InclusiveGateway', 'bpmn:EventBasedGateway'];
        if (element.wnType === typeConfluence)
            return false;
        return gatewayTypes.includes(element.type);
    }
    formatCanvas(visited, modeling, elementRegistry) {
        let obj = visited.reduce((acc, item) => {
            let x = item.offset?.x - item.x || 0;
            if (!acc[x])
                acc[x] = [];
            acc[x].push(elementRegistry.get(item.id));
            return acc;
        }, {});
        for (const key in obj) {
            if (obj.hasOwnProperty(key)) {
                let list = obj[key];
                modeling.moveElements(list, { x: Number(key), y: 0 });
            }
        }
    }
    formatCanvasHorizontal(visited, modeling, elementRegistry) {
        let obj = visited.reduce((acc, item) => {
            let y = item.offset?.y - item.y || 0;
            if (!acc[y])
                acc[y] = [];
            acc[y].push(elementRegistry.get(item.id));
            return acc;
        }, {});
        for (const key in obj)
            obj.hasOwnProperty(key) && modeling.moveElements(obj[key], { x: 0, y: Number(key) });
    }
    getParentOffsetById(data, id) {
        if (data.parentData) {
            if (data.parentData.id === id) {
                let offset = {
                    x: data.parentData.offset.x + data.parentData.width / 2,
                    y: data.parentData.offset.y,
                };
                return offset;
            }
            return this.getParentOffsetById(data.parentData, id);
        }
    }
    traverseTreeBFS(root, callback) {
        let queue = [root];
        const processedNodes = new Set();
        const AUTO_HEIGHT = 150;
        while (queue.length > 0) {
            let current = queue.shift();
            if (current && !processedNodes.has(current.id)) {
                processedNodes.add(current.id);
                if (current.id != root.id) {
                    let parentData = current.parentData;
                    let n = 0;
                    if (parentData) {
                        for (let i = 0; i < parentData.children.length; i++) {
                            if (parentData.children[i].id === current.id)
                                break;
                            if (parentData.children[i].wnType === typeConfluence) {
                                parentData.children[i].virtualWidth = 320;
                            }
                            n += parentData.children[i].virtualWidth;
                        }
                    }
                    let parentX = parentData.offset ? parentData.offset.x : parentData.x;
                    let parentY = parentData.offset ? parentData.offset.y : parentData.y;
                    let minX = parentX - parentData.virtualWidth / 2;
                    let currentVirtualWidth = current.virtualWidth / 2;
                    if (current.children)
                        if (parentData.virtualWidth > current.virtualWidth && parentData.children?.length === 1)
                            currentVirtualWidth = parentData.virtualWidth / 2;
                    if (parentData.children?.length === 1 && !parentData.children.some(o => o.wnType != typeTrigger)) {
                        minX = parentX - (parentData.virtualWidth + 320) / 2;
                    }
                    let isTrigger = parentData.children.every(s => s.wnType === typeTrigger);
                    if (isTrigger) {
                        n += 320;
                    }
                    let offset = {
                        x: minX + currentVirtualWidth - (current.width - parentData.width) / 2 + n,
                        y: parentY + AUTO_HEIGHT + current.height,
                    };
                    if (current.id.includes('_confluence')) {
                        let id = current.id.replace('_confluence', '');
                        let gatewayOffset = this.getParentOffsetById(current, id);
                        offset = {
                            x: gatewayOffset.x,
                            y: parentY + AUTO_HEIGHT + current.height,
                        };
                    }
                    current.offset = offset;
                }
                callback(current);
                current.children = current.children.map((children) => {
                    return { ...children, parentData: { ...current } };
                });
                queue.push(...current.children);
            }
        }
    }
    bpmnTraverseTreeBFS(root, callback, type) {
        let queue = [root];
        const AUTO_HEIGHT = 150;
        while (queue.length > 0) {
            let current = queue.shift();
            if (current) {
                if (current.id != root.id) {
                    let parentData = current.parentData;
                    let n = 0;
                    if (parentData) {
                        for (let i = 0; i < parentData.children.length; i++) {
                            if (parentData.children[i].id === current.id)
                                break;
                            type === 'horizontal' ? (n += parentData.children[i].virtualHeight || 208) : (n += parentData.children[i].virtualWidth || 320);
                        }
                    }
                    let parentX = parentData.offset ? parentData.offset.x : parentData.x;
                    let parentY = parentData.offset ? parentData.offset.y : parentData.y;
                    if (type === 'horizontal') {
                        let minY = parentY - parentData.virtualHeight / 2;
                        let currentVirtualHeight = current.virtualHeight / 2;
                        if (parentData.virtualHeight > current.virtualHeight && parentData.children?.length === 1)
                            currentVirtualHeight = parentData.virtualHeight / 2;
                        let offset = {
                            x: parentX + AUTO_HEIGHT + current.width,
                            y: minY + n + currentVirtualHeight + (parentData.height - current.height) / 2,
                        };
                        let level = current.level;
                        current.offset = offset;
                        current.level = level;
                    }
                    else {
                        let minX = parentX - parentData.virtualWidth / 2;
                        let currentVirtualWidth = current.virtualWidth / 2;
                        if (parentData.virtualWidth > current.virtualWidth && parentData.children?.length === 1)
                            currentVirtualWidth = parentData.virtualWidth / 2;
                        let offset = {
                            x: minX + n + currentVirtualWidth + (parentData.width - current.width) / 2,
                            y: parentY + AUTO_HEIGHT + current.height,
                        };
                        let level = current.level;
                        current.offset = offset;
                        current.level = level;
                    }
                }
                callback(current);
                let level = current?.level ?? 0;
                current.children = current.children.map((children) => {
                    return { ...children, parentData: { ...current }, level: level + 1 };
                });
                queue.push(...current.children);
            }
        }
    }
    updateConnectionWaypoints(connect, modeling, type) {
        let source = connect.source;
        let target = connect.target;
        let newWaypoints = [];
        if (type === 'vertical') {
            if (source.x < target.x) {
                newWaypoints = [
                    { x: source.x + source.width / 2, y: source.y + source.height },
                    { x: source.x + source.width / 2, y: target.y - 60 },
                    { x: target.x + target.width / 2, y: target.y - 60 },
                    { x: target.x + target.width / 2, y: target.y },
                ];
            }
            else if (source.x > target.x) {
                newWaypoints = [
                    { x: source.x + source.width / 2, y: source.y + source.height },
                    { x: source.x + source.width / 2, y: target.y - 60 },
                    { x: target.x + target.width / 2, y: target.y - 60 },
                    { x: target.x + target.width / 2, y: target.y },
                ];
            }
            else {
                newWaypoints = [
                    { x: source.x + source.width / 2, y: source.y + source.height },
                    { x: target.x + target.width / 2, y: target.y },
                ];
            }
        }
        else {
            if (source.y < target.y) {
                newWaypoints = [
                    { x: source.x + source.width, y: source.y + source.height / 2 },
                    { x: target.x - DEFAULT_CONNECT / 2, y: source.y + source.height / 2 },
                    { x: target.x - DEFAULT_CONNECT / 2, y: target.y + target.height / 2 },
                    { x: target.x, y: target.y + target.height / 2 },
                ];
            }
            else if (source.y > target.y) {
                newWaypoints = [
                    { x: source.x + source.width, y: source.y + source.height / 2 },
                    { x: target.x - DEFAULT_CONNECT / 2, y: source.y + source.height / 2 },
                    { x: target.x - DEFAULT_CONNECT / 2, y: target.y + target.height / 2 },
                    { x: target.x, y: target.y + target.height / 2 },
                ];
            }
            else {
                newWaypoints = [
                    { x: source.x + source.width, y: source.y + source.height / 2 },
                    { x: target.x, y: target.y + target.height / 2 },
                ];
            }
        }
        modeling.updateWaypoints(connect, newWaypoints);
    }
    isWithinThresholdDel(target, source, threshold) {
        let gatewayY = target.y;
        let sourceElementY = source.y;
        if (target.wnType === typeConfluence) {
            if (target.incoming?.length > 1) {
                let y = -Infinity;
                target.incoming.map((item) => {
                    if (item?.source?.y > y)
                        y = item.source.y;
                });
                return gatewayY - y <= threshold;
            }
        }
        return gatewayY - sourceElementY < threshold && gatewayY > sourceElementY;
    }
    moveConnectedElements(connection, height) {
        const stack = [];
        const processedElements = new Set();
        stack.push(connection);
        while (stack.length > 0) {
            const currentConnection = stack.pop();
            const target = currentConnection.target;
            if (!target)
                continue;
            if (processedElements.has(target))
                continue;
            if (this.isWithinThresholdDel(target, currentConnection.source, height))
                continue;
            processedElements.add(target);
            const outgoingConnections = target.outgoing || [];
            for (const outgoingConnection of outgoingConnections) {
                stack.push(outgoingConnection);
            }
        }
        return Array.from(processedElements);
    }
    getElementsByGateway(gateway) {
        const elementsMap = new Map();
        let allElements = this._allElement;
        function getList(list) {
            list.map((element) => {
                if (element.id === gateway.id + '_confluence')
                    return;
                if (!elementsMap.has(element.id)) {
                    elementsMap.set(element.id, element);
                    let childrenList = NodeUtils.getNextElementList(element, allElements);
                    getList(childrenList);
                }
                return;
            });
        }
        let list = NodeUtils.getNextElementList(gateway, allElements);
        getList(list);
        return Array.from(elementsMap.values());
    }
    resizeGroupShape(shapes, bpmn) {
        let elementRegistry = bpmn.get('elementRegistry');
        let modeling = bpmn.get('modeling');
        let groupSet = new Set();
        shapes.length > 0 &&
            shapes.map((shape) => {
                if (changeTypeByTaskShape[shape.wnType] || shape.wnType === typeTrigger) {
                    groupSet.add(shape.businessObject.$attrs.customGroupId);
                }
            });
        for (let groupId of groupSet) {
            let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
            let groupShape = elementRegistry.get(groupId);
            if (groupShape) {
                bpmn.get('elementRegistry').forEach(element => {
                    if (element.businessObject.$attrs.customGroupId === groupId) {
                        minX = Math.min(minX, element.x);
                        minY = Math.min(minY, element.y);
                        maxX = Math.max(maxX, element.x + element.width);
                        maxY = Math.max(maxY, element.y + element.height);
                    }
                });
                modeling.resizeShape(groupShape, {
                    x: minX - 25,
                    y: minY - 15,
                    width: maxX - minX + 50,
                    height: maxY - minY + 30,
                });
            }
        }
    }
    getGroupElementById(groupId, bpmn) {
        let groupList = [];
        let groupShape = bpmn.get('elementRegistry').get(groupId);
        if (groupShape) {
            bpmn.get('elementRegistry').forEach(element => {
                if (element.businessObject.$attrs?.customGroupId === groupId || element.id === groupId)
                    groupList.push(element);
            });
        }
        return groupList;
    }
    getOutgoingConnections(element) {
        return element.outgoing || [];
    }
    findUniqueElementsBetween(currentElement, targetElement, visitedElements = new Set()) {
        visitedElements.add(currentElement);
        if (currentElement === targetElement) {
            return visitedElements;
        }
        const outgoingConnections = this.getOutgoingConnections(currentElement);
        outgoingConnections.forEach(connection => {
            const nextElement = connection.target;
            if (!visitedElements.has(nextElement)) {
                this.findUniqueElementsBetween(nextElement, targetElement, visitedElements);
            }
        });
        return visitedElements;
    }
    onComputerMaxElementH(bpmn, current, gatewayElement, groupList, delElement, isGateway, processedElements, threshold) {
        let elementRegistry = bpmn.get('elementRegistry');
        let confluenceElement = elementRegistry.get(current.id);
        let treeBuilder = new BPMNTreeBuilder(elementRegistry.getAll());
        let list = [];
        let groupH = 0;
        const uniqueElementsSet = treeBuilder.findUniqueElementsBetween(gatewayElement, confluenceElement);
        let maxElement = null;
        let maxValue = -Infinity;
        uniqueElementsSet.forEach((element) => {
            let y = element?.y;
            if (processedElements?.has(element.id))
                y += threshold;
            if (element?.wnType === typeConfluence && maxElement?.wnType != typeConfluence)
                y = y + 220;
            if (y > maxValue && current.id != element.id) {
                maxValue = y;
                maxElement = element;
            }
        });
        if (maxValue <= current.y - 220) {
            list.push(current);
            const taskHeight = typeConfig?.[bpmnTask]?.renderer?.attr?.height;
            groupH = current.y - (maxValue + DEFAULT_DISTANCE + taskHeight);
        }
        else if (maxElement.wnType === current.wnType && current.wnType === typeConfluence) {
            list.push(current);
        }
        if (groupList?.length) {
            if (current.y > maxValue) {
                list.push(current);
            }
        }
        return { list: list, h: groupH };
    }
    handleCollisionsByLevel(list, type) {
        const MIN_SPACE_X = 120;
        const MIN_SPACE_Y = 30;
        const grouped = groupByLevel(list);
        grouped.forEach(group => {
            collisionFun(group);
        });
        function collisionFun(elements) {
            let y = elements[elements.length - 1].offset ? elements[elements.length - 1].offset.y : elements[elements.length - 1].y;
            let x = elements[elements.length - 1].offset ? elements[elements.length - 1].offset.x : elements[elements.length - 1].x;
            for (let i = 0; i < elements.length; i++) {
                for (let j = i + 1; j < elements.length; j++) {
                    const rect1 = elements[i];
                    const rect2 = elements[j];
                    if (isColliding(rect1, rect2, type)) {
                        if (type === 'horizontal') {
                            y = y + rect2.height + MIN_SPACE_Y;
                            rect2.offset.y = y;
                        }
                        else {
                            x = x + rect2.width + MIN_SPACE_X;
                            rect2.offset.x = x;
                        }
                    }
                }
            }
        }
        function groupByLevel(list) {
            list.sort((a, b) => a.level - b.level);
            const grouped = [];
            let currentLevel = null;
            let currentGroup = [];
            list.forEach(item => {
                if (item.level !== currentLevel) {
                    if (currentGroup.length > 0) {
                        grouped.push(currentGroup);
                    }
                    currentGroup = [item];
                    currentLevel = item.level;
                }
                else {
                    currentGroup.push(item);
                }
            });
            if (currentGroup.length > 0) {
                currentGroup.sort((a, b) => {
                    if (type === 'horizontal') {
                        let aY = a.offset ? a.offset.y : a.y;
                        let bY = b.offset ? b.offset.y : b.y;
                        return aY - bY;
                    }
                    else {
                        let aX = a.offset ? a.offset.x : a.x;
                        let bX = b.offset ? b.offset.x : b.x;
                        return aX - bX;
                    }
                });
                grouped.push(currentGroup);
            }
            return grouped;
        }
        function isColliding(element1, element2, type) {
            if (type === 'horizontal') {
                let element1Y = element1.offset ? element1.offset.y : element1.y;
                let element2Y = element2.offset ? element2.offset.y : element2.y;
                return element1Y < element2Y + element2.height + MIN_SPACE_Y && element1Y + element1.height + MIN_SPACE_Y > element2Y;
            }
            if (type === 'vertical') {
                let element1X = element1.offset ? element1.offset.x : element1.x;
                let element2X = element2.offset ? element2.offset.x : element2.x;
                return element1X < element2X + element2.width + MIN_SPACE_X && element1X + element1.width + MIN_SPACE_X > element2X;
            }
        }
    }
}