@zombie-mermaid/ascii-renderer 3.2.0 → 4.0.0
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/dist/index.cjs +7 -7
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +1804 -1428
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/src/__tests__/ascii-class-box-occupancy.test.ts +30 -30
- package/src/__tests__/ascii-class-cross-level-jog-corruption.test.ts +19 -19
- package/src/__tests__/ascii-class-namespace-1196.test.ts +109 -0
- package/src/__tests__/ascii-class-parent-alignment-971.test.ts +3 -3
- package/src/__tests__/ascii-cluster-entry-1283.test.ts +258 -0
- package/src/__tests__/ascii-cluster-exit-anchoring-1148.test.ts +6 -2
- package/src/__tests__/ascii-multibox-cjk-width.test.ts +1 -1
- package/src/__tests__/ascii-rl-direction-1231.test.ts +156 -0
- package/src/__tests__/ascii-subgraph-back-edge-1197.test.ts +10 -18
- package/src/__tests__/ascii-subgraph-frames-1252-1254.test.ts +135 -0
- package/src/__tests__/ascii-subgraph-title-edge-1222.test.ts +51 -42
- package/src/__tests__/ascii-subgraph-title-padding.test.ts +7 -4
- package/src/__tests__/ascii-subgraph-title-room-1278.test.ts +101 -0
- package/src/__tests__/ascii-subgraph-wall-clears-node-1288.test.ts +55 -0
- package/src/__tests__/testdata/ascii/cls_all_relationships.txt +1 -1
- package/src/__tests__/testdata/ascii/cls_annotation.txt +1 -1
- package/src/__tests__/testdata/ascii/cls_basic.txt +9 -9
- package/src/__tests__/testdata/ascii/cls_inheritance.txt +7 -7
- package/src/__tests__/testdata/ascii/cls_methods.txt +2 -2
- package/src/__tests__/testdata/ascii/graph_rl_direction.txt +8 -0
- package/src/__tests__/testdata/ascii/nested_subgraphs_with_labels.txt +35 -25
- package/src/__tests__/testdata/ascii/subgraph_direction_override.txt +21 -21
- package/src/__tests__/testdata/ascii/subgraph_mixed_nodes_td.txt +38 -38
- package/src/__tests__/testdata/unicode/cls_all_relationships.txt +1 -1
- package/src/__tests__/testdata/unicode/cls_annotation.txt +1 -1
- package/src/__tests__/testdata/unicode/cls_basic.txt +9 -9
- package/src/__tests__/testdata/unicode/cls_inheritance.txt +7 -7
- package/src/__tests__/testdata/unicode/cls_methods.txt +2 -2
- package/src/__tests__/testdata/unicode/graph_rl_direction.txt +8 -0
- package/src/canvas.ts +152 -1
- package/src/class-diagram.ts +174 -2
- package/src/cluster-boundary.ts +56 -0
- package/src/converter.ts +9 -1
- package/src/draw-arrows.ts +81 -8
- package/src/draw-subgraphs.ts +33 -22
- package/src/draw.ts +9 -0
- package/src/edge-routing.ts +75 -1
- package/src/flowchart.ts +22 -2
- package/src/frame-avoidance.ts +31 -19
- package/src/grid.ts +261 -6
- package/src/hyperlinks.ts +9 -0
- package/src/types.ts +14 -0
package/dist/index.cjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});let e=require("@zombie-mermaid/core"),t=require("@zombie-mermaid/mermaid-parser");function n(e){return[e.length-1,(e[0]?.length??1)-1]}var r=`\x1B]8;;\x1B\\`;function i(e){return`\x1b]8;;${e.replace(/[^\x21-\x7e]/gu,e=>encodeURIComponent(e))}\x1b\\`}var a=/\x1b\]8;[^\x07\x1b]*(?:\x1b\\|\x07)/g;function o(e){return e.replace(a,``)}function s(e,t){let n=[];for(let r=0;r<=e;r++){let e=[];for(let n=0;n<=t;n++)e.push(null);n.push(e)}return n}function c(e,t,n,r){let i=e[t];i===void 0||n<0||n>=i.length||(i[n]=r)}function l(e){for(let t of e)t.reverse();return e}function u(e,t,r,i,a){let[o,s]=n(t),l=Math.max(1,a?.from??1),u=Math.min(s-1,a?.to??s-1);for(let n=l;n<=u;n++){let a=-1,s=-1;for(let e=1;e<=o-1;e++)t[e]?.[n]!==` `&&(a===-1&&(a=e),s=e);if(a!==-1)for(let t=a;t<=s;t++)c(e,t+r.x,n+r.y,i)}}function
|
|
2
|
-
`)}var
|
|
3
|
-
`)}function Le(e,t,n,r,i){if(i===`none`)return e.join(``);let a=``,o=null,s=``;for(let c=0;c<e.length;c++){let l=e[c];if(l===` `){s.length>0&&(a+=o?A(s,o,i):s,s=``,o=null),a+=` `;continue}let u=n[c]??null,d=t[c]??null,f=u??(d?U(d,r):null);f===o?s+=l:(s.length>0&&(a+=o?A(s,o,i):s),s=l,o=f)}return s.length>0&&(a+=o?A(s,o,i):s),a}function Re(e){if(e.xAxis.categories)return e.xAxis.categories.length;for(let t of e.series)if(t.data.length>0)return t.data.length;return 0}function ze(e,t){if(e.xAxis.categories)return e.xAxis.categories;if(e.xAxis.range){let{min:n,max:r}=e.xAxis.range,i=t>1?(r-n)/(t-1):0;return Array.from({length:t},(e,t)=>He(n+i*t))}return Array.from({length:t},(e,t)=>String(t+1))}function Be(e,t){let n=t-e;if(n<=0)return[e];let r=n/6,i=10**Math.floor(Math.log10(r)),a=r/i,o;o=a<=1.5?i:a<=3?2*i:a<=7?5*i:10*i;let s=Math.ceil(e/o)*o,c=[];for(let e=s;e<=t+o*.001;e+=o)c.push(Math.round(e*1e10)/1e10);return c}function Ve(e){let t=e.length>1?Math.abs(e[1]-e[0]):0,n=t>0&&t<1?Math.ceil(-Math.log10(t)-1e-9):0;return e.map(e=>String(Number.isInteger(e)&&n===0?e:Number(e.toFixed(n))))}function He(e){return Number.isInteger(e)?String(e):e.toFixed(+(Math.abs(e)<10))}var K={x:1,y:0},q={x:1,y:2},J={x:0,y:1},Y={x:2,y:1},Ue={x:2,y:0},We={x:0,y:0},Ge={x:2,y:2},Ke={x:0,y:2},qe={x:1,y:1};function X(e,t){return e.x===t.x&&e.y===t.y}function Je(e){return X(e,K)||X(e,q)||X(e,J)||X(e,Y)}function Ye(e){if(!Je(e))throw Error(`Expected a cardinal direction (Up/Down/Left/Right); got {x:${e.x}, y:${e.y}}`);return e}function Xe(e,t,n,r){return Math.max(r,n+(e-t))}function Ze(e,t){return e.x===t.x&&e.y===t.y}function Qe(e,t){return e.x===t.x&&e.y===t.y}function $e(e,t){return{x:e.x+t.x,y:e.y+t.y}}function et(e){return`${e.x},${e.y}`}function Z(e){let t=e.gridCoord;if(t===null)throw Error(`Node "${e.name}" has no gridCoord; grid layout must run before it is read`);return t}var tt={name:``,styles:{}},nt=class{#e=new Set;has(e){return this.#e.has(e)}add(e){this.#e.add(e)}delete(e){this.#e.delete(e)}keys(){return this.#e.keys()}};function rt(){return new nt}function it(e){let t=rt();for(let n of e.keys())t.add(n);return t}function at(e,t){return e.has(et(t))}function ot(e,t){return t.x<0||t.y<0?!1:!at(e,t)}function st(e,t,n=3){for(let r=0;r<n;r++)for(let i=0;i<n;i++)if(at(e,{x:t.x+r,y:t.y+i}))return!1;return!0}function ct(e,t,n=3){if(!st(e,t,n))throw Error(`Grid block at (${t.x},${t.y}) is already occupied`);for(let r=0;r<n;r++)for(let i=0;i<n;i++){let n={x:t.x+r,y:t.y+i};e.add(et(n))}}var lt=1e5;function ut(e){if(e.length===0)return[];let t=[e[0]];for(let n=1;n<e.length;n++){let r=e[n-1],i=e[n],a=r.x,o=r.y,s=Math.abs(i.x-r.x),c=Math.abs(i.y-r.y),l=r.x<i.x?1:-1,u=r.y<i.y?1:-1,d=s-c,f=0;for(;a!==i.x||o!==i.y;){if(f>=lt)throw Error(`pathCells: segment from (${r.x},${r.y}) to (${i.x},${i.y}) did not reach its endpoint within ${lt} steps`);let e=2*d;e>-c&&(d-=c,a+=l),e<s&&(d+=s,o+=u),t.push({x:a,y:o}),f++}}return t}var dt=class{items=[];get length(){return this.items.length}push(e){this.items.push(e),this.bubbleUp(this.items.length-1)}pop(){if(this.items.length===0)return;let e=this.items[0],t=this.items.pop();return this.items.length>0&&(this.items[0]=t,this.sinkDown(0)),e}bubbleUp(e){let t=e;for(;t>0;){let e=t-1>>1;if(this.items[t].priority<this.items[e].priority)[this.items[t],this.items[e]]=[this.items[e],this.items[t]],t=e;else break}}sinkDown(e){let t=this.items.length,n=e;for(;;){let e=n,r=2*n+1,i=2*n+2;if(r<t&&this.items[r].priority<this.items[e].priority&&(e=r),i<t&&this.items[i].priority<this.items[e].priority&&(e=i),e!==n)[this.items[n],this.items[e]]=[this.items[e],this.items[n]],n=e;else break}}};function ft(e,t){let n=Math.abs(e.x-t.x),r=Math.abs(e.y-t.y);return n===0||r===0?n+r:n+r+1}var pt=.001,mt=[{x:1,y:0},{x:-1,y:0},{x:0,y:1},{x:0,y:-1}],ht=5e4,gt=2e5;function _t(e=gt){return{remaining:e}}function vt(e,t,n,r,i=!1){if(r&&r.remaining<=0)return null;let a=new dt;a.push({coord:t,priority:0});let o=new Map;o.set(et(t),0);let s=new Map;s.set(et(t),null);let c=0;for(;a.length>0;){if(++c>ht)return null;if(r){if(r.remaining<=0)return null;r.remaining--}let t=a.pop().coord;if(Ze(t,n)){let e=[],n=t;for(;n!==null;)e.unshift(n),n=s.get(et(n))??null;return e}let l=o.get(et(t));if(l===void 0)throw Error(`A* pathfinding: missing cost for visited cell ${et(t)}`);for(let r of mt){let c={x:t.x+r.x,y:t.y+r.y};if(!ot(e,c)&&!Ze(c,n))continue;let u=i?s.get(et(t)):null,d=u&&(t.x-u.x!==r.x||t.y-u.y!==r.y),f=l+1+(d?pt:0),p=et(c),m=o.get(p);if(m===void 0||f<m){o.set(p,f);let e=f+ft(c,n);a.push({coord:c,priority:e}),s.set(p,t)}}}return null}function yt(e){if(e.length<=2)return e;let t=new Set,n=e[0],r=e[1];for(let i=2;i<e.length;i++){let a=e[i],o=r.x-n.x,s=r.y-n.y,c=a.x-r.x,l=a.y-r.y;o===c&&s===l&&t.add(i-1),n=r,r=a}return e.filter((e,n)=>!t.has(n))}function bt(e,t,n,r){if(t.x===n.x&&t.y===n.y)return!0;let i=t.x===n.x;if(!i&&t.y!==n.y)return!1;let a=i?n.y>t.y?1:-1:n.x>t.x?1:-1,o=i?t.y:t.x,s=i?n.y:n.x;for(o+=a;;o+=a){let n=o===s,a=i?{x:t.x,y:o}:{x:o,y:t.y};if(!(n&&!r)&&!ot(e,a))return!1;if(n)break}return!0}function xt(e,t,n,r){let i=r?{x:n.x,y:t.y}:{x:t.x,y:n.y};return!bt(e,t,i,!0)||!bt(e,i,n,!1)?null:[t,i,n]}function St(e,t,n,r){if(t.x===n.x||t.y===n.y)return null;let i=X(r,J)||X(r,Y);return xt(e.grid,t,n,i)??xt(e.grid,t,n,!i)}function Ct(e,t,n,r){if(!e.pathBudget)throw Error(`routeEdge requires graph.pathBudget to be set; call createPathBudget() (see grid.ts createMapping) before routing edges`);let i=St(e,t,n,r)??vt(e.grid,t,n,e.pathBudget,e.preferStraightRoutes===!0);return i?yt(i):null}function wt(e){if(e.config.graphDirection!==`TD`)return[];let t=[],n=new Set,r=new Map;for(let t of e.edges){if(t.from===t.to)continue;let e=r.get(t.to)??[];e.push(t),r.set(t.to,e)}for(let[i,a]of r){if(a.length<2||!Tt(a,e)||a.some(e=>n.has(e)))continue;let r={type:`fan-in`,edges:[...a],sharedNode:i,otherNodes:a.map(e=>e.from),junctionPoint:null,sharedPath:[],junctionDir:qe,sharedNodeDir:qe};for(let e of a)e.bundle=r,n.add(e);t.push(r)}let i=new Map;for(let t of e.edges){if(t.from===t.to||n.has(t))continue;let e=i.get(t.from)??[];e.push(t),i.set(t.from,e)}for(let[r,a]of i){if(a.length<2||!Tt(a,e))continue;let i={type:`fan-out`,edges:[...a],sharedNode:r,otherNodes:a.map(e=>e.to),junctionPoint:null,sharedPath:[],junctionDir:qe,sharedNodeDir:qe};for(let e of a)e.bundle=i,n.add(e);t.push(i)}return t}function Tt(e,t){if(e.length<2)return!1;let n=e[0].style,r=Kn(t,e[0].from),i=Kn(t,e[0].to);for(let a of e){if(a.style!==n||a.text.length>0)return!1;let e=Kn(t,a.from),o=Kn(t,a.to);if(e!==r||o!==i||e!==o)return!1}let a=e.every(t=>t.to===e[0].to),o=a?e.map(e=>e.from):e.map(e=>e.to);if(new Set(o).size!==o.length||e.some(e=>e.parallelLane))return!1;let s=t.config.graphDirection===`LR`?`x`:`y`,c=(a?e[0].to:e[0].from).gridCoord;if(!c)return!1;for(let t of e){let e=(a?t.from:t.to).gridCoord;if(!e)return!1;if(a){if(e[s]>=c[s])return!1}else if(e[s]<=c[s])return!1}return!0}function Et(e,t){let n=e.config.graphDirection,r=Z(t.sharedNode);if(t.type===`fan-in`){if(n===`TD`){let e=r.y-1;return{x:r.x+1,y:e}}return{x:r.x-1,y:r.y+1}}if(n===`TD`){let e=r.y+3;return{x:r.x+1,y:e}}return{x:r.x+3,y:r.y+1}}function Dt(e,t){let n=e.config.graphDirection;t.junctionPoint=Et(e,t);let r=t.junctionPoint;if(t.type===`fan-in`){t.junctionDir=n===`TD`?K:J,t.sharedNodeDir=n===`TD`?q:Y;let i=Z(t.sharedNode),a=Ye(n===`TD`?K:J),o=$e(i,a);t.sharedPath=Ct(e,r,o,a)??[r,o];for(let i of t.edges){let o=Z(i.from),s=Ye(n===`TD`?q:Y),c=$e(o,s);i.pathToJunction=Ct(e,c,r,s)??[c,r],i.startDir=s,i.endDir=a,i.path=[...i.pathToJunction,...t.sharedPath.slice(1)]}}else{t.junctionDir=n===`TD`?q:Y,t.sharedNodeDir=n===`TD`?K:J;let i=Z(t.sharedNode),a=Ye(n===`TD`?q:Y),o=$e(i,a);t.sharedPath=Ct(e,o,r,a)??[o,r];for(let i of t.edges){let o=Z(i.to),s=Ye(n===`TD`?K:J),c=$e(o,s);i.pathToJunction=Ct(e,r,c,s)??[r,c],i.startDir=a,i.endDir=s,i.path=[...t.sharedPath,...i.pathToJunction.slice(1)]}}}function Ot(e){for(let t of e.bundles)Dt(e,t)}function kt(e){let t=null;for(let n of e.nodes){let e=n.gridCoord;if(e){if(!t){t={minX:e.x,minY:e.y,maxX:e.x+2,maxY:e.y+2};continue}t.minX=Math.min(t.minX,e.x),t.minY=Math.min(t.minY,e.y),t.maxX=Math.max(t.maxX,e.x+2),t.maxY=Math.max(t.maxY,e.y+2)}}return t}function At(e,t,n){let r=e.config.graphDirection!==`LR`,i=r?q:Y,a=r?K:J,o=$e(Z(t.anchor),i),s=$e(Z(n.to),a),c=Ct(e,t.gutter,s,Ye(r?Y:q));if(!c||c.length<2)return null;let l=[o,...c];return{path:l,startDir:i,endDir:a,labelSegment:jt(t)?[l[l.length-2],l[l.length-1]]:[l[1],l[2]]}}function jt(e){for(let t of e.edges)if(t.parallelLane)return!0;return!1}function Mt(e){let t=e.config.graphDirection!==`LR`,n=new Map;for(let t of e.edges){let e=t.clusterSource;if(!e)continue;let r=n.get(e);r?r.push(t):n.set(e,[t])}let r=new Map;for(let[i,a]of n){let n=Nt(e,i,a,t);n&&r.set(i,n)}r.size>0&&(e.clusterExitPlans=r)}function Nt(e,t,n,r){let i=kt(t);if(!i)return null;let a=n.filter(e=>{let n=e.from.gridCoord,a=e.to.gridCoord;return!n||!a||e.bundle||e.from===e.to||!t.nodes.includes(e.from)||t.nodes.includes(e.to)?!1:r?a.y>i.maxY+1:a.x>i.maxX+1}),o=new Set(a),s=new Map;for(let t of e.edges){if(!t.parallelLane)continue;let e=s.get(t.parallelLane.usedOffsets);e?e.push(t):s.set(t.parallelLane.usedOffsets,[t])}let c=a.filter(e=>!e.parallelLane||s.get(e.parallelLane.usedOffsets).every(e=>o.has(e)));if(c.length===0)return null;let l=c[0].from;if(c.some(e=>e.from!==l))return null;let u=Z(l),d=r?{x:u.x+1,y:i.maxY+1}:{x:i.maxX+1,y:u.y+1},f=$e(u,r?q:Y),p={box:i,anchor:l,gutter:d,edges:new Set(c)},m=new Set;for(let t of c){let n=t.parallelLane;n&&n.index>0&&!Pt(e,p,t)&&m.add(n.usedOffsets)}if(c=c.filter(e=>!e.parallelLane||!m.has(e.parallelLane.usedOffsets)),new Set(c.map(e=>e.parallelLane?.usedOffsets??e)).size<2)return null;let h=r?d.y-f.y:d.x-f.x;for(let t=1;t<=h;t++){let n=r?{x:f.x,y:f.y+t}:{x:f.x+t,y:f.y};if(!ot(e.grid,n))return null}let g={box:i,anchor:l,gutter:d,edges:new Set(c)};for(let t of c)if(!At(e,g,t))return null;return g}function Pt(e,t,n){let r=e.config.graphDirection!==`LR`,i=n.parallelLane.index;if(i>2)return null;let a=Z(n.to),o=t.gutter,s=r?a.x+1:a.y+1,c=r?o.x:o.y,l={lane:(r?a.x:a.y)+3,attach:(r?a.x:a.y)+2,dir:r?J:K},u={lane:(r?a.x:a.y)-1,attach:r?a.x:a.y,dir:r?Y:q},d=(c>=s?[l,u]:[u,l])[i-1];if(!(r?e.columnWidth:e.rowHeight).has(d.lane)||d.lane<0||d.lane===c)return null;let f=yt(r?[o,{x:d.lane,y:o.y},{x:d.lane,y:a.y+1},{x:d.attach,y:a.y+1}]:[o,{x:o.x,y:d.lane},{x:a.x+1,y:d.lane},{x:a.x+1,y:d.attach}]);if(!xr(e,ut(f),[n.from,n.to]))return null;let p=r?[{x:d.lane,y:o.y},{x:d.lane,y:a.y}]:[{x:o.x,y:d.lane},{x:a.x,y:d.lane}];return{path:f,endDir:d.dir,labelSegment:p}}var Ft=64;function It(e){let t=e.clusterExitPlans;if(!t)return!1;let n=e.config.graphDirection!==`LR`,r=!1,i=[...t].sort(([e],[t])=>Lt(e)-Lt(t));for(let[t,a]of i){let i=n?t.maxY:t.maxX,o=n?e.rowHeight:e.columnWidth,s=n?a.gutter.y:a.gutter.x;for(let t=0;t<Ft&&!Rt(e,a,i,n);t++)o.set(s,(o.get(s)??0)+1),r=!0}return r}function Lt(e){let t=0;for(let n=e.parent;n;n=n.parent)t++;return t}function Rt(e,t,n,r){let i=e=>r?e.y:e.x;if(i($(e,t.gutter))<=n)return!1;for(let a of t.edges){let t=Jr(e,a),o=a.path[a.path.length-1];if(!t||!o)continue;let s=i($(e,o))-1;for(let{x:e,y:i,text:a}of t){let t=r?i:e,o=r?i:e+P(a)-1;if(t<=n||o>=s)return!1}}return!0}function Q(e){return e.split(`
|
|
4
|
-
`)}function zt(e){let t=Q(e);return Math.max(...t.map(e=>P(e)),0)}function Bt(e){return Q(e).length}function Vt(e,t){let n=e.maxX-e.minX,r=e.maxY-e.minY;if(n<=0||r<=0)return F(0,0);let i={x:0,y:0},a={x:n,y:r},o=F(n,r);if(t.config.useAscii){for(let e=i.x+1;e<a.x;e++)z(o,e,i.y,`-`);for(let e=i.x+1;e<a.x;e++)z(o,e,a.y,`-`);for(let e=i.y+1;e<a.y;e++)z(o,i.x,e,`|`);for(let e=i.y+1;e<a.y;e++)z(o,a.x,e,`|`);z(o,i.x,i.y,`+`),z(o,a.x,i.y,`+`),z(o,i.x,a.y,`+`),z(o,a.x,a.y,`+`)}else{for(let e=i.x+1;e<a.x;e++)z(o,e,i.y,`─`);for(let e=i.x+1;e<a.x;e++)z(o,e,a.y,`─`);for(let e=i.y+1;e<a.y;e++)z(o,i.x,e,`│`);for(let e=i.y+1;e<a.y;e++)z(o,a.x,e,`│`);z(o,i.x,i.y,`┌`),z(o,a.x,i.y,`┐`),z(o,i.x,a.y,`└`),z(o,a.x,a.y,`┘`)}return o}var Ht=new Set([`│`,`┃`,`║`,`┆`,`┊`,`|`,`:`,`‖`,`┼`,`╋`]);function Ut(e){return e!==void 0&&Ht.has(e)}function Wt(e,t,n,r){let i=(t,i,a)=>{for(let o=t-a;o<t+e.length+a;o++){if(o<1||o>=n||!r(o))continue;let a=e[o-t];if(!(i&&a===` `))return!0}return!1};if(!i(t,!1,0))return t;let a=Math.max(1,n-e.length);for(let[e,n]of[[!1,1],[!1,0],[!0,0]]){let r=-1;for(let o=1;o<=a;o++)i(o,e,n)||(r<0||Math.abs(o-t)<Math.abs(r-t))&&(r=o);if(r>=0)return r}return null}function Gt(e,t){return Math.max(1,1+Math.ceil((t-1-e.length)/2))}function Kt(e,t,n){let r=ie(e);return Wt(r,Gt(r,t),t,n)!==null}function qt(e,t,n=()=>!1){let r=e.maxX-e.minX,i=e.maxY-e.minY;if(r<=0||i<=0)return[F(0,0),{x:0,y:0},[]];let a=F(r,i),o=[],s=Q(e.name);for(let e=0;e<s.length;e++){let t=s[e],c=1+e,l=1+Math.ceil((r-1-P(t))/2);l<1&&(l=1);let u=ie(t);l=Wt(u,l,r,e=>n(e,c))??l;for(let e=0;e<u.length;e++)l+e>=r||c>=i||u[e]===` `&&n(l+e,c)||(z(a,l+e,c,u[e]),o.push({x:l+e,y:c}))}return[a,{x:e.minX,y:e.minY},o]}var Jt=16;function Yt(e,t){return e.nodes.includes(t.from)||e.nodes.includes(t.to)}function Xt(e,t){let n=[],r=!1;for(let i of e.subgraphs){if(Yt(i,t))continue;let a=kt(i);if(a){r=!0;for(let t=a.minX;t<=a.maxX;t++)for(let r=a.minY;r<=a.maxY;r++){let i=et({x:t,y:r});e.grid.has(i)||(e.grid.add(i),n.push(i))}}}return{added:n,engaged:r}}function Zt(e,t){for(let n of t)e.grid.delete(n)}function Qt(e,t,n){if(t.size===0)return!1;let r=e.config.graphDirection!==`LR`,i=r?e.columnWidth:e.rowHeight,a=!1;for(let o=0;o<Jt;o++){let o=$t(e,t,r);if(!o)break;i.set(o.index,(i.get(o.index)??0)+o.by),a=!0,n()}return a}function $t(e,t,n){for(let r of t){let t=r.path;for(let r=1;r<t.length;r++){let i=t[r-1],a=t[r];if(n?i.x!==a.x||i.y===a.y:i.y!==a.y||i.x===a.x)continue;let o=n?i.x:i.y,s=n?$(e,i).x:$(e,i).y,c=n?$(e,i).y:$(e,i).x,l=n?$(e,a).y:$(e,a).x,u=Math.min(c,l),d=Math.max(c,l);for(let t of e.subgraphs){let e=kt(t);if(!e)continue;let r=n?t.minX:t.minY,i=n?t.maxX:t.maxY,a=n?t.minY:t.minX,c=n?t.maxY:t.maxX;if(d<=a||u>=c)continue;let l=n?e.minX:e.minY,f=n?e.maxX:e.maxY;if(o>f){if(s>i)continue;let e=i+1-s;return o-1>f?{index:o-1,by:e}:{index:o,by:2*e}}if(o<l){if(s<r)continue;let e=s+1-r;return o+1<l?{index:o+1,by:e}:{index:o,by:2*e}}}}}return null}var en=4;function tn(e,t){if(e.config.graphDirection===`LR`)return;let n=new Set;for(let r=0;r<e.subgraphs.length;r++){let r=e.subgraphs.find(t=>t.nodes.length>0&&!n.has(t)&&nn(e,t));if(!r)return;for(let i=1;i<=en&&(r.titleRoom=i,t(),nn(e,r));i++)i===en&&(r.titleRoom=0,t(),n.add(r))}}function nn(e,t){let n=t.maxX-t.minX,r=Q(t.name);for(let i=0;i<r.length;i++){let a=t.minY+1+i,o=new Set;for(let n of e.edges)for(let r=1;r<n.path.length;r++){let i=n.path[r-1],s=n.path[r];if(i.x!==s.x||i.y===s.y)continue;let c=$(e,i),l=$(e,s);a<Math.min(c.y,l.y)||a>Math.max(c.y,l.y)||c.x>t.minX&&c.x<t.maxX&&o.add(c.x-t.minX)}if(o.size!==0&&!Kt(r[i],n,e=>o.has(e)))return!0}return!1}function rn(){return new Map}function an(e,t,n,r){for(let i of ut(n)){if(at(e,i))continue;let n=t.get(et(i));if(n!==void 0&&n!==r)return i}return null}function on(e,t,n,r){for(let i of ut(n)){if(at(e,i))continue;let n=et(i);t.has(n)||t.set(n,r)}}function sn(){return new Map}function cn(e,t){return e.to===t.from||t.to===e.from}var ln=2;function un(e,t){return t(e)?1:ln}function dn(e,t,n,r,i=()=>!1){let a=new Map,o=new Map;for(let i of ut(n)){if(at(e,i))continue;let n=t.get(et(i));if(n!==void 0)for(let e of n)e!==r&&cn(e,r)&&(a.set(e,(a.get(e)??0)+1),o.has(e)||o.set(e,i))}for(let[e,t]of a)if(t>=un(e,i))return o.get(e);return null}function fn(e,t,n,r){for(let i of ut(n)){if(at(e,i))continue;let n=et(i),a=t.get(n);a===void 0&&(a=new Set,t.set(n,a)),a.add(r)}}var pn={rectangle:{unicode:{tl:`┌`,tr:`┐`,bl:`└`,br:`┘`},ascii:{tl:`+`,tr:`+`,bl:`+`,br:`+`}},rounded:{unicode:{tl:`╭`,tr:`╮`,bl:`╰`,br:`╯`},ascii:{tl:`.`,tr:`.`,bl:`'`,br:`'`}},circle:{unicode:{tl:`◯`,tr:`◯`,bl:`◯`,br:`◯`},ascii:{tl:`o`,tr:`o`,bl:`o`,br:`o`}},doublecircle:{unicode:{tl:`◎`,tr:`◎`,bl:`◎`,br:`◎`},ascii:{tl:`@`,tr:`@`,bl:`@`,br:`@`}},diamond:{unicode:{tl:`◇`,tr:`◇`,bl:`◇`,br:`◇`},ascii:{tl:`<`,tr:`>`,bl:`<`,br:`>`}},hexagon:{unicode:{tl:`⌜`,tr:`⌝`,bl:`⌞`,br:`⌟`},ascii:{tl:`*`,tr:`*`,bl:`*`,br:`*`}},stadium:{unicode:{tl:`(`,tr:`)`,bl:`(`,br:`)`},ascii:{tl:`(`,tr:`)`,bl:`(`,br:`)`}},subroutine:{unicode:{tl:`╟`,tr:`╢`,bl:`╟`,br:`╢`},ascii:{tl:`|`,tr:`|`,bl:`|`,br:`|`}},cylinder:{unicode:{tl:`╭`,tr:`╮`,bl:`╰`,br:`╯`},ascii:{tl:`.`,tr:`.`,bl:`'`,br:`'`}},asymmetric:{unicode:{tl:`▷`,tr:`┐`,bl:`▷`,br:`┘`},ascii:{tl:`>`,tr:`+`,bl:`>`,br:`+`}},trapezoid:{unicode:{tl:`/`,tr:`\\`,bl:`└`,br:`┘`},ascii:{tl:`/`,tr:`\\`,bl:`+`,br:`+`}},"trapezoid-alt":{unicode:{tl:`┌`,tr:`┐`,bl:`\\`,br:`/`},ascii:{tl:`+`,tr:`+`,bl:`\\`,br:`/`}},parallelogram:{unicode:{tl:`/`,tr:`/`,bl:`/`,br:`/`},ascii:{tl:`/`,tr:`/`,bl:`/`,br:`/`}},"parallelogram-alt":{unicode:{tl:`\\`,tr:`\\`,bl:`\\`,br:`\\`},ascii:{tl:`\\`,tr:`\\`,bl:`\\`,br:`\\`}},"state-start":{unicode:{tl:`●`,tr:`●`,bl:`●`,br:`●`},ascii:{tl:`*`,tr:`*`,bl:`*`,br:`*`}},"state-end":{unicode:{tl:`◉`,tr:`◉`,bl:`◉`,br:`◉`},ascii:{tl:`@`,tr:`@`,bl:`@`,br:`@`}},document:{unicode:{tl:`┌`,tr:`┐`,bl:`╰`,br:`╮`},ascii:{tl:`+`,tr:`+`,bl:`'`,br:`~`}},"stacked-document":{unicode:{tl:`┌`,tr:`╗`,bl:`╰`,br:`╮`},ascii:{tl:`+`,tr:`#`,bl:`'`,br:`~`}},"stacked-process":{unicode:{tl:`┌`,tr:`╗`,bl:`└`,br:`╝`},ascii:{tl:`+`,tr:`#`,bl:`+`,br:`#`}},card:{unicode:{tl:`╱`,tr:`┐`,bl:`└`,br:`┘`},ascii:{tl:`/`,tr:`+`,bl:`+`,br:`+`}},"lined-process":{unicode:{tl:`┌`,tr:`┐`,bl:`└`,br:`┘`},ascii:{tl:`+`,tr:`+`,bl:`+`,br:`+`}},"divided-process":{unicode:{tl:`┌`,tr:`┐`,bl:`└`,br:`┘`},ascii:{tl:`+`,tr:`+`,bl:`+`,br:`+`}},"window-pane":{unicode:{tl:`┌`,tr:`┐`,bl:`└`,br:`┘`},ascii:{tl:`+`,tr:`+`,bl:`+`,br:`+`}},triangle:{unicode:{tl:`╱`,tr:`╲`,bl:`└`,br:`┘`},ascii:{tl:`/`,tr:`\\`,bl:`+`,br:`+`}},"flipped-triangle":{unicode:{tl:`┌`,tr:`┐`,bl:`╲`,br:`╱`},ascii:{tl:`+`,tr:`+`,bl:`\\`,br:`/`}},"filled-circle":{unicode:{tl:`●`,tr:`●`,bl:`●`,br:`●`},ascii:{tl:`*`,tr:`*`,bl:`*`,br:`*`}},"crossed-circle":{unicode:{tl:`╳`,tr:`╳`,bl:`╳`,br:`╳`},ascii:{tl:`X`,tr:`X`,bl:`X`,br:`X`}},"fork-join":{unicode:{tl:`━`,tr:`━`,bl:`━`,br:`━`},ascii:{tl:`=`,tr:`=`,bl:`=`,br:`=`}},"notched-pentagon":{unicode:{tl:`╱`,tr:`╲`,bl:`└`,br:`┘`},ascii:{tl:`/`,tr:`\\`,bl:`+`,br:`+`}},"sloped-rectangle":{unicode:{tl:`╱`,tr:`┐`,bl:`└`,br:`┘`},ascii:{tl:`/`,tr:`+`,bl:`+`,br:`+`}},flag:{unicode:{tl:`╭`,tr:`╮`,bl:`╰`,br:`╯`},ascii:{tl:`~`,tr:`~`,bl:`~`,br:`~`}},"bow-tie-rectangle":{unicode:{tl:`╲`,tr:`╱`,bl:`╱`,br:`╲`},ascii:{tl:`\\`,tr:`/`,bl:`/`,br:`\\`}},"half-rounded-rectangle":{unicode:{tl:`┌`,tr:`╮`,bl:`└`,br:`╯`},ascii:{tl:`+`,tr:`.`,bl:`+`,br:`'`}},brace:{unicode:{tl:`╭`,tr:`┐`,bl:`╰`,br:`┘`},ascii:{tl:`{`,tr:`+`,bl:`{`,br:`+`}},"brace-right":{unicode:{tl:`┌`,tr:`╮`,bl:`└`,br:`╯`},ascii:{tl:`+`,tr:`}`,bl:`+`,br:`}`}},braces:{unicode:{tl:`╭`,tr:`╮`,bl:`╰`,br:`╯`},ascii:{tl:`{`,tr:`}`,bl:`{`,br:`}`}},bolt:{unicode:{tl:`╱`,tr:`╲`,bl:`╲`,br:`╱`},ascii:{tl:`/`,tr:`\\`,bl:`\\`,br:`/`}},text:{unicode:{tl:` `,tr:` `,bl:` `,br:` `},ascii:{tl:` `,tr:` `,bl:` `,br:` `}},anchor:{unicode:{tl:` `,tr:` `,bl:` `,br:` `},ascii:{tl:` `,tr:` `,bl:` `,br:` `}}};function mn(e,t){let n=pn[e]??pn.rectangle;return t?n.ascii:n.unicode}function hn(e,t){let n=Q(e),r=Math.max(...n.map(e=>P(e)),0),i=n.length,a=2*t.padding+r,o=a+2,s=i+2*t.padding,c=s%2==0?s+1:s;return{width:o,height:c+2,labelArea:{x:1+t.padding,y:1+t.padding,width:r,height:i},gridColumns:[1,a,1],gridRows:[1,c,1]}}function gn(e,t,n,r){let{width:i,height:a}=t,o=F(i-1,a-1),s={x:0,y:0},c={x:i-1,y:a-1},l=r?`-`:`─`,u=r?`|`:`│`;for(let e=s.x+1;e<c.x;e++)o[e][s.y]=l,o[e][c.y]=l;for(let e=s.y+1;e<c.y;e++)o[s.x][e]=u,o[c.x][e]=u;o[s.x][s.y]=n.tl,o[c.x][s.y]=n.tr,o[s.x][c.y]=n.bl,o[c.x][c.y]=n.br;let d=Q(e),f=i-1,p=a-1,m=Math.floor(p/2)-Math.floor((d.length-1)/2);for(let e=0;e<d.length;e++){let t=d[e],n=Math.floor(f/2)-Math.ceil(P(t)/2)+1,r=ie(t);for(let t=0;t<r.length;t++){let i=n+t,a=m+e;i>=0&&i<o.length&&a>=0&&a<o[0].length&&(o[i][a]=r[t])}}return o}function _n(e,t,n){let{width:r,height:i}=t,a=n.x+Math.floor(r/2),o=n.y+Math.floor(i/2);return X(e,K)?{x:a,y:n.y}:X(e,q)?{x:a,y:n.y+i-1}:X(e,J)?{x:n.x,y:o}:X(e,Y)?{x:n.x+r-1,y:o}:X(e,We)?{x:n.x,y:n.y}:X(e,Ue)?{x:n.x+r-1,y:n.y}:X(e,Ke)?{x:n.x,y:n.y+i-1}:X(e,Ge)?{x:n.x+r-1,y:n.y+i-1}:{x:a,y:o}}var vn={getDimensions:hn,render(e,t,n){return gn(e,t,mn(`rectangle`,n.useAscii),n.useAscii)},getAttachmentPoint:_n},yn={getDimensions(e,t){return{width:5,height:3,labelArea:{x:2,y:1,width:1,height:1},gridColumns:[1,3,1],gridRows:[1,1,1]}},render(e,t,n){let{width:r,height:i}=t,a=F(r-1,i-1),o=Math.floor(r/2);return n.useAscii?(a[0][0]=`.`,a[1][0]=`-`,a[2][0]=`-`,a[3][0]=`-`,a[4][0]=`.`,a[0][1]=`|`,a[o][1]=`*`,a[4][1]=`|`,a[0][2]=`'`,a[1][2]=`-`,a[2][2]=`-`,a[3][2]=`-`,a[4][2]=`'`):(a[0][0]=`╭`,a[1][0]=`─`,a[2][0]=`─`,a[3][0]=`─`,a[4][0]=`╮`,a[0][1]=`│`,a[o][1]=`●`,a[4][1]=`│`,a[0][2]=`╰`,a[1][2]=`─`,a[2][2]=`─`,a[3][2]=`─`,a[4][2]=`╯`),a},getAttachmentPoint(e,t,n){let{width:r,height:i}=t,a=n.x+Math.floor(r/2),o=n.y+Math.floor(i/2);return X(e,K)?{x:a,y:n.y}:X(e,q)?{x:a,y:n.y+i-1}:X(e,J)?{x:n.x,y:o}:X(e,Y)?{x:n.x+r-1,y:o}:{x:a,y:o}}},bn={getDimensions(e,t){return{width:5,height:3,labelArea:{x:2,y:1,width:1,height:1},gridColumns:[1,3,1],gridRows:[1,1,1]}},render(e,t,n){let{width:r,height:i}=t,a=F(r-1,i-1),o=Math.floor(r/2);return n.useAscii?(a[0][0]=`#`,a[1][0]=`=`,a[2][0]=`=`,a[3][0]=`=`,a[4][0]=`#`,a[0][1]=`#`,a[o][1]=`*`,a[4][1]=`#`,a[0][2]=`#`,a[1][2]=`=`,a[2][2]=`=`,a[3][2]=`=`,a[4][2]=`#`):(a[0][0]=`╔`,a[1][0]=`═`,a[2][0]=`═`,a[3][0]=`═`,a[4][0]=`╗`,a[0][1]=`║`,a[o][1]=`◎`,a[4][1]=`║`,a[0][2]=`╚`,a[1][2]=`═`,a[2][2]=`═`,a[3][2]=`═`,a[4][2]=`╝`),a},getAttachmentPoint(e,t,n){let{width:r,height:i}=t,a=n.x+Math.floor(r/2),o=n.y+Math.floor(i/2);return X(e,K)?{x:a,y:n.y}:X(e,q)?{x:a,y:n.y+i-1}:X(e,J)?{x:n.x,y:o}:X(e,Y)?{x:n.x+r-1,y:o}:{x:a,y:o}}},xn={getDimensions(e,t){let n=Q(e),r=Math.max(...n.map(e=>P(e)),0),i=n.length,a=2*t.padding+r,o=a+4,s=i+2*t.padding;return{width:o,height:Math.max(s+2,3),labelArea:{x:2+t.padding,y:1+t.padding,width:r,height:i},gridColumns:[2,a,2],gridRows:[1,s,1]}},render(e,t,n){let{width:r,height:i}=t,a=F(r-1,i-1),o=Math.floor(i/2),s=n.useAscii?`-`:`─`;if(i===3)a[0][o]=`(`,a[r-1][o]=`)`;else if(n.useAscii){for(let e=0;e<i;e++)a[0][e]=`(`,a[r-1][e]=`)`;for(let e=1;e<r-1;e++)a[e][0]=s,a[e][i-1]=s}else{a[0][0]=`╭`;for(let e=1;e<r-1;e++)a[e][0]=s;a[r-1][0]=`╮`;for(let e=1;e<i-1;e++)a[0][e]=`│`,a[r-1][e]=`│`;a[0][i-1]=`╰`;for(let e=1;e<r-1;e++)a[e][i-1]=s;a[r-1][i-1]=`╯`}let c=Q(e),l=o-Math.floor((c.length-1)/2);for(let e=0;e<c.length;e++){let t=c[e],n=Math.floor(r/2)-Math.floor(P(t)/2),o=ie(t);for(let t=0;t<o.length;t++){let s=n+t,c=l+e;s>0&&s<r-1&&c>=0&&c<i&&(a[s][c]=o[t])}}return a},getAttachmentPoint:_n},Sn={getDimensions(e,t){let n=Q(e),r=Math.max(...n.map(e=>P(e)),0),i=n.length,a=2*t.padding+r,o=a+4,s=i+2*t.padding;return{width:o,height:s+2,labelArea:{x:2+t.padding,y:1+t.padding,width:r,height:i},gridColumns:[2,a,2],gridRows:[1,s,1]}},render(e,t,n){let{width:r,height:i}=t,a=F(r-1,i-1),o=n.useAscii?`-`:`─`,s=n.useAscii?`|`:`│`;a[0][0]=n.useAscii?`+`:`┌`,a[1][0]=n.useAscii?`+`:`┬`;for(let e=2;e<r-2;e++)a[e][0]=o;a[r-2][0]=n.useAscii?`+`:`┬`,a[r-1][0]=n.useAscii?`+`:`┐`;for(let e=1;e<i-1;e++)a[0][e]=s,a[1][e]=s,a[r-2][e]=s,a[r-1][e]=s;a[0][i-1]=n.useAscii?`+`:`└`,a[1][i-1]=n.useAscii?`+`:`┴`;for(let e=2;e<r-2;e++)a[e][i-1]=o;a[r-2][i-1]=n.useAscii?`+`:`┴`,a[r-1][i-1]=n.useAscii?`+`:`┘`;let c=Q(e),l=Math.floor(i/2)-Math.floor((c.length-1)/2);for(let e=0;e<c.length;e++){let t=c[e],n=Math.floor(r/2)-Math.floor(P(t)/2),o=ie(t);for(let t=0;t<o.length;t++){let s=n+t,c=l+e;s>1&&s<r-2&&c>0&&c<i-1&&(a[s][c]=o[t])}}return a},getAttachmentPoint:_n},Cn={getDimensions:hn,render(e,t,n){return gn(e,t,mn(`doublecircle`,n.useAscii),n.useAscii)},getAttachmentPoint:_n},wn={getDimensions(e,t){let n=Q(e),r=Math.max(...n.map(e=>P(e)),0),i=n.length,a=2*t.padding+r,o=a+2,s=i+2*t.padding+2;return{width:o,height:s+2,labelArea:{x:1+t.padding,y:2+t.padding,width:r,height:i},gridColumns:[1,a,1],gridRows:[2,s-2,2]}},render(e,t,n){let{width:r,height:i}=t,a=F(r-1,i-1),o=n.useAscii?`-`:`─`,s=n.useAscii?`|`:`│`;a[0][0]=n.useAscii?`.`:`╭`;for(let e=1;e<r-1;e++)a[e][0]=o;a[r-1][0]=n.useAscii?`.`:`╮`,a[0][1]=s;for(let e=1;e<r-1;e++)a[e][1]=o;a[r-1][1]=s;for(let e=2;e<i-2;e++)a[0][e]=s,a[r-1][e]=s;a[0][i-2]=s;for(let e=1;e<r-1;e++)a[e][i-2]=o;a[r-1][i-2]=s,a[0][i-1]=n.useAscii?`'`:`╰`;for(let e=1;e<r-1;e++)a[e][i-1]=o;a[r-1][i-1]=n.useAscii?`'`:`╯`;let c=Q(e),l=Math.floor(i/2)-Math.floor((c.length-1)/2);for(let e=0;e<c.length;e++){let t=c[e],n=Math.floor(r/2)-Math.floor(P(t)/2),o=ie(t);for(let t=0;t<o.length;t++){let s=n+t,c=l+e;s>0&&s<r-1&&c>1&&c<i-2&&(a[s][c]=o[t])}}return a},getAttachmentPoint:_n},Tn={getDimensions:hn,render(e,t,n){return gn(e,t,mn(`asymmetric`,n.useAscii),n.useAscii)},getAttachmentPoint:_n},En={getDimensions:hn,render(e,t,n){return gn(e,t,mn(`trapezoid`,n.useAscii),n.useAscii)},getAttachmentPoint:_n},Dn={getDimensions:hn,render(e,t,n){return gn(e,t,mn(`trapezoid-alt`,n.useAscii),n.useAscii)},getAttachmentPoint:_n},On={getDimensions:hn,render(e,t,n){return gn(e,t,mn(`parallelogram`,n.useAscii),n.useAscii)},getAttachmentPoint:_n},kn={getDimensions:hn,render(e,t,n){return gn(e,t,mn(`parallelogram-alt`,n.useAscii),n.useAscii)},getAttachmentPoint:_n};function An(e){return{getDimensions:hn,render(t,n,r){return gn(t,n,mn(e,r.useAscii),r.useAscii)},getAttachmentPoint:_n}}var jn=An(`circle`),Mn=An(`diamond`),Nn=An(`rounded`),Pn=An(`hexagon`),Fn=new Map([[`rectangle`,vn],[`rounded`,Nn],[`diamond`,Mn],[`stadium`,xn],[`circle`,jn],[`subroutine`,Sn],[`doublecircle`,Cn],[`hexagon`,Pn],[`cylinder`,wn],[`asymmetric`,Tn],[`trapezoid`,En],[`trapezoid-alt`,Dn],[`parallelogram`,On],[`parallelogram-alt`,kn],[`state-start`,yn],[`state-end`,bn]]);function In(e){return Fn.get(e)??vn}function Ln(e,t,n){return In(e).getDimensions(t,n)}function Rn(e,t,n,r){return In(e).getAttachmentPoint(t,n,r)}function $(e,t,n){let r=n?{x:t.x+n.x,y:t.y+n.y}:t,i=0;for(let t=0;t<r.x;t++)i+=e.columnWidth.get(t)??0;let a=0;for(let t=0;t<r.y;t++)a+=e.rowHeight.get(t)??0;let o=e.columnWidth.get(r.x)??0,s=e.rowHeight.get(r.y)??0;return{x:i+Math.floor(o/2)+e.offsetX,y:a+Math.floor(s/2)+e.offsetY}}function zn(e,t){return t.map(t=>$(e,t))}function Bn(e,t,n,r){let i=r??Zn(e,t);return st(e.grid,n,3)?(ct(e.grid,n,3),t.gridCoord=n,n):i===`LR`?Bn(e,t,{x:n.x,y:n.y+4},i):Bn(e,t,{x:n.x+4,y:n.y},i)}function Vn(e,t){let n=Z(t),r=e.config.boxBorderPadding,i=Ln(t.shape,t.displayLabel,{useAscii:e.config.useAscii,padding:r}),a=i.gridColumns,o=i.gridRows;for(let t=0;t<a.length;t++){let r=n.x+t,i=e.columnWidth.get(r)??0;e.columnWidth.set(r,Math.max(i,a[t]))}for(let t=0;t<o.length;t++){let r=n.y+t,i=e.rowHeight.get(r)??0;e.rowHeight.set(r,Math.max(i,o[t]))}if(n.x>0){let t=e.columnWidth.get(n.x-1)??0;e.columnWidth.set(n.x-1,Math.max(t,e.config.paddingX))}if(n.y>0){let r=e.config.paddingY;Qn(e,t)&&(r+=4);let i=e.rowHeight.get(n.y-1)??0;e.rowHeight.set(n.y-1,Math.max(i,r))}}function Hn(e,t){for(let n of t)e.columnWidth.has(n.x)||e.columnWidth.set(n.x,Math.floor(e.config.paddingX/2)),e.rowHeight.has(n.y)||e.rowHeight.set(n.y,Math.floor(e.config.paddingY/2))}var Un=8;function Wn(e,t,n,r,i){let a=[],o=(e.clusterExitPlans?.size??0)>0;o&&(e.preferStraightRoutes=!0);try{for(let o=0;o<Un;o++){let o=an(i,n,t.path,t.style)??dn(i,r,t.path,t,t=>t.clusterSource!==void 0&&e.clusterExitPlans?.get(t.clusterSource)?.edges.has(t)===!0);if(!o)return;e.grid.add(et(o)),a.push(o),Dr(e,t)}}finally{o&&(e.preferStraightRoutes=!1);for(let t of a)e.grid.delete(et(t))}}function Gn(e,t){return e.subgraphs.some(e=>e.nodes.includes(t))}function Kn(e,t){let n=null;for(let r of e.subgraphs)r.nodes.includes(t)&&(!n||qn(n,r))&&(n=r);return n}function qn(e,t){let n=t;for(;n!==null;){if(n===e)return!0;n=n.parent}return!1}function Jn(e,t){let n=Kn(e,t);if(!n)return null;let r=n;for(;r.parent;)r=r.parent;return r}function Yn(e){let t=[...e.nodes];for(let n of e.children)t.push(...Yn(n));return t}function Xn(e,t,n){if(t===n)return!0;let r=new Set([t]),i=[t];for(;i.length>0;){let t=i.shift();for(let a of pr(e,t)){if(a===n)return!0;r.has(a)||(r.add(a),i.push(a))}}return!1}function Zn(e,t){let n=Kn(e,t);return n?.direction?n.direction:e.config.graphDirection}function Qn(e,t){let n=Kn(e,t);if(!n)return!1;let r=!1;for(let i of e.edges)if(i.to===t&&Kn(e,i.from)!==n){r=!0;break}if(!r)return!1;let i=Z(t).y;for(let r of n.nodes){if(r===t||!r.gridCoord)continue;let a=!1;for(let t of e.edges)if(t.to===r&&Kn(e,t.from)!==n){a=!0;break}if(a&&r.gridCoord.y<i)return!1}return!0}function $n(e,t){if(t.nodes.length===0)return;let n=1e6,r=1e6,i=-1e6,a=-1e6;for(let o of t.children)$n(e,o),o.nodes.length>0&&(n=Math.min(n,o.minX),r=Math.min(r,o.minY),i=Math.max(i,o.maxX),a=Math.max(a,o.maxY));for(let e of t.nodes){if(!e.drawingCoord||!e.drawing)continue;let t=e.drawingCoord.x,o=e.drawingCoord.y,s=t+e.drawing.length-1,c=o+e.drawing[0].length-1;n=Math.min(n,t),r=Math.min(r,o),i=Math.max(i,s),a=Math.max(a,c)}t.minX=n-2,t.minY=r-2-2,t.maxX=i+2,t.maxY=a+2;let o=Math.max(0,...Q(t.name).map(e=>P(e))),s=t.maxX-t.minX,c=o+1;if(c>s){let e=c-s,n=Math.floor(e/2),r=e-n;t.minX-=n,t.maxX+=r}let l=t.titleRoom??0;l>0&&(t.minX-=Math.floor(l/2),t.maxX+=l-Math.floor(l/2))}function er(e){let t=e.subgraphs.filter(e=>e.parent===null&&e.nodes.length>0);for(let e=0;e<t.length;e++)for(let n=e+1;n<t.length;n++){let r=t[e],i=t[n];r.minX<i.maxX&&r.maxX>i.minX&&(r.maxY>=i.minY-1&&r.minY<i.minY?i.minY=r.maxY+1+1:i.maxY>=r.minY-1&&i.minY<r.minY&&(r.minY=i.maxY+1+1)),r.minY<i.maxY&&r.maxY>i.minY&&(r.maxX>=i.minX-1&&r.minX<i.minX?i.minX=r.maxX+1+1:i.maxX>=r.minX-1&&i.minX<r.minX&&(r.minX=i.maxX+1+1))}}function tr(e){for(let t of e.subgraphs)$n(e,t);er(e)}function nr(e){if(e.subgraphs.length===0)return;let t=0,n=0;for(let r of e.subgraphs)t=Math.min(t,r.minX),n=Math.min(n,r.minY);let r=-t,i=-n;if(r!==0||i!==0){e.offsetX=r,e.offsetY=i;for(let t of e.subgraphs)t.minX+=r,t.minY+=i,t.maxX+=r,t.maxY+=i;for(let t of e.nodes)t.drawingCoord&&(t.drawingCoord.x+=r,t.drawingCoord.y+=i)}}function rr(e,t){let n=t=>{let n=pr(e,t);return n.length>0?n[0].name:null},r=new Map,i=t.map((e,t)=>{let i=n(e);if(i===null)return t;let a=r.get(i);return a===void 0?(r.set(i,t),t):a});return t.map((e,t)=>({node:e,i:t})).sort((e,t)=>i[e.i]-i[t.i]||e.i-t.i).map(({node:e})=>e)}function ir(e,t){let n=new Set,r=t=>{let r=[t];for(;r.length>0;){let t=r.shift();if(!n.has(t.name)){n.add(t.name);for(let n of pr(e,t))r.push(n)}}};for(let e of t)r(e);let i=[...t];for(let t of e.nodes)n.has(t.name)||(i.push(t),r(t));return i}function ar(e,t){return e.nodes.find(e=>e.gridCoord!==null&&e.gridCoord.x===t.x&&e.gridCoord.y===t.y)}function or(e,t,n,r,i){let a=r===`x`?`y`:`x`,o=i=>{let o=4;for(;;){let s=t[r]+i*o;if(s<0)return null;let c=r===`x`?{x:s,y:t[a]}:{x:t[a],y:s};if(st(e.grid,c,3))return c;let l=ar(e,c);if(!l||Jn(e,l)!==n)return null;o+=4}};return o(i)??o(i*-1)}function sr(e,t,n,r){let i=Jn(e,t);if(!i)return;let a=n.get(i);if(!a)return;n.delete(i);let o=Z(t),s=Zn(e,t)===`LR`?`y`:`x`;for(let t of a){let n=Zn(e,t),a=or(e,o,i,s,1);Bn(e,e.nodes[t.index],a??o,n),r.add(t)}}function cr(e,t){let n=!0;for(;n;){n=!1;let r=t=>e.config.graphDirection===`LR`?t.gridCoord?.y??0:t.gridCoord?.x??0,i=e.nodes.filter(e=>e.gridCoord!==null).sort((e,t)=>r(e)-r(t));for(let r of i){let i=r.gridCoord;if(i===null)continue;let a=[...pr(e,r)].sort((t,n)=>fr(e,t,n));for(let o of a){if(o.gridCoord!==null)continue;let a=Kn(e,r),s=Kn(e,o),c=a&&a===s&&a.direction?a.direction:e.config.graphDirection,l=c===`LR`?i.x+4:i.y+4,u;u=c===e.config.graphDirection?t[l]??0:c===`LR`?i.y:i.x;let d=c===`LR`?{x:l,y:u}:{x:u,y:l};Bn(e,e.nodes[o.index],d,c),c===e.config.graphDirection&&(t[l]=u+4),n=!0}}}}function lr(e){let t=e.config.graphDirection,n=[],r=new Set;for(let t of e.edges)t.from!==t.to&&r.add(t.to.name);let i=ir(e,e.nodes.filter(e=>!r.has(e.name))),a=new Set(i),o=[],s=[];for(let t of i){let n=Jn(e,t);n!==null&&Yn(n).some(n=>n!==t&&!a.has(n)&&!Xn(e,t,n))?o.push(t):s.push(t)}let c=rr(e,s),l=new Map;for(let t of o){let n=Jn(e,t),r=l.get(n);r?r.push(t):l.set(n,[t])}let u=new Set,d=!1,f=!1;for(let t of c)Gn(e,t)?pr(e,t).length>0&&(f=!0):d=!0;let p=t===`LR`&&d&&f,m,h=[];p?(m=c.filter(t=>!Gn(e,t)),h=c.filter(t=>Gn(e,t))):m=c;for(let r of m){let i=t===`LR`?{x:0,y:n[0]??0}:{x:n[0]??0,y:0};Bn(e,e.nodes[r.index],i),n[0]=(n[0]??0)+4,sr(e,r,l,u)}if(p&&h.length>0)for(let r of h){let i=t===`LR`?{x:4,y:n[4]??0}:{x:n[4]??0,y:4};Bn(e,e.nodes[r.index],i),n[4]=(n[4]??0)+4,sr(e,r,l,u)}cr(e,n);for(let t of o){if(u.has(t))continue;let r=Jn(e,t),i=Zn(e,t),a=Yn(r).filter(e=>e!==t&&e.gridCoord!==null),o;if(a.length>0){let t=a[0];for(let e of a)(i===`LR`?e.gridCoord.x<t.gridCoord.x:e.gridCoord.y<t.gridCoord.y)&&(t=e);let n=i===`LR`?`y`:`x`;o=or(e,t.gridCoord,r,n,1)??{x:t.gridCoord.x,y:t.gridCoord.y}}else o=i===`LR`?{x:0,y:n[0]??0}:{x:n[0]??0,y:0},n[0]=(n[0]??0)+4;Bn(e,e.nodes[t.index],o,i)}cr(e,n);for(let t of e.nodes)Vn(e,t);e.pathBudget=_t(),wr(e),Mt(e),e.bundles=wt(e),Ot(e);let g=rn(),_=sn(),v=it(e.grid),y=new Set;for(let t of e.edges){if(t.bundle&&t.path.length>0){Hn(e,t.path),on(v,g,t.path,t.style),fn(v,_,t.path,t),Ar(e,t);continue}let{added:n,engaged:r}=Xt(e,t);r&&y.add(t);let i=e.preferStraightRoutes;r&&(e.preferStraightRoutes=!0),Dr(e,t),Wn(e,t,g,_,v),e.preferStraightRoutes=i,Zt(e,n),Hn(e,t.path),on(v,g,t.path,t.style),fn(v,_,t.path,t),Ar(e,t)}for(let t of e.nodes)t.drawingCoord=$(e,Z(t)),t.drawing=ui(t,e);if(tr(e),It(e)){for(let t of e.nodes)t.drawingCoord=$(e,Z(t));tr(e)}let b=()=>{for(let t of e.nodes)t.drawingCoord=$(e,Z(t));tr(e)};Qt(e,y,b),tn(e,b),Qt(e,y,b),nr(e),ye(e.canvas,e.columnWidth,e.rowHeight,e.offsetX,e.offsetY),be(e.roleCanvas,e.columnWidth,e.rowHeight,e.offsetX,e.offsetY)}function ur(e,t){return e.edges.filter(e=>e.from.name===t.name)}function dr(e,t){let n=Kn(e,t),r=[],i=n;for(;i!==null;)r.unshift(i),i=i.parent;return r}function fr(e,t,n){let r=dr(e,t),i=dr(e,n),a=0;for(;a<r.length&&a<i.length&&r[a]===i[a];)a++;let o=r[a],s=i[a];return!o||!s||o===s?0:e.subgraphs.indexOf(s)-e.subgraphs.indexOf(o)}function pr(e,t){return ur(e,t).map(e=>e.to)}function mr(e){return e===K?q:e===q?K:e===J?Y:e===Y?J:e===Ue?Ke:e===We?Ge:e===Ge?We:e===Ke?Ue:qe}function hr(e,t){return e.x===t.x?e.y<t.y?q:K:e.y===t.y?e.x<t.x?Y:J:e.x<t.x?e.y<t.y?Ge:Ue:e.y<t.y?Ke:We}function gr(e){return e===`LR`?[Y,q,q,Y]:[q,Y,Y,q]}function _r(e,t){if(e.from===e.to)return gr(t);let n=hr(Z(e.from),Z(e.to)),r,i,a,o,s=t===`LR`?X(n,J)||X(n,We)||X(n,Ke):X(n,K)||X(n,We)||X(n,Ue);return X(n,Ge)?t===`LR`?(r=q,i=J,a=Y,o=K):(r=Y,i=K,a=q,o=J):X(n,Ue)?t===`LR`?(r=K,i=J,a=Y,o=q):(r=Y,i=q,a=K,o=J):X(n,Ke)?t===`LR`?(r=q,i=q,a=J,o=K):(r=J,i=K,a=q,o=Y):X(n,We)?t===`LR`?(r=q,i=q,a=J,o=q):(r=Y,i=Y,a=K,o=Y):s?t===`LR`&&X(n,J)?(r=q,i=q,a=J,o=Y):t===`TD`&&X(n,K)?(r=Y,i=Y,a=K,o=q):(r=n,i=mr(n),a=n,o=mr(n)):(r=n,i=mr(n),a=n,o=mr(n)),[r,i,a,o]}var vr=2,yr=200;function br(e,t){let n=e.gridCoord;return n?t.x>=n.x&&t.x<=n.x+2&&t.y>=n.y&&t.y<=n.y+2:!1}function xr(e,t,n=[]){for(let r=1;r<t.length-1;r++){let i=t[r];if(at(e.grid,i)&&!n.some(e=>br(e,i)))return!1}return!0}function Sr(e){let t=e.from.gridCoord,n=e.to.gridCoord;return!t||!n?!1:t.y===n.y&&t.x!==n.x}function Cr(e){let t=e.from.name,n=e.to.name;return Sr(e)&&n<t?JSON.stringify([n,t]):JSON.stringify([t,n])}function wr(e){let t=new Map;for(let n of e.edges){if(n.from===n.to)continue;let e=Cr(n),r=t.get(e);r?r.push(n):t.set(e,[n])}for(let e of t.values()){if(e.length<2)continue;let t=new Set;for(let n=0;n<e.length;n++)e[n].parallelLane={index:n,total:e.length,usedOffsets:t}}}function Tr(e,t,n,r,i){let a=$e(Z(t.from),n),o=$e(Z(t.to),r),s=X(n,J)||X(n,Y),c=[t.from,t.to],l=t.parallelLane.usedOffsets,u=X(n,Y)?1:X(n,J)?-1:X(n,q)?1:-1,d=X(r,Y)?1:X(r,J)?-1:X(r,q)?1:-1,f=s?{x:a.x+u,y:a.y}:{x:a.x,y:a.y+u},p=s?{x:o.x+d,y:o.y}:{x:o.x,y:o.y+d},m=[a,o],h=[a,o],g=vr*i;for(let t=0;t<yr;t++){let n=g+t;if(l.has(n))continue;let r=a.y+n,i=a.x+n,u=s?[{x:a.x,y:r},{x:o.x,y:r}]:[{x:i,y:a.y},{x:i,y:o.y}],d=yt([a,...u,o]);if(xr(e,ut(d),c))return l.add(n),{path:d,labelSegment:u};m=d,h=u;let _=s?f.x!==p.x:f.y!==p.y,v=s?_?[{x:f.x,y:r},{x:p.x,y:r}]:[{x:f.x,y:r},{x:f.x,y:r}]:_?[{x:i,y:f.y},{x:i,y:p.y}]:[{x:i,y:f.y},{x:i,y:f.y}],y=yt(s?[a,f,{x:f.x,y:r},{x:p.x,y:r},p,o]:[a,f,{x:i,y:f.y},{x:i,y:p.y},p,o]);if(xr(e,ut(y),c))return l.add(n),{path:y,labelSegment:v};m=y,h=v}return{path:m,labelSegment:h}}function Er(e,t,n){let r=e.config.graphDirection===`LR`?Y:q,i=$e(Z(t.anchor),r),a=Pt(e,t,n);return a?{path:[i,...a.path],startDir:r,endDir:a.endDir,labelSegment:a.labelSegment}:null}function Dr(e,t){let n=Kn(e,t.from),r=Kn(e,t.to),[i,a,o,s]=_r(t,n&&n===r&&n.direction?n.direction:e.config.graphDirection),c=t.clusterSource?e.clusterExitPlans?.get(t.clusterSource):void 0;if(c?.edges.has(t)){let n=t.parallelLane&&t.parallelLane.index>0?Er(e,c,t):At(e,c,t);if(n){t.startDir=n.startDir,t.endDir=n.endDir,t.path=n.path,t.text.length>0&&Mr(e,t,n.labelSegment,P(t.text));return}c.edges.delete(t),t.labelLine=[]}if(t.parallelLane&&t.parallelLane.index>0){let n=X(i,a),r=n?o:i,c=n?s:a;t.startDir=r,t.endDir=c;let l=Tr(e,t,r,c,t.parallelLane.index);t.path=l.path,t.text.length>0&&Mr(e,t,l.labelSegment,P(t.text));return}let l=$e(Z(t.from),i),u=$e(Z(t.to),a),d=Ct(e,l,u,Ye(i)),f=Ct(e,$e(Z(t.from),o),$e(Z(t.to),s),Ye(o));if(d!==null&&f!==null){d.length<=f.length?(t.startDir=i,t.endDir=a,t.path=d):(t.startDir=o,t.endDir=s,t.path=f);return}if(d!==null){t.startDir=i,t.endDir=a,t.path=d;return}if(f!==null){t.startDir=o,t.endDir=s,t.path=f;return}t.startDir=i,t.endDir=a,t.path=jr([l,u])}function Or(e,t){for(let n of e.nodes){let e=n.gridCoord;if(e&&t>=e.x&&t<=e.x+2)return!0}return!1}function kr(e,t,n,r){if(!Or(e,r))return r;for(let i=1;i<=n-t;i++){let a=r+i;if(a<=n&&!Or(e,a))return a;let o=r-i;if(o>=t&&!Or(e,o))return o}return r}function Ar(e,t){if(t.text.length===0||t.parallelLane&&t.parallelLane.index>0||t.clusterSource&&e.clusterExitPlans?.get(t.clusterSource)?.edges.has(t))return;let n=P(t.text),r=jr(t.path),i=r.length,a=[];for(let t=1;t<i;t++){let n=r[t-1],i=r[t],o=[n,i],s=Nr(e,o),c=n.x===i.x;a.push({line:o,width:s,index:t,isVertical:c})}let o=t=>xr(e,ut(t)),s=e=>e.index===1||e.index===a.length,c=a.filter(e=>e.width>=n&&!s(e)&&o(e.line)),l;if(c.length>0)c.sort((e,t)=>t.index-e.index),l=c[0].line;else{let e=a.filter(e=>e.width>=n&&o(e.line));if(e.length>0)e.sort((e,t)=>t.index-e.index),l=e[0].line;else{let e=a.filter(e=>!s(e)&&o(e.line)),n=a.filter(e=>o(e.line)),r=e.length>0?e:n.length>0?n:a;if(r.sort((e,t)=>t.width-e.width),r.length>0)l=r[0].line;else{let e=t.path[0]??{x:0,y:0};l=[e,e]}}}Mr(e,t,l,n)}function jr(e){let t=[];for(let n of e){let e=t[t.length-1];e&&e.x!==n.x&&e.y!==n.y&&t.push({x:n.x,y:e.y}),t.push(n)}return t}function Mr(e,t,n,r){let i=Math.min(n[0].x,n[1].x),a=Math.max(n[0].x,n[1].x),o=kr(e,i,a,i+Math.floor((a-i)/2)),s=e.columnWidth.get(o)??0;e.columnWidth.set(o,Math.max(s,r+2)),t.labelLine=[n[0],n[1]]}function Nr(e,t){let n=0,r=Math.min(t[0].x,t[1].x),i=Math.max(t[0].x,t[1].x);for(let t=r;t<=i;t++)n+=e.columnWidth.get(t)??0;return n}var Pr={solid:{h:{unicode:`─`,ascii:`-`},v:{unicode:`│`,ascii:`|`}},dotted:{h:{unicode:`┄`,ascii:`.`},v:{unicode:`┆`,ascii:`:`}},thick:{h:{unicode:`━`,ascii:`=`},v:{unicode:`┃`,ascii:`‖`}},invisible:{h:{unicode:` `,ascii:` `},v:{unicode:` `,ascii:` `}}};function Fr(e,t,n,r,i,a,o=`solid`){let s=hr(t,n),c=[],l=Pr[o],u=a?l.h.ascii:l.h.unicode,d=a?l.v.ascii:l.v.unicode;if(X(s,K))for(let a=t.y-r;a>=n.y-i;a--)c.push({x:t.x,y:a}),z(e,t.x,a,d);else if(X(s,q))for(let a=t.y+r;a<=n.y+i;a++)c.push({x:t.x,y:a}),z(e,t.x,a,d);else if(X(s,J))for(let a=t.x-r;a>=n.x-i;a--)c.push({x:a,y:t.y}),z(e,a,t.y,u);else if(X(s,Y))for(let a=t.x+r;a<=n.x+i;a++)c.push({x:a,y:t.y}),z(e,a,t.y,u);else if(X(s,We)){for(let i=t.x-r;i>=n.x;i--)c.push({x:i,y:t.y}),z(e,i,t.y,u);for(let r=t.y-1;r>=n.y-i;r--)c.push({x:n.x,y:r}),z(e,n.x,r,d)}else if(X(s,Ue)){for(let i=t.x+r;i<=n.x;i++)c.push({x:i,y:t.y}),z(e,i,t.y,u);for(let r=t.y-1;r>=n.y-i;r--)c.push({x:n.x,y:r}),z(e,n.x,r,d)}else if(X(s,Ke)){for(let i=t.x-r;i>=n.x;i--)c.push({x:i,y:t.y}),z(e,i,t.y,u);for(let r=t.y+1;r<=n.y+i;r++)c.push({x:n.x,y:r}),z(e,n.x,r,d)}else if(X(s,Ge)){if(n.x-t.x<=1)for(let a=t.y+r;a<=n.y+i;a++)c.push({x:t.x,y:a}),z(e,t.x,a,d);else{for(let i=t.x+r;i<=n.x;i++)c.push({x:i,y:t.y}),z(e,i,t.y,u);for(let r=t.y+1;r<=n.y+i;r++)c.push({x:n.x,y:r}),z(e,n.x,r,d)}}return c}function Ir(e,t){if(t.path.length===0){let t=I(e.canvas);return[t,t,t,t,t,t]}let n=Zr(e,t),[r,i,a]=Rr(e,t.path,t.style),o=i.length>0,s=t.style===`invisible`,c=o&&!s?Vr(e,t.path,i[0],t.from):I(e.canvas),l;l=t.hasArrowEnd&&o?Gr(e,i[i.length-1],a[a.length-1],t.endMarker):I(e.canvas);let u;if(t.hasArrowStart&&o){let n=i[0][0],r=Lr(a[0]),o={x:n.x,y:n.y};X(a[0],Y)?o.x=n.x-1:X(a[0],J)?o.x=n.x+1:X(a[0],q)?o.y=n.y-1:X(a[0],K)&&(o.y=n.y+1),u=Gr(e,[n,o],r,t.startMarker)}else u=I(e.canvas);let d=s?I(e.canvas):Kr(e,t.path);return[r,c,l,u,d,n]}function Lr(e){return X(e,K)?q:X(e,q)?K:X(e,J)?Y:X(e,Y)?J:X(e,We)?Ge:X(e,Ue)?Ke:X(e,Ke)?Ue:X(e,Ge)?We:qe}function Rr(e,t,n=`solid`){let r=I(e.canvas),i=t[0],a=[],o=[];for(let s=1;s<t.length;s++){let c=t[s],l=$(e,i),u=$(e,c);if(Qe(l,u)){i=c;continue}let d=hr(i,c),f=Fr(r,l,u,1,-1,e.config.useAscii,n);f.length===0&&f.push(l),a.push(f),o.push(d),i=c}return[r,a,o]}var zr=new Set([`│`,`┃`,`║`,`┆`,`┊`,`|`,`‖`]),Br=new Set([`─`,`━`,`═`,`╌`,`┄`,`-`,`=`]);function Vr(e,t,n,r){let i=I(e.canvas),a=e.config.useAscii;if(r.shape===`state-start`||r.shape===`state-end`)return i;let o=n[0],s=hr(t[0],t[1]),c=a?`+`:null,l=(t,n)=>e.canvas[t]?.[n];if(X(s,K)){let e=o.x,t=o.y+1,n=l(e,t);z(i,e,t,n!==void 0&&Br.has(n)?c??`┴`:a?`|`:`│`)}else if(X(s,q)){let e=o.x,t=o.y-1,n=l(e,t);z(i,e,t,n!==void 0&&Br.has(n)?c??`┬`:a?`|`:`│`)}else if(X(s,J)||X(s,Y)){let e=r.drawingCoord,t=r.drawing;if(e===null||t===null)throw Error(`drawBoxStart: node "${r.name}" has no drawingCoord/drawing assigned`);let n=t.length,u=X(s,J)?e.x:e.x+n-1,d=o.y,f=l(u,d);z(i,u,d,f!==void 0&&zr.has(f)?c??(X(s,J)?`┤`:`├`):a?`-`:`─`)}return i}function Hr(e,t){if(t===`circle`)return e?`o`:`○`;if(t===`cross`)return e?`x`:`✕`}function Ur(e){if(X(e,K))return`▲`;if(X(e,q))return`▼`;if(X(e,J))return`◄`;if(X(e,Y))return`►`;if(X(e,Ue))return`↗`;if(X(e,We))return`↖`;if(X(e,Ge))return`↘`;if(X(e,Ke))return`↙`}function Wr(e){if(X(e,K))return`^`;if(X(e,q))return`v`;if(X(e,J))return`<`;if(X(e,Y))return`>`}function Gr(e,t,n,r){let i=I(e.canvas);if(t.length===0)return i;let a=t[t.length-1],o=hr(t.length>=2?t[t.length-2]:t[0],a);(t.length===1||X(o,qe))&&(o=n);let s=Hr(e.config.useAscii,r),c;if(s!==void 0)c=s;else if(e.config.useAscii){let e=Wr(o);c=e===void 0?Wr(n)??`*`:e}else{let e=Ur(o);c=e===void 0?Ur(n)??`●`:e}return z(i,a.x,a.y,c),i}function Kr(e,t){let n=I(e.canvas);for(let r=1;r<t.length-1;r++){let i=t[r],a=$(e,i),o=hr(t[r-1],i),s=hr(i,t[r+1]),c;c=e.config.useAscii?`+`:X(o,s)?X(o,K)||X(o,q)?`│`:`─`:X(o,Y)&&X(s,q)||X(o,K)&&X(s,J)?`┐`:X(o,Y)&&X(s,K)||X(o,q)&&X(s,J)?`┘`:X(o,J)&&X(s,q)||X(o,K)&&X(s,Y)?`┌`:X(o,J)&&X(s,K)||X(o,q)&&X(s,Y)?`└`:`+`,z(n,a.x,a.y,c)}return n}function qr(e,t){return e.edges.some(e=>e!==t&&e.from===t.to&&e.to===t.from)}function Jr(e,t){if(t.text.length===0)return null;let n=zn(e,t.labelLine),r;if(t.path.length>=2){let e=t.path[0].y,n=t.path[t.path.length-1].y;n<e?r=!0:n>e&&(r=!1)}let i=qr(e,t)||e.config.graphDirection!==`LR`&&t.clusterSource!==void 0&&e.clusterExitPlans?.get(t.clusterSource)?.edges.has(t)===!0;return Yr(e,t,Xr(e,Qr(n,t.text,r,i),n[0]?.x===n[1]?.x?n[0]?.x:void 0))}function Yr(e,t,n){let r=t.clusterSource?e.clusterExitPlans?.get(t.clusterSource):void 0;if(e.config.graphDirection!==`LR`||!r?.edges.has(t)||!t.parallelLane||t.parallelLane.index===0)return n;let i=$(e,r.gutter).x+2;return n.map(e=>e.x<i?{...e,x:i}:e)}function Xr(e,t,n){return n===void 0?t:t.map(t=>{let r=P(t.text),{x:i}=t;for(let a of e.subgraphs)if(!(t.y<=a.minY||t.y>=a.maxY))for(let e of[a.minX,a.maxX])e<i||e>=i+r||n===e||(i=n>e?e+1:e-r);return i===t.x?t:{...t,x:i}})}function Zr(e,t){let n=I(e.canvas);for(let{x:r,y:i,text:a}of Jr(e,t)??[])ve(n,{x:r,y:i},a);return n}function Qr(e,t,n,r=!1){if(e.length<2)return[];let i=Math.min(e[0].x,e[1].x),a=Math.max(e[0].x,e[1].x),o=Math.min(e[0].y,e[1].y),s=Math.max(e[0].y,e[1].y),c=i+Math.floor((a-i)/2),l=o+Math.floor((s-o)/2);if(n!==void 0&&i===a){let e=s-o,t=Math.max(1,Math.floor(e/4));(r?n:!n)?l-=t:l+=t}let u=Q(t),d=l-Math.floor((u.length-1)/2);return u.map((e,t)=>({x:c-Math.floor(P(e)/2),y:d+t,text:e}))}function $r(e,t,n){let r=Z(t),i=0;for(let t=0;t<2;t++)i+=e.columnWidth.get(r.x+t)??0;let a=0;for(let t=0;t<2;t++)a+=e.rowHeight.get(r.y+t)??0;let o={width:i+1,height:a+1,labelArea:{x:0,y:0,width:0,height:0},gridColumns:[0,0,0],gridRows:[0,0,0]},s=t.drawingCoord;if(s===null)throw Error(`Node "${t.name}" has no drawingCoord; grid layout must run before bundled-edge drawing`);return Rn(t.shape,n,o,s)}function ei(e,t,n){let r=I(e.canvas),i=t.pathToJunction;if(!i||i.length===0)return[r,r,r,r,r,r];let a=I(e.canvas),o=e.config.useAscii,s=i.map((r,a)=>n.type===`fan-in`&&a===0?$r(e,t.from,t.startDir):n.type===`fan-out`&&a===i.length-1?$r(e,t.to,t.endDir):$(e,r));for(let e=1;e<s.length;e++){let n=s[e-1],r=s[e];Qe(n,r)||Fr(a,n,r,1,-1,o,t.style)}let c=I(e.canvas);for(let t=1;t<i.length-1;t++){let n=i[t],r=$(e,n),a=hr(i[t-1],n),s=hr(n,i[t+1]),l;l=o?`+`:X(a,Y)&&X(s,q)||X(a,K)&&X(s,J)?`┐`:X(a,Y)&&X(s,K)||X(a,q)&&X(s,J)?`┘`:X(a,J)&&X(s,q)||X(a,K)&&X(s,Y)?`┌`:X(a,J)&&X(s,K)||X(a,q)&&X(s,Y)?`└`:`+`,z(c,r.x,r.y,l)}let l=I(e.canvas);if(n.type===`fan-in`&&i.length>=2){let e=s[0],t=hr(i[0],i[1]),n=o?`+`:null;X(t,K)?z(l,e.x,e.y,n??`┴`):X(t,q)?z(l,e.x,e.y,n??`┬`):X(t,J)?z(l,e.x,e.y,n??`┤`):X(t,Y)&&z(l,e.x,e.y,n??`├`)}return[a,l,r,r,c,I(e.canvas)]}function ti(e,t){let n=I(e.canvas),r=I(e.canvas);if(t.sharedPath.length<2)return[n,r];let i=e.config.useAscii,a=t.edges[0]?.style??`solid`,o=e.config.graphDirection,s=t.sharedPath.map((n,r)=>{if(t.type===`fan-in`&&r===t.sharedPath.length-1){let n=o===`TD`?K:J;return $r(e,t.sharedNode,n)}if(t.type===`fan-out`&&r===0){let n=o===`TD`?q:Y;return $r(e,t.sharedNode,n)}return $(e,n)});for(let e=1;e<s.length;e++){let t=s[e-1],r=s[e];Qe(t,r)||Fr(n,t,r,1,-1,i,a)}for(let n=1;n<t.sharedPath.length-1;n++){let a=t.sharedPath[n],o=$(e,a),s=hr(t.sharedPath[n-1],a),c=hr(a,t.sharedPath[n+1]),l;l=i?`+`:X(s,Y)&&X(c,q)||X(s,K)&&X(c,J)?`┐`:X(s,Y)&&X(c,K)||X(s,q)&&X(c,J)?`┘`:X(s,J)&&X(c,q)||X(s,K)&&X(c,Y)?`┌`:X(s,J)&&X(c,K)||X(s,q)&&X(c,Y)?`└`:`+`,z(r,o.x,o.y,l)}return[n,r]}function ni(e,t){let n=I(e.canvas);if(t.sharedPath.length<2)return n;let r=t.sharedPath.length-1,i=t.sharedPath[r-1],a=t.sharedPath[r],o=hr(i,a),s=e.config.graphDirection,c=s===`TD`?K:J,l=$r(e,t.sharedNode,c);s===`TD`?--l.y:--l.x;let u=t.edges[0]?.endMarker,d=t.edges.every(e=>e.endMarker===u)?u:void 0,f=Hr(e.config.useAscii,d),p;return p=f===void 0?e.config.useAscii?X(o,K)?`^`:X(o,q)?`v`:X(o,J)?`<`:X(o,Y)?`>`:`v`:X(o,K)?`▲`:X(o,q)?`▼`:X(o,J)?`◄`:X(o,Y)?`►`:`▼`:f,z(n,l.x,l.y,p),n}function ri(e,t){let n=I(e.canvas);if(!t.pathToJunction||t.pathToJunction.length<2)return n;let r=t.pathToJunction.length-1,i=t.pathToJunction[r-1],a=t.pathToJunction[r],o=hr(i,a),s=e.config.graphDirection,c=s===`TD`?K:J,l=$r(e,t.to,c);s===`TD`?--l.y:--l.x;let u=Hr(e.config.useAscii,t.endMarker),d;return d=u===void 0?e.config.useAscii?X(o,K)?`^`:X(o,q)?`v`:X(o,J)?`<`:X(o,Y)?`>`:`v`:X(o,K)?`▲`:X(o,q)?`▼`:X(o,J)?`◄`:X(o,Y)?`►`:`▼`:u,z(n,l.x,l.y,d),n}function ii(e){return X(e,K)?q:X(e,q)?K:X(e,J)?Y:X(e,Y)?J:e}function ai(e,t){let n=I(e.canvas);if(t.sharedPath.length<2)return n;let r=t.sharedPath[0],i=t.sharedPath[1],a=ii(hr(r,i)),o=e.config.graphDirection,s=o===`TD`?q:Y,c=$r(e,t.sharedNode,s);o===`TD`?c.y+=1:c.x+=1;let l;return l=e.config.useAscii?X(a,K)?`^`:X(a,q)?`v`:X(a,J)?`<`:X(a,Y)?`>`:`^`:X(a,K)?`▲`:X(a,q)?`▼`:X(a,J)?`◄`:X(a,Y)?`►`:`▲`,z(n,c.x,c.y,l),n}function oi(e,t){let n=I(e.canvas);if(!t.pathToJunction||t.pathToJunction.length<2)return n;let r=t.pathToJunction[0],i=t.pathToJunction[1],a=ii(hr(r,i)),o=$r(e,t.from,t.startDir);X(t.startDir,K)?--o.y:X(t.startDir,q)?o.y+=1:X(t.startDir,J)?--o.x:X(t.startDir,Y)&&(o.x+=1);let s;return s=e.config.useAscii?X(a,K)?`^`:X(a,q)?`v`:X(a,J)?`<`:X(a,Y)?`>`:`v`:X(a,K)?`▲`:X(a,q)?`▼`:X(a,J)?`◄`:X(a,Y)?`►`:`▼`,z(n,o.x,o.y,s),n}function si(e,t){let n=I(e.canvas);if(!t.junctionPoint)return n;let r=$(e,t.junctionPoint),i=e.config.useAscii,a=!1,o=!1,s=!1,c=!1;if(t.sharedPath.length>=2){let e=t.type===`fan-in`?0:t.sharedPath.length-1,n=t.type===`fan-in`?1:t.sharedPath.length-2,r=hr(t.sharedPath[e],t.sharedPath[n]);X(r,q)?o=!0:X(r,K)?a=!0:X(r,Y)?c=!0:X(r,J)&&(s=!0)}for(let e of t.edges)if(e.pathToJunction&&e.pathToJunction.length>=2){let n=t.type===`fan-in`?e.pathToJunction.length-1:0,r=t.type===`fan-in`?e.pathToJunction.length-2:1,i=hr(e.pathToJunction[r],e.pathToJunction[n]);X(i,q)?a=!0:X(i,K)?o=!0:X(i,Y)?s=!0:X(i,J)&&(c=!0)}let l;return l=i?`+`:a&&o&&s&&c?`┼`:o&&s&&c&&!a?`┬`:a&&s&&c&&!o?`┴`:a&&o&&c&&!s?`├`:a&&o&&s&&!c?`┤`:s&&c?`─`:a&&o?`│`:o&&c?`┌`:o&&s?`┐`:a&&c?`└`:a&&s?`┘`:`┼`,z(n,r.x,r.y,l),n}function ci(e,t){return li(e,t)}function li(e,t){let n=Z(e),r=t.config.useAscii,i=0;for(let e=0;e<2;e++)i+=t.columnWidth.get(n.x+e)??0;let a=0;for(let e=0;e<2;e++)a+=t.rowHeight.get(n.y+e)??0;let o={x:0,y:0},s={x:i,y:a},c=F(Math.max(o.x,s.x),Math.max(o.y,s.y)),l=mn(e.shape,r),u=e.shape===`state-end`,d=r?u?`=`:`-`:u?`═`:`─`,f=r?u?`‖`:`|`:u?`║`:`│`,p=u?r?{tl:`#`,tr:`#`,bl:`#`,br:`#`}:{tl:`╔`,tr:`╗`,bl:`╚`,br:`╝`}:l;for(let e=o.x+1;e<s.x;e++)z(c,e,o.y,d);for(let e=o.x+1;e<s.x;e++)z(c,e,s.y,d);for(let e=o.y+1;e<s.y;e++)z(c,o.x,e,f);for(let e=o.y+1;e<s.y;e++)z(c,s.x,e,f);if(z(c,o.x,o.y,p.tl),z(c,s.x,o.y,p.tr),z(c,o.x,s.y,p.bl),z(c,s.x,s.y,p.br),e.shape===`cylinder`&&s.y-o.y>=4)for(let e of[o.y+1,s.y-1]){for(let t=o.x+1;t<s.x;t++)z(c,t,e,d);z(c,o.x,e,f),z(c,s.x,e,f)}let m=e.displayLabel,h=Q(m),g=o.y+Math.floor(a/2)-Math.floor((h.length-1)/2);e.labelRows={top:g,bottom:g+h.length-1};for(let e=0;e<h.length;e++){let t=h[e],n=P(t),r=o.x+Math.floor(i/2)-Math.ceil(n/2)+1,a=ie(t);for(let t=0;t<a.length;t++)z(c,r+t,g+e,a[t])}return c}function ui(e,t){return ci(e,t)}function di(e){return/^[┌┐└┘├┤┬┴┼│─╭╮╰╯+\-|]$/.test(e)?`border`:`text`}function fi(e,t=1){let n=Math.max(0,t),r=0;for(let t of e)for(let e of t)r=Math.max(r,P(e));let i=0;for(let t of e)i+=Math.max(t.length,1);return{width:r+2*n+2,height:i+(e.length-1)+2}}function pi(e,t,n=1){let r=Math.max(0,n),{width:i,height:a}=fi(e,r),o=t?`-`:`─`,s=t?`|`:`│`,c=t?`+`:`┌`,l=t?`+`:`┐`,u=t?`+`:`└`,d=t?`+`:`┘`,f=t?`+`:`├`,p=t?`+`:`┤`,m=F(i-1,a-1);z(m,0,0,c);for(let e=1;e<i-1;e++)z(m,e,0,o);z(m,i-1,0,l),z(m,0,a-1,u);for(let e=1;e<i-1;e++)z(m,e,a-1,o);z(m,i-1,a-1,d);for(let e=1;e<a-1;e++)z(m,0,e,s),z(m,i-1,e,s);let h=1;for(let t=0;t<e.length;t++){let n=e[t],a=n.length>0?n:[``];for(let e of a){let t=1+r,n=ie(e);for(let e=0;e<n.length;e++)z(m,t+e,h,n[e]);h++}if(t<e.length-1){z(m,0,h,f);for(let e=1;e<i-1;e++)z(m,e,h,o);z(m,i-1,h,p),h++}}return m}function mi(e){function t(e){return e.parent===null?0:1+t(e.parent)}let n=[...e];return n.sort((e,n)=>t(e)-t(n)),n}function hi(e,t,n,r){for(let i=0;i<t.length;i++)for(let a=0;a<(t[0]?.length??0);a++){let o=t[i]?.[a];if(o&&o!==` `){let t=i+n.x,o=a+n.y;t>=0&&o>=0&&L(e,t,o,r)}}}function gi(e,t,n,r){for(let i of t)hi(e,i,n,r)}function _i(e){let t=null;for(let[n,r]of e.entries())for(let[e,i]of r.entries())i!==` `&&(t=t?{x0:Math.min(t.x0,n),x1:Math.max(t.x1,n),y0:Math.min(t.y0,e),y1:Math.max(t.y1,e)}:{x0:n,x1:n,y0:e,y1:e});return t}function vi(e,t,n){let r=e=>/^[┌┐└┘├┤┬┴┼│─╭╮╰╯+\-|.':]$/.test(e);for(let i=0;i<t.length;i++)for(let a=0;a<(t[0]?.length??0);a++){let o=t[i]?.[a];if(o&&o!==` `){let t=i+n.x,s=a+n.y;t>=0&&s>=0&&L(e,t,s,r(o)?`border`:`text`)}}}function yi(e){let t=[];e.labelRects=t;let n=e.config.useAscii,r={x:0,y:0},i=mi(e.subgraphs);for(let t of i){let r=Vt(t,e),i={x:t.minX,y:t.minY};e.canvas=V(e.canvas,i,n,r),hi(e.roleCanvas,r,i,`border`)}for(let r of e.nodes)if(!r.drawn&&r.drawingCoord&&r.drawing){if(e.canvas=V(e.canvas,r.drawingCoord,n,r.drawing),vi(e.roleCanvas,r.drawing,r.drawingCoord),r.labelRows){let{x:n,y:i}=r.drawingCoord,a={x0:n+1,x1:n+r.drawing.length-2,y0:i+r.labelRows.top,y1:i+r.labelRows.bottom};t.push(a);for(let t=a.x0;t<=a.x1;t++)for(let n=a.y0;n<=a.y1;n++){let r=e.canvas[t]?.[n];r&&r!==` `&&L(e.roleCanvas,t,n,`text`)}}r.drawn=!0}let a=[],o=[],s=[],c=[],l=[],u=[],d=[],f=new Set;for(let t of e.edges)if(t.bundle&&t.pathToJunction){let n=t.bundle,[r,i,,,p,m]=ei(e,t,n);if(a.push(r),o.push(p),l.push(i),u.push(m),n.type===`fan-in`&&t.hasArrowStart){let n=oi(e,t);c.push(n)}if(!f.has(n)){f.add(n);let[t,r]=ti(e,n);if(a.push(t),o.push(r),n.type===`fan-in`){let t=ni(e,n);s.push(t)}if(n.type===`fan-out`&&n.edges.every(e=>e.hasArrowStart)){let t=ai(e,n);c.push(t)}let i=si(e,n);d.push(i)}if(n.type===`fan-out`&&t.hasArrowEnd){let n=ri(e,t);s.push(n)}}else{let[n,r,i,d,f,p]=Ir(e,t);a.push(n),o.push(f),s.push(i),c.push(d),l.push(r),u.push(p)}let p=B(a);e.canvas=V(e.canvas,r,n,...p),gi(e.roleCanvas,p,r,`line`),e.canvas=V(e.canvas,r,n,...o),gi(e.roleCanvas,o,r,`corner`),e.canvas=V(e.canvas,r,n,...d),gi(e.roleCanvas,d,r,`junction`),e.canvas=V(e.canvas,r,n,...s),gi(e.roleCanvas,s,r,`arrow`),e.canvas=V(e.canvas,r,n,...l),gi(e.roleCanvas,l,r,`junction`),e.canvas=V(e.canvas,r,n,...c),gi(e.roleCanvas,c,r,`arrow`),e.canvas=V(e.canvas,r,n,...u),gi(e.roleCanvas,u,r,`text`);for(let e of u){let n=_i(e);n&&t.push(n)}for(let t of e.subgraphs){if(t.nodes.length===0)continue;let[r,i,a]=qt(t,e,(n,r)=>Ut(e.canvas[n+t.minX]?.[r+t.minY])&&(e.canvas[n+t.minX]?.[r+t.minY+1]??` `)!==` `);e.canvas=V(e.canvas,i,n,r),hi(e.roleCanvas,r,i,`text`);for(let{x:t,y:n}of a){let a=r[t]?.[n];a===` `&&(z(e.canvas,t+i.x,n+i.y,a),L(e.roleCanvas,t+i.x,n+i.y,`text`))}}return e.canvas}function bi(e,t,n,r){if(r(e))return e;let i=Math.max(e-t,n-e);for(let a=1;a<=i;a++){let i=e+a;if(i<=n&&r(i))return i;let o=e-a;if(o>=t&&r(o))return o}}function xi(e){let t=e.keys.length>0?` `+e.keys.join(`,`):``;return`${e.type} ${e.name}${t}`}function Si(e){let t=Q(e.label),n=e.attributes.map(xi);return n.length===0?[t]:[t,n]}function Ci(e,t,n=!1,r=!1){if(t)switch(e){case`one`:return r?`+`:`|`;case`zero-one`:return n?`o|`:`|o`;case`many`:return n?`<`:`>`;case`zero-many`:return n?`o<`:`>o`}else switch(e){case`one`:return r?`┼`:`│`;case`zero-one`:return n?`○│`:`│○`;case`many`:return n?`╟`:`╢`;case`zero-many`:return n?`○╟`:`╢○`}}function wi(e){let t=new Set,n=[],r=new Map;for(let t of e.entities)r.set(t.id,new Set);for(let t of e.relationships)r.get(t.entity1)?.add(t.entity2),r.get(t.entity2)?.add(t.entity1);function i(e,n){let i=[e];for(;i.length>0;){let e=i.pop();if(!t.has(e)){t.add(e),n.add(e);for(let n of r.get(e)??[])t.has(n)||i.push(n)}}}for(let r of e.entities)if(!t.has(r.id)){let e=new Set;i(r.id,e),e.size>0&&n.push(e)}return n}function Ti(e,t,n){let r=e.get(t);if(r===void 0)throw Error(`ER diagram layout: missing ${n} for entity "${t}"`);return r}function Ei(n,r,i,a){let o=(0,e.splitStatements)(n),s=(0,t.parseErDiagram)(o);if(s.entities.length===0)return``;let c=r.useAscii,l=Xe(r.paddingX,5,6,6),u=Xe(r.paddingY,5,4,2),d=Xe(r.paddingY,5,2,1),f=Math.max(...[`one`,`zero-one`,`many`,`zero-many`].flatMap(e=>[Ci(e,c,!1).length,Ci(e,c,!0).length])),p=new Map,m=new Map,h=new Map,g=new Map;for(let e of s.entities){g.set(e.id,e);let t=Si(e);p.set(e.id,t);let{width:n,height:i}=fi(t,r.boxBorderPadding);m.set(e.id,n),h.set(e.id,i)}let _=new Map;for(let e of s.relationships){if(!e.label)continue;let t=[e.entity1,e.entity2].sort().join(`|`),n=zt(e.label);_.set(t,Math.max(_.get(t)??0,n))}let v=wi(s),y=new Map,b=0;for(let e of v){let t=s.entities.filter(t=>e.has(t.id)),n=Math.max(2,Math.ceil(Math.sqrt(t.length))),r=0,i=0,a=0;for(let e=0;e<t.length;e++){let o=t[e],s=Ti(m,o.id,`box width`),c=Ti(h,o.id,`box height`);a>=n&&(b+=i+u,r=0,i=0,a=0),y.set(o.id,{entity:o,sections:Ti(p,o.id,`sections`),x:r,y:b,width:s,height:c});let d=l,f=a+1>=n,g=t[e+1];if(!f&&g){let e=[o.id,g.id].sort().join(`|`),t=_.get(e)??0;t>0&&(d=Math.max(d,t+2))}r+=s+d,i=Math.max(i,c),a++}b+=i+d}let x=0,S=0;for(let e of y.values())x=Math.max(x,e.x+e.width),S=Math.max(S,e.y+e.height);x+=4,S+=2;let C=F(x-1,S-1),w=ae(x-1,S-1);function ee(e,t,n,r){z(C,e,t,n,{role:r,roleCanvas:w})}function T(e,t){let n=w[e]?.[t];return n===`text`||n===`border`}function E(e,t,n,r,i){for(let a=0;a<e.length;a++){let e=t+a;if(!(e<r||e>i)&&T(e,n))return!1}return!0}function D(e,t,n,r,i,a){for(let a=0;a<e.length;a++){let e=t+a;if(!(e<r||e>i)&&(T(e,n)||w[e]?.[n]===`arrow`||O.has(`${e},${n}`)))return!1}if(!a)return!0;for(let a=t-2;a<=t+e.length;a++)if(!(a<r-2||a>i+1)&&w[a]?.[n]===`line`&&!B.has(`${a},${n}`))return!1;return!0}for(let e of y.values()){let t=pi(e.sections,c,r.boxBorderPadding);for(let n=0;n<t.length;n++)for(let r=0;r<t[0].length;r++){let i=t[n][r];if(i!==` `){let t=e.x+n,a=e.y+r;t<x&&a<S&&ee(t,a,i,di(i))}}}let O=new Set;for(let e of y.values())for(let t=0;t<e.height;t++)for(let n=0;n<e.width;n++)O.add(`${e.x+n},${e.y+t}`);function k(e,t,n,r){O.has(`${e},${t}`)||(r===`text`||w[e]?.[t]!==`text`)&&(R(n)!==`dashed`||R(C[e]?.[t])!==`solid`)&&(ee(e,t,n,r),r===`line`&&B.add(`${e},${t}`))}function A(e,t,n,r){w[e-1]?.[t]!==`text`&&w[e+1]?.[t]!==`text`&&k(e,t,n,r)}function te(e,t,n,r){for(let i=n;i<=r;i++)for(let n=e;n<=t;n++){let e=C[n]?.[i];if(e!==void 0&&e!==` `)return!1}return!0}let j=[];function M(e,t,n){for(let r of j)if(Math.abs(r.x-e)===1&&t<=r.yEnd&&n>=r.yStart)return!0;return!1}function ne(e,t,n){for(let r of j)if(!(Math.abs(r.x-e)>1)&&t<=r.yEnd&&n>=r.yStart)return!0;return!1}function re(e,t,n){j.push({x:e,yStart:t,yEnd:n})}let N=c?`-`:`─`,P=c?`|`:`│`,I=c?`.`:`╌`,L=c?`:`:`┊`;function R(e){if(e===N||e===P)return`solid`;if(e===I||e===L)return`dashed`}function ce(e,t){return c?`+`:e===`down`?t===`right`?`┌`:`┐`:t===`right`?`└`:`┘`}function le(e,t,n,r){k(e,t,ce(n,r),`line`)}let ue=new Map,de=new Map;for(let e of s.relationships){let t=y.get(e.entity1),n=y.get(e.entity2);if(!t||!n)continue;let r=t.y+Math.floor(t.height/2),i=n.y+Math.floor(n.height/2);if(Math.abs(r-i)<Math.max(t.height,n.height))continue;let[a,o]=r<i?[t,n]:[n,t],s=a.x+Math.floor(a.width/2),c=o.x+Math.floor(o.width/2),l=de.get(a.entity.id);l||(l=[],de.set(a.entity.id,l)),l.push({rel:e,otherCenterX:c});let u=ue.get(o.entity.id);u||(u=[],ue.set(o.entity.id,u)),u.push({rel:e,otherCenterX:s})}function fe(e,t,n){let r=n.get(e.entity.id),i=e.x+Math.floor(e.width/2);if(!r||r.length<=1)return i;let a=[...r].sort((e,t)=>e.otherCenterX-t.otherCenterX),o=a.findIndex(e=>e.rel===t);if(o===-1)return i;let s=Math.min(1,Math.floor((e.width-1)/2)),c=Math.max(0,e.width-1-s*2),l=a.length>1?c/(a.length-1):0;return e.x+s+Math.round(o*l)}let B=new Set;for(let e of s.relationships){B=new Set;let t=y.get(e.entity1),n=y.get(e.entity2);if(!t||!n)continue;let r=e.identifying?N:I,i=e.identifying?P:L,a=t.x+Math.floor(t.width/2),o=t.y+Math.floor(t.height/2),s=n.x+Math.floor(n.width/2),l=n.y+Math.floor(n.height/2);if(Math.abs(o-l)<Math.max(t.height,n.height)){let[o,l]=a<s?[t,n]:[n,t],[d,p]=a<s?[e.cardinality1,e.cardinality2]:[e.cardinality2,e.cardinality1],m=o.x+o.width,h=l.x-1,g=o.y+Math.floor(o.height/2),_;for(let e of y.values())e!==o&&e!==l&&e.y===o.y&&e.x<h+1&&e.x+e.width>m&&(_=Math.max(_??0,e.y+e.height));let v=h-m+1,b=+(v>=f+1),x=Ci(d,c,!1),S=Ci(p,c,!0),ee=+(v>=3&&x[0]===P),T=+(v>=3&&S[S.length-1]===P),D=m+ee,O=h-T,j;if(_===void 0){for(let e=m;e<=h;e++)A(e,g,r,`line`);if(j=g+1,e.label){let t=m+b,n=h-b,r=Q(e.label).length,i=g+1+Math.max(u-1,1);for(;j<i&&!te(t,n,j,j+r-1);)j++}}else{let t=Math.max(o.y+o.height,l.y+l.height,_),n=e.label?Q(e.label).length:0,a=t+1,s=t+Math.max(u*3,4);for(;a<s&&!te(m,h,a,a+n);)a++;se(C,h+1,a+n+1),oe(w,h+1,a+n+1);for(let e=g;e<=a;e++)k(m,e,i,`line`),k(h,e,i,`line`);for(let e=m;e<=h;e++)A(e,a,r,`line`);m!==h&&(le(m,a,`up`,`right`),le(h,a,`up`,`left`)),j=a+1}for(let e=0;e<x.length;e++)k(D+e,g,x[e],`arrow`);for(let e=0;e<S.length;e++)k(O-S.length+1+e,g,S[e],`arrow`);if(e.label){let t=Q(e.label),n=Math.floor((m+h)/2),r=m+b,i=h-b;for(let e=0;e<t.length;e++){let a=t[e],o=ie(a),s=Math.max(r,n-Math.floor(o.length/2)),c=j+e;if(se(C,Math.max(s+o.length,1),Math.max(c+1,1)),oe(w,Math.max(s+o.length,1),Math.max(c+1,1)),E(o,s,c,r,i))for(let e=0;e<o.length;e++){let t=s+e;t>=r&&t<=i&&k(t,c,o[e],`text`)}}}}else{let[a,s]=o<l?[t,n]:[n,t],[u,d]=o<l?[e.cardinality1,e.cardinality2]:[e.cardinality2,e.cardinality1],f=a.y+a.height,p=s.y-1,m=fe(a,e,de),h=fe(s,e,ue);function V(e,t,n){for(let r=t;r<=n;r++)if(O.has(`${e},${r}`))return!1;return!0}let g=f,_=p,v=Math.max(...[...y.values()].filter(e=>e.y===a.y).map(e=>e.y+e.height)),b=s.y,x=Math.max(f,v+1),S=Math.min(p,b-1);function me(e,t,n){let r=Math.floor(x<=S?(x+S)/2:(f+p)/2);if(x>S)return r;for(let r=x;r<=S;r++)if(te(e,t,r,r+n))return r;return r}let T=m!==h,E=Math.min(m,h),j=Math.max(m,h),N=T?me(E,j,0):p,P=T?N-1:p,F=!V(m,x,S)||!V(h,x,S)||M(m,f,T?N:p)||T&&M(h,N,p),I=m;if(F){let e=C.length,t;for(let n=0;n<=e;n++){let e=m+n;if(V(e,f,p)&&!ne(e,f,p)){t=e;break}let r=m-n;if(n>0&&V(r,f,p)&&!ne(r,f,p)){t=r;break}}I=t??m;for(let e=f;e<=g;e++)k(m,e,i,`line`);for(let e=Math.min(m,I);e<=Math.max(m,I);e++)A(e,g,r,`line`);for(let e=g;e<=_;e++)k(I,e,i,`line`);for(let e=Math.min(I,h);e<=Math.max(I,h);e++)A(e,_,r,`line`);for(let e=_;e<=p;e++)k(h,e,i,`line`);if(m!==I){let e=I>m?`right`:`left`,t=e===`right`?`left`:`right`;le(m,g,`up`,e),le(I,g,`down`,t)}if(I!==h){let e=h>I?`right`:`left`,t=e===`right`?`left`:`right`;le(I,_,`up`,e),le(h,_,`down`,t)}re(I,g,_)}else{for(let e=f;e<=P;e++)k(m,e,i,`line`);if(re(m,f,T?N:P),T){for(let e=E;e<=j;e++)A(e,N,r,`line`);for(let e=N+1;e<=p;e++)k(h,e,i,`line`);re(h,N,p);let e=h>m?`right`:`left`,t=e===`right`?`left`:`right`;le(m,N,`up`,e),le(h,N,`down`,t),I=h}}let ae=Ci(u,c,!1,!0);for(let e=0;e<ae.length;e++)k(m-Math.floor(ae.length/2)+e,g,ae[e],`arrow`);let L=m===h?m:h,R=Ci(d,c,!0,!0);for(let e=0;e<R.length;e++)k(L-Math.floor(R.length/2)+e,_,R[e],`arrow`);if(e.label){let t=Q(e.label),n=Math.max(ae.length-1-Math.floor(ae.length/2),R.length-1-Math.floor(R.length/2)),r=I+n+2,i=t.map(e=>ie(e)),a=r+Math.max(...i.map(e=>e.length))-1,o=t.length,s=Math.floor((f+p)/2)-Math.floor((o-1)/2);a>=0&&(se(C,a+1,p+1),oe(w,a+1,p+1));let c=null;for(let e of[!0,!1]){for(let t=0;t<=p-f&&c===null;t++){let n=t===0?[s]:[s+t,s-t];for(let t of n)if(!(t<f||t+o-1>p)&&i.every((n,i)=>D(n,r,t+i,0,a,e))){c=t;break}}if(c!==null)break}if(c!==null)for(let e=0;e<o;e++){let t=i[e],n=c+e;for(let e=0;e<t.length;e++){let i=r+e;i>=0&&k(i,n,t[e],`text`)}w[r-1]?.[n]===`line`&&ee(r-1,n,` `,`line`);let a=r+t.length;w[a]?.[n]===`line`&&ee(a,n,` `,`line`)}}}}return pe(C,{roleCanvas:w,colorMode:i,theme:a})}var Di=4,Oi=[`O`,`/|\\`,`/ \\`],ki=3,Ai=4;function ji(e){return e.seqNumber===void 0?Di:Di+String(e.seqNumber).length}function Mi(n,r,i,a){let o=(0,e.splitStatements)(n),s=(0,t.parseSequenceDiagram)(o);if(s.actors.length===0)return``;let c=r.useAscii,l=c?`-`:`─`,u=c?`|`:`│`,d=c?`+`:`┌`,f=c?`+`:`┐`,p=c?`+`:`└`,m=c?`+`:`┘`,h=c?`+`:`┬`,g=c?`+`:`┴`,_=c?`+`:`├`,v=c?`+`:`┤`,y=c?`‖`:`║`,b=new Map;s.actors.forEach((e,t)=>b.set(e.id,t));function x(e){let t=b.get(e);if(t===void 0)throw Error(`Sequence diagram: unknown actor "${e}"`);return t}function S(e){let t=zt(e.label?`${e.type} [${e.label}]`:e.type);for(let n of e.dividers)n.label&&(t=Math.max(t,zt(`[${n.label}]`)));return t}let C=Math.max(0,r.boxBorderPadding),w=e=>Math.max(zt(e.label),e.type===`actor`?ki:0),ee=e=>Bt(e.label)+(e.type===`actor`?Oi.length:0),T=s.actors.map(e=>w(e)+2*C+2),E=T.map(e=>Math.ceil(e/2)),D=s.actors.map(e=>ee(e)+2),O=Math.max(...D,3),k=(e,t)=>t-Math.floor(T[e]/2),A=Math.floor((O-1)/2),te=O-1-A,j=new Map,M=new Map;s.actors.forEach((e,t)=>{e.createdAt!==void 0&&j.set(e.createdAt,t),e.destroyedAt!==void 0&&M.set(e.destroyedAt,t)});let ne=new Map,re=new Map,N=s.boxes.filter(e=>e.actorIds.length>0).map(e=>{let t=e.actorIds.map(x);return{label:e.label,lo:Math.min(...t),hi:Math.max(...t)}}),I=N.length>0,L=s.actors.map((e,t)=>N.findIndex(e=>e.lo<=t&&t<=e.hi)),ce=+!!I,le=e=>e===``?0:zt(e)+6,ue=Array(Math.max(s.actors.length-1,0)).fill(0);for(let e of s.messages){let t=x(e.from),n=x(e.to);if(t===n)continue;let r=Math.min(t,n),i=Math.max(t,n),a=zt(e.label)+4,o=i-r,s=Math.ceil(a/o);for(let e=r;e<i;e++)ue[e]=Math.max(ue[e],s)}function de(e){let t=Q(e.text);return Math.max(...t.map(e=>P(e)))+2+2*C}let fe=Array(s.actors.length).fill(0),B=Array(s.actors.length).fill(0),V=Array(s.actors.length).fill(0),me=Array(s.actors.length).fill(0);for(let e of s.notes)if(e.position===`left`||e.position===`right`){let t=x(e.actorIds[0]),n=de(e);e.position===`left`?fe[t]=Math.max(fe[t],n):B[t]=Math.max(B[t],n)}else if(e.position===`over`&&e.actorIds.length===1){let t=x(e.actorIds[0]),n=de(e),r=Math.floor(n/2),i=n-r;V[t]=Math.max(V[t],r),me[t]=Math.max(me[t],i)}let he=Xe(r.paddingX,5,10,4),H=[Math.max(E[0]+(L[0]===-1?0:2),fe[0]+1,V[0])];for(let e=1;e<s.actors.length;e++){let t=L[e-1],n=L[e],r=t===n?0:(t===-1?0:1)+(n===-1?0:1),i=r===0?0:r*2-1,a=Math.max(B[e-1],fe[e],me[e-1],V[e])+2,o=Math.max(E[e-1]+E[e]+2+i,ue[e-1]+2,a,he);H[e]=H[e-1]+o}let ge=e=>k(e.lo,H[e.lo])-1-1,_e=e=>Math.max(k(e.hi,H[e.hi])+T[e.hi]-1+1+1,ge(e)+le(e.label)-1);for(let e of N){if(e.hi+1>=s.actors.length)continue;let t=k(e.hi+1,H[e.hi+1]),n=L[e.hi+1]===-1?0:2,r=_e(e)+1+1+n-t;if(r>0){let t=H.slice(e.hi+1).map(e=>e+r);H.splice(e.hi+1,t.length,...t)}}for(let e of s.blocks){let t=-1,n=-1,r=1/0,i=-1;for(let a=e.startIndex;a<=e.endIndex&&!(a>=s.messages.length);a++){let e=s.messages[a],o=x(e.from),c=x(e.to);if(t=t===-1?Math.min(o,c):Math.min(t,o,c),n=Math.max(n,o,c),r=Math.min(r,H[Math.min(o,c)]),i=Math.max(i,H[Math.max(o,c)]),o===c){let t=H[o]+Di+2+zt(e.label);i=Math.max(i,t)}}if(n===-1||n+1>=s.actors.length)continue;let a=Math.max(0,r-Ai),o=i+Ai,c=a+1+S(e),l=Math.max(o,c),u=H[n+1],d=l+2-u;if(d>0){let e=H.slice(n+1).map(e=>e+d);H.splice(n+1,e.length,...e)}}let ve=[],ye=[],be=new Map,xe=new Map,Se=new Map,Ce=[],we=ce,Te=we+O,U=Te+ce,Ee=Xe(r.paddingY,5,1,0),De=Math.max(Ee,1);for(let e of s.notes){if(e.afterIndex!==-1)continue;U+=Ee;let t=Q(e.text),n=de(e),r=t.length+2,i=b.get(e.actorIds[0])??0,a;if(e.position===`left`)a=H[i]-n-1;else if(e.position===`right`)a=H[i]+2;else if(e.actorIds.length>=2){let t=b.get(e.actorIds[1])??i;a=Math.floor((H[i]+H[t])/2)-Math.floor(n/2)}else a=H[i]-Math.floor(n/2);a=Math.max(0,a),Ce.push({x:a,y:U,width:n,height:r,lines:t}),U+=r}for(let e=0;e<s.messages.length;e++){for(let t=0;t<s.blocks.length;t++)s.blocks[t].startIndex===e&&(U+=Ee+1,be.set(t,U-1));for(let t=0;t<s.blocks.length;t++)for(let n=0;n<s.blocks[t].dividers.length;n++)s.blocks[t].dividers[n].index===e&&(U+=Ee,Se.set(`${t}:${n}`,U),U+=De);U+=Ee;let t=s.messages[e],n=t.from===t.to,r=Bt(t.label),i=n?void 0:j.get(e),a=M.get(e);if(n)ye[e]=U+1,ve[e]=U,U+=2+r;else{let t=i===void 0?0:A,n=i===void 0?0:te;ye[e]=U,ve[e]=U+r+t,U+=r+t+1+n,i!==void 0&&ne.set(i,ve[e]-t)}a!==void 0&&(re.set(a,U),U+=1);for(let t=0;t<s.notes.length;t++)if(s.notes[t].afterIndex===e){U+=Ee;let e=s.notes[t],n=Q(e.text),r=de(e),i=n.length+2,a=b.get(e.actorIds[0])??0,o;if(e.position===`left`)o=H[a]-r-1;else if(e.position===`right`)o=H[a]+2;else if(e.actorIds.length>=2){let t=b.get(e.actorIds[1])??a;o=Math.floor((H[a]+H[t])/2)-Math.floor(r/2)}else o=H[a]-Math.floor(r/2);o=Math.max(0,o),Ce.push({x:o,y:U,width:r,height:i,lines:n}),U+=i}for(let t=0;t<s.blocks.length;t++)s.blocks[t].endIndex===e&&(U+=Ee,xe.set(t,U),U+=De)}U+=De;let Oe=U,ke=Oe+ce,Ae=ke+O+ce,je=(H[H.length-1]??0)+(E[E.length-1]??0)+2;for(let e of N)je=Math.max(je,_e(e)+2);for(let e=0;e<s.messages.length;e++){let t=s.messages[e];if(t.from===t.to){let e=H[x(t.from)]+ji(t)+2+2+zt(t.label);je=Math.max(je,e+1)}}for(let e of Ce)je=Math.max(je,e.x+e.width+1);let Me=F(je,Ae-1),Ne=ae(je,Ae-1);function W(e,t,n,r){z(Me,e,t,n,{role:r,roleCanvas:Ne})}function G(e,t,n,r,i){let a=ie(n);for(let n=0;n<a.length;n++){let o=e+n;if(i!==void 0&&o>=i||a[n+1]===``&&i!==void 0&&o+1>=i)break;W(o,t,a[n],r)}}function Pe(e,t,n,r,i){let a=r===`actor`?Oi:[],o=[...a,...Q(n)],s=Math.max(zt(n),a.length>0?ki:0),c=s+2*C+2,h=i,g=h-2,_=e-Math.floor(c/2);W(_,t,d,`border`);for(let e=1;e<c-1;e++)W(_+e,t,l,`border`);W(_+c-1,t,f,`border`);for(let e=0;e<g;e++){let n=t+1+e;W(_,n,u,`border`),W(_+c-1,n,u,`border`);let r=o[e];r!==void 0&&G(_+1+C+Math.floor((s-P(r))/2),n,r,`text`)}let v=t+h-1;W(_,v,p,`border`);for(let e=1;e<c-1;e++)W(_+e,v,l,`border`);W(_+c-1,v,m,`border`)}let Fe=e=>{let t=ne.get(e);return t===void 0?Te:t+O},Ie=e=>re.get(e)??Oe,Le=new Map,Re=new Map;function ze(e,t){let n=Re.get(e)??[];Re.set(e,n),n.push(t)}function Be(e,t){let n=Re.get(e)?.pop();if(n===void 0)return;let r=Le.get(e)??[];Le.set(e,r),r.push({topRow:n,bottomRow:t})}let Ve=new Map;for(let e of s.activations){let t=Ve.get(e.afterIndex)??[];t.push(e),Ve.set(e.afterIndex,t)}function He(e,t){for(let n of Ve.get(e)??[])n.kind===`start`?ze(n.actorId,t):Be(n.actorId,t)}He(-1,Te);for(let e=0;e<s.messages.length;e++){let t=s.messages[e],n=ve[e];t.activate&&ze(t.to,n),t.deactivate&&Be(t.from,n),He(e,n)}for(let[e,t]of Re){if(t.length===0)continue;let n=Ie(x(e)),r=Le.get(e)??[];Le.set(e,r);for(let e of t)r.push({topRow:e,bottomRow:n})}function K(e,t){let n=Le.get(e);return n?n.some(e=>t>=e.topRow&&t<=e.bottomRow):!1}for(let e=0;e<s.actors.length;e++){let t=H[e],n=s.actors[e].id;for(let r=Fe(e);r<=Ie(e);r++)W(t,r,K(n,r)?y:u,`line`)}let q=c?`+`:`┼`;function J(e,t,n){let r=ge(e),i=_e(e),a=t+O+1;W(r,t,d,`border`),W(i,t,f,`border`),W(r,a,p,`border`),W(i,a,m,`border`);for(let e=r+1;e<i;e++)W(e,t,l,`border`),W(e,a,l,`border`);for(let e=t+1;e<a;e++)W(r,e,u,`border`),W(i,e,u,`border`);n!==``&&G(r+2,t,` ${Q(n)[0]} `,`text`);for(let n=e.lo;n<=e.hi;n++){let e=H[n];Fe(n)<=t&&t<=Ie(n)&&W(e,t,q,`junction`),Fe(n)<=a&&a<=Ie(n)&&W(e,a,q,`junction`)}}for(let e of N)J(e,0,e.label),J(e,Oe,``);for(let e=0;e<s.actors.length;e++){let t=s.actors[e],n=ne.get(e)??we,r=re.has(e);Pe(H[e],n,t.label,t.type,O),r||Pe(H[e],ke,t.label,t.type,O),c||(W(H[e],n+O-1,h,`junction`),r||W(H[e],ke,g,`junction`))}for(let e=0;e<s.messages.length;e++){let t=s.messages[e],n=x(t.from),r=x(t.to),i=H[n],a=H[r],o=n===r,d=t.lineStyle===`dashed`,f=t.arrowHead===`filled`,p=t.isLost===!0,m=c?`x`:`✕`,h=d?c?`.`:`╌`:l;if(o){let n=ve[e],r=ji(t),a=t.seqNumber===void 0?void 0:String(t.seqNumber),o=Q(t.label);W(i,n,_,`junction`);let s=i+1;if(a!==void 0){for(let e=0;e<a.length;e++)W(i+1+e,n,a[e],`text`);s=i+1+a.length}for(let e=s;e<i+r;e++)W(e,n,h,`line`);W(i+r,n,c?`+`:`┐`,`corner`);let l=i+r+2;for(let e=0;e<o.length;e++){let t=n+1+e;W(i+r,t,u,`line`),G(l,t,o[e],`text`,je)}let d=n+1+o.length;W(i,d,p?m:f?c?`<`:`◀`:c?`<`:`◁`,`arrow`);for(let e=i+1;e<i+r;e++)W(e,d,h,`line`);W(i+r,d,c?`+`:`┘`,`corner`)}else{let n=ye[e],o=ve[e],s=i<a,l=j.get(e)===r,u=a;if(l){let e=k(r,a),t=e+T[r]-1;u=s?e-1:t+1}let d=Math.floor((i+a)/2),g=Q(t.label);for(let e=0;e<g.length;e++){let t=g[e];G(d-Math.floor(P(t)/2),n+e,t,`text`,je)}if(s){for(let e=i+1;e<u;e++)W(e,o,h,`line`);W(u,o,p?m:f?c?`>`:`▶`:c?`>`:`▷`,`arrow`),t.bidirectional&&W(i,o,c?`<`:`◀`,`arrow`)}else{for(let e=u+1;e<i;e++)W(e,o,h,`line`);W(u,o,p?m:f?c?`<`:`◀`:c?`<`:`◁`,`arrow`),t.bidirectional&&W(i,o,c?`>`:`▶`,`arrow`)}if(t.seqNumber!==void 0){let e=String(t.seqNumber);if(s){let t=i+1;if(t+e.length<a)for(let n=0;n<e.length;n++)W(t+n,o,e[n],`text`)}else{let t=i-e.length;if(t>u)for(let n=0;n<e.length;n++)W(t+n,o,e[n],`text`)}}}}for(let[e,t]of re)W(H[e],t,c?`x`:`✕`,`arrow`);let Y=je-1;for(let e=0;e<s.blocks.length;e++){let t=s.blocks[e],n=be.get(e),r=xe.get(e);if(n===void 0||r===void 0)continue;let i=je,a=0;for(let e=t.startIndex;e<=t.endIndex&&!(e>=s.messages.length);e++){let t=s.messages[e],n=b.get(t.from)??0,r=b.get(t.to)??0;if(i=Math.min(i,H[Math.min(n,r)]),a=Math.max(a,H[Math.max(n,r)]),n===r){let e=H[n]+ji(t)+2+zt(t.label);a=Math.max(a,e)}}let o=i-Ai,c=a+Ai,h=1/0,g=-1/0;for(let e of H)e>a&&e<h&&(h=e),e<i&&e>g&&(g=e);c>=h&&(c=Math.max(a,h-Ai)),o<=g&&(o=Math.min(i,g+Ai)),o=Math.max(0,o),c>Y&&(se(Me,c+1,Ae-1),oe(Ne,c+1,Ae-1),Y=c);let y=t.label?`${t.type} [${t.label}]`:t.type,x=o+1+S(t);if(x>c){c=x;let[e]=R(Me);c>e&&(se(Me,c,Ae-1),oe(Ne,c,Ae-1)),je=Math.max(je,c+1),Y=Math.max(Y,c)}W(o,n,d,`border`);for(let e=o+1;e<c;e++)W(e,n,l,`border`);W(c,n,f,`border`);let C=Q(y);for(let e=0;e<C.length&&n+e<r;e++)G(o+1,n+e,C[e],`text`,c);W(o,r,p,`border`);for(let e=o+1;e<c;e++)W(e,r,l,`border`);W(c,r,m,`border`);for(let e=n+1;e<r;e++)W(o,e,u,`border`),W(c,e,u,`border`);for(let n=0;n<t.dividers.length;n++){let r=Se.get(`${e}:${n}`);if(r===void 0)continue;let i=Ue();W(o,r,_,`junction`);for(let e=o+1;e<c;e++)W(e,r,i,`line`);W(c,r,v,`junction`);let a=t.dividers[n].label;if(a){let e=`[${a}]`;G(o+1,r,e,`text`,c)}}}for(let e of Ce){se(Me,e.x+e.width,e.y+e.height),oe(Ne,e.x+e.width,e.y+e.height),W(e.x,e.y,d,`border`);for(let t=1;t<e.width-1;t++)W(e.x+t,e.y,l,`border`);W(e.x+e.width-1,e.y,f,`border`);for(let t=0;t<e.lines.length;t++){let n=e.y+1+t;W(e.x,n,u,`border`),W(e.x+e.width-1,n,u,`border`);for(let t=e.x+1;t<e.x+e.width-1;t++)W(t,n,` `,`text`);G(e.x+1+C,n,e.lines[t],`text`)}let t=e.y+e.height-1;W(e.x,t,p,`border`);for(let n=1;n<e.width-1;n++)W(e.x+n,t,l,`border`);W(e.x+e.width-1,t,m,`border`)}return pe(Me,{roleCanvas:Ne,colorMode:i,theme:a});function Ue(){return c?`-`:`╌`}}function Ni(e,t){return e.rowStart<=t.rowEnd&&t.rowStart<=e.rowEnd}function Pi(e,t){let n=e.map(e=>({item:e,geom:t(e)})).sort((e,t)=>e.geom.idealMid-t.geom.idealMid),r=(e,t)=>{let r=n[e];if(r)for(let i=e+t;i>=0&&i<n.length;i+=t){let e=n[i];if(e&&Ni(e.geom,r.geom))return e.geom}},i=new Map;for(let[e,t]of n.entries()){let n=t.geom,a=r(e,-1),o=r(e,1),s=a&&a.end>=n.start?Math.floor((a.idealMid+n.idealMid)/2)+1:-1/0,c=o&&n.end>=o.start?Math.floor((n.idealMid+o.idealMid)/2):1/0;i.set(t.item,{left:s,right:c})}return i}function Fi(e){let n=[];e.annotation&&n.push(`<<${e.annotation}>>`);let r=Q(e.label);n.push(...r);let i=e.attributes.map(t.formatClassMember),a=e.methods.map(t.formatClassMember),o=[n];return i.length>0&&o.push(i),a.length>0&&o.push(a),o}function Ii(e,t){return{type:e,markerAt:t,dashed:e===`dependency`||e===`realization`}}function Li(e,t,n){switch(e){case`inheritance`:case`realization`:return n===`down`?t?`^`:`△`:n===`up`?t?`v`:`▽`:n===`left`?t?`>`:`◁`:t?`<`:`▷`;case`composition`:return t?`*`:`◆`;case`aggregation`:return t?`o`:`◇`;case`association`:case`dependency`:return n===`down`?t?`v`:`▼`:n===`up`?t?`^`:`▲`:n===`left`?t?`<`:`◀`:t?`>`:`▶`}}function Ri(e,t,n,r,i){let a=e+t.length-1,o=r>e,s=i<a;if(!o&&!s)return{start:e,cells:t};let c=o?r:e,l=Math.max(0,(s?i:a)-c+1),u=+!!o+ +!!s;if(l===0)return{start:c,cells:[]};if(l<u)return{start:c,cells:[`…`]};let d=Math.max(0,l-u),f=ie(n),p=f.length<=d?f:o&&!s?f.slice(f.length-d):f.slice(0,d),m=[...o?[`…`]:[],...p,...s?[`…`]:[]];return{start:c+Math.max(0,Math.floor((l-m.length)/2)),cells:m}}function zi(e){return Math.max(...Q(e).map(e=>P(e)))+2}function Bi(n,r,i,a,o={}){let c=(0,e.splitStatements)(n),{diagram:l,notesById:d}=Vi((0,t.parseClassDiagram)(c));if(l.classes.length===0)return``;let f=r.useAscii,p=Xe(r.paddingX,5,4,1),m=Xe(r.paddingY,5,3,1),h=new Map,g=new Map,_=new Map;for(let e of l.classes){let t=Fi(e);h.set(e.id,t);let{width:n,height:i}=fi(t,r.boxBorderPadding);g.set(e.id,n),_.set(e.id,i)}let v=new Map;for(let e of l.classes)v.set(e.id,e);let y=new Map,b=new Map;for(let e of l.relationships){let t=e.from,n=e.to,r=y.get(n)??new Set;y.set(n,r),r.add(t);let i=b.get(t)??new Set;b.set(t,i),i.add(n)}let x=new Map,S=l.classes.filter(e=>!y.has(e.id)||y.get(e.id).size===0).map(e=>e.id);for(let e of S)x.set(e,0);let C=l.classes.length-1,w=0;for(;w<S.length;){let e=S[w++],t=b.get(e);if(t)for(let n of t){let t=(x.get(e)??0)+1;t>C||(!x.has(n)||x.get(n)<t)&&(x.set(n,t),S.push(n))}}for(let e of l.classes)x.has(e.id)||x.set(e.id,0);Hi(d,x);let ee=Math.max(...x.values(),0),T=Array.from({length:ee+1},()=>[]);for(let e of l.classes)T[x.get(e.id)].push(e.id);let E=new Map,D=new Map;{let e=new Map;l.relationships.forEach((t,n)=>{let r=[t.from,t.to].sort().join(`::`),i=e.get(r)??[];i.push(n),e.set(r,i)});for(let t of e.values()){if(t.length<2)continue;let e=t.length,n=Math.max(...t.map(e=>{let t=l.relationships[e].label;return t?zi(t):3}))+1;t.forEach((t,r)=>{E.set(t,Math.round((r-(e-1)/2)*n)),D.set(t,(e-1)*n)})}}let O=new Map,k=new Map,A=new Map,te=new Map;{let e=(e,t,n)=>{let r=new Map;l.relationships.forEach((t,n)=>{if(E.has(n)||!v.has(t.from)||!v.has(t.to))return;let i=x.get(t.from)??0;if((x.get(t.to)??0)-i!==1)return;let a=e(t),o=r.get(a)??[];o.push(n),r.set(a,o)});for(let e of r.values()){if(e.length<2)continue;let r=e.length,i=Math.max(...e.map(e=>{let t=l.relationships[e].label;return t?zi(t):3}))+1;e.forEach((e,a)=>{t.set(e,Math.round((a-(r-1)/2)*i)),n.set(e,(r-1)*i)})}};e(e=>e.to,A,te),e(e=>e.from,O,k)}let j=new Map;for(let e of l.classes)j.set(e.id,{left:0,right:0});l.relationships.forEach((e,t)=>{if(!v.has(e.from)||!v.has(e.to))return;let n=e.label?zi(e.label):1,r=E.get(t)??O.get(t)??0,i=E.get(t)??A.get(t)??0,a=j.get(e.from);a.left=Math.max(a.left,Math.floor(n/2)-r),a.right=Math.max(a.right,r+n-1-Math.floor(n/2));let o=j.get(e.to);o.left=Math.max(o.left,Math.floor(n/2)-i),o.right=Math.max(o.right,i+n-1-Math.floor(n/2))});let M=new Map,ne=0,re=0;for(let e=0;e<=ee;e++){let t=T[e];if(t.length===0)continue;let n=e=>{let t=y.get(e);return t&&t.size>0?[...t].sort().join(`,`):`root:${e}`},r=new Map;for(let e of t){let t=n(e),i=r.get(t);i||(i=[],r.set(t,i)),i.push(e)}let i=new Map,a=new Map,o=new Map;{let e=0;for(let n of t){let t=g.get(n),r=j.get(n),s=Math.max(0,r.left-Math.floor(t/2)),c=Math.max(0,r.right-(t-1-Math.floor(t/2)));i.set(n,e),a.set(n,s+t+c),o.set(n,s),e+=s+t+c+p}}let s=0,c=0,l=(e,t)=>{let n=v.get(e),r=g.get(e),i=_.get(e),l=o.get(e),u=a.get(e);M.set(e,{cls:n,sections:h.get(e),x:t+l,y:ne,width:r,height:i}),re=Math.max(re,t+u),s=t+u+p,c=Math.max(c,i)},u=new Set;for(let c of t){if(u.has(c))continue;let t=n(c),d=y.get(c),f=d?[...d].filter(t=>x.get(t)<e):[];if(f.length===0){l(c,Math.max(i.get(c),s,0)),u.add(c);continue}let m=r.get(t),h=f.map(e=>{let t=M.get(e);return t.x+Math.floor(t.width/2)}),_=Math.round(h.reduce((e,t)=>e+t,0)/h.length),v=0;for(let e=0;e<m.length;e++)v+=a.get(m[e]),e<m.length-1&&(v+=p);let b=o.get(m[0]),S=m[m.length-1],C=a.get(S)-o.get(S)-g.get(S),w=v-b-C,ee=_-b-Math.floor(w/2),T=Math.max(ee,s,0);for(let e of m)l(e,T),u.add(e),T=s}ne+=c+m}let N=re,I=0;for(let e of M.values())N=Math.max(N,e.x+e.width),I=Math.max(I,e.y+e.height);N+=4,I+=2;let L=F(N-1,I-1),R=ae(N-1,I-1);function ce(e,t,n,r){z(L,e,t,n,{role:r,roleCanvas:R})}let ue=o.hyperlinks?s(N-1,I-1):void 0;for(let t of M.values()){let n=pi(t.sections,f,r.boxBorderPadding);for(let e=0;e<n.length;e++)for(let r=0;r<n[0].length;r++){let i=n[e][r];if(i!==` `){let n=t.x+e,a=t.y+r;n<N&&a<I&&ce(n,a,i,di(i))}}if(ue){let r=(0,e.safeHref)(l.interactions.get(t.cls.id)?.href),i=t.sections[0];if(r!==void 0&&i!==void 0){let e=1+ +!!t.cls.annotation;u(ue,n,{x:t.x,y:t.y},r,{from:e,to:i.length})}}}let de=new Set;for(let e of M.values())for(let t=0;t<e.height;t++)for(let n=0;n<e.width;n++)de.add(`${e.x+n},${e.y+t}`);function B(e,t,n,r){de.has(`${e},${t}`)||ce(e,t,n,r)}function V(e,t,n){for(let[r,i]of M.entries())if(!n?.has(r)&&e>=i.x&&e<=i.x+i.width-1&&t>=i.y&&t<=i.y+i.height-1)return!0;return!1}function me(e,t,n,r){return bi(e,0,e+N+9,e=>{for(let i=Math.min(t,n);i<=Math.max(t,n);i++)if(V(e,i,r))return!1;return!0})??N+2}let he=f?`-`:`─`,H=f?`|`:`│`,ge=f?`.`:`╌`,_e=f?`:`:`┊`;function ve(e,t,n){let r=Math.floor(n/2),i=n-1-r,a=t>r+i?(r+i)/t:1;return Math.max(-r,Math.min(i,Math.round(e*a)))}function ye(e,t,n){let r=E.get(e)??O.get(e)??0,i=E.get(e)??A.get(e)??0,a=D.get(e)??k.get(e)??0,o=D.get(e)??te.get(e)??0,s=t.x+Math.floor(t.width/2),c=n.x+Math.floor(n.width/2);return{fromCX:s+r,toCX:c+i,fromAnchorX:s+ve(r,a,t.width),toAnchorX:c+ve(i,o,n.width)}}function be(e,t,n,r){if(de.has(`${e},${t}`)||R[e]?.[t]===`arrow`)return;let i=L[e]?.[t];if(!f&&i!==void 0&&le(i)&&le(n)){let a=fe(i,n);ce(e,t,a,a===n?r:`junction`);return}ce(e,t,n,r)}function xe(e,t,n,r,i){if(t===n)return;let a=Math.min(t,n),o=Math.max(t,n);for(let t=a+1;t<o;t++)be(t,e,r,`line`);if(f){be(t,e,r,`line`),be(n,e,r,`line`);return}let s=n>t,c=i?s?`└`:`┘`:s?`┌`:`┐`,l=i?s?`┐`:`┌`:s?`┘`:`└`;be(t,e,c,`corner`),be(n,e,l,`corner`)}let Se=new Map,Ce=new Map;l.relationships.forEach((e,t)=>{let n=M.get(e.from),r=M.get(e.to);if(!n||!r)return;let i=Ii(e.type,e.markerAt),a=i.dashed?ge:he,o=i.dashed?_e:H,s=new Set([e.from,e.to]),{fromCX:c,toCX:l,fromAnchorX:u,toAnchorX:d}=ye(t,n,r),p=n.y+n.height-1,m=r.y;if(p<m){let e=me(c,p+1,m-1,s),n=e!==c;if(e>=N&&se(L,e+2,I),n){let n=p+1,r=m-1;Se.set(t,{routeX:e,exitY:n,entryY:r,fromAnchorX:u,toAnchorX:d,clearSide:e>c?`right`:`left`});let s=Math.min(u,e),l=Math.max(u,e);for(let e=s;e<=l;e++)B(e,n,a,`line`);!f&&n<(L[0]?.length??0)&&(u<e?(B(u,n,`└`,`corner`),B(e,n,`┐`,`corner`)):(B(u,n,`┘`,`corner`),B(e,n,`┌`,`corner`)));for(let t=n+1;t<=r;t++)B(e,t,o,`line`);if(e!==d){let t=Math.min(e,d),n=Math.max(e,d);for(let e=t;e<=n;e++)B(e,r,a,`line`);!f&&r<(L[0]?.length??0)&&(e<d?(B(e,r,`└`,`corner`),B(d,r,`┐`,`corner`)):(B(e,r,`┘`,`corner`),B(d,r,`┌`,`corner`)))}if(i.markerAt===`to`){let e=i.type===`inheritance`||i.type===`realization`;B(d,r,Li(i.type,f,e?`up`:`down`),`arrow`)}if(i.markerAt===`from`){let e=Li(i.type,f,`down`);B(u,p+1,e,`arrow`)}}else{let e=p+Math.floor((m-p)/2),t=c!==u,n=l!==d;xe(p+1,u,c,a,!0);for(let n=p+(t?2:1);n<=e;n++)B(c,n,o,`line`);if(c!==l&&e<(L[0]?.length??0)){let t=Math.min(c,l),n=Math.max(c,l);for(let r=t;r<=n;r++)B(r,e,a,`line`);f||(B(c,e,c<l?`└`:`┘`,`corner`),B(l,e,c<l?`┐`:`┌`,`corner`))}for(let t=e+1;t<m-+!!n;t++)B(l,t,o,`line`);if(xe(m-1,d,l,a,!1),i.markerAt===`to`){let e=i.type===`inheritance`||i.type===`realization`;B(d,m-1,Li(i.type,f,e?`up`:`down`),`arrow`)}i.markerAt===`from`&&B(u,p+1,Li(i.type,f,`down`),`arrow`)}}else if(r.y+r.height-1<n.y){let e=n.y,t=r.y+r.height-1,s=t+Math.floor((e-t)/2),p=c!==u,m=l!==d;xe(e-1,u,c,a,!1);for(let t=e-(p?2:1);t>=s;t--)B(c,t,o,`line`);if(c!==l){let e=Math.min(c,l),t=Math.max(c,l);for(let n=e;n<=t;n++)B(n,s,a,`line`);!f&&s>=0&&s<I&&(B(c,s,c<l?`┌`:`┐`,`corner`),B(l,s,c<l?`┘`:`└`,`corner`))}for(let e=s-1;e>t+ +!!m;e--)B(l,e,o,`line`);if(xe(t+1,d,l,a,!0),i.markerAt===`from`){let t=Li(i.type,f,`up`),n=e-1;for(let e=0;e<t.length;e++)B(u-Math.floor(t.length/2)+e,n,t[e],`arrow`)}if(i.markerAt===`to`){let e=i.type===`inheritance`||i.type===`realization`?`down`:`up`,n=Li(i.type,f,e),r=t+1;for(let e=0;e<n.length;e++)B(d-Math.floor(n.length/2)+e,r,n[e],`arrow`)}}else{let s=r.y+r.height-1,m=Math.min(c,l),h=Math.max(c,l),g=Math.max(p,s);for(let[t,r]of M.entries())t!==e.from&&t!==e.to&&r.y===n.y&&r.x<h+1&&r.x+r.width>m&&(g=Math.max(g,r.y+r.height-1));let _=g+2;Ce.set(t,_),se(L,N,_+1),oe(R,N,_+1);let v=c!==u,y=l!==d;xe(p+1,u,c,a,!0);for(let e=p+(v?2:1);e<=_;e++)B(c,e,o,`line`);for(let e=m;e<=h;e++)B(e,_,a,`line`);for(let e=_-1;e>=s+(y?2:1);e--)B(l,e,o,`line`);if(xe(s+1,d,l,a,!0),i.markerAt===`from`){let e=Li(i.type,f,`down`),t=p+1;for(let n=0;n<e.length;n++)B(u-Math.floor(e.length/2)+n,t,e[n],`arrow`)}if(i.markerAt===`to`){let e=i.type===`inheritance`||i.type===`realization`,t=Li(i.type,f,e?`down`:`up`),n=r.y+r.height;for(let e=0;e<t.length;e++)B(d-Math.floor(t.length/2)+e,n,t[e],`arrow`)}}});function we(e,t,n,r){let{fromCX:i,toCX:a}=ye(e,t,n),o=t.y+t.height-1,s=n.y;if(o<s){let t=Se.get(e);if(t){let e=t.exitY+1,n=t.entryY;if(n>=e)return{idealMidX:t.clearSide===`right`?t.routeX+1+Math.floor(r/2):t.routeX-1-Math.ceil(r/2),baseMidY:Math.floor((e+n)/2)};let i=Math.abs(t.routeX-t.fromAnchorX);return Math.abs(t.toAnchorX-t.routeX)>=i?{idealMidX:Math.floor((t.routeX+t.toAnchorX)/2),baseMidY:t.entryY}:{idealMidX:Math.floor((t.fromAnchorX+t.routeX)/2),baseMidY:t.exitY}}return{idealMidX:Math.floor((i+a)/2),baseMidY:Math.floor((o+1+s-1)/2)}}if(n.y+n.height-1<t.y){let e=n.y+n.height-1;return{idealMidX:Math.floor((i+a)/2),baseMidY:Math.floor((e+1+t.y-1)/2)}}return{idealMidX:Math.floor((i+a)/2),baseMidY:Ce.get(e)??Math.max(o,n.y+n.height-1)+2}}function Te(e,t,n,r,i,a,o){let s=r,c=Math.floor(a/2),l=e.y+e.height-1,u=t.y,d=!1;for(let e=0;e<a;e++){let t=s-c+e,r=n-Math.floor(i/2),a=Math.max(0,r);for(let e=a;e<a+i;e++)if(V(e,t,o)){d=!0;break}if(d)break}if(d){let e=l+1,t=u-1;for(let r=e;r<=t;r++){let e=!0,t=n-Math.floor(i/2),a=Math.max(0,t);for(let t=a;t<a+i;t++)if(V(t,r,o)){e=!1;break}if(e){s=r;break}}}return s}let U=new Map,Ee=[];for(let[e,t]of l.relationships.entries()){if(!t.label)continue;let n=M.get(t.from),r=M.get(t.to);if(!n||!r)continue;let i=Q(t.label),a=Math.floor(i.length/2),o=Math.max(...i.map(P))+2,{idealMidX:s,baseMidY:c}=we(e,n,r,o),l=new Set([t.from,t.to]),u=Te(n,r,s,c,o,i.length,l);U.set(t,u);let d=Math.max(0,s-Math.floor(o/2));Ee.push({rel:t,idealMidX:s,naturalStart:d,naturalEnd:d+o-1,rowStart:u-a,rowEnd:u+a})}let De=Pi(Ee,e=>({idealMid:e.idealMidX,start:e.naturalStart,end:e.naturalEnd,rowStart:e.rowStart,rowEnd:e.rowEnd})),Oe=new Map;for(let e of Ee){let t=De.get(e);t&&Oe.set(e.rel,t)}for(let[e,t]of l.relationships.entries()){let n=M.get(t.from),r=M.get(t.to);if(!(!n||!r)&&t.label){let i=Q(t.label),{idealMidX:a}=we(e,n,r,Math.max(...i.map(e=>P(e)))+2),o=U.get(t)-Math.floor(i.length/2),s=Oe.get(t);for(let e=0;e<i.length;e++){let t=o+e,n=i[e],r=ie(` ${n} `),{start:c,cells:l}=Ri(Math.max(0,a-Math.floor(r.length/2)),r,n,Math.max(0,s.left),s.right),u=c+l.length;u>0&&t>=0&&(se(L,Math.max(u,1),Math.max(t+1,1)),oe(R,Math.max(u,1),Math.max(t+1,1)));for(let e=0;e<l.length;e++){let n=c+e;n>=0&&t>=0&&ce(n,t,l[e],`text`)}}}}return Ui(d,M,L,f,ce),pe(L,{roleCanvas:R,colorMode:i,theme:a,linkCanvas:ue})}function Vi(e){let t=new Map;if(e.notes.length===0)return{diagram:e,notesById:t};let n=(e,n)=>{let r=`note ${e}`;return t.set(r,n),{id:r,label:n.text,attributes:[],methods:[]}},r=new Set(e.classes.map(e=>e.id)),i=[];for(let t of e.classes){i.push(t);for(let[r,a]of e.notes.entries())a.forClass===t.id&&i.push(n(r,a))}for(let[t,a]of e.notes.entries())(a.forClass===void 0||!r.has(a.forClass))&&i.push(n(t,a));return{diagram:{...e,classes:i},notesById:t}}function Hi(e,t){for(let[n,r]of e){if(r.forClass===void 0)continue;let e=t.get(r.forClass);e!==void 0&&t.set(n,e)}}function Ui(e,t,n,r,i){if(e.size===0)return;let a=mn(`rounded`,r),o=r?`.`:`╌`;for(let[r,s]of e){let e=t.get(r);if(!e)continue;let c=e.x+e.width-1,l=e.y+e.height-1;if(i(e.x,e.y,a.tl,`border`),i(c,e.y,a.tr,`border`),i(e.x,l,a.bl,`border`),i(c,l,a.br,`border`),s.forClass===void 0)continue;let u=t.get(s.forClass);if(!u||u.x+u.width>e.x||u.y!==e.y)continue;let d=u.y+1;for(let t=u.x+u.width;t<e.x;t++)n[t]?.[d]===` `&&i(t,d,o,`line`)}}function Wi(e){let t=e.trim();return t.length>=2&&(t[0]===`"`&&t[t.length-1]===`"`||t[0]===`'`&&t[t.length-1]===`'`)?t.slice(1,-1):t}var Gi=`"graph <dir>"/"flowchart <dir>" (dir: TD, TB, LR, BT, RL), "stateDiagram-v2", "sequenceDiagram", "classDiagram", "erDiagram", "xychart-beta", "C4Context"/"C4Container"/"C4Component"/"C4Dynamic"/"C4Deployment", "architecture-beta"`;function Ki(e){let t=e.trim().toLowerCase();if(/^sequence/.test(t))return`sequenceDiagram`;if(/^class/.test(t))return`classDiagram`;if(/^er/.test(t))return`erDiagram`;if(/^xychart/.test(t))return`xychart-beta`;if(/^state/.test(t))return`stateDiagram-v2`}var qi=/^(?:[\w-]+@)?<?(?:-{2,}|={2,}|-\.+-?|~{3,})/;function Ji(e){let t=[];for(let n of e)n.forEach((e,n)=>{if(n===0&&t.length>0&&qi.test(e.text)){let n=t[t.length-1];t[t.length-1]={text:`${n.text} ${e.text}`,line:n.line}}else t.push(e)});return t}function Yi(t){let n=(0,e.extractInitConfig)(t.split(`
|
|
5
|
-
`).map(e=>e.trim())),r=Ji((0,e.splitStatementsByLine)(t));if(r.length===0)throw Error(`Empty mermaid diagram`);let i=r[0],a=/^stateDiagram(-v2)?\s*$/i.test(i.text)?Zi(r):Xi(r);return a.initConfig=n,a}function Xi(t){let n=t[0];if(n===void 0)throw Error(`parseFlowchart called with no lines`);let r=n.text,i=r.match(/^(?:graph|flowchart)\s+(TD|TB|LR|BT|RL)\s*$/i);if(!i){let e=r.match(/^(?:graph|flowchart)\b\s*(.*)$/i);if(e){let t=e[1].trim();throw Error(t.length>0?`Line ${n.line}: Invalid direction "${t}" in header "${r}". Expected one of: TD, TB, LR, BT, RL.`:`Line ${n.line}: Missing direction in header "${r}". Expected e.g. "graph TD" or "flowchart LR" — one of: TD, TB, LR, BT, RL.`)}let t=Ki(r),i=t&&t.toLowerCase()!==r.trim().toLowerCase()?` Did you mean "${t}"?`:``;throw Error(`Line ${n.line}: Invalid mermaid header: "${r}".${i} Supported headers: ${Gi}.`)}let a={direction:(0,e.toDirection)(i[1]),nodes:new Map,edges:[],subgraphs:[],classDefs:new Map,classAssignments:new Map,nodeStyles:new Map,linkStyles:new Map,interactions:new Map},o=[];for(let n=1;n<t.length;n++){let r=t[n].text;if((0,e.tryApplyClassDef)(r,a)||(0,e.tryApplyClassAssignment)(r,a)||(0,e.tryApplyStyleStatement)(r,a))continue;if(/^click\s+/i.test(r)){ua(r,a);continue}if(fa(r,a))continue;let i=r.match(/^linkStyle\s+(default|[\d,\s]+)\s+(.+)$/);if(i){let t=i[1].trim(),n=(0,e.parseStyleProps)(i[2]);if(t==="default")a.linkStyles.set(`default`,{...a.linkStyles.get(`default`),...n});else{let e=t.split(`,`).map(e=>parseInt(e.trim(),10));for(let t of e)isNaN(t)||a.linkStyles.set(t,{...a.linkStyles.get(t),...n})}continue}let s=r.match(/^direction\s+(TD|TB|LR|BT|RL)\s*$/i);if(s&&o.length>0){o[o.length-1].direction=(0,e.toDirection)(s[1]);continue}let c=r.match(/^subgraph\s+(.+)$/);if(c){let t=c[1].trim(),n=t.match(/^([^\s[]+)\s*\[(.+)\]$/),r,i;n?(r=n[1],i=(0,e.normalizeBrTags)(Wi(n[2]))):(i=(0,e.normalizeBrTags)(Wi(t)),r=i.replace(/\s+/g,`_`).replace(/[^\p{L}\p{N}_-]/gu,``));let a={id:r,label:i,nodeIds:[],children:[]};o.push(a);continue}if(r===`end`){let e=o.pop();e&&(o.length>0?o[o.length-1].children.push(e):a.subgraphs.push(e));continue}ga(r,a,o)}return a}function Zi(t){let n={direction:`TD`,nodes:new Map,edges:[],subgraphs:[],classDefs:new Map,classAssignments:new Map,nodeStyles:new Map,linkStyles:new Map,interactions:new Map},r=[],i=new Set,a=0,o=0;for(let s=1;s<t.length;s++){let c=t[s].text;if((0,e.tryApplyClassDef)(c,n)||(0,e.tryApplyClassAssignment)(c,n)||(0,e.tryApplyStyleStatement)(c,n))continue;let l=c.match(/^direction\s+(TD|TB|LR|BT|RL)\s*$/i);if(l){r.length>0?r[r.length-1].direction=(0,e.toDirection)(l[1]):n.direction=(0,e.toDirection)(l[1]);continue}let u=c.match(/^linkStyle\s+(default|[\d,\s]+)\s+(.+)$/);if(u){let t=u[1].trim(),r=(0,e.parseStyleProps)(u[2]);if(t==="default")n.linkStyles.set(`default`,{...n.linkStyles.get(`default`),...r});else{let e=t.split(`,`).map(e=>parseInt(e.trim(),10));for(let t of e)isNaN(t)||n.linkStyles.set(t,{...n.linkStyles.get(t),...r})}continue}if(fa(c,n))continue;let d=c.match(/^state\s+(?:"([^"]+)"\s+as\s+)?([\w\p{L}]+)\s*\{$/u);if(d){let e=d[1]??d[2],t=d[2],a={id:t,label:e,nodeIds:[],children:[]};r.push(a),i.add(t),n.nodes.delete(t);continue}if(c===`}`){let e=r.pop();e&&(r.length>0?r[r.length-1].children.push(e):n.subgraphs.push(e));continue}let f=c.match(/^state\s+"([^"]+)"\s+as\s+([\w\p{L}]+)\s*$/u);if(f){let t=(0,e.normalizeBrTags)(f[1]),i=f[2];Qi(n,r,i,t);continue}let p=c.match(/^(\[\*\]|[\w\p{L}-]+)(?::::([\w][\w-]*))?\s*(?:([\w-]+)@)?-->\s*(\[\*\]|[\w\p{L}-]+)(?::::([\w][\w-]*))?(?:\s*:\s*(.+))?$/u);if(p){let t=p[1],s=p[2],c=p[3],l=p[4],u=p[5],d=p[6]?.trim(),f=d?(0,e.normalizeBrTags)(d):void 0;t===`[*]`?(a++,t=`_start${a>1?a:``}`,$i(n,r,{id:t,label:``,shape:`state-start`})):i.has(t)||ea(n,r,t),l===`[*]`?(o++,l=`_end${o>1?o:``}`,$i(n,r,{id:l,label:``,shape:`state-end`})):i.has(l)||ea(n,r,l),s!==void 0&&n.classAssignments.set(t,s),u!==void 0&&n.classAssignments.set(l,u),n.edges.push({source:t,target:l,label:f,style:`solid`,hasArrowStart:!1,hasArrowEnd:!0,...c===void 0?{}:{id:c}});continue}let m=c.match(/^([\w\p{L}-]+):::([\w][\w-]*)\s*$/u);if(m){let e=m[1];i.has(e)||ea(n,r,e),n.classAssignments.set(e,m[2]);continue}let h=c.match(/^([\w\p{L}-]+)\s*:\s*(.+)$/u);if(h){let t=h[1];Qi(n,r,t,(0,e.normalizeBrTags)(h[2].trim()));continue}}for(let e of n.classAssignments.keys())!n.nodes.has(e)&&!i.has(e)&&ea(n,[],e);return n}function Qi(e,t,n,r){let i=e.nodes.get(n);i&&(i.label=r),$i(e,t,{id:n,label:r,shape:`rounded`})}function $i(e,t,n){if(e.nodes.has(n.id)||e.nodes.set(n.id,n),t.length>0){let e=t[t.length-1];e.nodeIds.includes(n.id)||e.nodeIds.push(n.id)}}function ea(e,t,n){if(!e.nodes.has(n))$i(e,t,{id:n,label:n,shape:`rounded`});else if(t.length>0){let e=t[t.length-1];e.nodeIds.includes(n)||e.nodeIds.push(n)}}var ta=/^(<|o|x)?(-{2,}|={2,}|-\.+-|~{3,})(>|o|x)?(?:\|([^|]*)\|)?/,na=new Set([`--`,`==`]);function ra(e){if(e===`o`)return`circle`;if(e===`x`)return`cross`}var ia=/^(<|o|x)?(--|-\.|==)\s+(.+?)\s+(-{2,}[>ox]|={2,}[>ox]|\.+-[>ox]|-{3,}|={3,}|-\.+-)/,aa=[{regex:/^([\w\p{L}-]+)\(\(\(((?:"[^"]*"|(?!\)\)\)).)+)\)\)\)/u,shape:`doublecircle`},{regex:/^([\w\p{L}-]+)\(\[((?:"[^"]*"|(?!\]\)).)+)\]\)/u,shape:`stadium`},{regex:/^([\w\p{L}-]+)\(\(((?:"[^"]*"|(?!\)\)).)+)\)\)/u,shape:`circle`},{regex:/^([\w\p{L}-]+)\[\[((?:"[^"]*"|(?!\]\]).)+)\]\]/u,shape:`subroutine`},{regex:/^([\w\p{L}-]+)\[\(((?:"[^"]*"|(?!\)\]).)+)\)\]/u,shape:`cylinder`},{regex:/^([\w\p{L}-]+)\[\/((?:"[^"]*"|(?![\\/]\]).)+)([\\/])\]/u,shape:e=>e===`\\`?`trapezoid`:`parallelogram`},{regex:/^([\w\p{L}-]+)\[\\((?:"[^"]*"|(?![\\/]\]).)+)([\\/])\]/u,shape:e=>e===`/`?`trapezoid-alt`:`parallelogram-alt`},{regex:/^([\w\p{L}-]+)>((?:"[^"]*"|(?!\]).)+)\]/u,shape:`asymmetric`},{regex:/^([\w\p{L}-]+)\{\{((?:"[^"]*"|(?!\}\}).)+)\}\}/u,shape:`hexagon`},{regex:/^([\w\p{L}-]+)\[((?:"[^"]*"|(?!\]).)+)\]/u,shape:`rectangle`},{regex:/^([\w\p{L}-]+)\(((?:"[^"]*"|(?!\)).)+)\)/u,shape:`rounded`},{regex:/^([\w\p{L}-]+)\{((?:"[^"]*"|(?!\}).)+)\}/u,shape:`diamond`}],oa=/^([\w\p{L}]+(?:-[\w\p{L}]+)*)/u,sa=/^([\w\p{L}-]+)(?=@\{)/u;function ca(e){if(e.shape)return(0,t.resolveShapeName)(e.shape)??`rectangle`;if(e.icon!==void 0||e.img!==void 0)switch(e.form?.toLowerCase()){case`circle`:return`circle`;case`rounded`:return`rounded`;default:return`rectangle`}return`rectangle`}function la(e,t){return t.label!==void 0&&t.label.length>0?t.label:t.icon?t.icon:t.img?t.img:e}function ua(t,n){(0,e.applyClickStatement)(t,n.interactions)}var da=/^([\w-]+)(?=@\{)/;function fa(e,n){let r=e.match(da);if(!r||!n.edges.some(e=>e.id===r[1]))return!1;let i=(0,t.matchExpandedBlock)(e.slice(r[1].length));return i?(pa(n,r[1],(0,t.parseExpandedMeta)(i.body)),!0):!1}function pa(e,t,n){for(let r of e.edges)r.id===t&&(n.animate!==void 0&&(r.animate=n.animate.toLowerCase()!==`false`),n.animation!==void 0&&(r.animate=!0))}var ma=/^:::([\w][\w-]*)/,ha=/^([\w\p{L}-]+):::([\w][\w-]*)/u;function ga(t,n,r){let i=t.trim(),a=_a(i,n,r);if(!a||a.ids.length===0)return;i=a.remaining.trim();let o=a.ids;for(;i.length>0;){let t,a,s,c,l,u,d,f=i.match(/^([\w-]+)@(?=[-=<~ox])/);f&&(d=f[1],i=i.slice(f[0].length));let p=i.match(ta),m=p?.[2],h=p?.[1],g=p?.[3];if(p&&m!==void 0&&!(h===void 0&&g===void 0&&na.has(m))){t=h!==void 0,l=ra(h);let n=p[4]?.trim();c=n?(0,e.normalizeBrTags)(n):void 0,i=i.slice(p[0].length).trim(),a=xa(m),s=g!==void 0,u=ra(g)}else{let n=i.match(ia);if(!n)break;t=!!n[1],l=ra(n[1]);let r=n[3].trim();c=r?(0,e.normalizeBrTags)(r):void 0;let o=n[2],d=n[4];i=i.slice(n[0].length).trim(),a=Sa(o,d),u=ra(d.slice(-1)),s=d.endsWith(`>`)||u!==void 0}let _=_a(i,n,r);if(!_||_.ids.length===0)break;i=_.remaining.trim();for(let e of o)for(let r of _.ids)n.edges.push({source:e,target:r,label:c,style:a,hasArrowStart:t,hasArrowEnd:s,...l===void 0?{}:{startMarker:l},...u===void 0?{}:{endMarker:u},...d===void 0?{}:{id:d}});o=_.ids}}function _a(e,t,n){let r=va(e,t,n);if(!r)return null;let i=[r.id],a=r.remaining.trim();for(;a.startsWith(`&`);){a=a.slice(1).trim();let e=va(a,t,n);if(!e)break;i.push(e.id),a=e.remaining.trim()}return{ids:i,remaining:a}}function va(n,r,i){let a=null,o=n,s,c=o.match(ha);c&&(s=c[2],o=c[1]+o.slice(c[0].length));let l=o.match(sa);if(l){let n=l[1],s=(0,t.matchExpandedBlock)(o.slice(n.length));if(s){let c=(0,t.parseExpandedMeta)(s.body);ya(r,i,{id:n,label:(0,e.normalizeBrTags)(la(n,c)),shape:ca(c)}),a=n,o=o.slice(n.length+s.length)}}if(a===null)for(let{regex:t,shape:n}of aa){let s=o.match(t);if(s){a=s[1];let t=(0,e.normalizeBrTags)(s[2]),c=typeof n==`function`?n(s[3]??``):n;ya(r,i,{id:a,label:t,shape:c}),o=o.slice(s[0].length);break}}if(a===null){let e=o.match(oa);e&&(a=e[1],r.nodes.has(a)||ya(r,i,{id:a,label:a,shape:`rectangle`}),o=o.slice(e[0].length))}if(a===null)return null;s&&r.classAssignments.set(a,s);let u=o.match(ma);return u&&(r.classAssignments.set(a,u[1]),o=o.slice(u[0].length)),{id:a,remaining:o}}function ya(e,t,n){e.nodes.has(n.id)||e.nodes.set(n.id,n),ba(t,n.id)}function ba(e,t){if(e.length>0){let n=e[e.length-1];n.nodeIds.includes(t)||n.nodeIds.push(t)}}function xa(e){return e.startsWith(`~`)?`invisible`:e.includes(`.`)?`dotted`:e.startsWith(`=`)?`thick`:`solid`}function Sa(e,t){return e.includes(`.`)||t.includes(`.`)?`dotted`:e.startsWith(`=`)||t.startsWith(`=`)?`thick`:`solid`}function Ca(t,n){let r=new Set,i=new Map;for(let e of t.subgraphs)wa(e,r,i);let a=new Map,o=0;for(let[n,i]of t.nodes){if(r.has(n))continue;let t={name:n,displayLabel:(0,e.stripFormattingTags)(i.label),shape:i.shape,index:o,gridCoord:null,drawingCoord:null,drawing:null,drawn:!1,styleClassName:``,styleClass:tt};a.set(n,t),o++}let s=[...a.values()],c=[],l=new Map;for(let n of t.edges){let o=n.source,s=n.target,u;if(r.has(o)){let e=i.get(o),n=e?Da(e,t,`exit`):void 0;n&&(o=n),u=n?e:void 0}if(r.has(s)){let e=i.get(s),n=e?Da(e,t,`entry`):void 0;n&&(s=n)}let d=a.get(o),f=a.get(s);if(!d||!f)continue;let p={from:d,to:f,text:n.label?(0,e.stripFormattingTags)(n.label):``,path:[],labelLine:[],startDir:{x:0,y:0},endDir:{x:0,y:0},style:n.style,hasArrowStart:n.hasArrowStart,hasArrowEnd:n.hasArrowEnd,...n.startMarker===void 0?{}:{startMarker:n.startMarker},...n.endMarker===void 0?{}:{endMarker:n.endMarker}};c.push(p),u&&l.set(p,u)}let u=[];for(let e of t.subgraphs)Oa(e,null,a,u,t);if(ka(t.subgraphs,u,a),l.size>0){let e=new Map;ja(t.subgraphs,u,e);for(let[t,n]of l){let r=e.get(n);r&&(t.clusterSource=r)}}let d=t.classDefs.get(`default`);if(d)for(let e of a.values())e.styleClassName=`default`,e.styleClass={name:`default`,styles:d};for(let[e,n]of t.classAssignments){let r=a.get(e),i=t.classDefs.get(n);r&&i&&(r.styleClassName=n,r.styleClass={name:n,styles:d?{...d,...i}:i})}return{nodes:s,edges:c,canvas:F(0,0),roleCanvas:ae(0,0),grid:rt(),columnWidth:new Map,rowHeight:new Map,subgraphs:u,config:n,offsetX:0,offsetY:0,bundles:[]}}function wa(e,t,n){t.add(e.id),n.set(e.id,e);for(let r of e.children)wa(r,t,n)}function Ta(e,t){for(let n of e.nodeIds)t.add(n);for(let n of e.children)Ta(n,t)}function Ea(e,t){let n=new Set;if(Ta(e,n),n.size===0)return!0;let r=t=>n.has(t)||t===e.id;return!t.edges.some(e=>r(e.source)!==r(e.target))}function Da(e,t,n){let r=new Set;if(Ta(e,r),r.size===0)return;let i=[...t.nodes.keys()].filter(e=>r.has(e));if(i.length===0)return;let a=i.filter(e=>n===`entry`?!t.edges.some(t=>t.target===e&&r.has(t.source)):!t.edges.some(t=>t.source===e&&r.has(t.target))),o=a.length>0?a:i;return n===`entry`?o[0]:o[o.length-1]}function Oa(e,t,n,r,i){let a;e.direction&&Ea(e,i)&&(a=e.direction===`LR`||e.direction===`RL`?`LR`:`TD`);let o={name:e.label,nodes:[],parent:t,children:[],minX:0,minY:0,maxX:0,maxY:0,direction:a};for(let t of e.nodeIds){let e=n.get(t);e&&o.nodes.push(e)}r.push(o);for(let t of e.children){let e=Oa(t,o,n,r,i);o.children.push(e);for(let t of e.nodes)o.nodes.includes(t)||o.nodes.push(t)}return o}function ka(e,t,n){let r=new Map;ja(e,t,r);let i=new Map;function a(e){let t=r.get(e);if(t){for(let t of e.children)a(t);for(let n of e.nodeIds)i.has(n)||i.set(n,t)}}for(let t of e)a(t);for(let e of t)e.nodes=e.nodes.filter(t=>{let r;for(let[e,i]of n)if(i===t){r=e;break}if(!r)return!1;let a=i.get(r);return!a||Aa(e,a)})}function Aa(e,t){let n=t;for(;n!==null;){if(n===e)return!0;n=n.parent}return!1}function ja(e,t,n){let r=[];function i(e){for(let t of e)r.push(t),i(t.children)}i(e);for(let e=0;e<r.length&&e<t.length;e++){let i=r[e],a=t[e];!i||!a||n.set(i,a)}}function Ma(t,n,r,i,a={}){return Na((0,e.withDirectionOverride)(Yi(t),a.direction),n,r,i,a)}function Na(e,t,n,r,i){t.graphDirection=e.direction===`LR`||e.direction===`RL`?`LR`:`TD`;let a=Ca(e,t);lr(a),yi(a);let o=i.hyperlinks?d(a,e.interactions):void 0;return e.direction===`BT`&&(H(a.canvas,a.roleCanvas,a.labelRects??[],o),he(a.canvas,a.roleCanvas),_e(a.roleCanvas),o&&l(o)),pe(a.canvas,{roleCanvas:a.roleCanvas,colorMode:n,theme:r,linkCanvas:o})}function Pa(n,r,i,a,o={}){let s=(0,t.architectureToGraph)((0,t.parseArchitecture)((0,e.splitStatements)(n)));return Na((0,e.withDirectionOverride)(s,o.direction),r,i,a,o)}var Fa=28,Ia=2;function La(e,n){let r=new Map(e.map(e=>[e,e])),i=e=>{let t=e;for(;r.get(t)!==t;)t=r.get(t);return t},a=new Set(e);for(let e of n){let n=(0,t.c4Placement)(e);if(n.axis===`horizontal`&&a.has(n.source)&&a.has(n.target)){let e=i(n.source),t=i(n.target);e!==t&&r.set(t,e)}}let o=new Map;for(let t of e)o.set(i(t),o.get(i(t))??new Set);for(let e of n){let n=(0,t.c4Placement)(e);if(n.axis===`horizontal`||!a.has(n.source)||!a.has(n.target))continue;let r=i(n.source),s=i(n.target);r!==s&&o.get(r).add(s)}let s=new Map,c=new Map,l=e=>{s.set(e,1);let t=[];for(let n of o.get(e)??[]){let e=s.get(n)??0;e!==1&&(t.push(n),e===0&&l(n))}c.set(e,t),s.set(e,2)};for(let e of o.keys())s.has(e)||l(e);let u=new Map,d=e=>{let t=u.get(e);if(t!==void 0)return t;u.set(e,0);let n=0;for(let[t,r]of c)r.includes(e)&&(n=Math.max(n,d(t)+1));return u.set(e,n),n},f=new Map;for(let t of e)f.set(t,d(i(t)));return f}var Ra=[` o `,`/|\\`,`/ \\`];function za(e){let n=[];return e.kind===`person`&&n.push(...Ra),n.push(...(0,t.wrapC4Text)(e.label,Fa),(0,t.c4TypeLine)(e)),e.description&&n.push(``,...(0,t.wrapC4Text)(e.description,Fa)),{lines:n,glyph:e.kind===`person`}}function Ba(e,t){let{lines:n,glyph:r}=za(e),i=Math.max(...n.map(P)),a=e.shape===`db`?2:0;return{kind:`el`,el:e,lines:n,glyph:r,order:t,rank:0,box:{x:0,y:0,w:Math.max(14,i+4),h:n.length+2+a}}}function Va(n,r,i,a,o={}){let s=(0,e.withDirectionOverride)((0,t.parseC4Diagram)((0,e.splitStatements)(n)),o.direction);if(s.elements.length===0&&s.boundaries.length===0)return``;let c=r.useAscii,l=Xe(r.paddingX,5,6,4),u=Xe(r.paddingY,5,4,3)+ +!!s.relationships.some(e=>(0,t.c4RelLabelLines)(e).length>1),d=new Map;for(let[e,t]of s.elements.entries())d.set(t.alias,Ba(t,e));let f=La([...s.elements.map(e=>e.alias),...Ha(s.boundaries)],s.relationships);for(let[e,t]of d)t.rank=f.get(e)??0;let p=new Map,m=new Set,h=e=>{let n=[];for(let t of e.elementAliases)m.add(t),n.push(d.get(t));for(let t of e.children)n.push(h(t));let r=(0,t.c4BoundaryTypeLine)(e),i=r?2:1;if(n.length===0){let n=[...(0,t.wrapC4Text)(e.label,Fa)];r&&n.push(r);let a={kind:`b`,b:e,rows:[],vgaps:[],headerH:i,order:2**53-1,rank:f.get(e.alias)??0,box:{x:0,y:0,w:Math.max(14,Math.max(...n.map(P))+4),h:n.length+2}};return p.set(e.alias,a),a}let a={kind:`b`,b:e,rows:[],vgaps:[],headerH:i,order:0,rank:0,box:{x:0,y:0,w:0,h:0}};a.rows=Ka(n,s),s.direction===`BT`&&a.rows.reverse(),a.vgaps=Ya(a.rows,s,u),p.set(e.alias,a);let o=a.rows.flat();return a.order=Math.min(...o.map(e=>e.order)),a.rank=Math.min(...o.map(e=>e.rank)),qa(a,l),a},g=s.boundaries.map(h),_=Ka([...s.elements.filter(e=>!m.has(e.alias)).map(e=>d.get(e.alias)),...g],s);s.direction===`BT`&&_.reverse();let v=Ya(_,s,u),y=s.title?2:0,b=(e,t,n,r,i)=>{let a=r;for(let[r,o]of e.entries()){let e=Ga(o,s,l),c=o.reduce((e,t)=>e+t.box.w,0)+Ua(e),u=n+Math.floor((i-c)/2),d=Math.max(...o.map(e=>e.box.h));o.forEach((t,n)=>{x(t,u,a+Math.floor((d-t.box.h)/2)),u+=t.box.w+(e[n]??0)}),a+=d+(t[r]??0)}},x=(e,t,n)=>{e.box.x=t,e.box.y=n,e.kind===`b`&&e.rows.length>0&&b(e.rows,e.vgaps,t+2,n+e.headerH+2,e.box.w-4)},S=Math.max(..._.map(e=>e.reduce((e,t)=>e+t.box.w,0)+Ua(Ga(e,s,l))));b(_,v,Ia,Ia+y,S);let C=0,w=0,ee=[...d.values(),...p.values()].map(e=>e.box);for(let e of ee)C=Math.max(C,e.x+e.w),w=Math.max(w,e.y+e.h);let T=C+Ia+1,E=w+Ia+1,D=F(T,E),O=ae(T,E),k=(e,t,n,r)=>z(D,e,t,n,{role:r,roleCanvas:O}),A=(e,t,n,r)=>{let i=e;for(let e of ie(n))k(i,t,e,r),i++},te=Array.from({length:T+1},()=>Array(E+1).fill(0)),j=Array.from({length:T+1},()=>Array(E+1).fill(!1)),M=e=>{for(let t=e.x;t<e.x+e.w;t++)for(let n=e.y;n<e.y+e.h;n++)j[t][n]=!0};if(s.title){let e=P(s.title);A(Math.max(0,Math.floor((T-e)/2)),1,s.title,`text`);for(let e=0;e<T;e++)j[e][1]=!0}let ne=e=>{let{x:n,y:r,w:i,h:a}=e.box,o=c?`-`:`╌`,s=c?`:`:`╎`,[l,u,d,f]=c?[`+`,`+`,`+`,`+`]:[`╭`,`╮`,`╰`,`╯`];for(let e=0;e<i;e++)te[n+e][r]|=eo,te[n+e][r+a-1]|=eo;for(let e=0;e<a;e++)te[n][r+e]|=to,te[n+i-1][r+e]|=to;for(let e=1;e<i-1;e++)k(n+e,r,o,`border`),k(n+e,r+a-1,o,`border`);for(let e=1;e<a-1;e++)k(n,r+e,s,`border`),k(n+i-1,r+e,s,`border`);if(k(n,r,l,`border`),k(n+i-1,r,u,`border`),k(n,r+a-1,d,`border`),k(n+i-1,r+a-1,f,`border`),e.rows.length>0){A(n+2,r+1,e.b.label,`text`);let a=(0,t.c4BoundaryTypeLine)(e.b);a&&A(n+2,r+2,a,`text`);let o=Math.max(P(e.b.label),P(a??``))+2;for(let t=1;t<=o&&n+t<n+i-1;t++)for(let i=1;i<=e.headerH;i++)j[n+t][r+i]=!0}else{let a=[...(0,t.wrapC4Text)(e.b.label,Fa)],o=(0,t.c4BoundaryTypeLine)(e.b);o&&a.push(o),a.forEach((e,t)=>A(n+Math.floor((i-P(e))/2),r+1+t,e,`text`)),M(e.box)}},re=e=>{for(let t of e)t.kind===`b`&&(ne(t),re(t.rows.flat()))};re(_.flat());let N=e=>{let{x:t,y:n,w:r,h:i}=e.box,a=e.el,o=a.external,s=c?o?`.`:`-`:o?`┄`:`─`,l=c?o?`:`:`|`:o?`┆`:`│`,u=a.kind===`person`||a.shape!=="default",[d,f,p,m]=c?[`+`,`+`,`+`,`+`]:u?[`╭`,`╮`,`╰`,`╯`]:[`┌`,`┐`,`└`,`┘`];for(let e=1;e<r-1;e++)k(t+e,n,s,`border`),k(t+e,n+i-1,s,`border`);let h=a.shape===`queue`?`(`:l,g=a.shape===`queue`?`)`:l;for(let e=1;e<i-1;e++)k(t,n+e,h,`border`),k(t+r-1,n+e,g,`border`);k(t,n,d,`border`),k(t+r-1,n,f,`border`),k(t,n+i-1,p,`border`),k(t+r-1,n+i-1,m,`border`);let _=n+1;if(a.shape===`db`){let e=c?`+`:`├`,a=c?`+`:`┤`;for(let o of[n+1,n+i-2]){k(t,o,e,`junction`),k(t+r-1,o,a,`junction`);for(let e=1;e<r-1;e++)k(t+e,o,s,`border`)}_=n+2}e.lines.forEach((n,i)=>{let a=e.glyph&&i<Ra.length,o=P(n);A(t+Math.floor((r-o)/2),_+i,n,a?`border`:`text`)}),M(e.box)};for(let e of d.values())N(e);let I=e=>{let t=d.get(e);return t?{box:t.box,boundary:!1}:{box:p.get(e).box,boundary:!0}},oe=Array.from({length:T+1},()=>Array(E+1).fill(0)),L=new Map,R=[];for(let e of s.relationships){if(e.from===e.to)continue;let t=I(e.from),n=I(e.to),r=[];t.boundary&&p.get(e.from).rows.length>0&&r.push(t.box),n.boundary&&p.get(e.to).rows.length>0&&r.push(n.box);let i=lo(t.box,n.box,{width:T,height:E,blocked:j,masks:oe,arrows:L,borders:te},r);if(!i)continue;R.push({rel:e,path:i}),so(i,oe,t.box,n.box);let a=i[i.length-1],o=i[0];(!e.reversed||e.bidirectional)&&L.set(`${a[0]},${a[1]}`,ro(no(a,n.box),c)),(e.bidirectional||e.reversed)&&L.set(`${o[0]},${o[1]}`,ro(no(o,t.box),c))}for(let e=0;e<=T;e++)for(let t=0;t<=E;t++){let n=oe[e][t];if(n===0)continue;let r=L.get(`${e},${t}`);r?k(e,t,r,`arrow`):k(e,t,ao(n,c),oo(n)?`corner`:`line`)}for(let[e,t]of L){let[n,r]=e.split(`,`).map(Number);k(n,r,t,`arrow`)}let se=(e,t)=>e>=0&&t>=0&&e<=T&&t<=E&&D[e][t]===` `&&!j[e][t],ce=(e,t)=>e>=0&&t>=0&&e<=T&&t<=E&&!j[e][t]&&!L.has(`${e},${t}`)&&(D[e][t]===` `||oe[e][t]!==0);for(let{rel:e,path:n}of R){let r=(0,t.c4RelLabelLines)(e);if(r.length===0)continue;let i=Math.max(...r.map(P)),a=[r];for(let e of[18,12,8,6])i>e&&a.push(r.flatMap((n,r)=>r===0?(0,t.wrapC4Text)(n,e):[n]));let o=e=>a.some(t=>fo(t,n,se,ce,A,e));o(!1)||o(!0)}return pe(D,{roleCanvas:O,colorMode:i,theme:a})}function Ha(e){return e.flatMap(e=>e.elementAliases.length===0&&e.children.length===0?[e.alias]:Ha(e.children))}var Ua=e=>e.reduce((e,t)=>e+t,0);function Wa(e,n,r){let i=(e,t)=>e.kind===`el`?e.el.alias===t:e.b.alias===t,a=0;for(let o of r.relationships){let r=(0,t.c4Placement)(o);r.axis===`horizontal`&&(i(e,r.source)&&i(n,r.target)||i(n,r.source)&&i(e,r.target))&&(a=Math.max(a,...(0,t.c4RelLabelLines)(o).map(P)))}return a}function Ga(e,t,n){let r=[];for(let i=0;i<e.length-1;i++){let a=Wa(e[i],e[i+1],t);r.push(Math.max(n,a>0?a+4:0))}return r}function Ka(e,n){let r=new Map;for(let t of e)r.set(t.rank,[...r.get(t.rank)??[],t]);let i=[...r.entries()].sort((e,t)=>e[0]-t[0]).map(([,e])=>e.sort((e,t)=>e.order-t.order)),a=e=>e.kind===`el`?e.el.alias:e.b.alias;for(let e of i)for(let r=0;r<e.length;r++){let r=!1;for(let i of n.relationships){let n=(0,t.c4Placement)(i);if(n.axis!==`horizontal`)continue;let o=e.map(a),s=o.indexOf(n.source),c=o.indexOf(n.target);if(s<0||c<0||s<c)continue;let[l]=e.splice(s,1);e.splice(c,0,l),r=!0}if(!r)break}return i}function qa(e,n){let r=0,i=e.headerH+2;for(let[t,a]of e.rows.entries()){let o=Ua(a.map(e=>e.box.w))+(a.length-1)*n;r=Math.max(r,o),i+=Math.max(...a.map(e=>e.box.h))+(e.vgaps[t]??0)}let a=Math.max(P(e.b.label),P((0,t.c4BoundaryTypeLine)(e.b)??``));e.box.w=Math.max(r,a)+4,e.box.h=i+2}function Ja(e,t=new Set){if(e.kind===`el`)t.add(e.el.alias);else{t.add(e.b.alias);for(let n of e.rows)for(let e of n)Ja(e,t)}return t}function Ya(e,n,r){let i=new Map;e.forEach((e,t)=>{for(let n of e)for(let e of Ja(n))i.set(e,t)});let a=e.map(()=>0);for(let e of n.relationships){let n=i.get(e.from),r=i.get(e.to);if(n===void 0||r===void 0||n===r)continue;let o=(0,t.c4RelLabelLines)(e).length;if(o!==0)for(let e=Math.min(n,r);e<Math.max(n,r);e++)a[e]=a[e]+o+1}return e.slice(0,-1).map((e,t)=>Math.max(r,a[t]+1))}var Xa=1,Za=2,Qa=4,$a=8,eo=1,to=2;function no(e,t){let[n,r]=e;return r<t.y?Za:r>=t.y+t.h?Xa:n<t.x?$a:Qa}function ro(e,t){return e===Za?t?`v`:`▼`:e===Xa?t?`^`:`▲`:e===$a?t?`>`:`►`:t?`<`:`◄`}var io={[Xa]:`│`,[Za]:`│`,3:`│`,[Qa]:`─`,[$a]:`─`,12:`─`,10:`┌`,6:`┐`,9:`└`,5:`┘`,11:`├`,7:`┤`,14:`┬`,13:`┴`,15:`┼`};function ao(e,t){let n=io[e]??`┼`;return t?n===`│`?`|`:n===`─`?`-`:`+`:n}function oo(e){return e===10||e===6||e===9||e===5}function so(e,t,n,r){let i=(e,t)=>t[0]>e[0]?$a:t[0]<e[0]?Qa:t[1]>e[1]?Za:Xa,a=e=>e===Xa?Za:e===Za?Xa:e===Qa?$a:Qa;for(let n=0;n<e.length;n++){let[r,o]=e[n];n>0&&(t[r][o]|=a(i(e[n-1],e[n]))),n<e.length-1&&(t[r][o]|=i(e[n],e[n+1]))}let o=e[0],s=e[e.length-1];t[o[0]][o[1]]|=no(o,n),t[s[0]][s[1]]|=no(s,r)}function co(e,t,n){let r=[],i=e.x+e.w/2,a=e.y+e.h/2,o=t.x+t.w/2,s=t.y+t.h/2,c=t.y<e.y+e.h&&e.y<t.y+t.h,l=new Set;c?l.add(o>i?$a:Qa):l.add(s>a?Za:Xa);let u=e=>l.has(e)?0:e===Za&&l.has(Xa)||e===Xa&&l.has(Za)||e===Qa&&l.has($a)||e===$a&&l.has(Qa)?14:7,d=(e,t,i,a)=>{e<0||t<0||e>n.width||t>n.height||n.blocked[e][t]||r.push({cell:[e,t],cost:u(i)+Math.abs(a)*.2})};for(let t=e.x+1;t<e.x+e.w-1;t++)d(t,e.y-1,Xa,t-i),d(t,e.y+e.h,Za,t-i);for(let t=e.y+1;t<e.y+e.h-1;t++)d(e.x-1,t,Qa,t-a),d(e.x+e.w,t,$a,t-a);return r}function lo(e,t,n,r){let i=(e,t)=>r.some(n=>e>=n.x&&e<n.x+n.w&&t>=n.y&&t<n.y+n.h),a=co(e,t,n).filter(e=>!i(...e.cell)),o=new Map;for(let r of co(t,e,n))i(...r.cell)||o.set(`${r.cell[0]},${r.cell[1]}`,r.cost);if(a.length===0||o.size===0)return;let s=(e,n)=>(e<t.x?t.x-e:e>=t.x+t.w?e-(t.x+t.w-1):0)+(n<t.y?t.y-n:n>=t.y+t.h?n-(t.y+t.h-1):0),c=[],l=new Map;for(let e of a){let[t,n]=e.cell;c.push({x:t,y:n,dir:0,g:e.cost,f:e.cost+s(t,n),parent:void 0}),l.set(`${t},${n},0`,e.cost)}let u=[[0,-1,Xa],[0,1,Za],[-1,0,Qa],[1,0,$a]],d=0;for(;c.length>0&&d++<2e5;){let e=0;for(let t=1;t<c.length;t++)c[t].f<c[e].f&&(e=t);let t=c.splice(e,1)[0],r=`${t.x},${t.y}`;if(o.get(r)!==void 0){let e=[];for(let n=t;n;n=n.parent)e.push([n.x,n.y]);return e.reverse()}for(let[e,r,a]of u){let u=t.x+e,d=t.y+r;if(u<0||d<0||u>n.width||d>n.height)continue;let f=o.has(`${u},${d}`);if(n.blocked[u][d]&&!f||i(u,d)||n.arrows.has(`${u},${d}`))continue;let p=1;t.dir!==0&&t.dir!==a&&(p+=2);let m=n.borders[u][d];m!==0&&(a===Qa||a===$a?(m&eo)!==0:(m&to)!==0)&&(p+=12);let h=n.masks[u][d];if(h!==0){let e=a===Qa||a===$a,t=!!(h&12),n=!!(h&3);p+=e&&t||!e&&n?8:3}let g=t.g+p+(f?o.get(`${u},${d}`)??0:0),_=`${u},${d},${a}`;(l.get(_)??1/0)<=g||(l.set(_,g),c.push({x:u,y:d,dir:a,g,f:g+s(u,d),parent:t}))}}}function uo(e){let t=[],n=0;for(;n<e.length-1;){let r=e[n+1][1]===e[n][1],i=n+1;for(;i<e.length-1&&e[i+1][1]===e[i][1]===r;)i++;let[a,o]=e[n],[s,c]=e[i];t.push({horizontal:r,x0:Math.min(a,s),y0:Math.min(o,c),len:Math.abs(s-a)+Math.abs(c-o)+1}),n=i}return t}function fo(e,t,n,r,i,a){let o=e.map(P),s=e=>e.every(([e,t],r)=>{let i=o[r];for(let r=0;r<i;r++)if(!n(e+r,t))return!1;return n(e-1,t)&&n(e+i,t)}),c=e=>Array.from({length:e},(e,t)=>t).sort((t,n)=>Math.abs(t-e/2)-Math.abs(n-e/2));for(let n of a?[]:uo(t).sort((e,t)=>t.len-e.len))for(let t of c(n.len)){let r=[];if(n.horizontal){let i=n.x0+t;r.push(e.map((e,t)=>[i-Math.floor(o[t]/2),t===0?n.y0-1:n.y0+t])),e.length===1&&r.push([[i-Math.floor(o[0]/2),n.y0+1]])}else{let i=n.y0+t-Math.floor((e.length-1)/2);for(let t of[1,-1])r.push(e.map((e,r)=>[t>0?n.x0+2:n.x0-1-o[r],i+r]))}for(let t of r)if(s(t))return t.forEach(([t,n],r)=>i(t,n,e[r],`text`)),!0}let l=e=>e.every(([e,t],n)=>{for(let i=0;i<o[n];i++)if(!r(e+i,t))return!1;return!0});for(let n of a?uo(t).sort((e,t)=>t.len-e.len):[])for(let t of c(n.len)){let r=e.map((r,i)=>n.horizontal?[n.x0+t-Math.floor(o[i]/2),n.y0+i-+(e.length>1)]:[n.x0-Math.floor(o[i]/2),n.y0+t+i]);if(l(r))return r.forEach(([t,n],r)=>i(t,n,e[r],`text`)),!0}return!1}var po={xychart:(e,t,n,r)=>Ee(e,t,n,r),er:(e,t,n,r)=>Ei(e,t,n,r),sequence:(e,t,n,r)=>Mi(e,t,n,r),class:(e,t,n,r,i)=>Bi(e,t,n,r,i),flowchart:(e,t,n,r,i)=>Ma(e,t,n,r,i),architecture:(e,t,n,r,i)=>Pa(e,t,n,r,i),c4:(e,t,n,r,i)=>Va(e,t,n,r,i)},mo=/\x1b\[[0-9;]*m/g;function ho(e){let t=e.split(`
|
|
6
|
-
`),n=t.reduce((e,t)=>Math.max(e,o(t.replace(
|
|
7
|
-
`)}function
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});let e=require("@zombie-mermaid/core"),t=require("@zombie-mermaid/mermaid-parser");function n(e){return[e.length-1,(e[0]?.length??1)-1]}var r=`\x1B]8;;\x1B\\`;function i(e){return`\x1b]8;;${e.replace(/[^\x21-\x7e]/gu,e=>encodeURIComponent(e))}\x1b\\`}var a=/\x1b\]8;[^\x07\x1b]*(?:\x1b\\|\x07)/g;function o(e){return e.replace(a,``)}function s(e,t){let n=[];for(let r=0;r<=e;r++){let e=[];for(let n=0;n<=t;n++)e.push(null);n.push(e)}return n}function c(e,t,n,r){let i=e[t];i===void 0||n<0||n>=i.length||(i[n]=r)}function l(e){for(let t of e)t.reverse();return e}function u(e){return e.reverse(),e}function d(e,t,r,i,a){let[o,s]=n(t),l=Math.max(1,a?.from??1),u=Math.min(s-1,a?.to??s-1);for(let n=l;n<=u;n++){let a=-1,s=-1;for(let e=1;e<=o-1;e++)t[e]?.[n]!==` `&&(a===-1&&(a=e),s=e);if(a!==-1)for(let t=a;t<=s;t++)c(e,t+r.x,n+r.y,i)}}function f(t,r){let[i,a]=n(t.canvas),o=s(i,a);for(let n of t.nodes){if(!n.drawing||!n.drawingCoord)continue;let t=(0,e.safeHref)(r.get(n.name)?.href);t!==void 0&&d(o,n.drawing,n.drawingCoord,t)}return o}var p=class{current=null;advance(e){if(e===this.current)return``;let t=this.current===null?``:r;return e!==null&&(t+=i(e)),this.current=e,t}finish(){let e=this.current===null?``:r;return this.current=null,e}};function m(e,t){let n=new p,r=``;for(let[i,a]of e.entries())r+=n.advance(t[i]??null)+a;return r+n.finish()}var h={fg:`#27272a`,border:`#a1a1aa`,line:`#71717a`,arrow:`#52525b`,corner:`#71717a`,junction:`#a1a1aa`};function g(t){let n=t.line??(0,e.mixHexColors)(t.fg,t.bg,e.MIX.line),r=t.border??(0,e.mixHexColors)(t.fg,t.bg,e.MIX.nodeStroke);return{fg:t.fg,border:r,line:n,arrow:t.accent??(0,e.mixHexColors)(t.fg,t.bg,e.MIX.arrow),accent:t.accent,bg:t.bg,corner:n,junction:r}}function _(){let e=globalThis.process;if(e){if(!e.stdout?.isTTY)return`none`;let t=e.env?.COLORTERM?.toLowerCase()??``,n=e.env?.TERM?.toLowerCase()??``;return t===`truecolor`||t===`24bit`?`truecolor`:n.includes(`256color`)||n.includes(`256`)?`ansi256`:n&&n!==`dumb`?`ansi16`:`none`}return typeof document<`u`?`html`:`none`}function v(t){return(0,e.parseHexRgba)(t.startsWith(`#`)?t:`#${t}`)??{r:0,g:0,b:0}}var y=`\x1B[`,b=`${y}0m`;function x(e){let{r:t,g:n,b:r}=v(e);return`${y}38;2;${t};${n};${r}m`}function S(e,t,n){let r=(e+t+n)/3;if(Math.max(Math.abs(e-r),Math.abs(t-r),Math.abs(n-r))<10){let e=Math.round(r/255*23);return 232+Math.min(23,Math.max(0,e))}let i=e=>e<48?0:e<115?1:Math.min(5,Math.floor((e-35)/40)),a=i(e),o=i(t),s=i(n);return 16+36*a+6*o+s}function C(e){let{r:t,g:n,b:r}=v(e);return`${y}38;5;${S(t,n,r)}m`}function w(e){let{r:t,g:n,b:r}=v(e),i=.299*t+.587*n+.114*r,a=i>100?0:60,o;return o=t>180&&n<100&&r<100?31:n>180&&t<100&&r<100?32:t>150&&n>150&&r<100?33:r>180&&t<100&&n<100?34:t>150&&r>150&&n<100?35:n>150&&r>150&&t<100?36:i>200?37:i<50?30:37,`${y}${o+a}m`}function T(e){return e.replace(/&/g,`&`).replace(/</g,`<`).replace(/>/g,`>`)}var E=/^█+$/;function D(e,t){return`<span style="${E.test(t)?`color:${e};background:${e}`:`color:${e}`}">${T(t)}</span>`}function ee(e,t){switch(e){case`text`:return t.fg;case`border`:return t.border;case`line`:return t.line;case`arrow`:return t.arrow;case`corner`:return t.corner??t.line;case`junction`:return t.junction??t.border;default:return t.fg}}function O(e,t,n){if(n===`none`)return``;let r=ee(e,t);switch(n){case`truecolor`:return x(r);case`ansi256`:return C(r);case`ansi16`:return w(r);default:return``}}function k(e,t,n,r,i){if(r===`none`)return i?m(e,i):e.join(``);if(r===`html`)return A(e,t,n);let a=new p,o=``,s=null,c=``;for(let[l,u]of e.entries()){let e=t[l]??null,d=i?a.advance(i[l]??null):``;if(u===` `){c.length>0&&(o+=s===null?c:O(s,n,r)+c+b,c=``,s=null),o+=d+u;continue}if(e===s){c+=d+u;continue}c.length>0&&(o+=s===null?c:O(s,n,r)+c+b),c=d+u,s=e}return c.length>0&&s!==null?o+=O(s,n,r)+c+b:c.length>0&&(o+=c),o+a.finish()}function A(e,t,n){let r=``,i=null,a=``,o=()=>{a.length!==0&&(r+=i===null?T(a):D(ee(i,n),a),a=``,i=null)};for(let[n,s]of e.entries()){let e=t[n]??null;if(s===` `){o(),r+=` `;continue}if(e===i){a+=s;continue}o(),a=s,i=e}return o(),r}function te(e,t,n){if(n===`none`||e.length===0)return e;if(n===`html`)return D(t,e);let r;switch(n){case`truecolor`:r=x(t);break;case`ansi256`:r=C(t);break;case`ansi16`:r=w(t);break;default:return e}return`${r}${e}${b}`}var j=/\p{Mn}|\p{Me}/u;function ne(e){return j.test(e)}var re=new Intl.Segmenter(void 0,{granularity:`grapheme`}),M=`️`;function N(e){let t=[];for(let{segment:n}of re.segment(e))t.push(n);return t}function ie(t){if(t.includes(M))return 2;let n=!1,r=!1;for(let i of t)(0,e.isWideChar)(i)&&(n=!0),ne(i)||(r=!0);return n?2:+!!r}function P(e){let t=0;for(let n of N(e))t+=ie(n);return t}function ae(e){let t=[];for(let n of N(e)){let e=ie(n);e!==0&&(t.push(n),e===2&&t.push(``))}return t}function F(e,t){let n=[];for(let r=0;r<=e;r++){let e=[];for(let n=0;n<=t;n++)e.push(` `);n.push(e)}return n}function I(e){let[t,n]=R(e);return F(t,n)}function oe(e,t){let n=[];for(let r=0;r<=e;r++){let e=[];for(let n=0;n<=t;n++)e.push(null);n.push(e)}return n}function se(e,t,n){let r=e.length-1,i=(e[0]?.length??1)-1,a=oe(Math.max(t,r),Math.max(n,i));for(let t=0;t<a.length;t++)for(let n=0;n<(a[0]?.length??0);n++)t<e.length&&n<(e[0]?.length??0)&&(a[t][n]=e[t][n]);return e.length=0,e.push(...a),e}function L(e,t,n,r){(t>=e.length||n>=(e[0]?.length??0))&&se(e,t,n),e[t][n]=r}function R(e){return[e.length-1,(e[0]?.length??1)-1]}function ce(e,t,n){let[r,i]=R(e),a=F(Math.max(t,r),Math.max(n,i));for(let t=0;t<a.length;t++)for(let n=0;n<(a[0]?.length??0);n++)t<e.length&&n<(e[0]?.length??0)&&(a[t][n]=e[t][n]);return e.length=0,e.push(...a),e}function z(e,t,n,r,i){let[a,o]=R(e);t<0||t>a||n<0||n>o||(e[t][n]=r,i&&L(i.roleCanvas,t,n,i.role))}var B=new Set([`─`,`│`,`┌`,`┐`,`└`,`┘`,`├`,`┤`,`┬`,`┴`,`┼`,`╴`,`╵`,`╶`,`╷`]);function le(e){return B.has(e)}function ue(e){return/^[a-zA-Z0-9]$/.test(e)}var de={"─":{"│":`┼`,"┌":`┬`,"┐":`┬`,"└":`┴`,"┘":`┴`,"├":`┼`,"┤":`┼`,"┬":`┬`,"┴":`┴`},"│":{"─":`┼`,"┌":`├`,"┐":`┤`,"└":`├`,"┘":`┤`,"├":`├`,"┤":`┤`,"┬":`┼`,"┴":`┼`},"┌":{"─":`┬`,"│":`├`,"┐":`┬`,"└":`├`,"┘":`┼`,"├":`├`,"┤":`┼`,"┬":`┬`,"┴":`┼`},"┐":{"─":`┬`,"│":`┤`,"┌":`┬`,"└":`┼`,"┘":`┤`,"├":`┼`,"┤":`┤`,"┬":`┬`,"┴":`┼`},"└":{"─":`┴`,"│":`├`,"┌":`├`,"┐":`┼`,"┘":`┴`,"├":`├`,"┤":`┼`,"┬":`┼`,"┴":`┴`},"┘":{"─":`┴`,"│":`┤`,"┌":`┼`,"┐":`┤`,"└":`┴`,"├":`┼`,"┤":`┤`,"┬":`┼`,"┴":`┴`},"├":{"─":`┼`,"│":`├`,"┌":`├`,"┐":`┼`,"└":`├`,"┘":`┼`,"┤":`┼`,"┬":`┼`,"┴":`┼`},"┤":{"─":`┼`,"│":`┤`,"┌":`┼`,"┐":`┤`,"└":`┼`,"┘":`┤`,"├":`┼`,"┬":`┼`,"┴":`┼`},"┬":{"─":`┬`,"│":`┼`,"┌":`┬`,"┐":`┬`,"└":`┼`,"┘":`┼`,"├":`┼`,"┤":`┼`,"┴":`┼`},"┴":{"─":`┴`,"│":`┼`,"┌":`┼`,"┐":`┼`,"└":`┴`,"┘":`┴`,"├":`┼`,"┤":`┼`,"┬":`┼`}};function fe(e,t){return de[e]?.[t]??e}function pe(e){let t=new Map,n=[];for(let r of e){let[e,i]=R(r),a=I(r);for(let n=0;n<=e;n++)for(let e=0;e<=i;e++){let i=r[n]?.[e];if(i===void 0||i===` `)continue;let o=`${n},${e}`,s=t.get(o);if(s!==void 0){if(!(s!==i&&le(s)&&le(i)))continue}else t.set(o,i);a[n][e]=i}n.push(a)}return n}function me(e,t,n,...r){let[i,a]=R(e);for(let e of r){let[n,r]=R(e);i=Math.max(i,n+t.x),a=Math.max(a,r+t.y)}let o=F(i,a);for(let t=0;t<=i;t++)for(let n=0;n<=a;n++)t<e.length&&n<(e[0]?.length??0)&&(o[t][n]=e[t][n]);for(let e of r)for(let r=0;r<e.length;r++)for(let i=0;i<(e[0]?.length??0);i++){let a=e[r][i];if(a!==` `){let e=r+t.x,s=i+t.y,c=o[e][s];!n&&le(a)&&le(c)?o[e][s]=fe(c,a):ue(c)&&ue(a)||(o[e][s]=a)}}return o}function he(e,t){let[n,r]=R(e),i=[],a=t?.roleCanvas,o=t?.colorMode??`none`,s=t?.theme??h,c=o===`html`?void 0:t?.linkCanvas;for(let t=0;t<=r;t++)if(o===`none`||!a){let r=[];for(let i=0;i<=n;i++)r.push(e[i][t]);if(c){let e=r.map((e,n)=>c[n]?.[t]??null);i.push(m(r,e))}else i.push(r.join(``))}else{let r=[],l=[],u=[];for(let i=0;i<=n;i++)r.push(e[i][t]),l.push(a[i]?.[t]??null),u.push(c?.[i]?.[t]??null);i.push(k(r,l,s,o,c?u:void 0))}return i.join(`
|
|
2
|
+
`)}var V={"▲":`▼`,"▼":`▲`,"↖":`↙`,"↙":`↖`,"↗":`↘`,"↘":`↗`,"^":`v`,v:`^`,"┌":`└`,"└":`┌`,"┐":`┘`,"┘":`┐`,"┬":`┴`,"┴":`┬`,"╭":`╰`,"╰":`╭`,"╮":`╯`,"╯":`╮`,"╵":`╷`,"╷":`╵`};function H(e,t){for(let[n,r]of e.entries())for(let[e,i]of r.entries()){if(t?.[n]?.[e]===`text`)continue;let a=V[i];a&&(r[e]=a)}for(let t of e)t.reverse();return e}function ge(e,t,n,r){let i=[],a=new Set;for(let{x0:e,y0:r,x1:o,y1:s}of n){if(r===s)continue;let n=[];for(let i=e;i<=o;i++)for(let e=r;e<=s;e++){let r=`${i},${e}`;t[i]?.[e]!==`text`||a.has(r)||(a.add(r),n.push([i,e]))}i.push({cells:n,ySum:r+s})}for(let{cells:n,ySum:a}of i)_e(e,n,a,` `),_e(t,n,a,null),r&&_e(r,n,a,null)}function _e(e,t,n,r){let i=t.map(([t,n])=>e[t][n]);for(let[n,i]of t)e[n][i]=r;for(let[r,[a,o]]of t.entries())e[a][n-o]=i[r]}function ve(e){for(let t of e)t.reverse();return e}var ye={"►":`◄`,"◄":`►`,">":`<`,"<":`>`,"↖":`↗`,"↗":`↖`,"↙":`↘`,"↘":`↙`,"┌":`┐`,"┐":`┌`,"└":`┘`,"┘":`└`,"├":`┤`,"┤":`├`,"╭":`╮`,"╮":`╭`,"╰":`╯`,"╯":`╰`,"╔":`╗`,"╗":`╔`,"╚":`╝`,"╝":`╚`,"╟":`╢`,"╢":`╟`,"⌜":`⌝`,"⌝":`⌜`,"⌞":`⌟`,"⌟":`⌞`,"╱":`╲`,"╲":`╱`,"/":`\\`,"\\":`/`,"◢":`◣`,"◣":`◢`,"◤":`◥`,"◥":`◤`,"◸":`◹`,"◹":`◸`,"◺":`◿`,"◿":`◺`};function be(e,t){for(let[n,r]of e.entries())for(let[e,i]of r.entries()){if(t?.[n]?.[e]===`text`)continue;let a=ye[i];a&&(r[e]=a)}return e.reverse(),e}function xe(e,t,n,r){let i=[],a=new Set;for(let{x0:r,y0:o,x1:s,y1:c}of n){if(r===s)continue;let n=[];for(let i=r;i<=s;i++)for(let r=o;r<=c;r++){let o=`${i},${r}`;t[i]?.[r]!==`text`&&e[i]?.[r]!==``||a.has(o)||(a.add(o),n.push([i,r]))}i.push({cells:n,xSum:r+s})}for(let{cells:n,xSum:a}of i)Se(e,n,a,` `),Se(t,n,a,null),r&&Se(r,n,a,null)}function Se(e,t,n,r){let i=t.map(([t,n])=>e[t][n]);for(let[n,i]of t)e[n][i]=r;for(let[r,[a,o]]of t.entries())e[n-a][o]=i[r]}function Ce(e){return e.reverse(),e}function we(e,t,n,r=!1){let i=ae(n);ce(e,t.x+i.length,t.y);for(let[n,a]of i.entries()){let i=t.x+n,o=e[i][t.y];(r||o===` `)&&(e[i][t.y]=a)}}function Te(e,t,n,r=0,i=0){let a=r,o=i;for(let e of t.values())a+=e;for(let e of n.values())o+=e;ce(e,a-1,o-1)}function U(e,t,n,r=0,i=0){let a=r,o=i;for(let e of t.values())a+=e;for(let e of n.values())o+=e;se(e,a-1,o-1)}var Ee=60,De=20,Oe={hLine:`─`,vLine:`│`,origin:`┼`,yTick:`┤`,xTick:`┬`,bar:`█`,grid:`·`,cornerTL:`╭`,cornerTR:`╮`,cornerBL:`╰`,cornerBR:`╯`},ke={hLine:`-`,vLine:`|`,origin:`+`,yTick:`+`,xTick:`+`,bar:`#`,grid:`.`,cornerTL:`+`,cornerTR:`+`,cornerBL:`+`,cornerBR:`+`};function Ae(e,n){let r=n.accent??t.CHART_ACCENT_FALLBACK;return e<=1?[r]:Array.from({length:e},(e,i)=>(0,t.getSeriesColor)(i,r,n.bg))}function je(e,t){switch(e){case`text`:return t.fg;case`border`:return t.border;case`line`:return t.line;case`arrow`:return t.arrow;case`corner`:return t.corner??t.line;case`junction`:return t.junction??t.border;default:return t.fg}}function Me(n,r,i,a){let o=(0,e.splitStatements)(n),s=(0,t.parseXYChart)(o),c=r.useAscii?ke:Oe;return s.horizontal?W(s,c,i,a):Ne(s,c,i,a)}function Ne(e,t,n,r){let i=We(e);if(i===0)return``;let a=e.yAxis.range;if(!a)throw Error(`XY chart: y-axis range was not set by the parser`);let o=Ke(a.min,a.max),s=qe(o),c=Math.max(...s.map(e=>e.length))+1,l=Math.max(Ee,i*6),u=De,d=Math.floor(l/i),f=Ge(e,i),p=!!e.title,m=!!e.xAxis.title,h=!!e.yAxis.title,g=e.series.length>1,_=p?0:-1,v=(p?2:0)+ +!!g,y=h?2:0,b=y+c+1,x=b+d*i+2,S=v+u,C=S+1,w=m?C+1:-1,T=C+1+ +!!m+0,E=Le(x,T),D=Re(x,T),ee=ze(x,T),O=Ae(e.series.length,r),k=e=>{let t=(e-a.min)/(a.max-a.min||1);return Math.round(t*19)},A=e=>b+Math.floor(d*(e+.5));e.title&&_>=0&&Ve(E,D,_,Math.floor(x/2-e.title.length/2),e.title,`text`),g&&Ie(E,D,ee,e,+!!p,x,t,O);for(let e=0;e<u;e++)G(E,D,v+(19-e),b-1,t.vLine,`border`);G(E,D,S,b-1,t.origin,`border`);for(let[e,n]of o.entries()){let r=k(n);if(r<0||r>=u)continue;let i=v+(19-r),a=s[e];G(E,D,i,b-1,r===0?t.origin:t.yTick,`border`);let o=y+c-a.length;Ve(E,D,i,Math.max(0,o),a,`text`)}for(let e=b;e<b+d*i;e++)G(E,D,S,e,t.hLine,`border`);for(let e=0;e<i;e++){let n=A(e);G(E,D,S,n,t.xTick,`border`);let r=f[e],i=n-Math.floor(r.length/2);Ve(E,D,C,Math.max(0,i),r,`text`)}if(e.xAxis.title&&w>=0){let t=e.xAxis.title;Ve(E,D,w,Math.floor(x/2-t.length/2),t,`text`)}if(e.yAxis.title){let t=e.yAxis.title,n=v+Math.max(0,Math.floor((u-t.length)/2));for(let e=0;e<t.length;e++){let r=n+e;if(r>=v+u)break;G(E,D,r,0,t[e],`text`)}}for(let e of o){let n=k(e);if(n<0||n>=u)continue;let r=v+(19-n);for(let e=b;e<b+d*i;e++)Be(E,r,e)===` `&&G(E,D,r,e,t.grid,`line`)}let te=[];for(let t=0;t<e.series.length;t++)e.series[t].type===`bar`&&te.push({data:e.series[t].data,globalIdx:t});if(te.length>0){let e=Math.max(1,d-2),n=Math.max(1,Math.min(e,8)),r=k(Math.max(0,a.min));for(let e=0;e<te.length;e++){let i=te[e],a=O[i.globalIdx];for(let e=0;e<i.data.length;e++){let o=A(e)-Math.floor(n/2),s=k(i.data[e]),c=Math.min(r,s),l=Math.max(r,s);for(let e=c;e<=l;e++){let r=v+(19-e);for(let e=o;e<o+n;e++)G(E,D,r,e,t.bar,`arrow`,ee,a)}}}}let j=[];for(let t=0;t<e.series.length;t++)e.series[t].type===`line`&&j.push({data:e.series[t].data,globalIdx:t});for(let e of j){if(e.data.length===0)continue;let n=O[e.globalIdx];Pe(E,D,e.data,A,k,v,u,b,d*i,t,ee,n)}return He(E,D,ee,n,r)}function W(e,t,n,r){let i=We(e);if(i===0)return``;let a=e.yAxis.range;if(!a)throw Error(`XY chart: y-axis range was not set by the parser`);let o=Ke(a.min,a.max),s=qe(o),c=Ge(e,i),l=Math.max(...c.map(e=>e.length))+1,u=Math.max(Ee,40),d=Math.max(2,Math.floor(De/i)),f=d*i,p=!!e.title,m=!!e.yAxis.title,h=e.series.length>1,g=(p?2:0)+ +!!h,_=l+1,v=_+u+2,y=g+f+2+ +!!m,b=g+f,x=Le(v,y),S=Re(v,y),C=ze(v,y),w=Ae(e.series.length,r),T=e=>{let t=(e-a.min)/(a.max-a.min||1);return _+Math.round(t*59)},E=e=>g+Math.floor(d*(e+.5));e.title&&Ve(x,S,0,Math.floor(v/2-e.title.length/2),e.title,`text`),h&&Ie(x,S,C,e,+!!p,v,t,w);for(let e=g;e<g+f;e++)G(x,S,e,_-1,t.vLine,`border`);G(x,S,b,_-1,t.origin,`border`);for(let e=0;e<i;e++){let t=E(e),n=c[e],r=l-n.length;Ve(x,S,t,Math.max(0,r),n,`text`)}for(let e=_;e<_+u;e++)G(x,S,b,e,t.hLine,`border`);for(let[e,n]of o.entries()){let r=T(n);if(r<_||r>=_+u)continue;G(x,S,b,r,t.xTick,`border`);let i=s[e];Ve(x,S,b+1,r-Math.floor(i.length/2),i,`text`)}if(e.yAxis.title){let t=e.yAxis.title;Ve(x,S,y-1,Math.floor(v/2-t.length/2),t,`text`)}for(let e of o){let n=T(e);if(!(n<_||n>=_+u))for(let e=g;e<g+f;e++)Be(x,e,n)===` `&&G(x,S,e,n,t.grid,`line`)}let D=[];for(let t=0;t<e.series.length;t++)e.series[t].type===`bar`&&D.push({data:e.series[t].data,globalIdx:t});if(D.length>0){let e=T(Math.max(0,a.min));for(let n=0;n<D.length;n++){let r=D[n],i=w[r.globalIdx];for(let n=0;n<r.data.length;n++){let a=E(n),o=T(r.data[n]),s=Math.min(e,o),c=Math.max(e,o);for(let e=a;e<a+1;e++)for(let n=s;n<=c;n++)G(x,S,e,n,t.bar,`arrow`,C,i)}}}let ee=[];for(let t=0;t<e.series.length;t++)e.series[t].type===`line`&&ee.push({data:e.series[t].data,globalIdx:t});for(let e of ee){if(e.data.length===0)continue;let n=w[e.globalIdx];Fe(x,S,e.data,E,T,g,f,_,u,t,C,n)}return He(x,S,C,n,r)}function Pe(e,t,n,r,i,a,o,s,c,l,u,d){if(n.length===0)return;let f=n.map((e,t)=>({col:r(t),row:i(e)})),p=(n,r,i)=>{let l=a+(o-1-r);l>=0&&n>=s&&n<s+c&&G(e,t,l,n,i,`arrow`,u,d)};if(f.length===1){p(f[0].col,f[0].row,l.hLine);return}for(let e=0;e<f.length-1;e++){let t=f[e],n=f[e+1];if(t.row===n.row){for(let e=t.col;e<=n.col;e++)p(e,t.row,l.hLine);continue}let r=Math.round((t.col+n.col)/2),i=n.row>t.row;for(let e=t.col;e<r;e++)p(e,t.row,l.hLine);i?p(r,t.row,l.cornerBR):p(r,t.row,l.cornerTR);let a=Math.min(t.row,n.row),o=Math.max(t.row,n.row);for(let e=a+1;e<o;e++)p(r,e,l.vLine);i?p(r,n.row,l.cornerTL):p(r,n.row,l.cornerBL);for(let e=r+1;e<=n.col;e++)p(e,n.row,l.hLine);if(e===0){let e=Math.max(s,t.col-Math.floor((n.col-t.col)/4));for(let n=e;n<t.col;n++)p(n,t.row,l.hLine)}if(e===f.length-2){let e=Math.min(s+c-1,n.col+Math.floor((n.col-t.col)/4));for(let t=n.col+1;t<=e;t++)p(t,n.row,l.hLine)}}}function Fe(e,t,n,r,i,a,o,s,c,l,u,d){if(n.length===0)return;let f=n.map((e,t)=>({row:r(t),col:i(e)})),p=(n,r,i)=>{n>=a&&n<a+o&&r>=s&&r<s+c&&G(e,t,n,r,i,`arrow`,u,d)};if(f.length===1){p(f[0].row,f[0].col,l.vLine);return}for(let e=0;e<f.length-1;e++){let t=f[e],n=f[e+1];if(t.col===n.col){for(let e=t.row;e<=n.row;e++)p(e,t.col,l.vLine);continue}let r=Math.round((t.row+n.row)/2),i=n.col>t.col;for(let e=t.row;e<r;e++)p(e,t.col,l.vLine);i?p(r,t.col,l.cornerBL):p(r,t.col,l.cornerBR);let a=Math.min(t.col,n.col),o=Math.max(t.col,n.col);for(let e=a+1;e<o;e++)p(r,e,l.hLine);i?p(r,n.col,l.cornerTR):p(r,n.col,l.cornerTL);for(let e=r+1;e<=n.row;e++)p(e,n.col,l.vLine)}}function Ie(e,t,n,r,i,a,o,s){let c=[],l=0,u=0;for(let e=0;e<r.series.length;e++)r.series[e].type===`bar`?(c.push({symbol:o.bar,label:`Bar ${l+1}`,globalIdx:e}),l++):(c.push({symbol:o.hLine,label:`Line ${u+1}`,globalIdx:e}),u++);let d=0;for(let e=0;e<c.length;e++)e>0&&(d+=2),d+=2+c[e].label.length;let f=Math.max(0,Math.floor(a/2-d/2));for(let r=0;r<c.length;r++){r>0&&(f+=2);let a=c[r];G(e,t,i,f,a.symbol,`arrow`,n,s[a.globalIdx]),f+=1,f+=1,Ve(e,t,i,f,a.label,`text`),f+=a.label.length}}function Le(e,t){return F(e-1,t-1)}function Re(e,t){return oe(e-1,t-1)}function ze(e,t){return Array.from({length:e},()=>Array.from({length:t}).fill(null))}function G(e,t,n,r,i,a,o,s){r>=0&&r<e.length&&n>=0&&n<(e[0]?.length??0)&&(z(e,r,n,i,{role:a,roleCanvas:t}),o&&s&&(o[r][n]=s))}function Be(e,t,n){return n>=0&&n<e.length&&t>=0&&t<e[0].length?e[n][t]:` `}function Ve(e,t,n,r,i,a){for(let o=0;o<i.length;o++)G(e,t,n,r+o,i[o],a)}function He(e,t,n,r,i){if(e.length===0)return``;let a=e[0].length,o=e.length,s=[];for(let c=0;c<a;c++){let a=[],l=[],u=[];for(let r=0;r<o;r++)a.push(e[r][c]),l.push(t[r][c]),u.push(n[r][c]);let d=a.length-1;for(;d>=0&&a[d]===` `;)d--;d<0?s.push(``):s.push(Ue(a.slice(0,d+1),l.slice(0,d+1),u.slice(0,d+1),i,r))}for(;s.length>0&&s[s.length-1]===``;)s.pop();return s.join(`
|
|
3
|
+
`)}function Ue(e,t,n,r,i){if(i===`none`)return e.join(``);let a=``,o=null,s=``;for(let c=0;c<e.length;c++){let l=e[c];if(l===` `){s.length>0&&(a+=o?te(s,o,i):s,s=``,o=null),a+=` `;continue}let u=n[c]??null,d=t[c]??null,f=u??(d?je(d,r):null);f===o?s+=l:(s.length>0&&(a+=o?te(s,o,i):s),s=l,o=f)}return s.length>0&&(a+=o?te(s,o,i):s),a}function We(e){if(e.xAxis.categories)return e.xAxis.categories.length;for(let t of e.series)if(t.data.length>0)return t.data.length;return 0}function Ge(e,t){if(e.xAxis.categories)return e.xAxis.categories;if(e.xAxis.range){let{min:n,max:r}=e.xAxis.range,i=t>1?(r-n)/(t-1):0;return Array.from({length:t},(e,t)=>Je(n+i*t))}return Array.from({length:t},(e,t)=>String(t+1))}function Ke(e,t){let n=t-e;if(n<=0)return[e];let r=n/6,i=10**Math.floor(Math.log10(r)),a=r/i,o;o=a<=1.5?i:a<=3?2*i:a<=7?5*i:10*i;let s=Math.ceil(e/o)*o,c=[];for(let e=s;e<=t+o*.001;e+=o)c.push(Math.round(e*1e10)/1e10);return c}function qe(e){let t=e.length>1?Math.abs(e[1]-e[0]):0,n=t>0&&t<1?Math.ceil(-Math.log10(t)-1e-9):0;return e.map(e=>String(Number.isInteger(e)&&n===0?e:Number(e.toFixed(n))))}function Je(e){return Number.isInteger(e)?String(e):e.toFixed(+(Math.abs(e)<10))}var K={x:1,y:0},q={x:1,y:2},J={x:0,y:1},Y={x:2,y:1},Ye={x:2,y:0},Xe={x:0,y:0},Ze={x:2,y:2},Qe={x:0,y:2},$e={x:1,y:1};function X(e,t){return e.x===t.x&&e.y===t.y}function et(e){return X(e,K)||X(e,q)||X(e,J)||X(e,Y)}function tt(e){if(!et(e))throw Error(`Expected a cardinal direction (Up/Down/Left/Right); got {x:${e.x}, y:${e.y}}`);return e}function nt(e,t,n,r){return Math.max(r,n+(e-t))}function rt(e,t){return e.x===t.x&&e.y===t.y}function it(e,t){return e.x===t.x&&e.y===t.y}function at(e,t){return{x:e.x+t.x,y:e.y+t.y}}function ot(e){return`${e.x},${e.y}`}function Z(e){let t=e.gridCoord;if(t===null)throw Error(`Node "${e.name}" has no gridCoord; grid layout must run before it is read`);return t}var st={name:``,styles:{}},ct=class{#e=new Set;has(e){return this.#e.has(e)}add(e){this.#e.add(e)}delete(e){this.#e.delete(e)}keys(){return this.#e.keys()}};function lt(){return new ct}function ut(e){let t=lt();for(let n of e.keys())t.add(n);return t}function dt(e,t){return e.has(ot(t))}function ft(e,t){return t.x<0||t.y<0?!1:!dt(e,t)}function pt(e,t,n=3){for(let r=0;r<n;r++)for(let i=0;i<n;i++)if(dt(e,{x:t.x+r,y:t.y+i}))return!1;return!0}function mt(e,t,n=3){if(!pt(e,t,n))throw Error(`Grid block at (${t.x},${t.y}) is already occupied`);for(let r=0;r<n;r++)for(let i=0;i<n;i++){let n={x:t.x+r,y:t.y+i};e.add(ot(n))}}var ht=1e5;function gt(e){if(e.length===0)return[];let t=[e[0]];for(let n=1;n<e.length;n++){let r=e[n-1],i=e[n],a=r.x,o=r.y,s=Math.abs(i.x-r.x),c=Math.abs(i.y-r.y),l=r.x<i.x?1:-1,u=r.y<i.y?1:-1,d=s-c,f=0;for(;a!==i.x||o!==i.y;){if(f>=ht)throw Error(`pathCells: segment from (${r.x},${r.y}) to (${i.x},${i.y}) did not reach its endpoint within ${ht} steps`);let e=2*d;e>-c&&(d-=c,a+=l),e<s&&(d+=s,o+=u),t.push({x:a,y:o}),f++}}return t}var _t=class{items=[];get length(){return this.items.length}push(e){this.items.push(e),this.bubbleUp(this.items.length-1)}pop(){if(this.items.length===0)return;let e=this.items[0],t=this.items.pop();return this.items.length>0&&(this.items[0]=t,this.sinkDown(0)),e}bubbleUp(e){let t=e;for(;t>0;){let e=t-1>>1;if(this.items[t].priority<this.items[e].priority)[this.items[t],this.items[e]]=[this.items[e],this.items[t]],t=e;else break}}sinkDown(e){let t=this.items.length,n=e;for(;;){let e=n,r=2*n+1,i=2*n+2;if(r<t&&this.items[r].priority<this.items[e].priority&&(e=r),i<t&&this.items[i].priority<this.items[e].priority&&(e=i),e!==n)[this.items[n],this.items[e]]=[this.items[e],this.items[n]],n=e;else break}}};function vt(e,t){let n=Math.abs(e.x-t.x),r=Math.abs(e.y-t.y);return n===0||r===0?n+r:n+r+1}var yt=.001,bt=[{x:1,y:0},{x:-1,y:0},{x:0,y:1},{x:0,y:-1}],xt=5e4,St=2e5;function Ct(e=St){return{remaining:e}}function wt(e,t,n,r,i=!1){if(r&&r.remaining<=0)return null;let a=new _t;a.push({coord:t,priority:0});let o=new Map;o.set(ot(t),0);let s=new Map;s.set(ot(t),null);let c=0;for(;a.length>0;){if(++c>xt)return null;if(r){if(r.remaining<=0)return null;r.remaining--}let t=a.pop().coord;if(rt(t,n)){let e=[],n=t;for(;n!==null;)e.unshift(n),n=s.get(ot(n))??null;return e}let l=o.get(ot(t));if(l===void 0)throw Error(`A* pathfinding: missing cost for visited cell ${ot(t)}`);for(let r of bt){let c={x:t.x+r.x,y:t.y+r.y};if(!ft(e,c)&&!rt(c,n))continue;let u=i?s.get(ot(t)):null,d=u&&(t.x-u.x!==r.x||t.y-u.y!==r.y),f=l+1+(d?yt:0),p=ot(c),m=o.get(p);if(m===void 0||f<m){o.set(p,f);let e=f+vt(c,n);a.push({coord:c,priority:e}),s.set(p,t)}}}return null}function Tt(e){if(e.length<=2)return e;let t=new Set,n=e[0],r=e[1];for(let i=2;i<e.length;i++){let a=e[i],o=r.x-n.x,s=r.y-n.y,c=a.x-r.x,l=a.y-r.y;o===c&&s===l&&t.add(i-1),n=r,r=a}return e.filter((e,n)=>!t.has(n))}function Et(e,t,n,r){if(t.x===n.x&&t.y===n.y)return!0;let i=t.x===n.x;if(!i&&t.y!==n.y)return!1;let a=i?n.y>t.y?1:-1:n.x>t.x?1:-1,o=i?t.y:t.x,s=i?n.y:n.x;for(o+=a;;o+=a){let n=o===s,a=i?{x:t.x,y:o}:{x:o,y:t.y};if(!(n&&!r)&&!ft(e,a))return!1;if(n)break}return!0}function Dt(e,t,n,r){let i=r?{x:n.x,y:t.y}:{x:t.x,y:n.y};return!Et(e,t,i,!0)||!Et(e,i,n,!1)?null:[t,i,n]}function Ot(e,t,n,r){if(t.x===n.x||t.y===n.y)return null;let i=X(r,J)||X(r,Y);return Dt(e.grid,t,n,i)??Dt(e.grid,t,n,!i)}function kt(e,t,n,r){if(!e.pathBudget)throw Error(`routeEdge requires graph.pathBudget to be set; call createPathBudget() (see grid.ts createMapping) before routing edges`);let i=Ot(e,t,n,r)??wt(e.grid,t,n,e.pathBudget,e.preferStraightRoutes===!0);return i?Tt(i):null}function At(e){if(e.config.graphDirection!==`TD`)return[];let t=[],n=new Set,r=new Map;for(let t of e.edges){if(t.from===t.to)continue;let e=r.get(t.to)??[];e.push(t),r.set(t.to,e)}for(let[i,a]of r){if(a.length<2||!jt(a,e)||a.some(e=>n.has(e)))continue;let r={type:`fan-in`,edges:[...a],sharedNode:i,otherNodes:a.map(e=>e.from),junctionPoint:null,sharedPath:[],junctionDir:$e,sharedNodeDir:$e};for(let e of a)e.bundle=r,n.add(e);t.push(r)}let i=new Map;for(let t of e.edges){if(t.from===t.to||n.has(t))continue;let e=i.get(t.from)??[];e.push(t),i.set(t.from,e)}for(let[r,a]of i){if(a.length<2||!jt(a,e))continue;let i={type:`fan-out`,edges:[...a],sharedNode:r,otherNodes:a.map(e=>e.to),junctionPoint:null,sharedPath:[],junctionDir:$e,sharedNodeDir:$e};for(let e of a)e.bundle=i,n.add(e);t.push(i)}return t}function jt(e,t){if(e.length<2)return!1;let n=e[0].style,r=er(t,e[0].from),i=er(t,e[0].to);for(let a of e){if(a.style!==n||a.text.length>0)return!1;let e=er(t,a.from),o=er(t,a.to);if(e!==r||o!==i||e!==o)return!1}let a=e.every(t=>t.to===e[0].to),o=a?e.map(e=>e.from):e.map(e=>e.to);if(new Set(o).size!==o.length||e.some(e=>e.parallelLane))return!1;let s=t.config.graphDirection===`LR`?`x`:`y`,c=(a?e[0].to:e[0].from).gridCoord;if(!c)return!1;for(let t of e){let e=(a?t.from:t.to).gridCoord;if(!e)return!1;if(a){if(e[s]>=c[s])return!1}else if(e[s]<=c[s])return!1}return!0}function Mt(e,t){let n=e.config.graphDirection,r=Z(t.sharedNode);if(t.type===`fan-in`){if(n===`TD`){let e=r.y-1;return{x:r.x+1,y:e}}return{x:r.x-1,y:r.y+1}}if(n===`TD`){let e=r.y+3;return{x:r.x+1,y:e}}return{x:r.x+3,y:r.y+1}}function Nt(e,t){let n=e.config.graphDirection;t.junctionPoint=Mt(e,t);let r=t.junctionPoint;if(t.type===`fan-in`){t.junctionDir=n===`TD`?K:J,t.sharedNodeDir=n===`TD`?q:Y;let i=Z(t.sharedNode),a=tt(n===`TD`?K:J),o=at(i,a);t.sharedPath=kt(e,r,o,a)??[r,o];for(let i of t.edges){let o=Z(i.from),s=tt(n===`TD`?q:Y),c=at(o,s);i.pathToJunction=kt(e,c,r,s)??[c,r],i.startDir=s,i.endDir=a,i.path=[...i.pathToJunction,...t.sharedPath.slice(1)]}}else{t.junctionDir=n===`TD`?q:Y,t.sharedNodeDir=n===`TD`?K:J;let i=Z(t.sharedNode),a=tt(n===`TD`?q:Y),o=at(i,a);t.sharedPath=kt(e,o,r,a)??[o,r];for(let i of t.edges){let o=Z(i.to),s=tt(n===`TD`?K:J),c=at(o,s);i.pathToJunction=kt(e,r,c,s)??[r,c],i.startDir=a,i.endDir=s,i.path=[...t.sharedPath,...i.pathToJunction.slice(1)]}}}function Pt(e){for(let t of e.bundles)Nt(e,t)}function Ft(e){let t=null;for(let n of e.nodes){let e=n.gridCoord;if(e){if(!t){t={minX:e.x,minY:e.y,maxX:e.x+2,maxY:e.y+2};continue}t.minX=Math.min(t.minX,e.x),t.minY=Math.min(t.minY,e.y),t.maxX=Math.max(t.maxX,e.x+2),t.maxY=Math.max(t.maxY,e.y+2)}}return t}function It(e,t,n){let r=e.config.graphDirection!==`LR`,i=r?q:Y,a=r?K:J,o=at(Z(t.anchor),i),s=at(Z(n.to),a),c=kt(e,t.gutter,s,tt(r?Y:q));if(!c||c.length<2)return null;let l=[o,...c];return{path:l,startDir:i,endDir:a,labelSegment:Rt(t)?[l[l.length-2],l[l.length-1]]:[l[1],l[2]]}}function Lt(e,t){let n=t.clusterTarget;if(!n||t.bundle||t.from===t.to)return null;let r=Ft(n),i=t.from.gridCoord;if(!r||!i||n.nodes.includes(t.from)||!n.nodes.includes(t.to))return null;let a=e.config.graphDirection!==`LR`;if(a?i.y+2>=r.minY-1:i.x+2>=r.minX-1)return null;let o=(e,t,n)=>Math.min(Math.max(e,t),n),s=a?{x:o(i.x+1,r.minX+1,r.maxX-1),y:r.minY-1}:{x:r.minX-1,y:o(i.y+1,r.minY+1,r.maxY-1)};if(!ft(e.grid,s))return null;let c=a?q:Y,l=a?K:J,u=kt(e,at(i,c),s,tt(c));return!u||u.length<2?null:{path:u,startDir:c,endDir:l}}function Rt(e){for(let t of e.edges)if(t.parallelLane)return!0;return!1}function zt(e){let t=e.config.graphDirection!==`LR`,n=new Map;for(let t of e.edges){let e=t.clusterSource;if(!e)continue;let r=n.get(e);r?r.push(t):n.set(e,[t])}let r=new Map;for(let[i,a]of n){let n=Bt(e,i,a,t);n&&r.set(i,n)}r.size>0&&(e.clusterExitPlans=r)}function Bt(e,t,n,r){let i=Ft(t);if(!i)return null;let a=n.filter(e=>{let n=e.from.gridCoord,a=e.to.gridCoord;return!n||!a||e.bundle||e.from===e.to||!t.nodes.includes(e.from)||t.nodes.includes(e.to)?!1:r?a.y>i.maxY+1:a.x>i.maxX+1}),o=new Set(a),s=new Map;for(let t of e.edges){if(!t.parallelLane)continue;let e=s.get(t.parallelLane.usedOffsets);e?e.push(t):s.set(t.parallelLane.usedOffsets,[t])}let c=a.filter(e=>!e.parallelLane||s.get(e.parallelLane.usedOffsets).every(e=>o.has(e)));if(c.length===0)return null;let l=c[0].from;if(c.some(e=>e.from!==l))return null;let u=Z(l),d=r?{x:u.x+1,y:i.maxY+1}:{x:i.maxX+1,y:u.y+1},f=at(u,r?q:Y),p={box:i,anchor:l,gutter:d,edges:new Set(c)},m=new Set;for(let t of c){let n=t.parallelLane;n&&n.index>0&&!Vt(e,p,t)&&m.add(n.usedOffsets)}if(c=c.filter(e=>!e.parallelLane||!m.has(e.parallelLane.usedOffsets)),new Set(c.map(e=>e.parallelLane?.usedOffsets??e)).size<2)return null;let h=r?d.y-f.y:d.x-f.x;for(let t=1;t<=h;t++){let n=r?{x:f.x,y:f.y+t}:{x:f.x+t,y:f.y};if(!ft(e.grid,n))return null}let g={box:i,anchor:l,gutter:d,edges:new Set(c)};for(let t of c)if(!It(e,g,t))return null;return g}function Vt(e,t,n){let r=e.config.graphDirection!==`LR`,i=n.parallelLane.index;if(i>2)return null;let a=Z(n.to),o=t.gutter,s=r?a.x+1:a.y+1,c=r?o.x:o.y,l={lane:(r?a.x:a.y)+3,attach:(r?a.x:a.y)+2,dir:r?J:K},u={lane:(r?a.x:a.y)-1,attach:r?a.x:a.y,dir:r?Y:q},d=(c>=s?[l,u]:[u,l])[i-1];if(!(r?e.columnWidth:e.rowHeight).has(d.lane)||d.lane<0||d.lane===c)return null;let f=Tt(r?[o,{x:d.lane,y:o.y},{x:d.lane,y:a.y+1},{x:d.attach,y:a.y+1}]:[o,{x:o.x,y:d.lane},{x:a.x+1,y:d.lane},{x:a.x+1,y:d.attach}]);if(!Fr(e,gt(f),[n.from,n.to]))return null;let p=r?[{x:d.lane,y:o.y},{x:d.lane,y:a.y}]:[{x:o.x,y:d.lane},{x:a.x,y:d.lane}];return{path:f,endDir:d.dir,labelSegment:p}}var Ht=64;function Ut(e){let t=e.clusterExitPlans;if(!t)return!1;let n=e.config.graphDirection!==`LR`,r=!1,i=[...t].sort(([e],[t])=>Wt(e)-Wt(t));for(let[t,a]of i){let i=n?t.maxY:t.maxX,o=n?e.rowHeight:e.columnWidth,s=n?a.gutter.y:a.gutter.x;for(let t=0;t<Ht&&!Gt(e,a,i,n);t++)o.set(s,(o.get(s)??0)+1),r=!0}return r}function Wt(e){let t=0;for(let n=e.parent;n;n=n.parent)t++;return t}function Gt(e,t,n,r){let i=e=>r?e.y:e.x;if(i($(e,t.gutter))<=n)return!1;for(let a of t.edges){let t=li(e,a),o=a.path[a.path.length-1];if(!t||!o)continue;let s=i($(e,o))-1;for(let{x:e,y:i,text:a}of t){let t=r?i:e,o=r?i:e+P(a)-1;if(t<=n||o>=s)return!1}}return!0}function Q(e){return e.split(`
|
|
4
|
+
`)}function Kt(e){let t=Q(e);return Math.max(...t.map(e=>P(e)),0)}function qt(e){return Q(e).length}function Jt(e,t){let n=e.maxX-e.minX,r=e.maxY-e.minY;if(n<=0||r<=0)return F(0,0);let i={x:0,y:0},a={x:n,y:r},o=F(n,r);if(t.config.useAscii){for(let e=i.x+1;e<a.x;e++)z(o,e,i.y,`-`);for(let e=i.x+1;e<a.x;e++)z(o,e,a.y,`-`);for(let e=i.y+1;e<a.y;e++)z(o,i.x,e,`|`);for(let e=i.y+1;e<a.y;e++)z(o,a.x,e,`|`);z(o,i.x,i.y,`+`),z(o,a.x,i.y,`+`),z(o,i.x,a.y,`+`),z(o,a.x,a.y,`+`)}else{for(let e=i.x+1;e<a.x;e++)z(o,e,i.y,`─`);for(let e=i.x+1;e<a.x;e++)z(o,e,a.y,`─`);for(let e=i.y+1;e<a.y;e++)z(o,i.x,e,`│`);for(let e=i.y+1;e<a.y;e++)z(o,a.x,e,`│`);z(o,i.x,i.y,`┌`),z(o,a.x,i.y,`┐`),z(o,i.x,a.y,`└`),z(o,a.x,a.y,`┘`)}return o}var Yt=new Set([`│`,`┃`,`║`,`┆`,`┊`,`|`,`:`,`‖`,`┼`,`╋`]);function Xt(e){return e!==void 0&&Yt.has(e)}function Zt(e,t,n,r){let i=(t,i)=>{for(let a=t-i;a<t+e.length+i;a++)if(!(a<1||a>=n)&&r(a))return!0;return!1};if(!i(t,1))return t;let a=Math.max(1,n-e.length);for(let e of[1,0]){let n=-1;for(let r=1;r<=a;r++)i(r,e)||(n<0||Math.abs(r-t)<Math.abs(n-t))&&(n=r);if(n>=0)return n}return null}function Qt(e,t){return Math.max(1,1+Math.ceil((t-1-e.length)/2))}function $t(e,t,n){let r=ae(e),i=Zt(r,Qt(r,t),t,n);if(i===null||i+r.length>=t)return!1;for(let e=i-1;e<i+r.length+1;e++)if(e>=1&&e<t&&n(e))return!1;return!0}function en(e,t,n=()=>!1){let r=e.maxX-e.minX,i=e.maxY-e.minY;if(r<=0||i<=0)return[F(0,0),{x:0,y:0},[]];let a=F(r,i),o=[],s=Q(e.name);for(let e=0;e<s.length;e++){let t=s[e],c=1+e,l=1+Math.ceil((r-1-P(t))/2);l<1&&(l=1);let u=ae(t);l=Zt(u,l,r,e=>n(e,c))??l;for(let e=0;e<u.length;e++)l+e>=r||c>=i||u[e]===` `&&n(l+e,c)||(z(a,l+e,c,u[e]),o.push({x:l+e,y:c}))}return[a,{x:e.minX,y:e.minY},o]}var tn=16;function nn(e,t){return e.nodes.includes(t.from)||e.nodes.includes(t.to)}function rn(e,t){let n=[],r=!1;for(let i of e.subgraphs){if(nn(i,t))continue;let a=Ft(i);if(a){r=!0;for(let t=a.minX;t<=a.maxX;t++)for(let r=a.minY;r<=a.maxY;r++){let i=ot({x:t,y:r});e.grid.has(i)||(e.grid.add(i),n.push(i))}}}return{added:n,engaged:r}}function an(e,t){for(let n of t)e.grid.delete(n)}function on(e,t,n){if(t.size===0)return!1;let r=e.config.graphDirection!==`LR`,i=r?e.columnWidth:e.rowHeight,a=!1;for(let o=0;o<tn;o++){let o=sn(e,t,r);if(!o)break;i.set(o.index,(i.get(o.index)??0)+o.by),a=!0,n()}return a}function sn(e,t,n){for(let r of t){let t=r.path;for(let r=1;r<t.length;r++){let i=t[r-1],a=t[r];if(n?i.x!==a.x||i.y===a.y:i.y!==a.y||i.x===a.x)continue;let o=n?i.x:i.y,s=n?$(e,i).x:$(e,i).y,c=n?$(e,i).y:$(e,i).x,l=n?$(e,a).y:$(e,a).x,u=Math.min(c,l),d=Math.max(c,l);for(let t of e.subgraphs){let e=Ft(t);if(!e)continue;let r=n?t.minX:t.minY,i=n?t.maxX:t.maxY,a=n?t.minY:t.minX,c=n?t.maxY:t.maxX;if(d<=a||u>=c)continue;let l=n?e.minX:e.minY,f=n?e.maxX:e.maxY;if(o>f){if(s>i+1)continue;let e=i+2-s;return o-1>f?{index:o-1,by:e}:{index:o,by:2*e}}if(o<l){if(s<r-1)continue;let e=s+2-r;return o+1<l?{index:o+1,by:e}:{index:o,by:2*e}}}}}return null}var cn=4;function ln(e,t){if(e.config.graphDirection===`LR`)return;let n=new Set;for(let r=0;r<e.subgraphs.length;r++){let r=e.subgraphs.find(t=>t.nodes.length>0&&!n.has(t)&&dn(e,t));if(!r)return;let i=un(r)+cn;for(let a=1;a<=i&&(r.titleRoom=a,t(),dn(e,r));a++)a===i&&(r.titleRoom=0,t(),n.add(r))}}function un(e){return Math.max(0,...Q(e.name).map(e=>P(e)))}function dn(e,t){let n=t.maxX-t.minX,r=Q(t.name);for(let i=0;i<r.length;i++){let a=t.minY+1+i,o=new Set;for(let n of e.edges)for(let r=1;r<n.path.length;r++){let i=n.path[r-1],s=n.path[r];if(i.x!==s.x||i.y===s.y)continue;let c=$(e,i),l=$(e,s);a<Math.min(c.y,l.y)||a>Math.max(c.y,l.y)||c.x>t.minX&&c.x<t.maxX&&o.add(c.x-t.minX)}if(o.size!==0&&!$t(r[i],n,e=>o.has(e)))return!0}return!1}function fn(){return new Map}function pn(e,t,n,r){for(let i of gt(n)){if(dt(e,i))continue;let n=t.get(ot(i));if(n!==void 0&&n!==r)return i}return null}function mn(e,t,n,r){for(let i of gt(n)){if(dt(e,i))continue;let n=ot(i);t.has(n)||t.set(n,r)}}function hn(){return new Map}function gn(e,t){return e.to===t.from||t.to===e.from}var _n=2;function vn(e,t){return t(e)?1:_n}function yn(e,t,n,r,i=()=>!1){let a=new Map,o=new Map;for(let i of gt(n)){if(dt(e,i))continue;let n=t.get(ot(i));if(n!==void 0)for(let e of n)e!==r&&gn(e,r)&&(a.set(e,(a.get(e)??0)+1),o.has(e)||o.set(e,i))}for(let[e,t]of a)if(t>=vn(e,i))return o.get(e);return null}function bn(e,t,n,r){for(let i of gt(n)){if(dt(e,i))continue;let n=ot(i),a=t.get(n);a===void 0&&(a=new Set,t.set(n,a)),a.add(r)}}var xn={rectangle:{unicode:{tl:`┌`,tr:`┐`,bl:`└`,br:`┘`},ascii:{tl:`+`,tr:`+`,bl:`+`,br:`+`}},rounded:{unicode:{tl:`╭`,tr:`╮`,bl:`╰`,br:`╯`},ascii:{tl:`.`,tr:`.`,bl:`'`,br:`'`}},circle:{unicode:{tl:`◯`,tr:`◯`,bl:`◯`,br:`◯`},ascii:{tl:`o`,tr:`o`,bl:`o`,br:`o`}},doublecircle:{unicode:{tl:`◎`,tr:`◎`,bl:`◎`,br:`◎`},ascii:{tl:`@`,tr:`@`,bl:`@`,br:`@`}},diamond:{unicode:{tl:`◇`,tr:`◇`,bl:`◇`,br:`◇`},ascii:{tl:`<`,tr:`>`,bl:`<`,br:`>`}},hexagon:{unicode:{tl:`⌜`,tr:`⌝`,bl:`⌞`,br:`⌟`},ascii:{tl:`*`,tr:`*`,bl:`*`,br:`*`}},stadium:{unicode:{tl:`(`,tr:`)`,bl:`(`,br:`)`},ascii:{tl:`(`,tr:`)`,bl:`(`,br:`)`}},subroutine:{unicode:{tl:`╟`,tr:`╢`,bl:`╟`,br:`╢`},ascii:{tl:`|`,tr:`|`,bl:`|`,br:`|`}},cylinder:{unicode:{tl:`╭`,tr:`╮`,bl:`╰`,br:`╯`},ascii:{tl:`.`,tr:`.`,bl:`'`,br:`'`}},asymmetric:{unicode:{tl:`▷`,tr:`┐`,bl:`▷`,br:`┘`},ascii:{tl:`>`,tr:`+`,bl:`>`,br:`+`}},trapezoid:{unicode:{tl:`/`,tr:`\\`,bl:`└`,br:`┘`},ascii:{tl:`/`,tr:`\\`,bl:`+`,br:`+`}},"trapezoid-alt":{unicode:{tl:`┌`,tr:`┐`,bl:`\\`,br:`/`},ascii:{tl:`+`,tr:`+`,bl:`\\`,br:`/`}},parallelogram:{unicode:{tl:`/`,tr:`/`,bl:`/`,br:`/`},ascii:{tl:`/`,tr:`/`,bl:`/`,br:`/`}},"parallelogram-alt":{unicode:{tl:`\\`,tr:`\\`,bl:`\\`,br:`\\`},ascii:{tl:`\\`,tr:`\\`,bl:`\\`,br:`\\`}},"state-start":{unicode:{tl:`●`,tr:`●`,bl:`●`,br:`●`},ascii:{tl:`*`,tr:`*`,bl:`*`,br:`*`}},"state-end":{unicode:{tl:`◉`,tr:`◉`,bl:`◉`,br:`◉`},ascii:{tl:`@`,tr:`@`,bl:`@`,br:`@`}},document:{unicode:{tl:`┌`,tr:`┐`,bl:`╰`,br:`╮`},ascii:{tl:`+`,tr:`+`,bl:`'`,br:`~`}},"stacked-document":{unicode:{tl:`┌`,tr:`╗`,bl:`╰`,br:`╮`},ascii:{tl:`+`,tr:`#`,bl:`'`,br:`~`}},"stacked-process":{unicode:{tl:`┌`,tr:`╗`,bl:`└`,br:`╝`},ascii:{tl:`+`,tr:`#`,bl:`+`,br:`#`}},card:{unicode:{tl:`╱`,tr:`┐`,bl:`└`,br:`┘`},ascii:{tl:`/`,tr:`+`,bl:`+`,br:`+`}},"lined-process":{unicode:{tl:`┌`,tr:`┐`,bl:`└`,br:`┘`},ascii:{tl:`+`,tr:`+`,bl:`+`,br:`+`}},"divided-process":{unicode:{tl:`┌`,tr:`┐`,bl:`└`,br:`┘`},ascii:{tl:`+`,tr:`+`,bl:`+`,br:`+`}},"window-pane":{unicode:{tl:`┌`,tr:`┐`,bl:`└`,br:`┘`},ascii:{tl:`+`,tr:`+`,bl:`+`,br:`+`}},triangle:{unicode:{tl:`╱`,tr:`╲`,bl:`└`,br:`┘`},ascii:{tl:`/`,tr:`\\`,bl:`+`,br:`+`}},"flipped-triangle":{unicode:{tl:`┌`,tr:`┐`,bl:`╲`,br:`╱`},ascii:{tl:`+`,tr:`+`,bl:`\\`,br:`/`}},"filled-circle":{unicode:{tl:`●`,tr:`●`,bl:`●`,br:`●`},ascii:{tl:`*`,tr:`*`,bl:`*`,br:`*`}},"crossed-circle":{unicode:{tl:`╳`,tr:`╳`,bl:`╳`,br:`╳`},ascii:{tl:`X`,tr:`X`,bl:`X`,br:`X`}},"fork-join":{unicode:{tl:`━`,tr:`━`,bl:`━`,br:`━`},ascii:{tl:`=`,tr:`=`,bl:`=`,br:`=`}},"notched-pentagon":{unicode:{tl:`╱`,tr:`╲`,bl:`└`,br:`┘`},ascii:{tl:`/`,tr:`\\`,bl:`+`,br:`+`}},"sloped-rectangle":{unicode:{tl:`╱`,tr:`┐`,bl:`└`,br:`┘`},ascii:{tl:`/`,tr:`+`,bl:`+`,br:`+`}},flag:{unicode:{tl:`╭`,tr:`╮`,bl:`╰`,br:`╯`},ascii:{tl:`~`,tr:`~`,bl:`~`,br:`~`}},"bow-tie-rectangle":{unicode:{tl:`╲`,tr:`╱`,bl:`╱`,br:`╲`},ascii:{tl:`\\`,tr:`/`,bl:`/`,br:`\\`}},"half-rounded-rectangle":{unicode:{tl:`┌`,tr:`╮`,bl:`└`,br:`╯`},ascii:{tl:`+`,tr:`.`,bl:`+`,br:`'`}},brace:{unicode:{tl:`╭`,tr:`┐`,bl:`╰`,br:`┘`},ascii:{tl:`{`,tr:`+`,bl:`{`,br:`+`}},"brace-right":{unicode:{tl:`┌`,tr:`╮`,bl:`└`,br:`╯`},ascii:{tl:`+`,tr:`}`,bl:`+`,br:`}`}},braces:{unicode:{tl:`╭`,tr:`╮`,bl:`╰`,br:`╯`},ascii:{tl:`{`,tr:`}`,bl:`{`,br:`}`}},bolt:{unicode:{tl:`╱`,tr:`╲`,bl:`╲`,br:`╱`},ascii:{tl:`/`,tr:`\\`,bl:`\\`,br:`/`}},text:{unicode:{tl:` `,tr:` `,bl:` `,br:` `},ascii:{tl:` `,tr:` `,bl:` `,br:` `}},anchor:{unicode:{tl:` `,tr:` `,bl:` `,br:` `},ascii:{tl:` `,tr:` `,bl:` `,br:` `}}};function Sn(e,t){let n=xn[e]??xn.rectangle;return t?n.ascii:n.unicode}function Cn(e,t){let n=Q(e),r=Math.max(...n.map(e=>P(e)),0),i=n.length,a=2*t.padding+r,o=a+2,s=i+2*t.padding,c=s%2==0?s+1:s;return{width:o,height:c+2,labelArea:{x:1+t.padding,y:1+t.padding,width:r,height:i},gridColumns:[1,a,1],gridRows:[1,c,1]}}function wn(e,t,n,r){let{width:i,height:a}=t,o=F(i-1,a-1),s={x:0,y:0},c={x:i-1,y:a-1},l=r?`-`:`─`,u=r?`|`:`│`;for(let e=s.x+1;e<c.x;e++)o[e][s.y]=l,o[e][c.y]=l;for(let e=s.y+1;e<c.y;e++)o[s.x][e]=u,o[c.x][e]=u;o[s.x][s.y]=n.tl,o[c.x][s.y]=n.tr,o[s.x][c.y]=n.bl,o[c.x][c.y]=n.br;let d=Q(e),f=i-1,p=a-1,m=Math.floor(p/2)-Math.floor((d.length-1)/2);for(let e=0;e<d.length;e++){let t=d[e],n=Math.floor(f/2)-Math.ceil(P(t)/2)+1,r=ae(t);for(let t=0;t<r.length;t++){let i=n+t,a=m+e;i>=0&&i<o.length&&a>=0&&a<o[0].length&&(o[i][a]=r[t])}}return o}function Tn(e,t,n){let{width:r,height:i}=t,a=n.x+Math.floor(r/2),o=n.y+Math.floor(i/2);return X(e,K)?{x:a,y:n.y}:X(e,q)?{x:a,y:n.y+i-1}:X(e,J)?{x:n.x,y:o}:X(e,Y)?{x:n.x+r-1,y:o}:X(e,Xe)?{x:n.x,y:n.y}:X(e,Ye)?{x:n.x+r-1,y:n.y}:X(e,Qe)?{x:n.x,y:n.y+i-1}:X(e,Ze)?{x:n.x+r-1,y:n.y+i-1}:{x:a,y:o}}var En={getDimensions:Cn,render(e,t,n){return wn(e,t,Sn(`rectangle`,n.useAscii),n.useAscii)},getAttachmentPoint:Tn},Dn={getDimensions(e,t){return{width:5,height:3,labelArea:{x:2,y:1,width:1,height:1},gridColumns:[1,3,1],gridRows:[1,1,1]}},render(e,t,n){let{width:r,height:i}=t,a=F(r-1,i-1),o=Math.floor(r/2);return n.useAscii?(a[0][0]=`.`,a[1][0]=`-`,a[2][0]=`-`,a[3][0]=`-`,a[4][0]=`.`,a[0][1]=`|`,a[o][1]=`*`,a[4][1]=`|`,a[0][2]=`'`,a[1][2]=`-`,a[2][2]=`-`,a[3][2]=`-`,a[4][2]=`'`):(a[0][0]=`╭`,a[1][0]=`─`,a[2][0]=`─`,a[3][0]=`─`,a[4][0]=`╮`,a[0][1]=`│`,a[o][1]=`●`,a[4][1]=`│`,a[0][2]=`╰`,a[1][2]=`─`,a[2][2]=`─`,a[3][2]=`─`,a[4][2]=`╯`),a},getAttachmentPoint(e,t,n){let{width:r,height:i}=t,a=n.x+Math.floor(r/2),o=n.y+Math.floor(i/2);return X(e,K)?{x:a,y:n.y}:X(e,q)?{x:a,y:n.y+i-1}:X(e,J)?{x:n.x,y:o}:X(e,Y)?{x:n.x+r-1,y:o}:{x:a,y:o}}},On={getDimensions(e,t){return{width:5,height:3,labelArea:{x:2,y:1,width:1,height:1},gridColumns:[1,3,1],gridRows:[1,1,1]}},render(e,t,n){let{width:r,height:i}=t,a=F(r-1,i-1),o=Math.floor(r/2);return n.useAscii?(a[0][0]=`#`,a[1][0]=`=`,a[2][0]=`=`,a[3][0]=`=`,a[4][0]=`#`,a[0][1]=`#`,a[o][1]=`*`,a[4][1]=`#`,a[0][2]=`#`,a[1][2]=`=`,a[2][2]=`=`,a[3][2]=`=`,a[4][2]=`#`):(a[0][0]=`╔`,a[1][0]=`═`,a[2][0]=`═`,a[3][0]=`═`,a[4][0]=`╗`,a[0][1]=`║`,a[o][1]=`◎`,a[4][1]=`║`,a[0][2]=`╚`,a[1][2]=`═`,a[2][2]=`═`,a[3][2]=`═`,a[4][2]=`╝`),a},getAttachmentPoint(e,t,n){let{width:r,height:i}=t,a=n.x+Math.floor(r/2),o=n.y+Math.floor(i/2);return X(e,K)?{x:a,y:n.y}:X(e,q)?{x:a,y:n.y+i-1}:X(e,J)?{x:n.x,y:o}:X(e,Y)?{x:n.x+r-1,y:o}:{x:a,y:o}}},kn={getDimensions(e,t){let n=Q(e),r=Math.max(...n.map(e=>P(e)),0),i=n.length,a=2*t.padding+r,o=a+4,s=i+2*t.padding;return{width:o,height:Math.max(s+2,3),labelArea:{x:2+t.padding,y:1+t.padding,width:r,height:i},gridColumns:[2,a,2],gridRows:[1,s,1]}},render(e,t,n){let{width:r,height:i}=t,a=F(r-1,i-1),o=Math.floor(i/2),s=n.useAscii?`-`:`─`;if(i===3)a[0][o]=`(`,a[r-1][o]=`)`;else if(n.useAscii){for(let e=0;e<i;e++)a[0][e]=`(`,a[r-1][e]=`)`;for(let e=1;e<r-1;e++)a[e][0]=s,a[e][i-1]=s}else{a[0][0]=`╭`;for(let e=1;e<r-1;e++)a[e][0]=s;a[r-1][0]=`╮`;for(let e=1;e<i-1;e++)a[0][e]=`│`,a[r-1][e]=`│`;a[0][i-1]=`╰`;for(let e=1;e<r-1;e++)a[e][i-1]=s;a[r-1][i-1]=`╯`}let c=Q(e),l=o-Math.floor((c.length-1)/2);for(let e=0;e<c.length;e++){let t=c[e],n=Math.floor(r/2)-Math.floor(P(t)/2),o=ae(t);for(let t=0;t<o.length;t++){let s=n+t,c=l+e;s>0&&s<r-1&&c>=0&&c<i&&(a[s][c]=o[t])}}return a},getAttachmentPoint:Tn},An={getDimensions(e,t){let n=Q(e),r=Math.max(...n.map(e=>P(e)),0),i=n.length,a=2*t.padding+r,o=a+4,s=i+2*t.padding;return{width:o,height:s+2,labelArea:{x:2+t.padding,y:1+t.padding,width:r,height:i},gridColumns:[2,a,2],gridRows:[1,s,1]}},render(e,t,n){let{width:r,height:i}=t,a=F(r-1,i-1),o=n.useAscii?`-`:`─`,s=n.useAscii?`|`:`│`;a[0][0]=n.useAscii?`+`:`┌`,a[1][0]=n.useAscii?`+`:`┬`;for(let e=2;e<r-2;e++)a[e][0]=o;a[r-2][0]=n.useAscii?`+`:`┬`,a[r-1][0]=n.useAscii?`+`:`┐`;for(let e=1;e<i-1;e++)a[0][e]=s,a[1][e]=s,a[r-2][e]=s,a[r-1][e]=s;a[0][i-1]=n.useAscii?`+`:`└`,a[1][i-1]=n.useAscii?`+`:`┴`;for(let e=2;e<r-2;e++)a[e][i-1]=o;a[r-2][i-1]=n.useAscii?`+`:`┴`,a[r-1][i-1]=n.useAscii?`+`:`┘`;let c=Q(e),l=Math.floor(i/2)-Math.floor((c.length-1)/2);for(let e=0;e<c.length;e++){let t=c[e],n=Math.floor(r/2)-Math.floor(P(t)/2),o=ae(t);for(let t=0;t<o.length;t++){let s=n+t,c=l+e;s>1&&s<r-2&&c>0&&c<i-1&&(a[s][c]=o[t])}}return a},getAttachmentPoint:Tn},jn={getDimensions:Cn,render(e,t,n){return wn(e,t,Sn(`doublecircle`,n.useAscii),n.useAscii)},getAttachmentPoint:Tn},Mn={getDimensions(e,t){let n=Q(e),r=Math.max(...n.map(e=>P(e)),0),i=n.length,a=2*t.padding+r,o=a+2,s=i+2*t.padding+2;return{width:o,height:s+2,labelArea:{x:1+t.padding,y:2+t.padding,width:r,height:i},gridColumns:[1,a,1],gridRows:[2,s-2,2]}},render(e,t,n){let{width:r,height:i}=t,a=F(r-1,i-1),o=n.useAscii?`-`:`─`,s=n.useAscii?`|`:`│`;a[0][0]=n.useAscii?`.`:`╭`;for(let e=1;e<r-1;e++)a[e][0]=o;a[r-1][0]=n.useAscii?`.`:`╮`,a[0][1]=s;for(let e=1;e<r-1;e++)a[e][1]=o;a[r-1][1]=s;for(let e=2;e<i-2;e++)a[0][e]=s,a[r-1][e]=s;a[0][i-2]=s;for(let e=1;e<r-1;e++)a[e][i-2]=o;a[r-1][i-2]=s,a[0][i-1]=n.useAscii?`'`:`╰`;for(let e=1;e<r-1;e++)a[e][i-1]=o;a[r-1][i-1]=n.useAscii?`'`:`╯`;let c=Q(e),l=Math.floor(i/2)-Math.floor((c.length-1)/2);for(let e=0;e<c.length;e++){let t=c[e],n=Math.floor(r/2)-Math.floor(P(t)/2),o=ae(t);for(let t=0;t<o.length;t++){let s=n+t,c=l+e;s>0&&s<r-1&&c>1&&c<i-2&&(a[s][c]=o[t])}}return a},getAttachmentPoint:Tn},Nn={getDimensions:Cn,render(e,t,n){return wn(e,t,Sn(`asymmetric`,n.useAscii),n.useAscii)},getAttachmentPoint:Tn},Pn={getDimensions:Cn,render(e,t,n){return wn(e,t,Sn(`trapezoid`,n.useAscii),n.useAscii)},getAttachmentPoint:Tn},Fn={getDimensions:Cn,render(e,t,n){return wn(e,t,Sn(`trapezoid-alt`,n.useAscii),n.useAscii)},getAttachmentPoint:Tn},In={getDimensions:Cn,render(e,t,n){return wn(e,t,Sn(`parallelogram`,n.useAscii),n.useAscii)},getAttachmentPoint:Tn},Ln={getDimensions:Cn,render(e,t,n){return wn(e,t,Sn(`parallelogram-alt`,n.useAscii),n.useAscii)},getAttachmentPoint:Tn};function Rn(e){return{getDimensions:Cn,render(t,n,r){return wn(t,n,Sn(e,r.useAscii),r.useAscii)},getAttachmentPoint:Tn}}var zn=Rn(`circle`),Bn=Rn(`diamond`),Vn=Rn(`rounded`),Hn=Rn(`hexagon`),Un=new Map([[`rectangle`,En],[`rounded`,Vn],[`diamond`,Bn],[`stadium`,kn],[`circle`,zn],[`subroutine`,An],[`doublecircle`,jn],[`hexagon`,Hn],[`cylinder`,Mn],[`asymmetric`,Nn],[`trapezoid`,Pn],[`trapezoid-alt`,Fn],[`parallelogram`,In],[`parallelogram-alt`,Ln],[`state-start`,Dn],[`state-end`,On]]);function Wn(e){return Un.get(e)??En}function Gn(e,t,n){return Wn(e).getDimensions(t,n)}function Kn(e,t,n,r){return Wn(e).getAttachmentPoint(t,n,r)}function $(e,t,n){let r=n?{x:t.x+n.x,y:t.y+n.y}:t,i=0;for(let t=0;t<r.x;t++)i+=e.columnWidth.get(t)??0;let a=0;for(let t=0;t<r.y;t++)a+=e.rowHeight.get(t)??0;let o=e.columnWidth.get(r.x)??0,s=e.rowHeight.get(r.y)??0;return{x:i+Math.floor(o/2)+e.offsetX,y:a+Math.floor(s/2)+e.offsetY}}function qn(e,t){return t.map(t=>$(e,t))}function Jn(e,t,n,r){let i=r??ar(e,t);return pt(e.grid,n,3)?(mt(e.grid,n,3),t.gridCoord=n,n):i===`LR`?Jn(e,t,{x:n.x,y:n.y+4},i):Jn(e,t,{x:n.x+4,y:n.y},i)}function Yn(e,t){let n=Z(t),r=e.config.boxBorderPadding,i=Gn(t.shape,t.displayLabel,{useAscii:e.config.useAscii,padding:r}),a=i.gridColumns,o=i.gridRows;for(let t=0;t<a.length;t++){let r=n.x+t,i=e.columnWidth.get(r)??0;e.columnWidth.set(r,Math.max(i,a[t]))}for(let t=0;t<o.length;t++){let r=n.y+t,i=e.rowHeight.get(r)??0;e.rowHeight.set(r,Math.max(i,o[t]))}if(n.x>0){let t=e.columnWidth.get(n.x-1)??0;e.columnWidth.set(n.x-1,Math.max(t,e.config.paddingX))}if(n.y>0){let r=e.config.paddingY;or(e,t)&&(r+=4);let i=e.rowHeight.get(n.y-1)??0;e.rowHeight.set(n.y-1,Math.max(i,r))}}function Xn(e,t){for(let n of t)e.columnWidth.has(n.x)||e.columnWidth.set(n.x,Math.floor(e.config.paddingX/2)),e.rowHeight.has(n.y)||e.rowHeight.set(n.y,Math.floor(e.config.paddingY/2))}var Zn=8;function Qn(e,t,n,r,i){let a=[],o=(e.clusterExitPlans?.size??0)>0;o&&(e.preferStraightRoutes=!0);try{for(let o=0;o<Zn;o++){let o=pn(i,n,t.path,t.style)??yn(i,r,t.path,t,t=>t.clusterSource!==void 0&&e.clusterExitPlans?.get(t.clusterSource)?.edges.has(t)===!0);if(!o)return;e.grid.add(ot(o)),a.push(o),Vr(e,t)}}finally{o&&(e.preferStraightRoutes=!1);for(let t of a)e.grid.delete(ot(t))}}function $n(e,t){return e.subgraphs.some(e=>e.nodes.includes(t))}function er(e,t){let n=null;for(let r of e.subgraphs)r.nodes.includes(t)&&(!n||tr(n,r))&&(n=r);return n}function tr(e,t){let n=t;for(;n!==null;){if(n===e)return!0;n=n.parent}return!1}function nr(e,t){let n=er(e,t);if(!n)return null;let r=n;for(;r.parent;)r=r.parent;return r}function rr(e){let t=[...e.nodes];for(let n of e.children)t.push(...rr(n));return t}function ir(e,t,n){if(t===n)return!0;let r=new Set([t]),i=[t];for(;i.length>0;){let t=i.shift();for(let a of Dr(e,t)){if(a===n)return!0;r.has(a)||(r.add(a),i.push(a))}}return!1}function ar(e,t){let n=er(e,t);return n?.direction?n.direction:e.config.graphDirection}function or(e,t){let n=er(e,t);if(!n)return!1;let r=!1;for(let i of e.edges)if(i.to===t&&er(e,i.from)!==n){r=!0;break}if(!r)return!1;let i=Z(t).y;for(let r of n.nodes){if(r===t||!r.gridCoord)continue;let a=!1;for(let t of e.edges)if(t.to===r&&er(e,t.from)!==n){a=!0;break}if(a&&r.gridCoord.y<i)return!1}return!0}function sr(e,t){if(t.nodes.length===0)return;let n=1e6,r=1e6,i=-1e6,a=-1e6;for(let o of t.children)sr(e,o),o.nodes.length>0&&(n=Math.min(n,o.minX),r=Math.min(r,o.minY),i=Math.max(i,o.maxX),a=Math.max(a,o.maxY));for(let e of t.nodes){if(!e.drawingCoord||!e.drawing)continue;let t=e.drawingCoord.x,o=e.drawingCoord.y,s=t+e.drawing.length-1,c=o+e.drawing[0].length-1;n=Math.min(n,t),r=Math.min(r,o),i=Math.max(i,s),a=Math.max(a,c)}t.minX=n-2,t.minY=r-2-2,t.maxX=i+2,t.maxY=a+2;let o=Math.max(0,...Q(t.name).map(e=>P(e))),s=t.maxX-t.minX,c=o+1;if(c>s){let e=c-s,n=Math.floor(e/2),r=e-n;t.minX-=n,t.maxX+=r}let l=t.titleRoom??0;l>0&&(t.maxX+=l)}function cr(e){let t=e.subgraphs.filter(e=>e.parent===null&&e.nodes.length>0);for(let e=0;e<t.length;e++)for(let n=e+1;n<t.length;n++){let r=t[e],i=t[n];r.minX<i.maxX&&r.maxX>i.minX&&(r.maxY>=i.minY-1&&r.minY<i.minY?i.minY=r.maxY+1+1:i.maxY>=r.minY-1&&i.minY<r.minY&&(r.minY=i.maxY+1+1)),r.minY<i.maxY&&r.maxY>i.minY&&(r.maxX>=i.minX-1&&r.minX<i.minX?i.minX=r.maxX+1+1:i.maxX>=r.minX-1&&i.minX<r.minX&&(r.minX=i.maxX+1+1))}}function lr(e){return Math.max(0,...Q(e.name).map(e=>P(e)))+1}function ur(e){let t=null;for(let n of rr(e)){let e=n.gridCoord;e&&(t=t?{minX:Math.min(t.minX,e.x),minY:Math.min(t.minY,e.y),maxX:Math.max(t.maxX,e.x+2),maxY:Math.max(t.maxY,e.y+2)}:{minX:e.x,minY:e.y,maxX:e.x+2,maxY:e.y+2})}return t}function dr(e){let t=e.subgraphs.filter(e=>e.nodes.length>0),n=(e,t)=>tr(e,t)||tr(t,e),r=!1;for(let i=0;i<64;i++){for(let t of e.subgraphs)sr(e,t);let i=!1;for(let a of t){for(let o of t){if(n(a,o)||!(a.minX<o.minX)||!(a.minY<o.maxY&&a.maxY>o.minY))continue;let t=a.maxX+2-o.minX;if(t<=0)continue;let s=a.parent===null&&o.parent===null,c=a.maxX+2,l=rr(o).every(e=>!e.drawingCoord||e.drawingCoord.x>c);if(s&&l&&o.maxX-c>=lr(o))continue;let u=ur(a),d=ur(o);if(!u||!d||u.maxX+1>=d.minX)continue;let f=u.maxX+1;e.columnWidth.set(f,(e.columnWidth.get(f)??0)+t);for(let t of e.nodes)t.drawingCoord=$(e,Z(t));i=!0,r=!0;break}if(i)break}if(!i)break}return fr(e),r}function fr(e){for(let t of e.subgraphs)sr(e,t);cr(e)}function pr(e){if(e.subgraphs.length===0)return;let t=0,n=0;for(let r of e.subgraphs)t=Math.min(t,r.minX),n=Math.min(n,r.minY);let r=-t,i=-n;if(r!==0||i!==0){e.offsetX=r,e.offsetY=i;for(let t of e.subgraphs)t.minX+=r,t.minY+=i,t.maxX+=r,t.maxY+=i;for(let t of e.nodes)t.drawingCoord&&(t.drawingCoord.x+=r,t.drawingCoord.y+=i)}}function mr(e,t){let n=t=>{let n=Dr(e,t);return n.length>0?n[0].name:null},r=new Map,i=t.map((e,t)=>{let i=n(e);if(i===null)return t;let a=r.get(i);return a===void 0?(r.set(i,t),t):a});return t.map((e,t)=>({node:e,i:t})).sort((e,t)=>i[e.i]-i[t.i]||e.i-t.i).map(({node:e})=>e)}function hr(e,t){let n=new Set,r=t=>{let r=[t];for(;r.length>0;){let t=r.shift();if(!n.has(t.name)){n.add(t.name);for(let n of Dr(e,t))r.push(n)}}};for(let e of t)r(e);let i=[...t];for(let t of e.nodes)n.has(t.name)||(i.push(t),r(t));return i}function gr(e,t){return e.nodes.find(e=>e.gridCoord!==null&&e.gridCoord.x===t.x&&e.gridCoord.y===t.y)}function _r(e,t,n,r,i){let a=r===`x`?`y`:`x`,o=i=>{let o=4;for(;;){let s=t[r]+i*o;if(s<0)return null;let c=r===`x`?{x:s,y:t[a]}:{x:t[a],y:s};if(pt(e.grid,c,3))return c;let l=gr(e,c);if(!l||nr(e,l)!==n)return null;o+=4}};return o(i)??o(i*-1)}function vr(e,t,n,r){let i=nr(e,t);if(!i)return;let a=n.get(i);if(!a)return;n.delete(i);let o=Z(t),s=ar(e,t)===`LR`?`y`:`x`;for(let t of a){let n=ar(e,t),a=_r(e,o,i,s,1);Jn(e,e.nodes[t.index],a??o,n),r.add(t)}}function yr(e,t){let n=!1,r=new Set,i=!0;for(;i;){i=!1;let a=!1,o=t=>e.config.graphDirection===`LR`?t.gridCoord?.y??0:t.gridCoord?.x??0,s=e.nodes.filter(e=>e.gridCoord!==null).sort((e,t)=>o(e)-o(t));for(let o of s){let s=o.gridCoord;if(s===null)continue;let c=[...Dr(e,o)].sort((t,n)=>Er(e,t,n));for(let l of c){if(l.gridCoord!==null)continue;let c=br(e,l);if(!n&&c.some(e=>e.gridCoord===null)){if(a=!0,!r.has(l)){r.add(l);let n=e.config.graphDirection===`LR`?s.x+4:s.y+4;t[n]=(t[n]??0)+4}continue}let u=er(e,o),d=er(e,l),f=u&&u===d&&u.direction?u.direction:e.config.graphDirection,p=f===`LR`?s.x+4:s.y+4;if(f===e.config.graphDirection)for(let e of c){let t=e.gridCoord;t&&(p=Math.max(p,f===`LR`?t.x+4:t.y+4))}let m;m=f===e.config.graphDirection?t[p]??0:f===`LR`?s.y:s.x;let h=f===`LR`?{x:p,y:m}:{x:m,y:p};Jn(e,e.nodes[l.index],h,f),f===e.config.graphDirection&&(t[p]=m+4),i=!0}}!i&&a&&!n&&(n=!0,i=!0)}}function br(e,t){let n=[];for(let r of e.edges)r.to!==t||!r.clusterTarget||r.from===t||n.includes(r.from)||rr(r.clusterTarget).includes(r.from)||n.push(r.from);return n}function xr(e,t,n,r){let i=new Set(e.edges.filter(e=>e.to===t&&e.from!==t).map(e=>e.from));if(i.size!==1)return;let[a]=[...i],o=a.gridCoord;if(!(!o||$n(e,a))&&!e.edges.some(e=>e.to===a||e.from===a&&e.to!==t)&&!(r?o.y===n.y:o.x===n.x)){for(let t=0;t<3;t++)for(let n=0;n<3;n++)e.grid.delete(ot({x:o.x+t,y:o.y+n}));Jn(e,a,r?{x:o.x,y:n.y}:{x:n.x,y:o.y})}}function Sr(e){let t=e.config.graphDirection===`LR`;for(let n=0;n<e.nodes.length+1;n++){let n=!1;for(let r of e.subgraphs){let i=rr(r),a=ur(r);if(a)for(let r of e.nodes){let o=r.gridCoord;if(!(!o||i.includes(r))&&!(o.x+2<a.minX||o.x>a.maxX||o.y+2<a.minY||o.y>a.maxY)){for(let t=0;t<3;t++)for(let n=0;n<3;n++)e.grid.delete(ot({x:o.x+t,y:o.y+n}));xr(e,r,Jn(e,r,t?{x:o.x,y:a.maxY+2}:{x:a.maxX+2,y:o.y}),t),n=!0}}}if(!n)return}}function Cr(e){let t=e.config.graphDirection,n=[],r=new Set;for(let t of e.edges)t.from!==t.to&&r.add(t.to.name);let i=hr(e,e.nodes.filter(e=>!r.has(e.name))),a=new Set(i),o=[],s=[];for(let t of i){let n=nr(e,t);n!==null&&rr(n).some(n=>n!==t&&!a.has(n)&&!ir(e,t,n))?o.push(t):s.push(t)}let c=mr(e,s),l=new Map;for(let t of o){let n=nr(e,t),r=l.get(n);r?r.push(t):l.set(n,[t])}let u=new Set,d=!1,f=!1;for(let t of c)$n(e,t)?Dr(e,t).length>0&&(f=!0):d=!0;let p=t===`LR`&&d&&f,m,h=[];p?(m=c.filter(t=>!$n(e,t)),h=c.filter(t=>$n(e,t))):m=c;for(let r of m){let i=t===`LR`?{x:0,y:n[0]??0}:{x:n[0]??0,y:0};Jn(e,e.nodes[r.index],i),n[0]=(n[0]??0)+4,vr(e,r,l,u)}if(p&&h.length>0)for(let r of h){let i=t===`LR`?{x:4,y:n[4]??0}:{x:n[4]??0,y:4};Jn(e,e.nodes[r.index],i),n[4]=(n[4]??0)+4,vr(e,r,l,u)}yr(e,n);for(let t of o){if(u.has(t))continue;let r=nr(e,t),i=ar(e,t),a=rr(r).filter(e=>e!==t&&e.gridCoord!==null),o;if(a.length>0){let t=a[0];for(let e of a)(i===`LR`?e.gridCoord.x<t.gridCoord.x:e.gridCoord.y<t.gridCoord.y)&&(t=e);let n=i===`LR`?`y`:`x`;o=_r(e,t.gridCoord,r,n,1)??{x:t.gridCoord.x,y:t.gridCoord.y}}else o=i===`LR`?{x:0,y:n[0]??0}:{x:n[0]??0,y:0},n[0]=(n[0]??0)+4;Jn(e,e.nodes[t.index],o,i)}yr(e,n),Sr(e);for(let t of e.nodes)Yn(e,t);e.pathBudget=Ct(),Rr(e),zt(e),e.bundles=At(e),Pt(e);let g=fn(),_=hn(),v=ut(e.grid),y=new Set;for(let t of e.edges){if(t.bundle&&t.path.length>0){Xn(e,t.path),mn(v,g,t.path,t.style),bn(v,_,t.path,t),Wr(e,t);continue}let{added:n,engaged:r}=rn(e,t);r&&y.add(t);let i=e.preferStraightRoutes;r&&(e.preferStraightRoutes=!0),Vr(e,t),Qn(e,t,g,_,v),e.preferStraightRoutes=i,an(e,n),Xn(e,t.path),mn(v,g,t.path,t.style),bn(v,_,t.path,t),Wr(e,t)}for(let t of e.nodes)t.drawingCoord=$(e,Z(t)),t.drawing=Ei(t,e);if(fr(e),dr(e),Ut(e)){for(let t of e.nodes)t.drawingCoord=$(e,Z(t));fr(e)}let b=()=>{for(let t of e.nodes)t.drawingCoord=$(e,Z(t));fr(e)};on(e,y,b),ln(e,()=>{b(),on(e,y,b)}),dr(e),on(e,y,b),pr(e),Te(e.canvas,e.columnWidth,e.rowHeight,e.offsetX,e.offsetY),U(e.roleCanvas,e.columnWidth,e.rowHeight,e.offsetX,e.offsetY)}function wr(e,t){return e.edges.filter(e=>e.from.name===t.name)}function Tr(e,t){let n=er(e,t),r=[],i=n;for(;i!==null;)r.unshift(i),i=i.parent;return r}function Er(e,t,n){let r=Tr(e,t),i=Tr(e,n),a=0;for(;a<r.length&&a<i.length&&r[a]===i[a];)a++;let o=r[a],s=i[a];return!o||!s||o===s?0:e.subgraphs.indexOf(s)-e.subgraphs.indexOf(o)}function Dr(e,t){return wr(e,t).map(e=>e.to)}function Or(e){return e===K?q:e===q?K:e===J?Y:e===Y?J:e===Ye?Qe:e===Xe?Ze:e===Ze?Xe:e===Qe?Ye:$e}function kr(e,t){return e.x===t.x?e.y<t.y?q:K:e.y===t.y?e.x<t.x?Y:J:e.x<t.x?e.y<t.y?Ze:Ye:e.y<t.y?Qe:Xe}function Ar(e){return e===`LR`?[Y,q,q,Y]:[q,Y,Y,q]}function jr(e,t){if(e.from===e.to)return Ar(t);let n=kr(Z(e.from),Z(e.to)),r,i,a,o,s=t===`LR`?X(n,J)||X(n,Xe)||X(n,Qe):X(n,K)||X(n,Xe)||X(n,Ye);return X(n,Ze)?t===`LR`?(r=q,i=J,a=Y,o=K):(r=Y,i=K,a=q,o=J):X(n,Ye)?t===`LR`?(r=K,i=J,a=Y,o=q):(r=Y,i=q,a=K,o=J):X(n,Qe)?t===`LR`?(r=q,i=q,a=J,o=K):(r=J,i=K,a=q,o=Y):X(n,Xe)?t===`LR`?(r=q,i=q,a=J,o=q):(r=Y,i=Y,a=K,o=Y):s?t===`LR`&&X(n,J)?(r=q,i=q,a=J,o=Y):t===`TD`&&X(n,K)?(r=Y,i=Y,a=K,o=q):(r=n,i=Or(n),a=n,o=Or(n)):(r=n,i=Or(n),a=n,o=Or(n)),[r,i,a,o]}var Mr=2,Nr=200;function Pr(e,t){let n=e.gridCoord;return n?t.x>=n.x&&t.x<=n.x+2&&t.y>=n.y&&t.y<=n.y+2:!1}function Fr(e,t,n=[]){for(let r=1;r<t.length-1;r++){let i=t[r];if(dt(e.grid,i)&&!n.some(e=>Pr(e,i)))return!1}return!0}function Ir(e){let t=e.from.gridCoord,n=e.to.gridCoord;return!t||!n?!1:t.y===n.y&&t.x!==n.x}function Lr(e){let t=e.from.name,n=e.to.name;return Ir(e)&&n<t?JSON.stringify([n,t]):JSON.stringify([t,n])}function Rr(e){let t=new Map;for(let n of e.edges){if(n.from===n.to)continue;let e=Lr(n),r=t.get(e);r?r.push(n):t.set(e,[n])}for(let e of t.values()){if(e.length<2)continue;let t=new Set;for(let n=0;n<e.length;n++)e[n].parallelLane={index:n,total:e.length,usedOffsets:t}}}function zr(e,t,n,r,i){let a=at(Z(t.from),n),o=at(Z(t.to),r),s=X(n,J)||X(n,Y),c=[t.from,t.to],l=t.parallelLane.usedOffsets,u=X(n,Y)?1:X(n,J)?-1:X(n,q)?1:-1,d=X(r,Y)?1:X(r,J)?-1:X(r,q)?1:-1,f=s?{x:a.x+u,y:a.y}:{x:a.x,y:a.y+u},p=s?{x:o.x+d,y:o.y}:{x:o.x,y:o.y+d},m=[a,o],h=[a,o],g=Mr*i;for(let n=0;n<Nr;n++){let r=g+n;if(l.has(r))continue;let i=a.y+r,u=a.x+r;{let n=s?q:Y,i=at(Z(t.from),n),a=at(Z(t.to),n),o=s?[{x:i.x,y:Math.max(i.y,a.y)+r-1},{x:a.x,y:Math.max(i.y,a.y)+r-1}]:[{x:Math.max(i.x,a.x)+r-1,y:i.y},{x:Math.max(i.x,a.x)+r-1,y:a.y}],u=Tt([i,...o,a]);if(Fr(e,gt(u),c))return l.add(r),{path:u,labelSegment:o,faces:{startDir:n,endDir:n}}}let d=s?[{x:a.x,y:i},{x:o.x,y:i}]:[{x:u,y:a.y},{x:u,y:o.y}],_=Tt([a,d[0],s?{x:p.x,y:i}:{x:u,y:p.y},p,o]);if(Fr(e,gt(_),c))return l.add(r),{path:_,labelSegment:d};m=_,h=d;let v=s?f.x!==p.x:f.y!==p.y,y=s?v?[{x:f.x,y:i},{x:p.x,y:i}]:[{x:f.x,y:i},{x:f.x,y:i}]:v?[{x:u,y:f.y},{x:u,y:p.y}]:[{x:u,y:f.y},{x:u,y:f.y}],b=Tt(s?[a,f,{x:f.x,y:i},{x:p.x,y:i},p,o]:[a,f,{x:u,y:f.y},{x:u,y:p.y},p,o]);if(Fr(e,gt(b),c))return l.add(r),{path:b,labelSegment:y};m=b,h=y}return{path:m,labelSegment:h}}function Br(e,t,n){let r=e.config.graphDirection===`LR`?Y:q,i=at(Z(t.anchor),r),a=Vt(e,t,n);return a?{path:[i,...a.path],startDir:r,endDir:a.endDir,labelSegment:a.labelSegment}:null}function Vr(e,t){let n=er(e,t.from),r=er(e,t.to),[i,a,o,s]=jr(t,n&&n===r&&n.direction?n.direction:e.config.graphDirection),c=t.clusterSource?e.clusterExitPlans?.get(t.clusterSource):void 0;if(c?.edges.has(t)){let n=t.parallelLane&&t.parallelLane.index>0?Br(e,c,t):It(e,c,t);if(n){t.startDir=n.startDir,t.endDir=n.endDir,t.path=n.path,t.text.length>0&&Kr(e,t,n.labelSegment,P(t.text));return}c.edges.delete(t),t.labelLine=[]}if(t.clusterTarget){let n=Lt(e,t);if(n){t.startDir=n.startDir,t.endDir=n.endDir,t.path=n.path,t.clusterEntered=!0;let r=n.path[n.path.length-1];if(t.text.length>0&&e.config.graphDirection===`LR`){let n=e.columnWidth.get(r.x)??0;e.columnWidth.set(r.x,Math.max(n,P(t.text)+5))}return}t.clusterEntered=!1}if(t.parallelLane&&t.parallelLane.index>0){let n=X(i,a),r=n?o:i,c=n?s:a;t.startDir=r,t.endDir=c;let l=zr(e,t,r,c,t.parallelLane.index);t.path=l.path,l.faces&&(t.startDir=l.faces.startDir,t.endDir=l.faces.endDir),t.text.length>0&&Kr(e,t,l.labelSegment,P(t.text));return}let l=at(Z(t.from),i),u=at(Z(t.to),a),d=kt(e,l,u,tt(i)),f=kt(e,at(Z(t.from),o),at(Z(t.to),s),tt(o));if(d!==null&&f!==null){d.length<=f.length?(t.startDir=i,t.endDir=a,t.path=d):(t.startDir=o,t.endDir=s,t.path=f);return}if(d!==null){t.startDir=i,t.endDir=a,t.path=d;return}if(f!==null){t.startDir=o,t.endDir=s,t.path=f;return}t.startDir=i,t.endDir=a,t.path=Gr([l,u])}function Hr(e,t){for(let n of e.nodes){let e=n.gridCoord;if(e&&t>=e.x&&t<=e.x+2)return!0}return!1}function Ur(e,t,n,r){if(!Hr(e,r))return r;for(let i=1;i<=n-t;i++){let a=r+i;if(a<=n&&!Hr(e,a))return a;let o=r-i;if(o>=t&&!Hr(e,o))return o}return r}function Wr(e,t){if(t.text.length===0||t.parallelLane&&t.parallelLane.index>0||t.clusterSource&&e.clusterExitPlans?.get(t.clusterSource)?.edges.has(t))return;let n=P(t.text),r=Gr(t.path),i=r.length,a=[];for(let t=1;t<i;t++){let n=r[t-1],i=r[t],o=[n,i],s=qr(e,o),c=n.x===i.x;a.push({line:o,width:s,index:t,isVertical:c})}let o=t=>Fr(e,gt(t)),s=e=>e.index===1||e.index===a.length,c=a.filter(e=>e.width>=n&&!s(e)&&o(e.line)),l;if(c.length>0)c.sort((e,t)=>t.index-e.index),l=c[0].line;else{let e=a.filter(e=>e.width>=n&&o(e.line));if(e.length>0)e.sort((e,t)=>t.index-e.index),l=e[0].line;else{let e=a.filter(e=>!s(e)&&o(e.line)),n=a.filter(e=>o(e.line)),r=e.length>0?e:n.length>0?n:a;if(r.sort((e,t)=>t.width-e.width),r.length>0)l=r[0].line;else{let e=t.path[0]??{x:0,y:0};l=[e,e]}}}Kr(e,t,l,n)}function Gr(e){let t=[];for(let n of e){let e=t[t.length-1];e&&e.x!==n.x&&e.y!==n.y&&t.push({x:n.x,y:e.y}),t.push(n)}return t}function Kr(e,t,n,r){let i=Math.min(n[0].x,n[1].x),a=Math.max(n[0].x,n[1].x),o=Ur(e,i,a,i+Math.floor((a-i)/2)),s=e.columnWidth.get(o)??0;e.columnWidth.set(o,Math.max(s,r+2)),t.labelLine=[n[0],n[1]]}function qr(e,t){let n=0,r=Math.min(t[0].x,t[1].x),i=Math.max(t[0].x,t[1].x);for(let t=r;t<=i;t++)n+=e.columnWidth.get(t)??0;return n}var Jr={solid:{h:{unicode:`─`,ascii:`-`},v:{unicode:`│`,ascii:`|`}},dotted:{h:{unicode:`┄`,ascii:`.`},v:{unicode:`┆`,ascii:`:`}},thick:{h:{unicode:`━`,ascii:`=`},v:{unicode:`┃`,ascii:`‖`}},invisible:{h:{unicode:` `,ascii:` `},v:{unicode:` `,ascii:` `}}};function Yr(e,t,n,r,i,a,o=`solid`){let s=kr(t,n),c=[],l=Jr[o],u=a?l.h.ascii:l.h.unicode,d=a?l.v.ascii:l.v.unicode;if(X(s,K))for(let a=t.y-r;a>=n.y-i;a--)c.push({x:t.x,y:a}),z(e,t.x,a,d);else if(X(s,q))for(let a=t.y+r;a<=n.y+i;a++)c.push({x:t.x,y:a}),z(e,t.x,a,d);else if(X(s,J))for(let a=t.x-r;a>=n.x-i;a--)c.push({x:a,y:t.y}),z(e,a,t.y,u);else if(X(s,Y))for(let a=t.x+r;a<=n.x+i;a++)c.push({x:a,y:t.y}),z(e,a,t.y,u);else if(X(s,Xe)){for(let i=t.x-r;i>=n.x;i--)c.push({x:i,y:t.y}),z(e,i,t.y,u);for(let r=t.y-1;r>=n.y-i;r--)c.push({x:n.x,y:r}),z(e,n.x,r,d)}else if(X(s,Ye)){for(let i=t.x+r;i<=n.x;i++)c.push({x:i,y:t.y}),z(e,i,t.y,u);for(let r=t.y-1;r>=n.y-i;r--)c.push({x:n.x,y:r}),z(e,n.x,r,d)}else if(X(s,Qe)){for(let i=t.x-r;i>=n.x;i--)c.push({x:i,y:t.y}),z(e,i,t.y,u);for(let r=t.y+1;r<=n.y+i;r++)c.push({x:n.x,y:r}),z(e,n.x,r,d)}else if(X(s,Ze)){if(n.x-t.x<=1)for(let a=t.y+r;a<=n.y+i;a++)c.push({x:t.x,y:a}),z(e,t.x,a,d);else{for(let i=t.x+r;i<=n.x;i++)c.push({x:i,y:t.y}),z(e,i,t.y,u);for(let r=t.y+1;r<=n.y+i;r++)c.push({x:n.x,y:r}),z(e,n.x,r,d)}}return c}function Xr(e,t){if(t.path.length===0){let t=I(e.canvas);return[t,t,t,t,t,t]}let n=pi(e,t),[r,i,a]=$r(e,t.path,t.style,Qr(e,t)),o=i.length>0,s=t.style===`invisible`,c=o&&!s?ni(e,t.path,i[0],t.from):I(e.canvas),l;l=t.hasArrowEnd&&o?oi(e,i[i.length-1],a[a.length-1],t.endMarker):I(e.canvas);let u;if(t.hasArrowStart&&o){let n=i[0][0],r=Zr(a[0]),o={x:n.x,y:n.y};X(a[0],Y)?o.x=n.x-1:X(a[0],J)?o.x=n.x+1:X(a[0],q)?o.y=n.y-1:X(a[0],K)&&(o.y=n.y+1),u=oi(e,[n,o],r,t.startMarker)}else u=I(e.canvas);let d=s?I(e.canvas):si(e,t.path);return[r,c,l,u,d,n]}function Zr(e){return X(e,K)?q:X(e,q)?K:X(e,J)?Y:X(e,Y)?J:X(e,Xe)?Ze:X(e,Ye)?Qe:X(e,Qe)?Ye:X(e,Ze)?Xe:$e}function Qr(e,t){let n=t.clusterTarget,r=t.path[t.path.length-1],i=t.path[t.path.length-2];if(!t.clusterEntered||!n||!r||!i)return;let a=$(e,i),o=$(e,r);return e.config.graphDirection===`LR`?n.minX>a.x?{x:n.minX,y:o.y}:void 0:n.minY>a.y?{x:o.x,y:n.minY}:void 0}function $r(e,t,n=`solid`,r){let i=I(e.canvas),a=t[0],o=[],s=[];for(let c=1;c<t.length;c++){let l=t[c],u=$(e,a),d=r&&c===t.length-1?r:$(e,l);if(it(u,d)){a=l;continue}let f=kr(a,l),p=Yr(i,u,d,1,-1,e.config.useAscii,n);p.length===0&&p.push(u),o.push(p),s.push(f),a=l}return[i,o,s]}var ei=new Set([`│`,`┃`,`║`,`┆`,`┊`,`|`,`‖`]),ti=new Set([`─`,`━`,`═`,`╌`,`┄`,`-`,`=`]);function ni(e,t,n,r){let i=I(e.canvas),a=e.config.useAscii;if(r.shape===`state-start`||r.shape===`state-end`)return i;let o=n[0],s=kr(t[0],t[1]),c=a?`+`:null,l=(t,n)=>e.canvas[t]?.[n];if(X(s,K)){let e=o.x,t=o.y+1,n=l(e,t);z(i,e,t,n!==void 0&&ti.has(n)?c??`┴`:a?`|`:`│`)}else if(X(s,q)){let e=o.x,t=o.y-1,n=l(e,t);z(i,e,t,n!==void 0&&ti.has(n)?c??`┬`:a?`|`:`│`)}else if(X(s,J)||X(s,Y)){let e=r.drawingCoord,t=r.drawing;if(e===null||t===null)throw Error(`drawBoxStart: node "${r.name}" has no drawingCoord/drawing assigned`);let n=t.length,u=X(s,J)?e.x:e.x+n-1,d=o.y,f=l(u,d);z(i,u,d,f!==void 0&&ei.has(f)?c??(X(s,J)?`┤`:`├`):a?`-`:`─`)}return i}function ri(e,t){if(t===`circle`)return e?`o`:`○`;if(t===`cross`)return e?`x`:`✕`}function ii(e){if(X(e,K))return`▲`;if(X(e,q))return`▼`;if(X(e,J))return`◄`;if(X(e,Y))return`►`;if(X(e,Ye))return`↗`;if(X(e,Xe))return`↖`;if(X(e,Ze))return`↘`;if(X(e,Qe))return`↙`}function ai(e){if(X(e,K))return`^`;if(X(e,q))return`v`;if(X(e,J))return`<`;if(X(e,Y))return`>`}function oi(e,t,n,r){let i=I(e.canvas);if(t.length===0)return i;let a=t[t.length-1],o=kr(t.length>=2?t[t.length-2]:t[0],a);(t.length===1||X(o,$e))&&(o=n);let s=ri(e.config.useAscii,r),c;if(s!==void 0)c=s;else if(e.config.useAscii){let e=ai(o);c=e===void 0?ai(n)??`*`:e}else{let e=ii(o);c=e===void 0?ii(n)??`●`:e}return z(i,a.x,a.y,c),i}function si(e,t){let n=I(e.canvas);for(let r=1;r<t.length-1;r++){let i=t[r],a=$(e,i),o=kr(t[r-1],i),s=kr(i,t[r+1]),c;c=e.config.useAscii?`+`:X(o,s)?X(o,K)||X(o,q)?`│`:`─`:X(o,Y)&&X(s,q)||X(o,K)&&X(s,J)?`┐`:X(o,Y)&&X(s,K)||X(o,q)&&X(s,J)?`┘`:X(o,J)&&X(s,q)||X(o,K)&&X(s,Y)?`┌`:X(o,J)&&X(s,K)||X(o,q)&&X(s,Y)?`└`:`+`,z(n,a.x,a.y,c)}return n}function ci(e,t){return e.edges.some(e=>e!==t&&e.from===t.to&&e.to===t.from)}function li(e,t){if(t.text.length===0)return null;let n=qn(e,t.labelLine),r;if(t.path.length>=2){let e=t.path[0].y,n=t.path[t.path.length-1].y;n<e?r=!0:n>e&&(r=!1)}let i=ci(e,t)||e.config.graphDirection!==`LR`&&t.clusterSource!==void 0&&e.clusterExitPlans?.get(t.clusterSource)?.edges.has(t)===!0;return di(e,t,ui(e,fi(e,mi(n,t.text,r,i),n[0]?.x===n[1]?.x?n[0]?.x:void 0),n))}function ui(e,t,n){if(n.length<2||e.subgraphs.length===0)return t;let r=Math.min(n[0].y,n[1].y),i=Math.max(n[0].y,n[1].y),a=(t,n)=>e.subgraphs.some(e=>{if(e.nodes.length===0)return!1;let r=Q(e.name).length;return n>=e.minY&&n<=e.minY+r&&t.x<=e.maxX&&t.x+P(t.text)-1>=e.minX});return t.map(e=>{if(!a(e,e.y))return e;let t=null;for(let n=r+1;n<i;n++)a(e,n)||(t===null||Math.abs(n-e.y)<Math.abs(t-e.y))&&(t=n);return t===null?e:{...e,y:t}})}function di(e,t,n){let r=t.clusterSource?e.clusterExitPlans?.get(t.clusterSource):void 0;if(e.config.graphDirection!==`LR`||!r?.edges.has(t)||!t.parallelLane||t.parallelLane.index===0)return n;let i=$(e,r.gutter).x+2;return n.map(e=>e.x<i?{...e,x:i}:e)}function fi(e,t,n){return n===void 0?t:t.map(t=>{let r=P(t.text),{x:i}=t;for(let a of e.subgraphs)if(!(t.y<=a.minY||t.y>=a.maxY))for(let e of[a.minX,a.maxX])e<i||e>=i+r||n===e||(i=n>e?e+1:e-r);return i===t.x?t:{...t,x:i}})}function pi(e,t){let n=I(e.canvas);for(let{x:r,y:i,text:a}of li(e,t)??[])we(n,{x:r,y:i},a);return n}function mi(e,t,n,r=!1){if(e.length<2)return[];let i=Math.min(e[0].x,e[1].x),a=Math.max(e[0].x,e[1].x),o=Math.min(e[0].y,e[1].y),s=Math.max(e[0].y,e[1].y),c=i+Math.floor((a-i)/2),l=o+Math.floor((s-o)/2);if(n!==void 0&&i===a){let e=s-o,t=Math.max(1,Math.floor(e/4));(r?n:!n)?l-=t:l+=t}let u=Q(t),d=l-Math.floor((u.length-1)/2);return u.map((e,t)=>({x:c-Math.floor(P(e)/2),y:d+t,text:e}))}function hi(e,t,n){let r=Z(t),i=0;for(let t=0;t<2;t++)i+=e.columnWidth.get(r.x+t)??0;let a=0;for(let t=0;t<2;t++)a+=e.rowHeight.get(r.y+t)??0;let o={width:i+1,height:a+1,labelArea:{x:0,y:0,width:0,height:0},gridColumns:[0,0,0],gridRows:[0,0,0]},s=t.drawingCoord;if(s===null)throw Error(`Node "${t.name}" has no drawingCoord; grid layout must run before bundled-edge drawing`);return Kn(t.shape,n,o,s)}function gi(e,t,n){let r=I(e.canvas),i=t.pathToJunction;if(!i||i.length===0)return[r,r,r,r,r,r];let a=I(e.canvas),o=e.config.useAscii,s=i.map((r,a)=>n.type===`fan-in`&&a===0?hi(e,t.from,t.startDir):n.type===`fan-out`&&a===i.length-1?hi(e,t.to,t.endDir):$(e,r));for(let e=1;e<s.length;e++){let n=s[e-1],r=s[e];it(n,r)||Yr(a,n,r,1,-1,o,t.style)}let c=I(e.canvas);for(let t=1;t<i.length-1;t++){let n=i[t],r=$(e,n),a=kr(i[t-1],n),s=kr(n,i[t+1]),l;l=o?`+`:X(a,Y)&&X(s,q)||X(a,K)&&X(s,J)?`┐`:X(a,Y)&&X(s,K)||X(a,q)&&X(s,J)?`┘`:X(a,J)&&X(s,q)||X(a,K)&&X(s,Y)?`┌`:X(a,J)&&X(s,K)||X(a,q)&&X(s,Y)?`└`:`+`,z(c,r.x,r.y,l)}let l=I(e.canvas);if(n.type===`fan-in`&&i.length>=2){let e=s[0],t=kr(i[0],i[1]),n=o?`+`:null;X(t,K)?z(l,e.x,e.y,n??`┴`):X(t,q)?z(l,e.x,e.y,n??`┬`):X(t,J)?z(l,e.x,e.y,n??`┤`):X(t,Y)&&z(l,e.x,e.y,n??`├`)}return[a,l,r,r,c,I(e.canvas)]}function _i(e,t){let n=I(e.canvas),r=I(e.canvas);if(t.sharedPath.length<2)return[n,r];let i=e.config.useAscii,a=t.edges[0]?.style??`solid`,o=e.config.graphDirection,s=t.sharedPath.map((n,r)=>{if(t.type===`fan-in`&&r===t.sharedPath.length-1){let n=o===`TD`?K:J;return hi(e,t.sharedNode,n)}if(t.type===`fan-out`&&r===0){let n=o===`TD`?q:Y;return hi(e,t.sharedNode,n)}return $(e,n)});for(let e=1;e<s.length;e++){let t=s[e-1],r=s[e];it(t,r)||Yr(n,t,r,1,-1,i,a)}for(let n=1;n<t.sharedPath.length-1;n++){let a=t.sharedPath[n],o=$(e,a),s=kr(t.sharedPath[n-1],a),c=kr(a,t.sharedPath[n+1]),l;l=i?`+`:X(s,Y)&&X(c,q)||X(s,K)&&X(c,J)?`┐`:X(s,Y)&&X(c,K)||X(s,q)&&X(c,J)?`┘`:X(s,J)&&X(c,q)||X(s,K)&&X(c,Y)?`┌`:X(s,J)&&X(c,K)||X(s,q)&&X(c,Y)?`└`:`+`,z(r,o.x,o.y,l)}return[n,r]}function vi(e,t){let n=I(e.canvas);if(t.sharedPath.length<2)return n;let r=t.sharedPath.length-1,i=t.sharedPath[r-1],a=t.sharedPath[r],o=kr(i,a),s=e.config.graphDirection,c=s===`TD`?K:J,l=hi(e,t.sharedNode,c);s===`TD`?--l.y:--l.x;let u=t.edges[0]?.endMarker,d=t.edges.every(e=>e.endMarker===u)?u:void 0,f=ri(e.config.useAscii,d),p;return p=f===void 0?e.config.useAscii?X(o,K)?`^`:X(o,q)?`v`:X(o,J)?`<`:X(o,Y)?`>`:`v`:X(o,K)?`▲`:X(o,q)?`▼`:X(o,J)?`◄`:X(o,Y)?`►`:`▼`:f,z(n,l.x,l.y,p),n}function yi(e,t){let n=I(e.canvas);if(!t.pathToJunction||t.pathToJunction.length<2)return n;let r=t.pathToJunction.length-1,i=t.pathToJunction[r-1],a=t.pathToJunction[r],o=kr(i,a),s=e.config.graphDirection,c=s===`TD`?K:J,l=hi(e,t.to,c);s===`TD`?--l.y:--l.x;let u=ri(e.config.useAscii,t.endMarker),d;return d=u===void 0?e.config.useAscii?X(o,K)?`^`:X(o,q)?`v`:X(o,J)?`<`:X(o,Y)?`>`:`v`:X(o,K)?`▲`:X(o,q)?`▼`:X(o,J)?`◄`:X(o,Y)?`►`:`▼`:u,z(n,l.x,l.y,d),n}function bi(e){return X(e,K)?q:X(e,q)?K:X(e,J)?Y:X(e,Y)?J:e}function xi(e,t){let n=I(e.canvas);if(t.sharedPath.length<2)return n;let r=t.sharedPath[0],i=t.sharedPath[1],a=bi(kr(r,i)),o=e.config.graphDirection,s=o===`TD`?q:Y,c=hi(e,t.sharedNode,s);o===`TD`?c.y+=1:c.x+=1;let l;return l=e.config.useAscii?X(a,K)?`^`:X(a,q)?`v`:X(a,J)?`<`:X(a,Y)?`>`:`^`:X(a,K)?`▲`:X(a,q)?`▼`:X(a,J)?`◄`:X(a,Y)?`►`:`▲`,z(n,c.x,c.y,l),n}function Si(e,t){let n=I(e.canvas);if(!t.pathToJunction||t.pathToJunction.length<2)return n;let r=t.pathToJunction[0],i=t.pathToJunction[1],a=bi(kr(r,i)),o=hi(e,t.from,t.startDir);X(t.startDir,K)?--o.y:X(t.startDir,q)?o.y+=1:X(t.startDir,J)?--o.x:X(t.startDir,Y)&&(o.x+=1);let s;return s=e.config.useAscii?X(a,K)?`^`:X(a,q)?`v`:X(a,J)?`<`:X(a,Y)?`>`:`v`:X(a,K)?`▲`:X(a,q)?`▼`:X(a,J)?`◄`:X(a,Y)?`►`:`▼`,z(n,o.x,o.y,s),n}function Ci(e,t){let n=I(e.canvas);if(!t.junctionPoint)return n;let r=$(e,t.junctionPoint),i=e.config.useAscii,a=!1,o=!1,s=!1,c=!1;if(t.sharedPath.length>=2){let e=t.type===`fan-in`?0:t.sharedPath.length-1,n=t.type===`fan-in`?1:t.sharedPath.length-2,r=kr(t.sharedPath[e],t.sharedPath[n]);X(r,q)?o=!0:X(r,K)?a=!0:X(r,Y)?c=!0:X(r,J)&&(s=!0)}for(let e of t.edges)if(e.pathToJunction&&e.pathToJunction.length>=2){let n=t.type===`fan-in`?e.pathToJunction.length-1:0,r=t.type===`fan-in`?e.pathToJunction.length-2:1,i=kr(e.pathToJunction[r],e.pathToJunction[n]);X(i,q)?a=!0:X(i,K)?o=!0:X(i,Y)?s=!0:X(i,J)&&(c=!0)}let l;return l=i?`+`:a&&o&&s&&c?`┼`:o&&s&&c&&!a?`┬`:a&&s&&c&&!o?`┴`:a&&o&&c&&!s?`├`:a&&o&&s&&!c?`┤`:s&&c?`─`:a&&o?`│`:o&&c?`┌`:o&&s?`┐`:a&&c?`└`:a&&s?`┘`:`┼`,z(n,r.x,r.y,l),n}function wi(e,t){return Ti(e,t)}function Ti(e,t){let n=Z(e),r=t.config.useAscii,i=0;for(let e=0;e<2;e++)i+=t.columnWidth.get(n.x+e)??0;let a=0;for(let e=0;e<2;e++)a+=t.rowHeight.get(n.y+e)??0;let o={x:0,y:0},s={x:i,y:a},c=F(Math.max(o.x,s.x),Math.max(o.y,s.y)),l=Sn(e.shape,r),u=e.shape===`state-end`,d=r?u?`=`:`-`:u?`═`:`─`,f=r?u?`‖`:`|`:u?`║`:`│`,p=u?r?{tl:`#`,tr:`#`,bl:`#`,br:`#`}:{tl:`╔`,tr:`╗`,bl:`╚`,br:`╝`}:l;for(let e=o.x+1;e<s.x;e++)z(c,e,o.y,d);for(let e=o.x+1;e<s.x;e++)z(c,e,s.y,d);for(let e=o.y+1;e<s.y;e++)z(c,o.x,e,f);for(let e=o.y+1;e<s.y;e++)z(c,s.x,e,f);if(z(c,o.x,o.y,p.tl),z(c,s.x,o.y,p.tr),z(c,o.x,s.y,p.bl),z(c,s.x,s.y,p.br),e.shape===`cylinder`&&s.y-o.y>=4)for(let e of[o.y+1,s.y-1]){for(let t=o.x+1;t<s.x;t++)z(c,t,e,d);z(c,o.x,e,f),z(c,s.x,e,f)}let m=e.displayLabel,h=Q(m),g=o.y+Math.floor(a/2)-Math.floor((h.length-1)/2);e.labelRows={top:g,bottom:g+h.length-1};for(let e=0;e<h.length;e++){let t=h[e],n=P(t),r=o.x+Math.floor(i/2)-Math.ceil(n/2)+1,a=ae(t);for(let t=0;t<a.length;t++)z(c,r+t,g+e,a[t])}return c}function Ei(e,t){return wi(e,t)}function Di(e){return/^[┌┐└┘├┤┬┴┼│─╭╮╰╯+\-|]$/.test(e)?`border`:`text`}function Oi(e,t=1){let n=Math.max(0,t),r=0;for(let t of e)for(let e of t)r=Math.max(r,P(e));let i=0;for(let t of e)i+=Math.max(t.length,1);return{width:r+2*n+2,height:i+(e.length-1)+2}}function ki(e,t,n=1){let r=Math.max(0,n),{width:i,height:a}=Oi(e,r),o=t?`-`:`─`,s=t?`|`:`│`,c=t?`+`:`┌`,l=t?`+`:`┐`,u=t?`+`:`└`,d=t?`+`:`┘`,f=t?`+`:`├`,p=t?`+`:`┤`,m=F(i-1,a-1);z(m,0,0,c);for(let e=1;e<i-1;e++)z(m,e,0,o);z(m,i-1,0,l),z(m,0,a-1,u);for(let e=1;e<i-1;e++)z(m,e,a-1,o);z(m,i-1,a-1,d);for(let e=1;e<a-1;e++)z(m,0,e,s),z(m,i-1,e,s);let h=1;for(let t=0;t<e.length;t++){let n=e[t],a=n.length>0?n:[``];for(let e of a){let t=1+r,n=ae(e);for(let e=0;e<n.length;e++)z(m,t+e,h,n[e]);h++}if(t<e.length-1){z(m,0,h,f);for(let e=1;e<i-1;e++)z(m,e,h,o);z(m,i-1,h,p),h++}}return m}function Ai(e){function t(e){return e.parent===null?0:1+t(e.parent)}let n=[...e];return n.sort((e,n)=>t(e)-t(n)),n}function ji(e,t,n,r){for(let i=0;i<t.length;i++)for(let a=0;a<(t[0]?.length??0);a++){let o=t[i]?.[a];if(o&&o!==` `){let t=i+n.x,o=a+n.y;t>=0&&o>=0&&L(e,t,o,r)}}}function Mi(e,t,n,r){for(let i of t)ji(e,i,n,r)}function Ni(e){let t=null;for(let[n,r]of e.entries())for(let[e,i]of r.entries())i!==` `&&(t=t?{x0:Math.min(t.x0,n),x1:Math.max(t.x1,n),y0:Math.min(t.y0,e),y1:Math.max(t.y1,e)}:{x0:n,x1:n,y0:e,y1:e});return t}function Pi(e,t,n){let r=e=>/^[┌┐└┘├┤┬┴┼│─╭╮╰╯+\-|.':]$/.test(e);for(let i=0;i<t.length;i++)for(let a=0;a<(t[0]?.length??0);a++){let o=t[i]?.[a];if(o&&o!==` `){let t=i+n.x,s=a+n.y;t>=0&&s>=0&&L(e,t,s,r(o)?`border`:`text`)}}}function Fi(e){let t=[];e.labelRects=t;let n=e.config.useAscii,r={x:0,y:0},i=Ai(e.subgraphs);for(let t of i){let r=Jt(t,e),i={x:t.minX,y:t.minY};e.canvas=me(e.canvas,i,n,r),ji(e.roleCanvas,r,i,`border`)}for(let r of e.nodes)if(!r.drawn&&r.drawingCoord&&r.drawing){if(e.canvas=me(e.canvas,r.drawingCoord,n,r.drawing),Pi(e.roleCanvas,r.drawing,r.drawingCoord),r.labelRows){let{x:n,y:i}=r.drawingCoord,a={x0:n+1,x1:n+r.drawing.length-2,y0:i+r.labelRows.top,y1:i+r.labelRows.bottom};t.push(a);for(let t=a.x0;t<=a.x1;t++)for(let n=a.y0;n<=a.y1;n++){let r=e.canvas[t]?.[n];r&&r!==` `&&L(e.roleCanvas,t,n,`text`)}}r.drawn=!0}let a=[],o=[],s=[],c=[],l=[],u=[],d=[],f=new Set;for(let t of e.edges)if(t.bundle&&t.pathToJunction){let n=t.bundle,[r,i,,,p,m]=gi(e,t,n);if(a.push(r),o.push(p),l.push(i),u.push(m),n.type===`fan-in`&&t.hasArrowStart){let n=Si(e,t);c.push(n)}if(!f.has(n)){f.add(n);let[t,r]=_i(e,n);if(a.push(t),o.push(r),n.type===`fan-in`){let t=vi(e,n);s.push(t)}if(n.type===`fan-out`&&n.edges.every(e=>e.hasArrowStart)){let t=xi(e,n);c.push(t)}let i=Ci(e,n);d.push(i)}if(n.type===`fan-out`&&t.hasArrowEnd){let n=yi(e,t);s.push(n)}}else{let[n,r,i,d,f,p]=Xr(e,t);a.push(n),o.push(f),s.push(i),c.push(d),l.push(r),u.push(p)}let p=pe(a);e.canvas=me(e.canvas,r,n,...p),Mi(e.roleCanvas,p,r,`line`),e.canvas=me(e.canvas,r,n,...o),Mi(e.roleCanvas,o,r,`corner`),e.canvas=me(e.canvas,r,n,...d),Mi(e.roleCanvas,d,r,`junction`),e.canvas=me(e.canvas,r,n,...s),Mi(e.roleCanvas,s,r,`arrow`),e.canvas=me(e.canvas,r,n,...l),Mi(e.roleCanvas,l,r,`junction`),e.canvas=me(e.canvas,r,n,...c),Mi(e.roleCanvas,c,r,`arrow`),e.canvas=me(e.canvas,r,n,...u),Mi(e.roleCanvas,u,r,`text`);for(let e of u){let n=Ni(e);n&&t.push(n)}for(let r of e.subgraphs){if(r.nodes.length===0)continue;let[i,a,o]=en(r,e,(t,n)=>Xt(e.canvas[t+r.minX]?.[n+r.minY])&&(e.canvas[t+r.minX]?.[n+r.minY+1]??` `)!==` `);e.canvas=me(e.canvas,a,n,i),ji(e.roleCanvas,i,a,`text`);let s=Ni(i);s&&t.push({x0:s.x0+a.x,x1:s.x1+a.x,y0:s.y0+a.y,y1:s.y1+a.y});for(let{x:t,y:n}of o){let r=i[t]?.[n];r===` `&&(z(e.canvas,t+a.x,n+a.y,r),L(e.roleCanvas,t+a.x,n+a.y,`text`))}}return e.canvas}function Ii(e,t,n,r){if(r(e))return e;let i=Math.max(e-t,n-e);for(let a=1;a<=i;a++){let i=e+a;if(i<=n&&r(i))return i;let o=e-a;if(o>=t&&r(o))return o}}function Li(e){let t=e.keys.length>0?` `+e.keys.join(`,`):``;return`${e.type} ${e.name}${t}`}function Ri(e){let t=Q(e.label),n=e.attributes.map(Li);return n.length===0?[t]:[t,n]}function zi(e,t,n=!1,r=!1){if(t)switch(e){case`one`:return r?`+`:`|`;case`zero-one`:return n?`o|`:`|o`;case`many`:return n?`<`:`>`;case`zero-many`:return n?`o<`:`>o`}else switch(e){case`one`:return r?`┼`:`│`;case`zero-one`:return n?`○│`:`│○`;case`many`:return n?`╟`:`╢`;case`zero-many`:return n?`○╟`:`╢○`}}function Bi(e){let t=new Set,n=[],r=new Map;for(let t of e.entities)r.set(t.id,new Set);for(let t of e.relationships)r.get(t.entity1)?.add(t.entity2),r.get(t.entity2)?.add(t.entity1);function i(e,n){let i=[e];for(;i.length>0;){let e=i.pop();if(!t.has(e)){t.add(e),n.add(e);for(let n of r.get(e)??[])t.has(n)||i.push(n)}}}for(let r of e.entities)if(!t.has(r.id)){let e=new Set;i(r.id,e),e.size>0&&n.push(e)}return n}function Vi(e,t,n){let r=e.get(t);if(r===void 0)throw Error(`ER diagram layout: missing ${n} for entity "${t}"`);return r}function Hi(n,r,i,a){let o=(0,e.splitStatements)(n),s=(0,t.parseErDiagram)(o);if(s.entities.length===0)return``;let c=r.useAscii,l=nt(r.paddingX,5,6,6),u=nt(r.paddingY,5,4,2),d=nt(r.paddingY,5,2,1),f=Math.max(...[`one`,`zero-one`,`many`,`zero-many`].flatMap(e=>[zi(e,c,!1).length,zi(e,c,!0).length])),p=new Map,m=new Map,h=new Map,g=new Map;for(let e of s.entities){g.set(e.id,e);let t=Ri(e);p.set(e.id,t);let{width:n,height:i}=Oi(t,r.boxBorderPadding);m.set(e.id,n),h.set(e.id,i)}let _=new Map;for(let e of s.relationships){if(!e.label)continue;let t=[e.entity1,e.entity2].sort().join(`|`),n=Kt(e.label);_.set(t,Math.max(_.get(t)??0,n))}let v=Bi(s),y=new Map,b=0;for(let e of v){let t=s.entities.filter(t=>e.has(t.id)),n=Math.max(2,Math.ceil(Math.sqrt(t.length))),r=0,i=0,a=0;for(let e=0;e<t.length;e++){let o=t[e],s=Vi(m,o.id,`box width`),c=Vi(h,o.id,`box height`);a>=n&&(b+=i+u,r=0,i=0,a=0),y.set(o.id,{entity:o,sections:Vi(p,o.id,`sections`),x:r,y:b,width:s,height:c});let d=l,f=a+1>=n,g=t[e+1];if(!f&&g){let e=[o.id,g.id].sort().join(`|`),t=_.get(e)??0;t>0&&(d=Math.max(d,t+2))}r+=s+d,i=Math.max(i,c),a++}b+=i+d}let x=0,S=0;for(let e of y.values())x=Math.max(x,e.x+e.width),S=Math.max(S,e.y+e.height);x+=4,S+=2;let C=F(x-1,S-1),w=oe(x-1,S-1);function T(e,t,n,r){z(C,e,t,n,{role:r,roleCanvas:w})}function E(e,t){let n=w[e]?.[t];return n===`text`||n===`border`}function D(e,t,n,r,i){for(let a=0;a<e.length;a++){let e=t+a;if(!(e<r||e>i)&&E(e,n))return!1}return!0}function ee(e,t,n,r,i,a){for(let a=0;a<e.length;a++){let e=t+a;if(!(e<r||e>i)&&(E(e,n)||w[e]?.[n]===`arrow`||O.has(`${e},${n}`)))return!1}if(!a)return!0;for(let a=t-2;a<=t+e.length;a++)if(!(a<r-2||a>i+1)&&w[a]?.[n]===`line`&&!fe.has(`${a},${n}`))return!1;return!0}for(let e of y.values()){let t=ki(e.sections,c,r.boxBorderPadding);for(let n=0;n<t.length;n++)for(let r=0;r<t[0].length;r++){let i=t[n][r];if(i!==` `){let t=e.x+n,a=e.y+r;t<x&&a<S&&T(t,a,i,Di(i))}}}let O=new Set;for(let e of y.values())for(let t=0;t<e.height;t++)for(let n=0;n<e.width;n++)O.add(`${e.x+n},${e.y+t}`);function k(e,t,n,r){O.has(`${e},${t}`)||(r===`text`||w[e]?.[t]!==`text`)&&(L(n)!==`dashed`||L(C[e]?.[t])!==`solid`)&&(T(e,t,n,r),r===`line`&&fe.add(`${e},${t}`))}function A(e,t,n,r){w[e-1]?.[t]!==`text`&&w[e+1]?.[t]!==`text`&&k(e,t,n,r)}function te(e,t,n,r){for(let i=n;i<=r;i++)for(let n=e;n<=t;n++){let e=C[n]?.[i];if(e!==void 0&&e!==` `)return!1}return!0}let j=[];function ne(e,t,n){for(let r of j)if(Math.abs(r.x-e)===1&&t<=r.yEnd&&n>=r.yStart)return!0;return!1}function re(e,t,n){for(let r of j)if(!(Math.abs(r.x-e)>1)&&t<=r.yEnd&&n>=r.yStart)return!0;return!1}function M(e,t,n){j.push({x:e,yStart:t,yEnd:n})}let N=c?`-`:`─`,ie=c?`|`:`│`,P=c?`.`:`╌`,I=c?`:`:`┊`;function L(e){if(e===N||e===ie)return`solid`;if(e===P||e===I)return`dashed`}function R(e,t){return c?`+`:e===`down`?t===`right`?`┌`:`┐`:t===`right`?`└`:`┘`}function B(e,t,n,r){k(e,t,R(n,r),`line`)}let le=new Map,ue=new Map;for(let e of s.relationships){let t=y.get(e.entity1),n=y.get(e.entity2);if(!t||!n)continue;let r=t.y+Math.floor(t.height/2),i=n.y+Math.floor(n.height/2);if(Math.abs(r-i)<Math.max(t.height,n.height))continue;let[a,o]=r<i?[t,n]:[n,t],s=a.x+Math.floor(a.width/2),c=o.x+Math.floor(o.width/2),l=ue.get(a.entity.id);l||(l=[],ue.set(a.entity.id,l)),l.push({rel:e,otherCenterX:c});let u=le.get(o.entity.id);u||(u=[],le.set(o.entity.id,u)),u.push({rel:e,otherCenterX:s})}function de(e,t,n){let r=n.get(e.entity.id),i=e.x+Math.floor(e.width/2);if(!r||r.length<=1)return i;let a=[...r].sort((e,t)=>e.otherCenterX-t.otherCenterX),o=a.findIndex(e=>e.rel===t);if(o===-1)return i;let s=Math.min(1,Math.floor((e.width-1)/2)),c=Math.max(0,e.width-1-s*2),l=a.length>1?c/(a.length-1):0;return e.x+s+Math.round(o*l)}let fe=new Set;for(let e of s.relationships){fe=new Set;let t=y.get(e.entity1),n=y.get(e.entity2);if(!t||!n)continue;let r=e.identifying?N:P,i=e.identifying?ie:I,a=t.x+Math.floor(t.width/2),o=t.y+Math.floor(t.height/2),s=n.x+Math.floor(n.width/2),l=n.y+Math.floor(n.height/2);if(Math.abs(o-l)<Math.max(t.height,n.height)){let[o,l]=a<s?[t,n]:[n,t],[d,p]=a<s?[e.cardinality1,e.cardinality2]:[e.cardinality2,e.cardinality1],m=o.x+o.width,h=l.x-1,g=o.y+Math.floor(o.height/2),_;for(let e of y.values())e!==o&&e!==l&&e.y===o.y&&e.x<h+1&&e.x+e.width>m&&(_=Math.max(_??0,e.y+e.height));let v=h-m+1,b=+(v>=f+1),x=zi(d,c,!1),S=zi(p,c,!0),T=+(v>=3&&x[0]===ie),E=+(v>=3&&S[S.length-1]===ie),ee=m+T,O=h-E,j;if(_===void 0){for(let e=m;e<=h;e++)A(e,g,r,`line`);if(j=g+1,e.label){let t=m+b,n=h-b,r=Q(e.label).length,i=g+1+Math.max(u-1,1);for(;j<i&&!te(t,n,j,j+r-1);)j++}}else{let t=Math.max(o.y+o.height,l.y+l.height,_),n=e.label?Q(e.label).length:0,a=t+1,s=t+Math.max(u*3,4);for(;a<s&&!te(m,h,a,a+n);)a++;ce(C,h+1,a+n+1),se(w,h+1,a+n+1);for(let e=g;e<=a;e++)k(m,e,i,`line`),k(h,e,i,`line`);for(let e=m;e<=h;e++)A(e,a,r,`line`);m!==h&&(B(m,a,`up`,`right`),B(h,a,`up`,`left`)),j=a+1}for(let e=0;e<x.length;e++)k(ee+e,g,x[e],`arrow`);for(let e=0;e<S.length;e++)k(O-S.length+1+e,g,S[e],`arrow`);if(e.label){let t=Q(e.label),n=Math.floor((m+h)/2),r=m+b,i=h-b;for(let e=0;e<t.length;e++){let a=t[e],o=ae(a),s=Math.max(r,n-Math.floor(o.length/2)),c=j+e;if(ce(C,Math.max(s+o.length,1),Math.max(c+1,1)),se(w,Math.max(s+o.length,1),Math.max(c+1,1)),D(o,s,c,r,i))for(let e=0;e<o.length;e++){let t=s+e;t>=r&&t<=i&&k(t,c,o[e],`text`)}}}}else{let[a,s]=o<l?[t,n]:[n,t],[u,d]=o<l?[e.cardinality1,e.cardinality2]:[e.cardinality2,e.cardinality1],f=a.y+a.height,p=s.y-1,m=de(a,e,ue),h=de(s,e,le);function pe(e,t,n){for(let r=t;r<=n;r++)if(O.has(`${e},${r}`))return!1;return!0}let g=f,_=p,v=Math.max(...[...y.values()].filter(e=>e.y===a.y).map(e=>e.y+e.height)),b=s.y,x=Math.max(f,v+1),S=Math.min(p,b-1);function me(e,t,n){let r=Math.floor(x<=S?(x+S)/2:(f+p)/2);if(x>S)return r;for(let r=x;r<=S;r++)if(te(e,t,r,r+n))return r;return r}let E=m!==h,D=Math.min(m,h),j=Math.max(m,h),N=E?me(D,j,0):p,ie=E?N-1:p,P=!pe(m,x,S)||!pe(h,x,S)||ne(m,f,E?N:p)||E&&ne(h,N,p),F=m;if(P){let e=C.length,t;for(let n=0;n<=e;n++){let e=m+n;if(pe(e,f,p)&&!re(e,f,p)){t=e;break}let r=m-n;if(n>0&&pe(r,f,p)&&!re(r,f,p)){t=r;break}}F=t??m;for(let e=f;e<=g;e++)k(m,e,i,`line`);for(let e=Math.min(m,F);e<=Math.max(m,F);e++)A(e,g,r,`line`);for(let e=g;e<=_;e++)k(F,e,i,`line`);for(let e=Math.min(F,h);e<=Math.max(F,h);e++)A(e,_,r,`line`);for(let e=_;e<=p;e++)k(h,e,i,`line`);if(m!==F){let e=F>m?`right`:`left`,t=e===`right`?`left`:`right`;B(m,g,`up`,e),B(F,g,`down`,t)}if(F!==h){let e=h>F?`right`:`left`,t=e===`right`?`left`:`right`;B(F,_,`up`,e),B(h,_,`down`,t)}M(F,g,_)}else{for(let e=f;e<=ie;e++)k(m,e,i,`line`);if(M(m,f,E?N:ie),E){for(let e=D;e<=j;e++)A(e,N,r,`line`);for(let e=N+1;e<=p;e++)k(h,e,i,`line`);M(h,N,p);let e=h>m?`right`:`left`,t=e===`right`?`left`:`right`;B(m,N,`up`,e),B(h,N,`down`,t),F=h}}let I=zi(u,c,!1,!0);for(let e=0;e<I.length;e++)k(m-Math.floor(I.length/2)+e,g,I[e],`arrow`);let oe=m===h?m:h,L=zi(d,c,!0,!0);for(let e=0;e<L.length;e++)k(oe-Math.floor(L.length/2)+e,_,L[e],`arrow`);if(e.label){let t=Q(e.label),n=Math.max(I.length-1-Math.floor(I.length/2),L.length-1-Math.floor(L.length/2)),r=F+n+2,i=t.map(e=>ae(e)),a=r+Math.max(...i.map(e=>e.length))-1,o=t.length,s=Math.floor((f+p)/2)-Math.floor((o-1)/2);a>=0&&(ce(C,a+1,p+1),se(w,a+1,p+1));let c=null;for(let e of[!0,!1]){for(let t=0;t<=p-f&&c===null;t++){let n=t===0?[s]:[s+t,s-t];for(let t of n)if(!(t<f||t+o-1>p)&&i.every((n,i)=>ee(n,r,t+i,0,a,e))){c=t;break}}if(c!==null)break}if(c!==null)for(let e=0;e<o;e++){let t=i[e],n=c+e;for(let e=0;e<t.length;e++){let i=r+e;i>=0&&k(i,n,t[e],`text`)}w[r-1]?.[n]===`line`&&T(r-1,n,` `,`line`);let a=r+t.length;w[a]?.[n]===`line`&&T(a,n,` `,`line`)}}}}return he(C,{roleCanvas:w,colorMode:i,theme:a})}var Ui=4,Wi=[`O`,`/|\\`,`/ \\`],Gi=3,Ki=4;function qi(e){return e.seqNumber===void 0?Ui:Ui+String(e.seqNumber).length}function Ji(n,r,i,a){let o=(0,e.splitStatements)(n),s=(0,t.parseSequenceDiagram)(o);if(s.actors.length===0)return``;let c=r.useAscii,l=c?`-`:`─`,u=c?`|`:`│`,d=c?`+`:`┌`,f=c?`+`:`┐`,p=c?`+`:`└`,m=c?`+`:`┘`,h=c?`+`:`┬`,g=c?`+`:`┴`,_=c?`+`:`├`,v=c?`+`:`┤`,y=c?`‖`:`║`,b=new Map;s.actors.forEach((e,t)=>b.set(e.id,t));function x(e){let t=b.get(e);if(t===void 0)throw Error(`Sequence diagram: unknown actor "${e}"`);return t}function S(e){let t=Kt(e.label?`${e.type} [${e.label}]`:e.type);for(let n of e.dividers)n.label&&(t=Math.max(t,Kt(`[${n.label}]`)));return t}let C=Math.max(0,r.boxBorderPadding),w=e=>Math.max(Kt(e.label),e.type===`actor`?Gi:0),T=e=>qt(e.label)+(e.type===`actor`?Wi.length:0),E=s.actors.map(e=>w(e)+2*C+2),D=E.map(e=>Math.ceil(e/2)),ee=s.actors.map(e=>T(e)+2),O=Math.max(...ee,3),k=(e,t)=>t-Math.floor(E[e]/2),A=Math.floor((O-1)/2),te=O-1-A,j=new Map,ne=new Map;s.actors.forEach((e,t)=>{e.createdAt!==void 0&&j.set(e.createdAt,t),e.destroyedAt!==void 0&&ne.set(e.destroyedAt,t)});let re=new Map,M=new Map,N=s.boxes.filter(e=>e.actorIds.length>0).map(e=>{let t=e.actorIds.map(x);return{label:e.label,lo:Math.min(...t),hi:Math.max(...t)}}),ie=N.length>0,I=s.actors.map((e,t)=>N.findIndex(e=>e.lo<=t&&t<=e.hi)),L=+!!ie,B=e=>e===``?0:Kt(e)+6,le=Array(Math.max(s.actors.length-1,0)).fill(0);for(let e of s.messages){let t=x(e.from),n=x(e.to);if(t===n)continue;let r=Math.min(t,n),i=Math.max(t,n),a=Kt(e.label)+4,o=i-r,s=Math.ceil(a/o);for(let e=r;e<i;e++)le[e]=Math.max(le[e],s)}function ue(e){let t=Q(e.text);return Math.max(...t.map(e=>P(e)))+2+2*C}let de=Array(s.actors.length).fill(0),fe=Array(s.actors.length).fill(0),pe=Array(s.actors.length).fill(0),me=Array(s.actors.length).fill(0);for(let e of s.notes)if(e.position===`left`||e.position===`right`){let t=x(e.actorIds[0]),n=ue(e);e.position===`left`?de[t]=Math.max(de[t],n):fe[t]=Math.max(fe[t],n)}else if(e.position===`over`&&e.actorIds.length===1){let t=x(e.actorIds[0]),n=ue(e),r=Math.floor(n/2),i=n-r;pe[t]=Math.max(pe[t],r),me[t]=Math.max(me[t],i)}let V=nt(r.paddingX,5,10,4),H=[Math.max(D[0]+(I[0]===-1?0:2),de[0]+1,pe[0])];for(let e=1;e<s.actors.length;e++){let t=I[e-1],n=I[e],r=t===n?0:(t===-1?0:1)+(n===-1?0:1),i=r===0?0:r*2-1,a=Math.max(fe[e-1],de[e],me[e-1],pe[e])+2,o=Math.max(D[e-1]+D[e]+2+i,le[e-1]+2,a,V);H[e]=H[e-1]+o}let ge=e=>k(e.lo,H[e.lo])-1-1,_e=e=>Math.max(k(e.hi,H[e.hi])+E[e.hi]-1+1+1,ge(e)+B(e.label)-1);for(let e of N){if(e.hi+1>=s.actors.length)continue;let t=k(e.hi+1,H[e.hi+1]),n=I[e.hi+1]===-1?0:2,r=_e(e)+1+1+n-t;if(r>0){let t=H.slice(e.hi+1).map(e=>e+r);H.splice(e.hi+1,t.length,...t)}}for(let e of s.blocks){let t=-1,n=-1,r=1/0,i=-1;for(let a=e.startIndex;a<=e.endIndex&&!(a>=s.messages.length);a++){let e=s.messages[a],o=x(e.from),c=x(e.to);if(t=t===-1?Math.min(o,c):Math.min(t,o,c),n=Math.max(n,o,c),r=Math.min(r,H[Math.min(o,c)]),i=Math.max(i,H[Math.max(o,c)]),o===c){let t=H[o]+Ui+2+Kt(e.label);i=Math.max(i,t)}}if(n===-1||n+1>=s.actors.length)continue;let a=Math.max(0,r-Ki),o=i+Ki,c=a+1+S(e),l=Math.max(o,c),u=H[n+1],d=l+2-u;if(d>0){let e=H.slice(n+1).map(e=>e+d);H.splice(n+1,e.length,...e)}}let ve=[],ye=[],be=new Map,xe=new Map,Se=new Map,Ce=[],we=L,Te=we+O,U=Te+L,Ee=nt(r.paddingY,5,1,0),De=Math.max(Ee,1);for(let e of s.notes){if(e.afterIndex!==-1)continue;U+=Ee;let t=Q(e.text),n=ue(e),r=t.length+2,i=b.get(e.actorIds[0])??0,a;if(e.position===`left`)a=H[i]-n-1;else if(e.position===`right`)a=H[i]+2;else if(e.actorIds.length>=2){let t=b.get(e.actorIds[1])??i;a=Math.floor((H[i]+H[t])/2)-Math.floor(n/2)}else a=H[i]-Math.floor(n/2);a=Math.max(0,a),Ce.push({x:a,y:U,width:n,height:r,lines:t}),U+=r}for(let e=0;e<s.messages.length;e++){for(let t=0;t<s.blocks.length;t++)s.blocks[t].startIndex===e&&(U+=Ee+1,be.set(t,U-1));for(let t=0;t<s.blocks.length;t++)for(let n=0;n<s.blocks[t].dividers.length;n++)s.blocks[t].dividers[n].index===e&&(U+=Ee,Se.set(`${t}:${n}`,U),U+=De);U+=Ee;let t=s.messages[e],n=t.from===t.to,r=qt(t.label),i=n?void 0:j.get(e),a=ne.get(e);if(n)ye[e]=U+1,ve[e]=U,U+=2+r;else{let t=i===void 0?0:A,n=i===void 0?0:te;ye[e]=U,ve[e]=U+r+t,U+=r+t+1+n,i!==void 0&&re.set(i,ve[e]-t)}a!==void 0&&(M.set(a,U),U+=1);for(let t=0;t<s.notes.length;t++)if(s.notes[t].afterIndex===e){U+=Ee;let e=s.notes[t],n=Q(e.text),r=ue(e),i=n.length+2,a=b.get(e.actorIds[0])??0,o;if(e.position===`left`)o=H[a]-r-1;else if(e.position===`right`)o=H[a]+2;else if(e.actorIds.length>=2){let t=b.get(e.actorIds[1])??a;o=Math.floor((H[a]+H[t])/2)-Math.floor(r/2)}else o=H[a]-Math.floor(r/2);o=Math.max(0,o),Ce.push({x:o,y:U,width:r,height:i,lines:n}),U+=i}for(let t=0;t<s.blocks.length;t++)s.blocks[t].endIndex===e&&(U+=Ee,xe.set(t,U),U+=De)}U+=De;let Oe=U,ke=Oe+L,Ae=ke+O+L,je=(H[H.length-1]??0)+(D[D.length-1]??0)+2;for(let e of N)je=Math.max(je,_e(e)+2);for(let e=0;e<s.messages.length;e++){let t=s.messages[e];if(t.from===t.to){let e=H[x(t.from)]+qi(t)+2+2+Kt(t.label);je=Math.max(je,e+1)}}for(let e of Ce)je=Math.max(je,e.x+e.width+1);let Me=F(je,Ae-1),Ne=oe(je,Ae-1);function W(e,t,n,r){z(Me,e,t,n,{role:r,roleCanvas:Ne})}function Pe(e,t,n,r,i){let a=ae(n);for(let n=0;n<a.length;n++){let o=e+n;if(i!==void 0&&o>=i||a[n+1]===``&&i!==void 0&&o+1>=i)break;W(o,t,a[n],r)}}function Fe(e,t,n,r,i){let a=r===`actor`?Wi:[],o=[...a,...Q(n)],s=Math.max(Kt(n),a.length>0?Gi:0),c=s+2*C+2,h=i,g=h-2,_=e-Math.floor(c/2);W(_,t,d,`border`);for(let e=1;e<c-1;e++)W(_+e,t,l,`border`);W(_+c-1,t,f,`border`);for(let e=0;e<g;e++){let n=t+1+e;W(_,n,u,`border`),W(_+c-1,n,u,`border`);let r=o[e];r!==void 0&&Pe(_+1+C+Math.floor((s-P(r))/2),n,r,`text`)}let v=t+h-1;W(_,v,p,`border`);for(let e=1;e<c-1;e++)W(_+e,v,l,`border`);W(_+c-1,v,m,`border`)}let Ie=e=>{let t=re.get(e);return t===void 0?Te:t+O},Le=e=>M.get(e)??Oe,Re=new Map,ze=new Map;function G(e,t){let n=ze.get(e)??[];ze.set(e,n),n.push(t)}function Be(e,t){let n=ze.get(e)?.pop();if(n===void 0)return;let r=Re.get(e)??[];Re.set(e,r),r.push({topRow:n,bottomRow:t})}let Ve=new Map;for(let e of s.activations){let t=Ve.get(e.afterIndex)??[];t.push(e),Ve.set(e.afterIndex,t)}function He(e,t){for(let n of Ve.get(e)??[])n.kind===`start`?G(n.actorId,t):Be(n.actorId,t)}He(-1,Te);for(let e=0;e<s.messages.length;e++){let t=s.messages[e],n=ve[e];t.activate&&G(t.to,n),t.deactivate&&Be(t.from,n),He(e,n)}for(let[e,t]of ze){if(t.length===0)continue;let n=Le(x(e)),r=Re.get(e)??[];Re.set(e,r);for(let e of t)r.push({topRow:e,bottomRow:n})}function Ue(e,t){let n=Re.get(e);return n?n.some(e=>t>=e.topRow&&t<=e.bottomRow):!1}for(let e=0;e<s.actors.length;e++){let t=H[e],n=s.actors[e].id;for(let r=Ie(e);r<=Le(e);r++)W(t,r,Ue(n,r)?y:u,`line`)}let We=c?`+`:`┼`;function Ge(e,t,n){let r=ge(e),i=_e(e),a=t+O+1;W(r,t,d,`border`),W(i,t,f,`border`),W(r,a,p,`border`),W(i,a,m,`border`);for(let e=r+1;e<i;e++)W(e,t,l,`border`),W(e,a,l,`border`);for(let e=t+1;e<a;e++)W(r,e,u,`border`),W(i,e,u,`border`);n!==``&&Pe(r+2,t,` ${Q(n)[0]} `,`text`);for(let n=e.lo;n<=e.hi;n++){let e=H[n];Ie(n)<=t&&t<=Le(n)&&W(e,t,We,`junction`),Ie(n)<=a&&a<=Le(n)&&W(e,a,We,`junction`)}}for(let e of N)Ge(e,0,e.label),Ge(e,Oe,``);for(let e=0;e<s.actors.length;e++){let t=s.actors[e],n=re.get(e)??we,r=M.has(e);Fe(H[e],n,t.label,t.type,O),r||Fe(H[e],ke,t.label,t.type,O),c||(W(H[e],n+O-1,h,`junction`),r||W(H[e],ke,g,`junction`))}for(let e=0;e<s.messages.length;e++){let t=s.messages[e],n=x(t.from),r=x(t.to),i=H[n],a=H[r],o=n===r,d=t.lineStyle===`dashed`,f=t.arrowHead===`filled`,p=t.isLost===!0,m=c?`x`:`✕`,h=d?c?`.`:`╌`:l;if(o){let n=ve[e],r=qi(t),a=t.seqNumber===void 0?void 0:String(t.seqNumber),o=Q(t.label);W(i,n,_,`junction`);let s=i+1;if(a!==void 0){for(let e=0;e<a.length;e++)W(i+1+e,n,a[e],`text`);s=i+1+a.length}for(let e=s;e<i+r;e++)W(e,n,h,`line`);W(i+r,n,c?`+`:`┐`,`corner`);let l=i+r+2;for(let e=0;e<o.length;e++){let t=n+1+e;W(i+r,t,u,`line`),Pe(l,t,o[e],`text`,je)}let d=n+1+o.length;W(i,d,p?m:f?c?`<`:`◀`:c?`<`:`◁`,`arrow`);for(let e=i+1;e<i+r;e++)W(e,d,h,`line`);W(i+r,d,c?`+`:`┘`,`corner`)}else{let n=ye[e],o=ve[e],s=i<a,l=j.get(e)===r,u=a;if(l){let e=k(r,a),t=e+E[r]-1;u=s?e-1:t+1}let d=Math.floor((i+a)/2),g=Q(t.label);for(let e=0;e<g.length;e++){let t=g[e];Pe(d-Math.floor(P(t)/2),n+e,t,`text`,je)}if(s){for(let e=i+1;e<u;e++)W(e,o,h,`line`);W(u,o,p?m:f?c?`>`:`▶`:c?`>`:`▷`,`arrow`),t.bidirectional&&W(i,o,c?`<`:`◀`,`arrow`)}else{for(let e=u+1;e<i;e++)W(e,o,h,`line`);W(u,o,p?m:f?c?`<`:`◀`:c?`<`:`◁`,`arrow`),t.bidirectional&&W(i,o,c?`>`:`▶`,`arrow`)}if(t.seqNumber!==void 0){let e=String(t.seqNumber);if(s){let t=i+1;if(t+e.length<a)for(let n=0;n<e.length;n++)W(t+n,o,e[n],`text`)}else{let t=i-e.length;if(t>u)for(let n=0;n<e.length;n++)W(t+n,o,e[n],`text`)}}}}for(let[e,t]of M)W(H[e],t,c?`x`:`✕`,`arrow`);let Ke=je-1;for(let e=0;e<s.blocks.length;e++){let t=s.blocks[e],n=be.get(e),r=xe.get(e);if(n===void 0||r===void 0)continue;let i=je,a=0;for(let e=t.startIndex;e<=t.endIndex&&!(e>=s.messages.length);e++){let t=s.messages[e],n=b.get(t.from)??0,r=b.get(t.to)??0;if(i=Math.min(i,H[Math.min(n,r)]),a=Math.max(a,H[Math.max(n,r)]),n===r){let e=H[n]+qi(t)+2+Kt(t.label);a=Math.max(a,e)}}let o=i-Ki,c=a+Ki,h=1/0,g=-1/0;for(let e of H)e>a&&e<h&&(h=e),e<i&&e>g&&(g=e);c>=h&&(c=Math.max(a,h-Ki)),o<=g&&(o=Math.min(i,g+Ki)),o=Math.max(0,o),c>Ke&&(ce(Me,c+1,Ae-1),se(Ne,c+1,Ae-1),Ke=c);let y=t.label?`${t.type} [${t.label}]`:t.type,x=o+1+S(t);if(x>c){c=x;let[e]=R(Me);c>e&&(ce(Me,c,Ae-1),se(Ne,c,Ae-1)),je=Math.max(je,c+1),Ke=Math.max(Ke,c)}W(o,n,d,`border`);for(let e=o+1;e<c;e++)W(e,n,l,`border`);W(c,n,f,`border`);let C=Q(y);for(let e=0;e<C.length&&n+e<r;e++)Pe(o+1,n+e,C[e],`text`,c);W(o,r,p,`border`);for(let e=o+1;e<c;e++)W(e,r,l,`border`);W(c,r,m,`border`);for(let e=n+1;e<r;e++)W(o,e,u,`border`),W(c,e,u,`border`);for(let n=0;n<t.dividers.length;n++){let r=Se.get(`${e}:${n}`);if(r===void 0)continue;let i=qe();W(o,r,_,`junction`);for(let e=o+1;e<c;e++)W(e,r,i,`line`);W(c,r,v,`junction`);let a=t.dividers[n].label;if(a){let e=`[${a}]`;Pe(o+1,r,e,`text`,c)}}}for(let e of Ce){ce(Me,e.x+e.width,e.y+e.height),se(Ne,e.x+e.width,e.y+e.height),W(e.x,e.y,d,`border`);for(let t=1;t<e.width-1;t++)W(e.x+t,e.y,l,`border`);W(e.x+e.width-1,e.y,f,`border`);for(let t=0;t<e.lines.length;t++){let n=e.y+1+t;W(e.x,n,u,`border`),W(e.x+e.width-1,n,u,`border`);for(let t=e.x+1;t<e.x+e.width-1;t++)W(t,n,` `,`text`);Pe(e.x+1+C,n,e.lines[t],`text`)}let t=e.y+e.height-1;W(e.x,t,p,`border`);for(let n=1;n<e.width-1;n++)W(e.x+n,t,l,`border`);W(e.x+e.width-1,t,m,`border`)}return he(Me,{roleCanvas:Ne,colorMode:i,theme:a});function qe(){return c?`-`:`╌`}}function Yi(e,t){return e.rowStart<=t.rowEnd&&t.rowStart<=e.rowEnd}function Xi(e,t){let n=e.map(e=>({item:e,geom:t(e)})).sort((e,t)=>e.geom.idealMid-t.geom.idealMid),r=(e,t)=>{let r=n[e];if(r)for(let i=e+t;i>=0&&i<n.length;i+=t){let e=n[i];if(e&&Yi(e.geom,r.geom))return e.geom}},i=new Map;for(let[e,t]of n.entries()){let n=t.geom,a=r(e,-1),o=r(e,1),s=a&&a.end>=n.start?Math.floor((a.idealMid+n.idealMid)/2)+1:-1/0,c=o&&n.end>=o.start?Math.floor((n.idealMid+o.idealMid)/2):1/0;i.set(t.item,{left:s,right:c})}return i}function Zi(e){let n=[];e.annotation&&n.push(`<<${e.annotation}>>`);let r=Q(e.label);n.push(...r);let i=e.attributes.map(t.formatClassMember),a=e.methods.map(t.formatClassMember),o=[n];return i.length>0&&o.push(i),a.length>0&&o.push(a),o}function Qi(e,t){return{type:e,markerAt:t,dashed:e===`dependency`||e===`realization`}}function $i(e,t,n){switch(e){case`inheritance`:case`realization`:return n===`down`?t?`^`:`△`:n===`up`?t?`v`:`▽`:n===`left`?t?`>`:`◁`:t?`<`:`▷`;case`composition`:return t?`*`:`◆`;case`aggregation`:return t?`o`:`◇`;case`association`:case`dependency`:return n===`down`?t?`v`:`▼`:n===`up`?t?`^`:`▲`:n===`left`?t?`<`:`◀`:t?`>`:`▶`}}function ea(e,t,n,r,i){let a=e+t.length-1,o=r>e,s=i<a;if(!o&&!s)return{start:e,cells:t};let c=o?r:e,l=Math.max(0,(s?i:a)-c+1),u=+!!o+ +!!s;if(l===0)return{start:c,cells:[]};if(l<u)return{start:c,cells:[`…`]};let d=Math.max(0,l-u),f=ae(n),p=f.length<=d?f:o&&!s?f.slice(f.length-d):f.slice(0,d),m=[...o?[`…`]:[],...p,...s?[`…`]:[]];return{start:c+Math.max(0,Math.floor((l-m.length)/2)),cells:m}}function ta(e){return Math.max(...Q(e).map(e=>P(e)))+2}var na=2,ra=2,ia=2,aa=4,oa=3;function sa(e,t){let n=new Set(e.classes.map(e=>e.id)),r=new Set,i=[];for(let a of e.namespaces){let e=[];for(let i of a.classIds)if(!(!n.has(i)||r.has(i))){r.add(i),e.push(i);for(let[n,r]of t)r.forClass===i&&e.push(n)}e.length>0&&i.push({name:a.name,ids:e})}return i}function ca(e,t){let n=[],r=new Set;for(let i of e){let a=t.get(i);a===void 0?n.push(i):r.has(a)||(r.add(a),n.push(...e.filter(e=>t.get(e)===a)))}return n}function la(n,r,i,a,o={}){let c=(0,e.splitStatements)(n),{diagram:l,notesById:u}=ua((0,t.parseClassDiagram)(c));if(l.classes.length===0)return``;let f=sa(l,u),p=new Map;for(let[e,t]of f.entries())for(let n of t.ids)p.set(n,e);let m=r.useAscii,h=nt(r.paddingX,5,4,1)+(f.length>0?aa:0),g=nt(r.paddingY,5,3,1)+(f.length>0?oa:0),_=new Map,v=new Map,y=new Map;for(let e of l.classes){let t=Zi(e);_.set(e.id,t);let{width:n,height:i}=Oi(t,r.boxBorderPadding);v.set(e.id,n),y.set(e.id,i)}let b=new Map;for(let e of l.classes)b.set(e.id,e);let x=new Map,S=new Map;for(let e of l.relationships){let t=e.from,n=e.to,r=x.get(n)??new Set;x.set(n,r),r.add(t);let i=S.get(t)??new Set;S.set(t,i),i.add(n)}let C=new Map,w=l.classes.filter(e=>!x.has(e.id)||x.get(e.id).size===0).map(e=>e.id);for(let e of w)C.set(e,0);let T=l.classes.length-1,E=0;for(;E<w.length;){let e=w[E++],t=S.get(e);if(t)for(let n of t){let t=(C.get(e)??0)+1;t>T||(!C.has(n)||C.get(n)<t)&&(C.set(n,t),w.push(n))}}for(let e of l.classes)C.has(e.id)||C.set(e.id,0);da(u,C);let D=Math.max(...C.values(),0),ee=Array.from({length:D+1},()=>[]);for(let e of l.classes)ee[C.get(e.id)].push(e.id);if(f.length>0)for(let[e,t]of ee.entries())ee[e]=ca(t,p);let O=new Map,k=new Map;{let e=new Map;l.relationships.forEach((t,n)=>{let r=[t.from,t.to].sort().join(`::`),i=e.get(r)??[];i.push(n),e.set(r,i)});for(let t of e.values()){if(t.length<2)continue;let e=t.length,n=Math.max(...t.map(e=>{let t=l.relationships[e].label;return t?ta(t):3}))+1;t.forEach((t,r)=>{O.set(t,Math.round((r-(e-1)/2)*n)),k.set(t,(e-1)*n)})}}let A=new Map,te=new Map,j=new Map,ne=new Map;{let e=(e,t,n)=>{let r=new Map;l.relationships.forEach((t,n)=>{if(O.has(n)||!b.has(t.from)||!b.has(t.to))return;let i=C.get(t.from)??0;if((C.get(t.to)??0)-i!==1)return;let a=e(t),o=r.get(a)??[];o.push(n),r.set(a,o)});for(let e of r.values()){if(e.length<2)continue;let r=e.length,i=Math.max(...e.map(e=>{let t=l.relationships[e].label;return t?ta(t):3}))+1;e.forEach((e,a)=>{t.set(e,Math.round((a-(r-1)/2)*i)),n.set(e,(r-1)*i)})}};e(e=>e.to,j,ne),e(e=>e.from,A,te)}let re=new Map;for(let e of l.classes)re.set(e.id,{left:0,right:0});l.relationships.forEach((e,t)=>{if(!b.has(e.from)||!b.has(e.to))return;let n=e.label?ta(e.label):1,r=O.get(t)??A.get(t)??0,i=O.get(t)??j.get(t)??0,a=re.get(e.from);a.left=Math.max(a.left,Math.floor(n/2)-r),a.right=Math.max(a.right,r+n-1-Math.floor(n/2));let o=re.get(e.to);o.left=Math.max(o.left,Math.floor(n/2)-i),o.right=Math.max(o.right,i+n-1-Math.floor(n/2))});let M=new Map,N=0,ie=0;for(let e=0;e<=D;e++){let t=ee[e];if(t.length===0)continue;let n=e=>{let t=x.get(e);return t&&t.size>0?[...t].sort().join(`,`):`root:${e}`},r=new Map;for(let e of t){let t=n(e),i=r.get(t);i||(i=[],r.set(t,i)),i.push(e)}let i=new Map,a=new Map,o=new Map;{let e=0;for(let n of t){let t=v.get(n),r=re.get(n),s=Math.max(0,r.left-Math.floor(t/2)),c=Math.max(0,r.right-(t-1-Math.floor(t/2)));i.set(n,e),a.set(n,s+t+c),o.set(n,s),e+=s+t+c+h}}let s=0,c=0,l=(e,t)=>{let n=b.get(e),r=v.get(e),i=y.get(e),l=o.get(e),u=a.get(e);M.set(e,{cls:n,sections:_.get(e),x:t+l,y:N,width:r,height:i}),ie=Math.max(ie,t+u),s=t+u+h,c=Math.max(c,i)},u=new Set;for(let c of t){if(u.has(c))continue;let t=n(c),d=x.get(c),f=d?[...d].filter(t=>C.get(t)<e):[];if(f.length===0){l(c,Math.max(i.get(c),s,0)),u.add(c);continue}let p=r.get(t),m=f.map(e=>{let t=M.get(e);return t.x+Math.floor(t.width/2)}),g=Math.round(m.reduce((e,t)=>e+t,0)/m.length),_=0;for(let e=0;e<p.length;e++)_+=a.get(p[e]),e<p.length-1&&(_+=h);let y=o.get(p[0]),b=p[p.length-1],S=a.get(b)-o.get(b)-v.get(b),w=_-y-S,T=g-y-Math.floor(w/2),E=Math.max(T,s,0);for(let e of p)l(e,E),u.add(e),E=s}N+=c+g}let I=[];if(f.length>0){for(let e of M.values())e.x+=na,e.y+=ra;ie+=na;for(let e of f){let t=1/0,n=1/0,r=-1/0,i=-1/0;for(let a of e.ids){let e=M.get(a);e&&(t=Math.min(t,e.x),n=Math.min(n,e.y),r=Math.max(r,e.x+e.width-1),i=Math.max(i,e.y+e.height-1))}if(t===1/0)continue;let a=t-na,o=Math.max(r+na,a+P(e.name)+5);I.push({name:e.name,x0:a,y0:n-ra,x1:o,y1:i+ia})}}let L=ie;for(let e of I)L=Math.max(L,e.x1+1);let R=0;for(let e of M.values())L=Math.max(L,e.x+e.width),R=Math.max(R,e.y+e.height);for(let e of I)R=Math.max(R,e.y1+1);L+=4,R+=2;let B=F(L-1,R-1),ue=oe(L-1,R-1);function de(e,t,n,r){z(B,e,t,n,{role:r,roleCanvas:ue})}let pe=o.hyperlinks?s(L-1,R-1):void 0;for(let t of M.values()){let n=ki(t.sections,m,r.boxBorderPadding);for(let e=0;e<n.length;e++)for(let r=0;r<n[0].length;r++){let i=n[e][r];if(i!==` `){let n=t.x+e,a=t.y+r;n<L&&a<R&&de(n,a,i,Di(i))}}if(pe){let r=(0,e.safeHref)(l.interactions.get(t.cls.id)?.href),i=t.sections[0];if(r!==void 0&&i!==void 0){let e=1+ +!!t.cls.annotation;d(pe,n,{x:t.x,y:t.y},r,{from:e,to:i.length})}}}let me=new Set;for(let e of M.values())for(let t=0;t<e.height;t++)for(let n=0;n<e.width;n++)me.add(`${e.x+n},${e.y+t}`);function V(e,t,n,r){me.has(`${e},${t}`)||de(e,t,n,r)}function H(e,t,n){for(let[r,i]of M.entries())if(!n?.has(r)&&e>=i.x&&e<=i.x+i.width-1&&t>=i.y&&t<=i.y+i.height-1)return!0;return!1}function ge(e,t,n,r){return Ii(e,0,e+L+9,e=>{for(let i=Math.min(t,n);i<=Math.max(t,n);i++)if(H(e,i,r))return!1;return!0})??L+2}let _e=m?`-`:`─`,ve=m?`|`:`│`,ye=m?`.`:`╌`,be=m?`:`:`┊`;function xe(e,t,n){let r=Math.floor(n/2),i=n-1-r,a=t>r+i?(r+i)/t:1;return Math.max(-r,Math.min(i,Math.round(e*a)))}function Se(e,t,n){let r=O.get(e)??A.get(e)??0,i=O.get(e)??j.get(e)??0,a=k.get(e)??te.get(e)??0,o=k.get(e)??ne.get(e)??0,s=t.x+Math.floor(t.width/2),c=n.x+Math.floor(n.width/2);return{fromCX:s+r,toCX:c+i,fromAnchorX:s+xe(r,a,t.width),toAnchorX:c+xe(i,o,n.width)}}function Ce(e,t,n,r){if(me.has(`${e},${t}`)||ue[e]?.[t]===`arrow`)return;let i=B[e]?.[t];if(!m&&i!==void 0&&le(i)&&le(n)){let a=fe(i,n);de(e,t,a,a===n?r:`junction`);return}de(e,t,n,r)}function we(e,t,n,r,i){if(t===n)return;let a=Math.min(t,n),o=Math.max(t,n);for(let t=a+1;t<o;t++)Ce(t,e,r,`line`);if(m){Ce(t,e,r,`line`),Ce(n,e,r,`line`);return}let s=n>t,c=i?s?`└`:`┘`:s?`┌`:`┐`,l=i?s?`┐`:`┌`:s?`┘`:`└`;Ce(t,e,c,`corner`),Ce(n,e,l,`corner`)}let Te=new Map,U=new Map;l.relationships.forEach((e,t)=>{let n=M.get(e.from),r=M.get(e.to);if(!n||!r)return;let i=Qi(e.type,e.markerAt),a=i.dashed?ye:_e,o=i.dashed?be:ve,s=new Set([e.from,e.to]),{fromCX:c,toCX:l,fromAnchorX:u,toAnchorX:d}=Se(t,n,r),f=n.y+n.height-1,p=r.y;if(f<p){let e=ge(c,f+1,p-1,s),n=e!==c;if(e>=L&&ce(B,e+2,R),n){let n=f+1,r=p-1;Te.set(t,{routeX:e,exitY:n,entryY:r,fromAnchorX:u,toAnchorX:d,clearSide:e>c?`right`:`left`});let s=Math.min(u,e),l=Math.max(u,e);for(let e=s;e<=l;e++)V(e,n,a,`line`);!m&&n<(B[0]?.length??0)&&(u<e?(V(u,n,`└`,`corner`),V(e,n,`┐`,`corner`)):(V(u,n,`┘`,`corner`),V(e,n,`┌`,`corner`)));for(let t=n+1;t<=r;t++)V(e,t,o,`line`);if(e!==d){let t=Math.min(e,d),n=Math.max(e,d);for(let e=t;e<=n;e++)V(e,r,a,`line`);!m&&r<(B[0]?.length??0)&&(e<d?(V(e,r,`└`,`corner`),V(d,r,`┐`,`corner`)):(V(e,r,`┘`,`corner`),V(d,r,`┌`,`corner`)))}if(i.markerAt===`to`){let e=i.type===`inheritance`||i.type===`realization`;V(d,r,$i(i.type,m,e?`up`:`down`),`arrow`)}if(i.markerAt===`from`){let e=$i(i.type,m,`down`);V(u,f+1,e,`arrow`)}}else{let e=f+Math.floor((p-f)/2),t=c!==u,n=l!==d;we(f+1,u,c,a,!0);for(let n=f+(t?2:1);n<=e;n++)V(c,n,o,`line`);if(c!==l&&e<(B[0]?.length??0)){let t=Math.min(c,l),n=Math.max(c,l);for(let r=t;r<=n;r++)V(r,e,a,`line`);m||(V(c,e,c<l?`└`:`┘`,`corner`),V(l,e,c<l?`┐`:`┌`,`corner`))}for(let t=e+1;t<p-+!!n;t++)V(l,t,o,`line`);if(we(p-1,d,l,a,!1),i.markerAt===`to`){let e=i.type===`inheritance`||i.type===`realization`;V(d,p-1,$i(i.type,m,e?`up`:`down`),`arrow`)}i.markerAt===`from`&&V(u,f+1,$i(i.type,m,`down`),`arrow`)}}else if(r.y+r.height-1<n.y){let e=n.y,t=r.y+r.height-1,s=t+Math.floor((e-t)/2),f=c!==u,p=l!==d;we(e-1,u,c,a,!1);for(let t=e-(f?2:1);t>=s;t--)V(c,t,o,`line`);if(c!==l){let e=Math.min(c,l),t=Math.max(c,l);for(let n=e;n<=t;n++)V(n,s,a,`line`);!m&&s>=0&&s<R&&(V(c,s,c<l?`┌`:`┐`,`corner`),V(l,s,c<l?`┘`:`└`,`corner`))}for(let e=s-1;e>t+ +!!p;e--)V(l,e,o,`line`);if(we(t+1,d,l,a,!0),i.markerAt===`from`){let t=$i(i.type,m,`up`),n=e-1;for(let e=0;e<t.length;e++)V(u-Math.floor(t.length/2)+e,n,t[e],`arrow`)}if(i.markerAt===`to`){let e=i.type===`inheritance`||i.type===`realization`?`down`:`up`,n=$i(i.type,m,e),r=t+1;for(let e=0;e<n.length;e++)V(d-Math.floor(n.length/2)+e,r,n[e],`arrow`)}}else{let s=r.y+r.height-1,p=Math.min(c,l),h=Math.max(c,l),g=Math.max(f,s);for(let[t,r]of M.entries())t!==e.from&&t!==e.to&&r.y===n.y&&r.x<h+1&&r.x+r.width>p&&(g=Math.max(g,r.y+r.height-1));let _=g+2;U.set(t,_),ce(B,L,_+1),se(ue,L,_+1);let v=c!==u,y=l!==d;we(f+1,u,c,a,!0);for(let e=f+(v?2:1);e<=_;e++)V(c,e,o,`line`);for(let e=p;e<=h;e++)V(e,_,a,`line`);for(let e=_-1;e>=s+(y?2:1);e--)V(l,e,o,`line`);if(we(s+1,d,l,a,!0),i.markerAt===`from`){let e=$i(i.type,m,`down`),t=f+1;for(let n=0;n<e.length;n++)V(u-Math.floor(e.length/2)+n,t,e[n],`arrow`)}if(i.markerAt===`to`){let e=i.type===`inheritance`||i.type===`realization`,t=$i(i.type,m,e?`down`:`up`),n=r.y+r.height;for(let e=0;e<t.length;e++)V(d-Math.floor(t.length/2)+e,n,t[e],`arrow`)}}});function Ee(e,t,n,r){let{fromCX:i,toCX:a}=Se(e,t,n),o=t.y+t.height-1,s=n.y;if(o<s){let t=Te.get(e);if(t){let e=t.exitY+1,n=t.entryY;if(n>=e)return{idealMidX:t.clearSide===`right`?t.routeX+1+Math.floor(r/2):t.routeX-1-Math.ceil(r/2),baseMidY:Math.floor((e+n)/2)};let i=Math.abs(t.routeX-t.fromAnchorX);return Math.abs(t.toAnchorX-t.routeX)>=i?{idealMidX:Math.floor((t.routeX+t.toAnchorX)/2),baseMidY:t.entryY}:{idealMidX:Math.floor((t.fromAnchorX+t.routeX)/2),baseMidY:t.exitY}}return{idealMidX:Math.floor((i+a)/2),baseMidY:Math.floor((o+1+s-1)/2)}}if(n.y+n.height-1<t.y){let e=n.y+n.height-1;return{idealMidX:Math.floor((i+a)/2),baseMidY:Math.floor((e+1+t.y-1)/2)}}return{idealMidX:Math.floor((i+a)/2),baseMidY:U.get(e)??Math.max(o,n.y+n.height-1)+2}}function De(e,t,n,r,i,a,o){let s=r,c=Math.floor(a/2),l=e.y+e.height-1,u=t.y,d=!1;for(let e=0;e<a;e++){let t=s-c+e,r=n-Math.floor(i/2),a=Math.max(0,r);for(let e=a;e<a+i;e++)if(H(e,t,o)){d=!0;break}if(d)break}if(d){let e=l+1,t=u-1;for(let r=e;r<=t;r++){let e=!0,t=n-Math.floor(i/2),a=Math.max(0,t);for(let t=a;t<a+i;t++)if(H(t,r,o)){e=!1;break}if(e){s=r;break}}}return s}let Oe=new Map,ke=[];for(let[e,t]of l.relationships.entries()){if(!t.label)continue;let n=M.get(t.from),r=M.get(t.to);if(!n||!r)continue;let i=Q(t.label),a=Math.floor(i.length/2),o=Math.max(...i.map(P))+2,{idealMidX:s,baseMidY:c}=Ee(e,n,r,o),l=new Set([t.from,t.to]),u=De(n,r,s,c,o,i.length,l);Oe.set(t,u);let d=Math.max(0,s-Math.floor(o/2));ke.push({rel:t,idealMidX:s,naturalStart:d,naturalEnd:d+o-1,rowStart:u-a,rowEnd:u+a})}let Ae=Xi(ke,e=>({idealMid:e.idealMidX,start:e.naturalStart,end:e.naturalEnd,rowStart:e.rowStart,rowEnd:e.rowEnd})),je=new Map;for(let e of ke){let t=Ae.get(e);t&&je.set(e.rel,t)}for(let[e,t]of l.relationships.entries()){let n=M.get(t.from),r=M.get(t.to);if(!(!n||!r)&&t.label){let i=Q(t.label),{idealMidX:a}=Ee(e,n,r,Math.max(...i.map(e=>P(e)))+2),o=Oe.get(t)-Math.floor(i.length/2),s=je.get(t);for(let e=0;e<i.length;e++){let t=o+e,n=i[e],r=ae(` ${n} `),{start:c,cells:l}=ea(Math.max(0,a-Math.floor(r.length/2)),r,n,Math.max(0,s.left),s.right),u=c+l.length;u>0&&t>=0&&(ce(B,Math.max(u,1),Math.max(t+1,1)),se(ue,Math.max(u,1),Math.max(t+1,1)));for(let e=0;e<l.length;e++){let n=c+e;n>=0&&t>=0&&de(n,t,l[e],`text`)}}}}for(let e of I){let t=m?`-`:`─`,n=m?`|`:`│`,r=m?{tl:`+`,tr:`+`,bl:`+`,br:`+`}:{tl:`┌`,tr:`┐`,bl:`└`,br:`┘`},i=ae(` ${e.name} `),a=t=>(B[t]?.[e.y0]??` `)===` `,o=e.x0+2;for(;Array.from({length:i.length+2},(e,t)=>o-1+t).some(e=>!a(e));)o++;e.x1=Math.max(e.x1,o+i.length+1),ce(B,e.x1+1,e.y1+1),se(ue,e.x1+1,e.y1+1);let s=(e,t,n,r)=>{B[e]?.[t]===` `&&de(e,t,n,r)};for(let n=e.x0+1;n<e.x1;n++)s(n,e.y0,t,`border`),s(n,e.y1,t,`border`);for(let t=e.y0+1;t<e.y1;t++)s(e.x0,t,n,`border`),s(e.x1,t,n,`border`);s(e.x0,e.y0,r.tl,`border`),s(e.x1,e.y0,r.tr,`border`),s(e.x0,e.y1,r.bl,`border`),s(e.x1,e.y1,r.br,`border`);for(let[n,r]of i.entries())B[o+n]?.[e.y0]===t&&de(o+n,e.y0,r,`text`)}return fa(u,M,B,m,de),he(B,{roleCanvas:ue,colorMode:i,theme:a,linkCanvas:pe})}function ua(e){let t=new Map;if(e.notes.length===0)return{diagram:e,notesById:t};let n=(e,n)=>{let r=`note ${e}`;return t.set(r,n),{id:r,label:n.text,attributes:[],methods:[]}},r=new Set(e.classes.map(e=>e.id)),i=[];for(let t of e.classes){i.push(t);for(let[r,a]of e.notes.entries())a.forClass===t.id&&i.push(n(r,a))}for(let[t,a]of e.notes.entries())(a.forClass===void 0||!r.has(a.forClass))&&i.push(n(t,a));return{diagram:{...e,classes:i},notesById:t}}function da(e,t){for(let[n,r]of e){if(r.forClass===void 0)continue;let e=t.get(r.forClass);e!==void 0&&t.set(n,e)}}function fa(e,t,n,r,i){if(e.size===0)return;let a=Sn(`rounded`,r),o=r?`.`:`╌`;for(let[r,s]of e){let e=t.get(r);if(!e)continue;let c=e.x+e.width-1,l=e.y+e.height-1;if(i(e.x,e.y,a.tl,`border`),i(c,e.y,a.tr,`border`),i(e.x,l,a.bl,`border`),i(c,l,a.br,`border`),s.forClass===void 0)continue;let u=t.get(s.forClass);if(!u||u.x+u.width>e.x||u.y!==e.y)continue;let d=u.y+1;for(let t=u.x+u.width;t<e.x;t++)n[t]?.[d]===` `&&i(t,d,o,`line`)}}function pa(e){let t=e.trim();return t.length>=2&&(t[0]===`"`&&t[t.length-1]===`"`||t[0]===`'`&&t[t.length-1]===`'`)?t.slice(1,-1):t}var ma=`"graph <dir>"/"flowchart <dir>" (dir: TD, TB, LR, BT, RL), "stateDiagram-v2", "sequenceDiagram", "classDiagram", "erDiagram", "xychart-beta", "C4Context"/"C4Container"/"C4Component"/"C4Dynamic"/"C4Deployment", "architecture-beta"`;function ha(e){let t=e.trim().toLowerCase();if(/^sequence/.test(t))return`sequenceDiagram`;if(/^class/.test(t))return`classDiagram`;if(/^er/.test(t))return`erDiagram`;if(/^xychart/.test(t))return`xychart-beta`;if(/^state/.test(t))return`stateDiagram-v2`}var ga=/^(?:[\w-]+@)?<?(?:-{2,}|={2,}|-\.+-?|~{3,})/;function _a(e){let t=[];for(let n of e)n.forEach((e,n)=>{if(n===0&&t.length>0&&ga.test(e.text)){let n=t[t.length-1];t[t.length-1]={text:`${n.text} ${e.text}`,line:n.line}}else t.push(e)});return t}function va(t){let n=(0,e.extractInitConfig)(t.split(`
|
|
5
|
+
`).map(e=>e.trim())),r=_a((0,e.splitStatementsByLine)(t));if(r.length===0)throw Error(`Empty mermaid diagram`);let i=r[0],a=/^stateDiagram(-v2)?\s*$/i.test(i.text)?ba(r):ya(r);return a.initConfig=n,a}function ya(t){let n=t[0];if(n===void 0)throw Error(`parseFlowchart called with no lines`);let r=n.text,i=r.match(/^(?:graph|flowchart)\s+(TD|TB|LR|BT|RL)\s*$/i);if(!i){let e=r.match(/^(?:graph|flowchart)\b\s*(.*)$/i);if(e){let t=e[1].trim();throw Error(t.length>0?`Line ${n.line}: Invalid direction "${t}" in header "${r}". Expected one of: TD, TB, LR, BT, RL.`:`Line ${n.line}: Missing direction in header "${r}". Expected e.g. "graph TD" or "flowchart LR" — one of: TD, TB, LR, BT, RL.`)}let t=ha(r),i=t&&t.toLowerCase()!==r.trim().toLowerCase()?` Did you mean "${t}"?`:``;throw Error(`Line ${n.line}: Invalid mermaid header: "${r}".${i} Supported headers: ${ma}.`)}let a={direction:(0,e.toDirection)(i[1]),nodes:new Map,edges:[],subgraphs:[],classDefs:new Map,classAssignments:new Map,nodeStyles:new Map,linkStyles:new Map,interactions:new Map},o=[];for(let n=1;n<t.length;n++){let r=t[n].text;if((0,e.tryApplyClassDef)(r,a)||(0,e.tryApplyClassAssignment)(r,a)||(0,e.tryApplyStyleStatement)(r,a))continue;if(/^click\s+/i.test(r)){Na(r,a);continue}if(Fa(r,a))continue;let i=r.match(/^linkStyle\s+(default|[\d,\s]+)\s+(.+)$/);if(i){let t=i[1].trim(),n=(0,e.parseStyleProps)(i[2]);if(t==="default")a.linkStyles.set(`default`,{...a.linkStyles.get(`default`),...n});else{let e=t.split(`,`).map(e=>parseInt(e.trim(),10));for(let t of e)isNaN(t)||a.linkStyles.set(t,{...a.linkStyles.get(t),...n})}continue}let s=r.match(/^direction\s+(TD|TB|LR|BT|RL)\s*$/i);if(s&&o.length>0){o[o.length-1].direction=(0,e.toDirection)(s[1]);continue}let c=r.match(/^subgraph\s+(.+)$/);if(c){let t=c[1].trim(),n=t.match(/^([^\s[]+)\s*\[(.+)\]$/),r,i;n?(r=n[1],i=(0,e.normalizeBrTags)(pa(n[2]))):(i=(0,e.normalizeBrTags)(pa(t)),r=i.replace(/\s+/g,`_`).replace(/[^\p{L}\p{N}_-]/gu,``));let a={id:r,label:i,nodeIds:[],children:[]};o.push(a);continue}if(r===`end`){let e=o.pop();e&&(o.length>0?o[o.length-1].children.push(e):a.subgraphs.push(e));continue}za(r,a,o)}return a}function ba(t){let n={direction:`TD`,nodes:new Map,edges:[],subgraphs:[],classDefs:new Map,classAssignments:new Map,nodeStyles:new Map,linkStyles:new Map,interactions:new Map},r=[],i=new Set,a=0,o=0;for(let s=1;s<t.length;s++){let c=t[s].text;if((0,e.tryApplyClassDef)(c,n)||(0,e.tryApplyClassAssignment)(c,n)||(0,e.tryApplyStyleStatement)(c,n))continue;let l=c.match(/^direction\s+(TD|TB|LR|BT|RL)\s*$/i);if(l){r.length>0?r[r.length-1].direction=(0,e.toDirection)(l[1]):n.direction=(0,e.toDirection)(l[1]);continue}let u=c.match(/^linkStyle\s+(default|[\d,\s]+)\s+(.+)$/);if(u){let t=u[1].trim(),r=(0,e.parseStyleProps)(u[2]);if(t==="default")n.linkStyles.set(`default`,{...n.linkStyles.get(`default`),...r});else{let e=t.split(`,`).map(e=>parseInt(e.trim(),10));for(let t of e)isNaN(t)||n.linkStyles.set(t,{...n.linkStyles.get(t),...r})}continue}if(Fa(c,n))continue;let d=c.match(/^state\s+(?:"([^"]+)"\s+as\s+)?([\w\p{L}]+)\s*\{$/u);if(d){let e=d[1]??d[2],t=d[2],a={id:t,label:e,nodeIds:[],children:[]};r.push(a),i.add(t),n.nodes.delete(t);continue}if(c===`}`){let e=r.pop();e&&(r.length>0?r[r.length-1].children.push(e):n.subgraphs.push(e));continue}let f=c.match(/^state\s+"([^"]+)"\s+as\s+([\w\p{L}]+)\s*$/u);if(f){let t=(0,e.normalizeBrTags)(f[1]),i=f[2];xa(n,r,i,t);continue}let p=c.match(/^(\[\*\]|[\w\p{L}-]+)(?::::([\w][\w-]*))?\s*(?:([\w-]+)@)?-->\s*(\[\*\]|[\w\p{L}-]+)(?::::([\w][\w-]*))?(?:\s*:\s*(.+))?$/u);if(p){let t=p[1],s=p[2],c=p[3],l=p[4],u=p[5],d=p[6]?.trim(),f=d?(0,e.normalizeBrTags)(d):void 0;t===`[*]`?(a++,t=`_start${a>1?a:``}`,Sa(n,r,{id:t,label:``,shape:`state-start`})):i.has(t)||Ca(n,r,t),l===`[*]`?(o++,l=`_end${o>1?o:``}`,Sa(n,r,{id:l,label:``,shape:`state-end`})):i.has(l)||Ca(n,r,l),s!==void 0&&n.classAssignments.set(t,s),u!==void 0&&n.classAssignments.set(l,u),n.edges.push({source:t,target:l,label:f,style:`solid`,hasArrowStart:!1,hasArrowEnd:!0,...c===void 0?{}:{id:c}});continue}let m=c.match(/^([\w\p{L}-]+):::([\w][\w-]*)\s*$/u);if(m){let e=m[1];i.has(e)||Ca(n,r,e),n.classAssignments.set(e,m[2]);continue}let h=c.match(/^([\w\p{L}-]+)\s*:\s*(.+)$/u);if(h){let t=h[1];xa(n,r,t,(0,e.normalizeBrTags)(h[2].trim()));continue}}for(let e of n.classAssignments.keys())!n.nodes.has(e)&&!i.has(e)&&Ca(n,[],e);return n}function xa(e,t,n,r){let i=e.nodes.get(n);i&&(i.label=r),Sa(e,t,{id:n,label:r,shape:`rounded`})}function Sa(e,t,n){if(e.nodes.has(n.id)||e.nodes.set(n.id,n),t.length>0){let e=t[t.length-1];e.nodeIds.includes(n.id)||e.nodeIds.push(n.id)}}function Ca(e,t,n){if(!e.nodes.has(n))Sa(e,t,{id:n,label:n,shape:`rounded`});else if(t.length>0){let e=t[t.length-1];e.nodeIds.includes(n)||e.nodeIds.push(n)}}var wa=/^(<|o|x)?(-{2,}|={2,}|-\.+-|~{3,})(>|o|x)?(?:\|([^|]*)\|)?/,Ta=new Set([`--`,`==`]);function Ea(e){if(e===`o`)return`circle`;if(e===`x`)return`cross`}var Da=/^(<|o|x)?(--|-\.|==)\s+(.+?)\s+(-{2,}[>ox]|={2,}[>ox]|\.+-[>ox]|-{3,}|={3,}|-\.+-)/,Oa=[{regex:/^([\w\p{L}-]+)\(\(\(((?:"[^"]*"|(?!\)\)\)).)+)\)\)\)/u,shape:`doublecircle`},{regex:/^([\w\p{L}-]+)\(\[((?:"[^"]*"|(?!\]\)).)+)\]\)/u,shape:`stadium`},{regex:/^([\w\p{L}-]+)\(\(((?:"[^"]*"|(?!\)\)).)+)\)\)/u,shape:`circle`},{regex:/^([\w\p{L}-]+)\[\[((?:"[^"]*"|(?!\]\]).)+)\]\]/u,shape:`subroutine`},{regex:/^([\w\p{L}-]+)\[\(((?:"[^"]*"|(?!\)\]).)+)\)\]/u,shape:`cylinder`},{regex:/^([\w\p{L}-]+)\[\/((?:"[^"]*"|(?![\\/]\]).)+)([\\/])\]/u,shape:e=>e===`\\`?`trapezoid`:`parallelogram`},{regex:/^([\w\p{L}-]+)\[\\((?:"[^"]*"|(?![\\/]\]).)+)([\\/])\]/u,shape:e=>e===`/`?`trapezoid-alt`:`parallelogram-alt`},{regex:/^([\w\p{L}-]+)>((?:"[^"]*"|(?!\]).)+)\]/u,shape:`asymmetric`},{regex:/^([\w\p{L}-]+)\{\{((?:"[^"]*"|(?!\}\}).)+)\}\}/u,shape:`hexagon`},{regex:/^([\w\p{L}-]+)\[((?:"[^"]*"|(?!\]).)+)\]/u,shape:`rectangle`},{regex:/^([\w\p{L}-]+)\(((?:"[^"]*"|(?!\)).)+)\)/u,shape:`rounded`},{regex:/^([\w\p{L}-]+)\{((?:"[^"]*"|(?!\}).)+)\}/u,shape:`diamond`}],ka=/^([\w\p{L}]+(?:-[\w\p{L}]+)*)/u,Aa=/^([\w\p{L}-]+)(?=@\{)/u;function ja(e){if(e.shape)return(0,t.resolveShapeName)(e.shape)??`rectangle`;if(e.icon!==void 0||e.img!==void 0)switch(e.form?.toLowerCase()){case`circle`:return`circle`;case`rounded`:return`rounded`;default:return`rectangle`}return`rectangle`}function Ma(e,t){return t.label!==void 0&&t.label.length>0?t.label:t.icon?t.icon:t.img?t.img:e}function Na(t,n){(0,e.applyClickStatement)(t,n.interactions)}var Pa=/^([\w-]+)(?=@\{)/;function Fa(e,n){let r=e.match(Pa);if(!r||!n.edges.some(e=>e.id===r[1]))return!1;let i=(0,t.matchExpandedBlock)(e.slice(r[1].length));return i?(Ia(n,r[1],(0,t.parseExpandedMeta)(i.body)),!0):!1}function Ia(e,t,n){for(let r of e.edges)r.id===t&&(n.animate!==void 0&&(r.animate=n.animate.toLowerCase()!==`false`),n.animation!==void 0&&(r.animate=!0))}var La=/^:::([\w][\w-]*)/,Ra=/^([\w\p{L}-]+):::([\w][\w-]*)/u;function za(t,n,r){let i=t.trim(),a=Ba(i,n,r);if(!a||a.ids.length===0)return;i=a.remaining.trim();let o=a.ids;for(;i.length>0;){let t,a,s,c,l,u,d,f=i.match(/^([\w-]+)@(?=[-=<~ox])/);f&&(d=f[1],i=i.slice(f[0].length));let p=i.match(wa),m=p?.[2],h=p?.[1],g=p?.[3];if(p&&m!==void 0&&!(h===void 0&&g===void 0&&Ta.has(m))){t=h!==void 0,l=Ea(h);let n=p[4]?.trim();c=n?(0,e.normalizeBrTags)(n):void 0,i=i.slice(p[0].length).trim(),a=Ga(m),s=g!==void 0,u=Ea(g)}else{let n=i.match(Da);if(!n)break;t=!!n[1],l=Ea(n[1]);let r=n[3].trim();c=r?(0,e.normalizeBrTags)(r):void 0;let o=n[2],d=n[4];i=i.slice(n[0].length).trim(),a=Ka(o,d),u=Ea(d.slice(-1)),s=d.endsWith(`>`)||u!==void 0}let _=Ba(i,n,r);if(!_||_.ids.length===0)break;i=_.remaining.trim();for(let e of o)for(let r of _.ids)n.edges.push({source:e,target:r,label:c,style:a,hasArrowStart:t,hasArrowEnd:s,...l===void 0?{}:{startMarker:l},...u===void 0?{}:{endMarker:u},...d===void 0?{}:{id:d}});o=_.ids}}function Ba(e,t,n){let r=Va(e,t,n);if(!r)return null;let i=[r.id],a=r.remaining.trim();for(;a.startsWith(`&`);){a=a.slice(1).trim();let e=Va(a,t,n);if(!e)break;i.push(e.id),a=e.remaining.trim()}return{ids:i,remaining:a}}function Va(n,r,i){let a=null,o=n,s,c=o.match(Ra);c&&(s=c[2],o=c[1]+o.slice(c[0].length));let l=o.match(Aa);if(l){let n=l[1],s=(0,t.matchExpandedBlock)(o.slice(n.length));if(s){let c=(0,t.parseExpandedMeta)(s.body);Ha(r,i,{id:n,label:(0,e.normalizeBrTags)(Ma(n,c)),shape:ja(c)}),a=n,o=o.slice(n.length+s.length)}}if(a===null)for(let{regex:t,shape:n}of Oa){let s=o.match(t);if(s){a=s[1];let t=(0,e.normalizeBrTags)(s[2]),c=typeof n==`function`?n(s[3]??``):n;Ha(r,i,{id:a,label:t,shape:c}),o=o.slice(s[0].length);break}}if(a===null){let e=o.match(ka);e&&(a=e[1],r.nodes.has(a)?Ua(r,i,a)||Wa(i,a):Ha(r,i,{id:a,label:a,shape:`rectangle`}),o=o.slice(e[0].length))}if(a===null)return null;s&&r.classAssignments.set(a,s);let u=o.match(La);return u&&(r.classAssignments.set(a,u[1]),o=o.slice(u[0].length)),{id:a,remaining:o}}function Ha(e,t,n){e.nodes.has(n.id)||e.nodes.set(n.id,n),Wa(t,n.id)}function Ua(e,t,n){let r=e=>e.nodeIds.includes(n)||e.children.some(r);return e.subgraphs.some(r)||t.some(r)}function Wa(e,t){if(e.length>0){let n=e[e.length-1];n.nodeIds.includes(t)||n.nodeIds.push(t)}}function Ga(e){return e.startsWith(`~`)?`invisible`:e.includes(`.`)?`dotted`:e.startsWith(`=`)?`thick`:`solid`}function Ka(e,t){return e.includes(`.`)||t.includes(`.`)?`dotted`:e.startsWith(`=`)||t.startsWith(`=`)?`thick`:`solid`}function qa(t,n){let r=new Set,i=new Map;for(let e of t.subgraphs)Ja(e,r,i);let a=new Map,o=0;for(let[n,i]of t.nodes){if(r.has(n))continue;let t={name:n,displayLabel:(0,e.stripFormattingTags)(i.label),shape:i.shape,index:o,gridCoord:null,drawingCoord:null,drawing:null,drawn:!1,styleClassName:``,styleClass:st};a.set(n,t),o++}let s=[...a.values()],c=[],l=new Map,u=new Map;for(let n of t.edges){let o=n.source,s=n.target,d,f;if(r.has(o)){let e=i.get(o),n=e?Za(e,t,`exit`):void 0;n&&(o=n),d=n?e:void 0}if(r.has(s)){let e=i.get(s),n=e?Za(e,t,`entry`):void 0;n&&(s=n),f=n?e:void 0}let p=a.get(o),m=a.get(s);if(!p||!m)continue;let h={from:p,to:m,text:n.label?(0,e.stripFormattingTags)(n.label):``,path:[],labelLine:[],startDir:{x:0,y:0},endDir:{x:0,y:0},style:n.style,hasArrowStart:n.hasArrowStart,hasArrowEnd:n.hasArrowEnd,...n.startMarker===void 0?{}:{startMarker:n.startMarker},...n.endMarker===void 0?{}:{endMarker:n.endMarker}};c.push(h),d&&l.set(h,d),f&&u.set(h,f)}let d=[];for(let e of t.subgraphs)Qa(e,null,a,d,t);if($a(t.subgraphs,d,a),l.size>0||u.size>0){let e=new Map;to(t.subgraphs,d,e);for(let[t,n]of l){let r=e.get(n);r&&(t.clusterSource=r)}for(let[t,n]of u){let r=e.get(n);r&&(t.clusterTarget=r)}}let f=t.classDefs.get(`default`);if(f)for(let e of a.values())e.styleClassName=`default`,e.styleClass={name:`default`,styles:f};for(let[e,n]of t.classAssignments){let r=a.get(e),i=t.classDefs.get(n);r&&i&&(r.styleClassName=n,r.styleClass={name:n,styles:f?{...f,...i}:i})}return{nodes:s,edges:c,canvas:F(0,0),roleCanvas:oe(0,0),grid:lt(),columnWidth:new Map,rowHeight:new Map,subgraphs:d,config:n,offsetX:0,offsetY:0,bundles:[]}}function Ja(e,t,n){t.add(e.id),n.set(e.id,e);for(let r of e.children)Ja(r,t,n)}function Ya(e,t){for(let n of e.nodeIds)t.add(n);for(let n of e.children)Ya(n,t)}function Xa(e,t){let n=new Set;if(Ya(e,n),n.size===0)return!0;let r=t=>n.has(t)||t===e.id;return!t.edges.some(e=>r(e.source)!==r(e.target))}function Za(e,t,n){let r=new Set;if(Ya(e,r),r.size===0)return;let i=[...t.nodes.keys()].filter(e=>r.has(e));if(i.length===0)return;let a=i.filter(e=>n===`entry`?!t.edges.some(t=>t.target===e&&r.has(t.source)):!t.edges.some(t=>t.source===e&&r.has(t.target))),o=a.length>0?a:i;return n===`entry`?o[0]:o[o.length-1]}function Qa(e,t,n,r,i){let a;e.direction&&Xa(e,i)&&(a=e.direction===`LR`||e.direction===`RL`?`LR`:`TD`);let o={name:e.label,nodes:[],parent:t,children:[],minX:0,minY:0,maxX:0,maxY:0,direction:a};for(let t of e.nodeIds){let e=n.get(t);e&&o.nodes.push(e)}r.push(o);for(let t of e.children){let e=Qa(t,o,n,r,i);o.children.push(e);for(let t of e.nodes)o.nodes.includes(t)||o.nodes.push(t)}return o}function $a(e,t,n){let r=new Map;to(e,t,r);let i=new Map;function a(e){let t=r.get(e);if(t){for(let t of e.children)a(t);for(let n of e.nodeIds)i.has(n)||i.set(n,t)}}for(let t of e)a(t);for(let e of t)e.nodes=e.nodes.filter(t=>{let r;for(let[e,i]of n)if(i===t){r=e;break}if(!r)return!1;let a=i.get(r);return!a||eo(e,a)})}function eo(e,t){let n=t;for(;n!==null;){if(n===e)return!0;n=n.parent}return!1}function to(e,t,n){let r=[];function i(e){for(let t of e)r.push(t),i(t.children)}i(e);for(let e=0;e<r.length&&e<t.length;e++){let i=r[e],a=t[e];!i||!a||n.set(i,a)}}function no(t,n,r,i,a={}){return ro((0,e.withDirectionOverride)(va(t),a.direction),n,r,i,a)}function ro(e,t,n,r,i){t.graphDirection=e.direction===`LR`||e.direction===`RL`?`LR`:`TD`;let a=qa(e,t);Cr(a),Fi(a);let o=i.hyperlinks?f(a,e.interactions):void 0;return e.direction===`BT`&&(ge(a.canvas,a.roleCanvas,a.labelRects??[],o),H(a.canvas,a.roleCanvas),ve(a.roleCanvas),o&&l(o)),e.direction===`RL`&&(xe(a.canvas,a.roleCanvas,a.labelRects??[],o),be(a.canvas,a.roleCanvas),Ce(a.roleCanvas),o&&u(o)),he(a.canvas,{roleCanvas:a.roleCanvas,colorMode:n,theme:r,linkCanvas:o})}function io(n,r,i,a,o={}){let s=(0,t.architectureToGraph)((0,t.parseArchitecture)((0,e.splitStatements)(n)));return ro((0,e.withDirectionOverride)(s,o.direction),r,i,a,o)}var ao=28,oo=2;function so(e,n){let r=new Map(e.map(e=>[e,e])),i=e=>{let t=e;for(;r.get(t)!==t;)t=r.get(t);return t},a=new Set(e);for(let e of n){let n=(0,t.c4Placement)(e);if(n.axis===`horizontal`&&a.has(n.source)&&a.has(n.target)){let e=i(n.source),t=i(n.target);e!==t&&r.set(t,e)}}let o=new Map;for(let t of e)o.set(i(t),o.get(i(t))??new Set);for(let e of n){let n=(0,t.c4Placement)(e);if(n.axis===`horizontal`||!a.has(n.source)||!a.has(n.target))continue;let r=i(n.source),s=i(n.target);r!==s&&o.get(r).add(s)}let s=new Map,c=new Map,l=e=>{s.set(e,1);let t=[];for(let n of o.get(e)??[]){let e=s.get(n)??0;e!==1&&(t.push(n),e===0&&l(n))}c.set(e,t),s.set(e,2)};for(let e of o.keys())s.has(e)||l(e);let u=new Map,d=e=>{let t=u.get(e);if(t!==void 0)return t;u.set(e,0);let n=0;for(let[t,r]of c)r.includes(e)&&(n=Math.max(n,d(t)+1));return u.set(e,n),n},f=new Map;for(let t of e)f.set(t,d(i(t)));return f}var co=[` o `,`/|\\`,`/ \\`];function lo(e){let n=[];return e.kind===`person`&&n.push(...co),n.push(...(0,t.wrapC4Text)(e.label,ao),(0,t.c4TypeLine)(e)),e.description&&n.push(``,...(0,t.wrapC4Text)(e.description,ao)),{lines:n,glyph:e.kind===`person`}}function uo(e,t){let{lines:n,glyph:r}=lo(e),i=Math.max(...n.map(P)),a=e.shape===`db`?2:0;return{kind:`el`,el:e,lines:n,glyph:r,order:t,rank:0,box:{x:0,y:0,w:Math.max(14,i+4),h:n.length+2+a}}}function fo(n,r,i,a,o={}){let s=(0,e.withDirectionOverride)((0,t.parseC4Diagram)((0,e.splitStatements)(n)),o.direction);if(s.elements.length===0&&s.boundaries.length===0)return``;let c=r.useAscii,l=nt(r.paddingX,5,6,4),u=nt(r.paddingY,5,4,3)+ +!!s.relationships.some(e=>(0,t.c4RelLabelLines)(e).length>1),d=new Map;for(let[e,t]of s.elements.entries())d.set(t.alias,uo(t,e));let f=so([...s.elements.map(e=>e.alias),...po(s.boundaries)],s.relationships);for(let[e,t]of d)t.rank=f.get(e)??0;let p=new Map,m=new Set,h=e=>{let n=[];for(let t of e.elementAliases)m.add(t),n.push(d.get(t));for(let t of e.children)n.push(h(t));let r=(0,t.c4BoundaryTypeLine)(e),i=r?2:1;if(n.length===0){let n=[...(0,t.wrapC4Text)(e.label,ao)];r&&n.push(r);let a={kind:`b`,b:e,rows:[],vgaps:[],headerH:i,order:2**53-1,rank:f.get(e.alias)??0,box:{x:0,y:0,w:Math.max(14,Math.max(...n.map(P))+4),h:n.length+2}};return p.set(e.alias,a),a}let a={kind:`b`,b:e,rows:[],vgaps:[],headerH:i,order:0,rank:0,box:{x:0,y:0,w:0,h:0}};a.rows=_o(n,s),s.direction===`BT`&&a.rows.reverse(),a.vgaps=bo(a.rows,s,u),p.set(e.alias,a);let o=a.rows.flat();return a.order=Math.min(...o.map(e=>e.order)),a.rank=Math.min(...o.map(e=>e.rank)),vo(a,l),a},g=s.boundaries.map(h),_=_o([...s.elements.filter(e=>!m.has(e.alias)).map(e=>d.get(e.alias)),...g],s);s.direction===`BT`&&_.reverse();let v=bo(_,s,u),y=s.title?2:0,b=(e,t,n,r,i)=>{let a=r;for(let[r,o]of e.entries()){let e=go(o,s,l),c=o.reduce((e,t)=>e+t.box.w,0)+mo(e),u=n+Math.floor((i-c)/2),d=Math.max(...o.map(e=>e.box.h));o.forEach((t,n)=>{x(t,u,a+Math.floor((d-t.box.h)/2)),u+=t.box.w+(e[n]??0)}),a+=d+(t[r]??0)}},x=(e,t,n)=>{e.box.x=t,e.box.y=n,e.kind===`b`&&e.rows.length>0&&b(e.rows,e.vgaps,t+2,n+e.headerH+2,e.box.w-4)},S=Math.max(..._.map(e=>e.reduce((e,t)=>e+t.box.w,0)+mo(go(e,s,l))));b(_,v,oo,oo+y,S);let C=0,w=0,T=[...d.values(),...p.values()].map(e=>e.box);for(let e of T)C=Math.max(C,e.x+e.w),w=Math.max(w,e.y+e.h);let E=C+oo+1,D=w+oo+1,ee=F(E,D),O=oe(E,D),k=(e,t,n,r)=>z(ee,e,t,n,{role:r,roleCanvas:O}),A=(e,t,n,r)=>{let i=e;for(let e of ae(n))k(i,t,e,r),i++},te=Array.from({length:E+1},()=>Array(D+1).fill(0)),j=Array.from({length:E+1},()=>Array(D+1).fill(!1)),ne=e=>{for(let t=e.x;t<e.x+e.w;t++)for(let n=e.y;n<e.y+e.h;n++)j[t][n]=!0};if(s.title){let e=P(s.title);A(Math.max(0,Math.floor((E-e)/2)),1,s.title,`text`);for(let e=0;e<E;e++)j[e][1]=!0}let re=e=>{let{x:n,y:r,w:i,h:a}=e.box,o=c?`-`:`╌`,s=c?`:`:`╎`,[l,u,d,f]=c?[`+`,`+`,`+`,`+`]:[`╭`,`╮`,`╰`,`╯`];for(let e=0;e<i;e++)te[n+e][r]|=To,te[n+e][r+a-1]|=To;for(let e=0;e<a;e++)te[n][r+e]|=Eo,te[n+i-1][r+e]|=Eo;for(let e=1;e<i-1;e++)k(n+e,r,o,`border`),k(n+e,r+a-1,o,`border`);for(let e=1;e<a-1;e++)k(n,r+e,s,`border`),k(n+i-1,r+e,s,`border`);if(k(n,r,l,`border`),k(n+i-1,r,u,`border`),k(n,r+a-1,d,`border`),k(n+i-1,r+a-1,f,`border`),e.rows.length>0){A(n+2,r+1,e.b.label,`text`);let a=(0,t.c4BoundaryTypeLine)(e.b);a&&A(n+2,r+2,a,`text`);let o=Math.max(P(e.b.label),P(a??``))+2;for(let t=1;t<=o&&n+t<n+i-1;t++)for(let i=1;i<=e.headerH;i++)j[n+t][r+i]=!0}else{let a=[...(0,t.wrapC4Text)(e.b.label,ao)],o=(0,t.c4BoundaryTypeLine)(e.b);o&&a.push(o),a.forEach((e,t)=>A(n+Math.floor((i-P(e))/2),r+1+t,e,`text`)),ne(e.box)}},M=e=>{for(let t of e)t.kind===`b`&&(re(t),M(t.rows.flat()))};M(_.flat());let N=e=>{let{x:t,y:n,w:r,h:i}=e.box,a=e.el,o=a.external,s=c?o?`.`:`-`:o?`┄`:`─`,l=c?o?`:`:`|`:o?`┆`:`│`,u=a.kind===`person`||a.shape!=="default",[d,f,p,m]=c?[`+`,`+`,`+`,`+`]:u?[`╭`,`╮`,`╰`,`╯`]:[`┌`,`┐`,`└`,`┘`];for(let e=1;e<r-1;e++)k(t+e,n,s,`border`),k(t+e,n+i-1,s,`border`);let h=a.shape===`queue`?`(`:l,g=a.shape===`queue`?`)`:l;for(let e=1;e<i-1;e++)k(t,n+e,h,`border`),k(t+r-1,n+e,g,`border`);k(t,n,d,`border`),k(t+r-1,n,f,`border`),k(t,n+i-1,p,`border`),k(t+r-1,n+i-1,m,`border`);let _=n+1;if(a.shape===`db`){let e=c?`+`:`├`,a=c?`+`:`┤`;for(let o of[n+1,n+i-2]){k(t,o,e,`junction`),k(t+r-1,o,a,`junction`);for(let e=1;e<r-1;e++)k(t+e,o,s,`border`)}_=n+2}e.lines.forEach((n,i)=>{let a=e.glyph&&i<co.length,o=P(n);A(t+Math.floor((r-o)/2),_+i,n,a?`border`:`text`)}),ne(e.box)};for(let e of d.values())N(e);let ie=e=>{let t=d.get(e);return t?{box:t.box,boundary:!1}:{box:p.get(e).box,boundary:!0}},I=Array.from({length:E+1},()=>Array(D+1).fill(0)),se=new Map,L=[];for(let e of s.relationships){if(e.from===e.to)continue;let t=ie(e.from),n=ie(e.to),r=[];t.boundary&&p.get(e.from).rows.length>0&&r.push(t.box),n.boundary&&p.get(e.to).rows.length>0&&r.push(n.box);let i=Po(t.box,n.box,{width:E,height:D,blocked:j,masks:I,arrows:se,borders:te},r);if(!i)continue;L.push({rel:e,path:i}),Mo(i,I,t.box,n.box);let a=i[i.length-1],o=i[0];(!e.reversed||e.bidirectional)&&se.set(`${a[0]},${a[1]}`,Oo(Do(a,n.box),c)),(e.bidirectional||e.reversed)&&se.set(`${o[0]},${o[1]}`,Oo(Do(o,t.box),c))}for(let e=0;e<=E;e++)for(let t=0;t<=D;t++){let n=I[e][t];if(n===0)continue;let r=se.get(`${e},${t}`);r?k(e,t,r,`arrow`):k(e,t,Ao(n,c),jo(n)?`corner`:`line`)}for(let[e,t]of se){let[n,r]=e.split(`,`).map(Number);k(n,r,t,`arrow`)}let R=(e,t)=>e>=0&&t>=0&&e<=E&&t<=D&&ee[e][t]===` `&&!j[e][t],ce=(e,t)=>e>=0&&t>=0&&e<=E&&t<=D&&!j[e][t]&&!se.has(`${e},${t}`)&&(ee[e][t]===` `||I[e][t]!==0);for(let{rel:e,path:n}of L){let r=(0,t.c4RelLabelLines)(e);if(r.length===0)continue;let i=Math.max(...r.map(P)),a=[r];for(let e of[18,12,8,6])i>e&&a.push(r.flatMap((n,r)=>r===0?(0,t.wrapC4Text)(n,e):[n]));let o=e=>a.some(t=>Io(t,n,R,ce,A,e));o(!1)||o(!0)}return he(ee,{roleCanvas:O,colorMode:i,theme:a})}function po(e){return e.flatMap(e=>e.elementAliases.length===0&&e.children.length===0?[e.alias]:po(e.children))}var mo=e=>e.reduce((e,t)=>e+t,0);function ho(e,n,r){let i=(e,t)=>e.kind===`el`?e.el.alias===t:e.b.alias===t,a=0;for(let o of r.relationships){let r=(0,t.c4Placement)(o);r.axis===`horizontal`&&(i(e,r.source)&&i(n,r.target)||i(n,r.source)&&i(e,r.target))&&(a=Math.max(a,...(0,t.c4RelLabelLines)(o).map(P)))}return a}function go(e,t,n){let r=[];for(let i=0;i<e.length-1;i++){let a=ho(e[i],e[i+1],t);r.push(Math.max(n,a>0?a+4:0))}return r}function _o(e,n){let r=new Map;for(let t of e)r.set(t.rank,[...r.get(t.rank)??[],t]);let i=[...r.entries()].sort((e,t)=>e[0]-t[0]).map(([,e])=>e.sort((e,t)=>e.order-t.order)),a=e=>e.kind===`el`?e.el.alias:e.b.alias;for(let e of i)for(let r=0;r<e.length;r++){let r=!1;for(let i of n.relationships){let n=(0,t.c4Placement)(i);if(n.axis!==`horizontal`)continue;let o=e.map(a),s=o.indexOf(n.source),c=o.indexOf(n.target);if(s<0||c<0||s<c)continue;let[l]=e.splice(s,1);e.splice(c,0,l),r=!0}if(!r)break}return i}function vo(e,n){let r=0,i=e.headerH+2;for(let[t,a]of e.rows.entries()){let o=mo(a.map(e=>e.box.w))+(a.length-1)*n;r=Math.max(r,o),i+=Math.max(...a.map(e=>e.box.h))+(e.vgaps[t]??0)}let a=Math.max(P(e.b.label),P((0,t.c4BoundaryTypeLine)(e.b)??``));e.box.w=Math.max(r,a)+4,e.box.h=i+2}function yo(e,t=new Set){if(e.kind===`el`)t.add(e.el.alias);else{t.add(e.b.alias);for(let n of e.rows)for(let e of n)yo(e,t)}return t}function bo(e,n,r){let i=new Map;e.forEach((e,t)=>{for(let n of e)for(let e of yo(n))i.set(e,t)});let a=e.map(()=>0);for(let e of n.relationships){let n=i.get(e.from),r=i.get(e.to);if(n===void 0||r===void 0||n===r)continue;let o=(0,t.c4RelLabelLines)(e).length;if(o!==0)for(let e=Math.min(n,r);e<Math.max(n,r);e++)a[e]=a[e]+o+1}return e.slice(0,-1).map((e,t)=>Math.max(r,a[t]+1))}var xo=1,So=2,Co=4,wo=8,To=1,Eo=2;function Do(e,t){let[n,r]=e;return r<t.y?So:r>=t.y+t.h?xo:n<t.x?wo:Co}function Oo(e,t){return e===So?t?`v`:`▼`:e===xo?t?`^`:`▲`:e===wo?t?`>`:`►`:t?`<`:`◄`}var ko={[xo]:`│`,[So]:`│`,3:`│`,[Co]:`─`,[wo]:`─`,12:`─`,10:`┌`,6:`┐`,9:`└`,5:`┘`,11:`├`,7:`┤`,14:`┬`,13:`┴`,15:`┼`};function Ao(e,t){let n=ko[e]??`┼`;return t?n===`│`?`|`:n===`─`?`-`:`+`:n}function jo(e){return e===10||e===6||e===9||e===5}function Mo(e,t,n,r){let i=(e,t)=>t[0]>e[0]?wo:t[0]<e[0]?Co:t[1]>e[1]?So:xo,a=e=>e===xo?So:e===So?xo:e===Co?wo:Co;for(let n=0;n<e.length;n++){let[r,o]=e[n];n>0&&(t[r][o]|=a(i(e[n-1],e[n]))),n<e.length-1&&(t[r][o]|=i(e[n],e[n+1]))}let o=e[0],s=e[e.length-1];t[o[0]][o[1]]|=Do(o,n),t[s[0]][s[1]]|=Do(s,r)}function No(e,t,n){let r=[],i=e.x+e.w/2,a=e.y+e.h/2,o=t.x+t.w/2,s=t.y+t.h/2,c=t.y<e.y+e.h&&e.y<t.y+t.h,l=new Set;c?l.add(o>i?wo:Co):l.add(s>a?So:xo);let u=e=>l.has(e)?0:e===So&&l.has(xo)||e===xo&&l.has(So)||e===Co&&l.has(wo)||e===wo&&l.has(Co)?14:7,d=(e,t,i,a)=>{e<0||t<0||e>n.width||t>n.height||n.blocked[e][t]||r.push({cell:[e,t],cost:u(i)+Math.abs(a)*.2})};for(let t=e.x+1;t<e.x+e.w-1;t++)d(t,e.y-1,xo,t-i),d(t,e.y+e.h,So,t-i);for(let t=e.y+1;t<e.y+e.h-1;t++)d(e.x-1,t,Co,t-a),d(e.x+e.w,t,wo,t-a);return r}function Po(e,t,n,r){let i=(e,t)=>r.some(n=>e>=n.x&&e<n.x+n.w&&t>=n.y&&t<n.y+n.h),a=No(e,t,n).filter(e=>!i(...e.cell)),o=new Map;for(let r of No(t,e,n))i(...r.cell)||o.set(`${r.cell[0]},${r.cell[1]}`,r.cost);if(a.length===0||o.size===0)return;let s=(e,n)=>(e<t.x?t.x-e:e>=t.x+t.w?e-(t.x+t.w-1):0)+(n<t.y?t.y-n:n>=t.y+t.h?n-(t.y+t.h-1):0),c=[],l=new Map;for(let e of a){let[t,n]=e.cell;c.push({x:t,y:n,dir:0,g:e.cost,f:e.cost+s(t,n),parent:void 0}),l.set(`${t},${n},0`,e.cost)}let u=[[0,-1,xo],[0,1,So],[-1,0,Co],[1,0,wo]],d=0;for(;c.length>0&&d++<2e5;){let e=0;for(let t=1;t<c.length;t++)c[t].f<c[e].f&&(e=t);let t=c.splice(e,1)[0],r=`${t.x},${t.y}`;if(o.get(r)!==void 0){let e=[];for(let n=t;n;n=n.parent)e.push([n.x,n.y]);return e.reverse()}for(let[e,r,a]of u){let u=t.x+e,d=t.y+r;if(u<0||d<0||u>n.width||d>n.height)continue;let f=o.has(`${u},${d}`);if(n.blocked[u][d]&&!f||i(u,d)||n.arrows.has(`${u},${d}`))continue;let p=1;t.dir!==0&&t.dir!==a&&(p+=2);let m=n.borders[u][d];m!==0&&(a===Co||a===wo?(m&To)!==0:(m&Eo)!==0)&&(p+=12);let h=n.masks[u][d];if(h!==0){let e=a===Co||a===wo,t=!!(h&12),n=!!(h&3);p+=e&&t||!e&&n?8:3}let g=t.g+p+(f?o.get(`${u},${d}`)??0:0),_=`${u},${d},${a}`;(l.get(_)??1/0)<=g||(l.set(_,g),c.push({x:u,y:d,dir:a,g,f:g+s(u,d),parent:t}))}}}function Fo(e){let t=[],n=0;for(;n<e.length-1;){let r=e[n+1][1]===e[n][1],i=n+1;for(;i<e.length-1&&e[i+1][1]===e[i][1]===r;)i++;let[a,o]=e[n],[s,c]=e[i];t.push({horizontal:r,x0:Math.min(a,s),y0:Math.min(o,c),len:Math.abs(s-a)+Math.abs(c-o)+1}),n=i}return t}function Io(e,t,n,r,i,a){let o=e.map(P),s=e=>e.every(([e,t],r)=>{let i=o[r];for(let r=0;r<i;r++)if(!n(e+r,t))return!1;return n(e-1,t)&&n(e+i,t)}),c=e=>Array.from({length:e},(e,t)=>t).sort((t,n)=>Math.abs(t-e/2)-Math.abs(n-e/2));for(let n of a?[]:Fo(t).sort((e,t)=>t.len-e.len))for(let t of c(n.len)){let r=[];if(n.horizontal){let i=n.x0+t;r.push(e.map((e,t)=>[i-Math.floor(o[t]/2),t===0?n.y0-1:n.y0+t])),e.length===1&&r.push([[i-Math.floor(o[0]/2),n.y0+1]])}else{let i=n.y0+t-Math.floor((e.length-1)/2);for(let t of[1,-1])r.push(e.map((e,r)=>[t>0?n.x0+2:n.x0-1-o[r],i+r]))}for(let t of r)if(s(t))return t.forEach(([t,n],r)=>i(t,n,e[r],`text`)),!0}let l=e=>e.every(([e,t],n)=>{for(let i=0;i<o[n];i++)if(!r(e+i,t))return!1;return!0});for(let n of a?Fo(t).sort((e,t)=>t.len-e.len):[])for(let t of c(n.len)){let r=e.map((r,i)=>n.horizontal?[n.x0+t-Math.floor(o[i]/2),n.y0+i-+(e.length>1)]:[n.x0-Math.floor(o[i]/2),n.y0+t+i]);if(l(r))return r.forEach(([t,n],r)=>i(t,n,e[r],`text`)),!0}return!1}var Lo={xychart:(e,t,n,r)=>Me(e,t,n,r),er:(e,t,n,r)=>Hi(e,t,n,r),sequence:(e,t,n,r)=>Ji(e,t,n,r),class:(e,t,n,r,i)=>la(e,t,n,r,i),flowchart:(e,t,n,r,i)=>no(e,t,n,r,i),architecture:(e,t,n,r,i)=>io(e,t,n,r,i),c4:(e,t,n,r,i)=>fo(e,t,n,r,i)},Ro=/\x1b\[[0-9;]*m/g;function zo(e){let t=e.split(`
|
|
6
|
+
`),n=t.reduce((e,t)=>Math.max(e,o(t.replace(Ro,``)).length),0),r=String(Math.max(0,t.length-1)).length,i=` `.repeat(r+1);return[i+Array.from({length:n},(e,t)=>String(Math.floor(t/10)%10)).join(``),i+Array.from({length:n},(e,t)=>String(t%10)).join(``),...t.map((e,t)=>`${String(t).padStart(r,` `)} ${e}`)].join(`
|
|
7
|
+
`)}function Bo(t,n={}){let r={useAscii:n.useAscii??!1,paddingX:n.paddingX??5,paddingY:n.paddingY??5,boxBorderPadding:n.boxBorderPadding??1,graphDirection:`TD`},i=n.colorMode===`auto`||n.colorMode===void 0?_():n.colorMode,a={...h,...n.theme},o=Lo[(0,e.detectDiagramType)(t)](t,r,i,a,{hyperlinks:n.hyperlinks??!1,direction:n.direction});return n.showCoords?zo(o):o}var Vo=Bo;exports.DEFAULT_ASCII_THEME=h,exports.detectColorMode=_,exports.diagramColorsToAsciiTheme=g,exports.renderMermaidASCII=Bo,exports.renderMermaidAscii=Vo;
|
|
8
8
|
//# sourceMappingURL=index.cjs.map
|