UNPKG

mermaid

Version:

Markdown-ish syntax for generating flowcharts, mindmaps, sequence diagrams, class diagrams, gantt charts, git graphs and more.

2 lines • 131 kB
import{a as Tn,b as Dn}from"./chunk-ELN4OFEY.mjs";import{g as _n}from"./chunk-CQYJW6SN.mjs";import"./chunk-H3Y7VZGV.mjs";import"./chunk-I3TGAWCN.mjs";import{g as Nn}from"./chunk-MTL3NAOM.mjs";import"./chunk-PJ2FCULI.mjs";import"./chunk-CBDDXUFN.mjs";import"./chunk-JENJ2NYI.mjs";import"./chunk-J3HIBH47.mjs";import"./chunk-PNVTB6QF.mjs";import"./chunk-QMVAV2W2.mjs";import"./chunk-JMS3MFCX.mjs";import"./chunk-3A3SYRXV.mjs";import"./chunk-VHEAKMU3.mjs";import{k as Kt,l as Ut,m as Xt}from"./chunk-OI4SXON4.mjs";import{b as Be}from"./chunk-NXZFCOWJ.mjs";import{a as c}from"./chunk-V3YWWTDH.mjs";function br(t,e,o=1){let n=Math.abs(e.x-t.x),r=Math.abs(e.y-t.y);return n<=o&&r<=o?"Z":r<=o?"H":"V"}c(br,"segmentOrientation");function xr(t,e=1){let o=[];for(let n=0;n<t.length-1;n++){let r=t[n],i=t[n+1];o.push({a:r,b:i,orientation:br(r,i,e)})}return o}c(xr,"segmentsFromPoints");function Rr(t,e=1){if(t.length===0)return[];let o=[],n={...t[0]};for(let r=1;r<t.length;r++){let i=t[r];if(i.orientation!=="Z"){if(n.orientation==="Z"){n={...i};continue}if(n.orientation===i.orientation){if(n.orientation==="H"&&Math.abs(n.b.y-i.a.y)<=e&&Math.abs(n.a.y-i.a.y)<=e){n.b=i.b;continue}if(n.orientation==="V"&&Math.abs(n.b.x-i.a.x)<=e&&Math.abs(n.a.x-i.a.x)<=e){n.b=i.b;continue}}n.orientation!=="Z"&&o.push(n),n={...i}}}return n.orientation!=="Z"&&o.push(n),o}c(Rr,"mergeCollinear");function V(t,e=1){if(t.length<2)return{points:[...t],segments:[],bends:0};let o=xr(t,e).filter(a=>a.orientation!=="Z"),n=Rr(o,e),r=[];for(let[a,s]of n.entries())a===0&&r.push(s.a),r.push(s.b);let i=0;for(let a=1;a<n.length;a++)n[a-1].orientation!==n[a].orientation&&i++;return{points:r.length>0?r:[...t],segments:n,bends:i}}c(V,"normalizePolyline");function Mt(t,e){if(t.orientation==="Z"||e.orientation==="Z"||t.orientation===e.orientation)return!1;let o=t.orientation==="H"?t:e,n=t.orientation==="V"?t:e,r=o.a.y,i=Math.min(o.a.x,o.b.x),a=Math.max(o.a.x,o.b.x),s=n.a.x,d=Math.min(n.a.y,n.b.y),u=Math.max(n.a.y,n.b.y);if(s>=i&&s<=a&&r>=d&&r<=u){let f=s,p=r,h=Math.abs(f-o.a.x)<1e-6&&Math.abs(p-o.a.y)<1e-6||Math.abs(f-o.b.x)<1e-6&&Math.abs(p-o.b.y)<1e-6,g=Math.abs(f-n.a.x)<1e-6&&Math.abs(p-n.a.y)<1e-6||Math.abs(f-n.b.x)<1e-6&&Math.abs(p-n.b.y)<1e-6;return!(h&&g)}return!1}c(Mt,"segmentsCross");function z(t,e){return t<e?-1:t>e?1:0}c(z,"compareCodeUnits");function k(t){let e=t.x??0,o=t.y??0,n=t.width??40,r=t.height??40;return{cx:e,cy:o,left:e-n/2,right:e+n/2,top:o-r/2,bottom:o+r/2}}c(k,"rectForNode");function kn(t,e,o,n,r=0){if(t.length<2)return null;let i=e==="end"?t[t.length-1]:t[0],a=e==="end"?t[t.length-2]:t[1],s=a.x-i.x,d=a.y-i.y;if(Math.abs(s)<=r&&Math.abs(d)<=r)return null;if(Math.abs(d)<=r){let u=i.x+Math.sign(s)*o,l=Math.min(i.x,u),f=Math.max(i.x,u);return{cx:(l+f)/2,cy:i.y,left:l,right:f,top:i.y-n,bottom:i.y+n}}if(Math.abs(s)<=r){let u=i.y+Math.sign(d)*o,l=Math.min(i.y,u),f=Math.max(i.y,u);return{cx:i.x,cy:(l+f)/2,left:i.x-n,right:i.x+n,top:l,bottom:f}}return null}c(kn,"terminalMarkerClearanceRect");function Vn(t,e){let o=e==="start"?t.arrowTypeStart:t.arrowTypeEnd;if(typeof o=="string"){let n=o.trim();if(n.length>0)return n!=="none"&&n!=="arrow_open"}return typeof t.type!="string"?!1:e==="start"&&t.type.startsWith("double_")?!0:e==="end"&&/arrow_(point|cross|circle|barb)|double_arrow/.test(t.type)}c(Vn,"hasTerminalMarker");function qe(t,e,o=1e-6){return Math.abs(t-e)<=o}c(qe,"approxEqual");function Fn(t,e,o=1e-6){for(let n=1;n<t.length;n++){let r=t[n-1],i=t[n];if(qe(r.y,i.y,o)&&!qe(r.x,i.x,o)){if(qe(e.y,r.y,o)&&e.x>=Math.min(r.x,i.x)-o&&e.x<=Math.max(r.x,i.x)+o)return!0}else if(qe(r.x,i.x,o)&&!qe(r.y,i.y,o)&&qe(e.x,r.x,o)&&e.y>=Math.min(r.y,i.y)-o&&e.y<=Math.max(r.y,i.y)+o)return!0}return!1}c(Fn,"orthogonalPolylineContainsPoint");function Fe(t){let e=0;for(let o=1;o<t.length;o++){let n=t[o-1],r=t[o];e+=Math.abs(r.x-n.x)+Math.abs(r.y-n.y)}return e}c(Fe,"manhattanLength");function Ne(t,e,o){if(t.x===e.x&&t.y===e.y)return!1;if(t.y===e.y){let n=t.y,r=Math.min(t.x,e.x),i=Math.max(t.x,e.x),a=n>=o.top&&n<=o.bottom,s=Math.max(r,o.left)<Math.min(i,o.right);return a&&s}if(t.x===e.x){let n=t.x,r=Math.min(t.y,e.y),i=Math.max(t.y,e.y),a=n>=o.left&&n<=o.right,s=Math.max(r,o.top)<Math.min(i,o.bottom);return a&&s}return!1}c(Ne,"segmentIntersectsRectInterior");function Re(t,e){for(let o=1;o<t.length;o++)if(Ne(t[o-1],t[o],e))return!0;return!1}c(Re,"polylineIntersectsRect");function Ce(t,e,o){return Math.max(e,Math.min(o,t))}c(Ce,"clamp");var F="__grid_root__",jn="edge-label-";function D(t,e,o){let n=new Error(`${t}: ${e}`);return n.code=t,n.details=o,n}c(D,"gridError");var fe={columns:0,rowGap:50,columnGap:50,cellGap:20,containerPadding:20,titleGap:8,horizontalAlign:"center",verticalAlign:"center",curve:"rounded",edgeCornerRadius:5};function ce(t){return!!t.isEdgeLabel}c(ce,"isEdgeLabelNode");function Q(t){return typeof t=="number"&&Number.isFinite(t)&&t>0}c(Q,"isFinitePositiveNumber");var _e=6,wt=12,Wt=2;function Yt(t,e){if(!Q(t.width)||!Q(t.height))return null;let o=e==="H"?t.width:t.height,n=e==="H"?t.height:t.width,r=Math.max(_e,wt),i=Math.max(_e,wt);return{along:o,cross:n,startInset:r,endInset:i,requiredAlongSpan:o+r+i+Wt}}c(Yt,"gridLabelSpanRequirement");function zn(t,e){let o=e==="H"?t.width:t.height;return Q(o)?o+_e*2:0}c(zn,"gridLabelRequiredSegmentLength");var me=_e,re=14,Er=18,Cr=16,Wn=128,Sr=64,Hn=2e6,Qt=wt,ee=1e-6,Mr=7,wr=8,ue=c(t=>V(t,ee),"normalizePolyline");function $n(t,e,o,n){let r=Math.max(Math.min(t,e),Math.min(o,n)),i=Math.min(Math.max(t,e),Math.max(o,n));return Math.max(0,i-r)}c($n,"rangeOverlapLength");function Yn(t){let e=ue([...t]).segments;for(let o=0;o<e.length;o++){let n=e[o];for(let r=o+2;r<e.length;r++){let i=e[r];if(Mt(n,i))return!0;if(n.orientation!==i.orientation||n.orientation==="Z")continue;let a=n.orientation==="H"?Math.abs(n.a.y-i.a.y)<=ee:Math.abs(n.a.x-i.a.x)<=ee,s=n.orientation==="H"?$n(n.a.x,n.b.x,i.a.x,i.b.x):$n(n.a.y,n.b.y,i.a.y,i.b.y);if(a&&s>ee)return!0;let d=n.orientation==="H"?Math.abs(n.a.y-i.a.y):Math.abs(n.a.x-i.a.x);if(!a&&d<Mr&&s>=wr)return!0}}return!1}c(Yn,"polylineHasSelfConflict");var Pt=class extends Error{constructor(o){super(`Could not place label for edge "${o.edgeId}"`);this.details=o;this.name="LabelPlacementSearchExhaustedError"}static{c(this,"LabelPlacementSearchExhaustedError")}},Ot=class extends Error{static{c(this,"LabelIndexWorkLimitError")}constructor(){super("Grid label index work cap exceeded"),this.name="LabelIndexWorkLimitError"}},qt=class{constructor(e,o){this.limit=e;this.metrics=o;this.used=0}static{c(this,"LabelIndexWorkBudget")}charge(e){if(!(e<=0)){if(this.used+e>this.limit)throw new Ot;this.used+=e,U(this.metrics,"indexWorkUnits",e)}}},Qe=class{constructor(e,o){this.workBudget=e;this.metrics=o;this.entriesById=new Map;this.sortedByLeft=[];this.dirty=!1}static{c(this,"CompressedBoundsIndex")}upsert(e){let o=!this.entriesById.has(e.id);this.entriesById.set(e.id,e),this.dirty=!0,o&&this.metrics&&(this.metrics.indexCoordinateCount+=4)}remove(e){this.entriesById.delete(e)&&(this.dirty=!0)}query(e){if(this.dirty){let n=this.entriesById.size;this.workBudget.charge(n*Math.max(1,Math.ceil(Math.log2(n+1)))),this.sortedByLeft=[...this.entriesById.values()].sort((r,i)=>r.bounds.left-i.bounds.left||z(r.id,i.id)),this.dirty=!1}let o=[];for(let n of this.sortedByLeft){if(this.workBudget.charge(1),n.bounds.left>e.right)break;qn(n.bounds,e)&&o.push(n)}return o}};function Kn(t,e){return t.left<e.right&&t.right>e.left&&t.top<e.bottom&&t.bottom>e.top}c(Kn,"rectOverlap");function pt(t,e){let o=t.width??0,n=t.height??0;return{cx:e.x,cy:e.y,left:e.x-o/2,right:e.x+o/2,top:e.y-n/2,bottom:e.y+n/2}}c(pt,"labelRectAt");function Gr(t){let e=t.groupTitleRect;return e?{cx:(e.left+e.right)/2,cy:(e.top+e.bottom)/2,left:e.left,right:e.right,top:e.top,bottom:e.bottom}:null}c(Gr,"groupTitleRect");function U(t,e,o=1){t&&(t[e]+=o)}c(U,"incrementMetric");function vt(t){return{...t}}c(vt,"clonePoint");function je(t){return{left:t.left,right:t.right,top:t.top,bottom:t.bottom}}c(je,"boundsForRect");function Zt(t){return{left:Math.min(t.a.x,t.b.x),right:Math.max(t.a.x,t.b.x),top:Math.min(t.a.y,t.b.y),bottom:Math.max(t.a.y,t.b.y)}}c(Zt,"boundsForSegment");function vr(t){if(t.length===0)return null;let e=t[0].x,o=t[0].x,n=t[0].y,r=t[0].y;for(let i of t.slice(1))e=Math.min(e,i.x),o=Math.max(o,i.x),n=Math.min(n,i.y),r=Math.max(r,i.y);return{left:e,right:o,top:n,bottom:r}}c(vr,"boundsForPoints");function Pr(t,e){return{left:Math.min(t.left,e.left),right:Math.max(t.right,e.right),top:Math.min(t.top,e.top),bottom:Math.max(t.bottom,e.bottom)}}c(Pr,"mergeBounds");function Or(t,e){return{left:t.left-e,right:t.right+e,top:t.top-e,bottom:t.bottom+e}}c(Or,"expandBounds");function qn(t,e){return t.left<=e.right&&t.right>=e.left&&t.top<=e.bottom&&t.bottom>=e.top}c(qn,"boundsOverlap");function Qn(t){return(t.length<=2?t.map((o,n)=>n):[...t.slice(1,-1).map((o,n)=>n+1),0,t.length-1]).map((o,n)=>({segment:t[o],segmentIndex:o,order:n}))}c(Qn,"candidateSegmentsFromSegments");function Zn(t,e){return["start","end"].filter(n=>Vn(t,n)).map(n=>kn(e,n,Qt,7,ee)).filter(n=>n!==null)}c(Zn,"markerClearanceRectsFromPoints");function Jt(t,e){let o=ue(e);return{points:o.points,segments:o.segments,candidateSegments:Qn(o.segments),markerRects:Zn(t,o.points)}}c(Jt,"buildEdgeGeometry");function Ze(t){return t.orientation==="H"?Math.abs(t.b.x-t.a.x):Math.abs(t.b.y-t.a.y)}c(Ze,"segmentLength");function Lr(t,e){let o=[{x:e.left,y:e.top},{x:e.right,y:e.top},{x:e.right,y:e.bottom},{x:e.left,y:e.bottom}];for(let n=0;n<o.length;n++)if(Ne(o[n],o[(n+1)%o.length],t))return!0;return!1}c(Lr,"borderCutsRect");function Jn(t){return Number.isFinite(t.x)&&Number.isFinite(t.y)}c(Jn,"hasFinitePosition");function Le(t,e,o){let n=e?.get(t.id);if(n)return ue(n).points;let r=o?.edgeGeometryById.get(t.id);return r?r.points:ue(t.points??[]).points}c(Le,"pointsForEdge");function en(t,e,o){if(!e?.has(t.id)){let n=o?.edgeGeometryById.get(t.id);if(n)return n.markerRects}return Zn(t,Le(t,e,o))}c(en,"markerClearanceRectsForEdge");function Gt(t,e,o,n){return Math.max(Math.min(t,e),Math.min(o,n))<Math.min(Math.max(t,e),Math.max(o,n))-ee}c(Gt,"rangeOverlaps");function Ar(t,e,o){let n=Ce(Math.min(t.start,t.end),e,o),r=Ce(Math.max(t.start,t.end),e,o);return r-n>ee?{start:n,end:r}:null}c(Ar,"clampInterval");function Br(t,e,o){let n=t.map(i=>Ar(i,e,o)).filter(i=>i!==null).sort((i,a)=>i.start-a.start||i.end-a.end);if(n.length===0)return[];let r=[n[0]];for(let i=1;i<n.length;i++){let a=n[i],s=r[r.length-1];a.start<=s.end+ee?s.end=Math.max(s.end,a.end):r.push(a)}return r}c(Br,"mergeBlockedLabelIntervals");function tn(t,e,o,n){if(e-t<=ee)return[];let r=[],i=t;for(let u of Br(o,t,e))u.start-i>ee&&r.push({start:i,end:u.start}),i=Math.max(i,u.end);e-i>ee&&r.push({start:i,end:e});let a=r.map(u=>({value:Ce(n,u.start,u.end),start:u.start,end:u.end}));a.sort((u,l)=>{let f=Math.abs(u.value-n)-Math.abs(l.value-n);if(Math.abs(f)>ee)return f;let p=l.end-l.start-(u.end-u.start);return Math.abs(p)>ee?p:u.value-l.value});let s=new Set,d=[];for(let u of a){let l=Math.round(u.value*1e6)/1e6;s.has(l)||(s.add(l),d.push(u.value))}return d}c(tn,"centerCandidatesFromIntervals");function Nr(t){return t.isGroup||ce(t)&&!Jn(t)?null:k(t)}c(Nr,"obstacleRectForNode");function _r(t,e){let o=e?.edgeGeometryById.get(t.id);return o?o.candidateSegments:Qn(ue(t.points??[]).segments)}c(_r,"candidateSegments");function eo(t,e,o){let n=t.edgeGeometryById.get(e);n&&n.segments.forEach((r,i)=>t.segmentIndex.remove(`${e}:${i}`)),t.edgeGeometryById.set(e,o),o.segments.forEach((r,i)=>{t.segmentIndex.upsert({id:`${e}:${i}`,edgeId:e,segment:r,bounds:Zt(r)})})}c(eo,"storeEdgeGeometry");function ze(t,e,o){let n=Jt(e,o);e.points=n.points.map(vt),eo(t,e.id,n)}c(ze,"setEdgePoints");function Tr(t,e){t.placedLabelIndex.upsert({id:`label:${e.nodeId}`,nodeId:e.nodeId,rect:e.rect,bounds:je(e.rect)})}c(Tr,"upsertPlacedLabelObstacle");function Dr(t,e,o){let n={edgeById:new Map,edgeGeometryById:new Map,obstacleIndex:new Qe(e,o),placedLabelIndex:new Qe(e,o),groupBorderIndex:new Qe(e,o),segmentIndex:new Qe(e,o),searchBounds:{left:0,right:0,top:0,bottom:0},metrics:o},r=null,i=c(a=>{r=r?Pr(r,a):{...a}},"rememberBounds");for(let a of t.nodes){if(a.isGroup){let u=k(a),l=je(u);n.groupBorderIndex.upsert({id:`border:${a.id}`,rect:u,bounds:l}),i(l);let f=Gr(a);if(f){let p=je(f);n.obstacleIndex.upsert({id:`title:${a.id}`,rect:f,bounds:p}),i(p)}continue}let s=Nr(a);if(!s)continue;let d={id:`${ce(a)?"label":"node"}:${a.id}`,rect:s,bounds:je(s),nodeId:a.id};ce(a)?n.placedLabelIndex.upsert(d):n.obstacleIndex.upsert(d),i(d.bounds)}for(let a of t.edges){n.edgeById.set(a.id,a);let s=Jt(a,a.points??[]);eo(n,a.id,s);let d=vr(s.points);d&&i(d)}return n.searchBounds=Or(r??{left:0,right:0,top:0,bottom:0},128),n}c(Dr,"createEdgeLabelContext");function Je(t,e,o,n={}){U(t.metrics,o);let r=[],i=c(a=>{for(let s of a)s.nodeId&&(s.nodeId===n.excludeNodeId||n.skipNodeIds?.has(s.nodeId))||r.push(s)},"appendEntries");return i(t.obstacleIndex.query(e)),i(t.placedLabelIndex.query(e)),U(t.metrics,"obstacleCandidatesVisited",r.length),r}c(Je,"queryObstacleEntries");function nn(t,e,o,n,r){U(t.metrics,r);let i=n?new Set(n.keys()):void 0,a=[],s=t.segmentIndex.query(e);for(let u of s)u.edgeId===o||i?.has(u.edgeId)||a.push(u);let d=s.length;if(n)for(let[u,l]of n){if(u===o)continue;let f=t.edgeById.get(u);if(!f)continue;Jt(f,l).segments.forEach((h,g)=>{let b=Zt(h);qn(b,e)&&(d+=1,a.push({id:`override:${u}:${g}`,edgeId:u,segment:h,bounds:b}))})}return U(t.metrics,"segmentCandidatesVisited",d),a}c(nn,"queryForeignSegments");function on(t,e,o,n){U(t.metrics,"foreignEdgeLookups");let r=new Set;for(let i of nn(t,je(e),o,n,"segmentRectQueries"))Ne(i.segment.a,i.segment.b,e)&&r.add(i.edgeId);return[...r].sort(z)}c(on,"foreignEdgeIdsIntersectingRect");function to(t,e){return!Q(e.width)||!Q(e.height)?null:t.orientation==="H"?{left:Math.min(t.a.x,t.b.x)-e.width/2,right:Math.max(t.a.x,t.b.x)+e.width/2,top:t.a.y-e.height/2,bottom:t.a.y+e.height/2}:{left:t.a.x-e.width/2,right:t.a.x+e.width/2,top:Math.min(t.a.y,t.b.y)-e.height/2,bottom:Math.max(t.a.y,t.b.y)+e.height/2}}c(to,"obstacleBandQueryBounds");function kr(t,e,o){if(t.orientation==="H"){let r=(e.width??0)+me*2+re;return{left:Math.min(t.a.x,t.b.x)-r,right:Math.max(t.a.x,t.b.x)+r,top:o.top,bottom:o.bottom}}let n=(e.height??0)+me*2+re;return{left:o.left,right:o.right,top:Math.min(t.a.y,t.b.y)-n,bottom:Math.max(t.a.y,t.b.y)+n}}c(kr,"detourLineQueryBounds");function Vr(t,e){return t.orientation==="H"?{left:Math.min(t.a.x,t.b.x)-me,right:Math.max(t.a.x,t.b.x)+me,top:e.top,bottom:e.bottom}:{left:e.left,right:e.right,top:Math.min(t.a.y,t.b.y)-me,bottom:Math.max(t.a.y,t.b.y)+me}}c(Vr,"rerouteLineQueryBounds");function ft(t,e,o,n,r,i){for(let a of Je(n,je(t),"obstacleRectQueries",{excludeNodeId:o}))if(Kn(t,a.rect))return!1;for(let a of n.groupBorderIndex.query(je(t)))if(Lr(t,a.rect))return!1;for(let a of en(e,r,n))if(Kn(t,a))return!1;for(let a of nn(n,je(t),e.id,r,"segmentRectQueries"))if(!i?.has(a.edgeId)&&Ne(a.segment.a,a.segment.b,t))return!1;return!0}c(ft,"rectSafeForLabel");function Fr(t,e){return e.orientation==="Z"?0:zn(t,e.orientation)}c(Fr,"requiredSegmentLength");function rn(t,e,o,n,r,i){if(!Q(e.width)||!Q(e.height))return null;let a=e.width,s=e.height,d=o.orientation==="H"?a/2+me:s/2+me,u=o.orientation==="H"?Math.min(o.a.x,o.b.x)+d:Math.min(o.a.y,o.b.y)+d,l=o.orientation==="H"?Math.max(o.a.x,o.b.x)-d:Math.max(o.a.y,o.b.y)-d;if(l-u<=ee)return null;let f=[],p=to(o,e);if(!p)return null;let h=c(g=>{if(o.orientation==="H"){let y=o.a.y-s/2,R=o.a.y+s/2;Gt(y,R,g.top,g.bottom)&&f.push({start:g.left-a/2,end:g.right+a/2});return}let b=o.a.x-a/2,m=o.a.x+a/2;Gt(b,m,g.left,g.right)&&f.push({start:g.top-s/2,end:g.bottom+s/2})},"addRect");for(let g of Je(n,p,"obstacleBandQueries",{excludeNodeId:e.id}))h(g.rect);for(let g of en(t,r,n))h(g);if(i)for(let g of nn(n,p,t.id,r,"segmentBandQueries")){let b=g.segment;if(o.orientation==="H"){let R=o.a.y-e.height/2,E=o.a.y+e.height/2;b.orientation==="H"?R<b.a.y&&E>b.a.y&&f.push({start:Math.min(b.a.x,b.b.x)-e.width/2,end:Math.max(b.a.x,b.b.x)+e.width/2}):Gt(R,E,Math.min(b.a.y,b.b.y),Math.max(b.a.y,b.b.y))&&f.push({start:b.a.x-e.width/2,end:b.a.x+e.width/2});continue}let m=o.a.x-e.width/2,y=o.a.x+e.width/2;b.orientation==="V"?m<b.a.x&&y>b.a.x&&f.push({start:Math.min(b.a.y,b.b.y)-e.height/2,end:Math.max(b.a.y,b.b.y)+e.height/2}):Gt(m,y,Math.min(b.a.x,b.b.x),Math.max(b.a.x,b.b.x))&&f.push({start:b.a.y-e.height/2,end:b.a.y+e.height/2})}return{low:u,high:l,blocked:f}}c(rn,"blockedCenterIntervals");function jr(t,e,o,n){if(Ze(o)<Fr(e,o))return null;let r=rn(t,e,o,n,void 0,!0);if(!r)return null;let i=o.orientation==="H"?(Math.min(o.a.x,o.b.x)+Math.max(o.a.x,o.b.x))/2:(Math.min(o.a.y,o.b.y)+Math.max(o.a.y,o.b.y))/2,a=tn(r.low,r.high,r.blocked,i);for(let s of a){let d=o.orientation==="H"?{x:s,y:o.a.y}:{x:o.a.x,y:s},u=pt(e,d);if(ft(u,t,e.id,n))return d}return null}c(jr,"placementOnExistingSegment");function zr(t,e,o,n,r){let i=rn(t,e,o,n,void 0,!1);if(!i)return null;let a=o.orientation==="H"?(Math.min(o.a.x,o.b.x)+Math.max(o.a.x,o.b.x))/2:(Math.min(o.a.y,o.b.y)+Math.max(o.a.y,o.b.y))/2,s=tn(i.low,i.high,i.blocked,a);for(let d of s){let u=o.orientation==="H"?{x:d,y:o.a.y}:{x:o.a.x,y:d},l=pt(e,u),f=new Map,p=on(n,l,t.id).map(g=>n.edgeById.get(g)).filter(g=>g!==void 0),h=!0;for(let g of p){let b=mt(g,l,n,r,f);if(!b){h=!1;break}f.set(g.id,b)}if(h&&ft(l,t,e.id,n,f)){for(let[g,b]of f){let m=n.edgeById.get(g);m&&ze(n,m,b)}return u}}return null}c(zr,"placementOnExistingSegmentWithReroutes");function Lt(t,e,o){for(let n of ue(t).segments)for(let r of Je(e,Zt(n),"obstaclePolylineQueries",{skipNodeIds:o}))if(Ne(n.a,n.b,r.rect))return!0;return!1}c(Lt,"polylineHitsProtectedObstacles");function lt(t,e,o){let n=o.get(t);return!n||Fn(e,n.center,ee)}c(lt,"labelStillAnchored");function no(t){let e=[];for(let o=0;o<Cr;o++){let n=t+re+o*Er;e.push(-n,n)}return e}c(no,"detourOffsets");function At(t,e){let o=new Map;for(let n of t){let r=Math.round(n*1e6)/1e6;o.has(r)||o.set(r,n)}return[...o.values()].sort((n,r)=>{let i=Math.abs(n-e)-Math.abs(r-e);return Math.abs(i)>ee?i:n-r})}c(At,"uniqueSortedCandidates");function oo(t,e,o,n){if(!Q(e.width)||!Q(e.height))return[];let r=e.width,i=e.height,a=o.orientation==="H"?o.a.y:o.a.x,s=o.orientation==="H"?i/2:r/2,d=no(s).map(l=>a+l),u=c(l=>{if(o.orientation==="H"){d.push(l.top-s-re),d.push(l.bottom+s+re);return}d.push(l.left-s-re),d.push(l.right+s+re)},"addRect");for(let l of Je(n,kr(o,e,n.searchBounds),"obstacleBandQueries",{excludeNodeId:e.id}))u(l.rect);for(let l of en(t,void 0,n))u(l);return At(d,a).slice(0,Wn)}c(oo,"detourLineCandidates");function Un(t,e,o,n,r){let i=t.orientation==="H"?t.a.y:t.a.x,a=[];for(let l of no(0))t.orientation==="H"?a.push(l<0?e.top+l:e.bottom+l):a.push(l<0?e.left+l:e.right+l);let s=c(l=>{if(t.orientation==="H"){a.push(l.top-re),a.push(l.bottom+re);return}a.push(l.left-re),a.push(l.right+re)},"addRect");for(let l of Je(o,Vr(t,o.searchBounds),"obstacleBandQueries",{skipNodeIds:n}))s(l.rect);let d=At(a,i);return(r?.orientation===t.orientation?d.filter(l=>r.side==="top"?l<e.top:r.side==="bottom"?l>e.bottom:r.side==="left"?l<e.left:l>e.right):d).slice(0,Wn)}c(Un,"rerouteLineCandidates");function Hr(t,e,o,n,r){if(!Q(e.width)||!Q(e.height))return[];let i=e.width,a=e.height,s=o.orientation==="H"?{orientation:"H",a:{x:o.a.x,y:n},b:{x:o.b.x,y:n}}:{orientation:"V",a:{x:n,y:o.a.y},b:{x:n,y:o.b.y}},d=rn(t,e,s,r,void 0,!1);if(!d){let l=o.orientation==="H"?(Math.min(o.a.x,o.b.x)+Math.max(o.a.x,o.b.x))/2:(Math.min(o.a.y,o.b.y)+Math.max(o.a.y,o.b.y))/2,f=[l],p=to(s,e),h=c(g=>{if(o.orientation==="H"){f.push(g.left-re-i/2),f.push(g.right+re+i/2);return}f.push(g.top-re-a/2),f.push(g.bottom+re+a/2)},"addRect");if(p)for(let g of Je(r,p,"obstacleBandQueries",{excludeNodeId:e.id}))h(g.rect);return At(f,l).slice(0,Sr)}let u=o.orientation==="H"?(Math.min(o.a.x,o.b.x)+Math.max(o.a.x,o.b.x))/2:(Math.min(o.a.y,o.b.y)+Math.max(o.a.y,o.b.y))/2;return tn(d.low,d.high,d.blocked,u)}c(Hr,"detourCenterCandidates");function ro(t,e,o){let n=e>=t?1:-1;return e+n*(o+me)}c(ro,"detourReturnCoordinate");var $r={along:c(t=>t.x,"along"),cross:c(t=>t.y,"cross"),point:c((t,e)=>({x:t,y:e}),"point"),labelAlongSize:c(t=>t.width??0,"labelAlongSize"),labelCrossSize:c(t=>t.height??0,"labelCrossSize")},Kr={along:c(t=>t.y,"along"),cross:c(t=>t.x,"cross"),point:c((t,e)=>({x:e,y:t}),"point"),labelAlongSize:c(t=>t.height??0,"labelAlongSize"),labelCrossSize:c(t=>t.width??0,"labelCrossSize")};function io(t){return t.orientation==="H"?$r:Kr}c(io,"detourAxis");function Ur(t,e,o,n,r,i){if(!Q(n.width)||!Q(n.height))return null;let a=t[e],s=t[e+1],d=io(o),u=d.cross(o.a),l=d.along(a),f=d.along(s),p=d.labelAlongSize(n)+me*2,h=r-p/2,g=r+p/2,b=h<Math.min(l,f)-ee,m=g>Math.max(l,f)+ee,y=f>=l?1:-1,R=Math.max(0,Math.min(Qt,Math.abs(f-l)/2-ee)),E=l+y*R,S=f-y*R;if(!b&&!m)return ue([...t.slice(0,e),a,d.point(h,u),d.point(h,i),d.point(g,i),d.point(g,u),s,...t.slice(e+2)]).points;let M=ro(u,i,d.labelCrossSize(n)/2),I=[...t.slice(0,e),a];return b?I.push(d.point(E,u),d.point(E,M),d.point(h,M),d.point(h,i)):I.push(d.point(h,u),d.point(h,i)),I.push(d.point(g,i)),m?I.push(d.point(g,M),d.point(S,M),d.point(S,u)):I.push(d.point(g,u)),I.push(s,...t.slice(e+2)),ue(I).points}c(Ur,"buildLabelDetourPoints");function Xr(t,e,o,n,r,i,a){if(!Q(n.width)||!Q(n.height))return null;let s=t[e],d=t[e+1],u=a?4:2,l=io(o),f=l.cross(o.a),p=l.along(s),h=l.along(d),g=l.labelAlongSize(n)+me*2,b=r-g/2,m=r+g/2,y=h>=p?1:-1,R=Math.max(0,Math.min(Qt,Math.abs(h-p)/u-ee)),E=p+y*R,S=h-y*R,M=ro(f,i,l.labelCrossSize(n)/2);return ue([...t.slice(0,e),s,l.point(E,f),l.point(E,i),l.point(b,i),l.point(m,i),l.point(m,M),l.point(S,M),l.point(S,f),d,...t.slice(e+2)]).points}c(Xr,"buildExtendedLabelDetourPoints");function Wr(t,e){if(!Q(e.width)||!Q(e.height))return[];if(t.orientation==="H"){let r=Math.min(t.a.x,t.b.x),i=Math.max(t.a.x,t.b.x);return[r-e.width/2-re,i+e.width/2+re]}let o=Math.min(t.a.y,t.b.y),n=Math.max(t.a.y,t.b.y);return[o-e.height/2-re,n+e.height/2+re]}c(Wr,"externalDetourCenterCandidates");function ao(t,e){let o=0,n=0;for(let r of ue([...t]).segments)Ne(r.a,r.b,e)&&(o++,r.orientation==="H"?n+=Math.max(0,Math.min(Math.max(r.a.x,r.b.x),e.right)-Math.max(Math.min(r.a.x,r.b.x),e.left)):r.orientation==="V"&&(n+=Math.max(0,Math.min(Math.max(r.a.y,r.b.y),e.bottom)-Math.max(Math.min(r.a.y,r.b.y),e.top))));return{count:o,length:n}}c(ao,"blockedIntersectionScore");function Xn(t,e,o){let n=ao(t,e);return n.count<o.count||n.count===o.count&&n.length<o.length-ee}c(Xn,"reducesBlockedIntersections");function Yr(t,e,o){let n=ue([...t]).segments.find(a=>Ne(a.a,a.b,e));if(!n||n.orientation==="Z")return null;let r=n.orientation==="H"?Math.abs(n.a.y-e.top)<=Math.abs(e.bottom-n.a.y)?"top":"bottom":Math.abs(n.a.x-e.left)<=Math.abs(e.right-n.a.x)?"left":"right",i=o.map(a=>a.edgeId).sort(z);return{key:JSON.stringify([i,n.orientation,r]),orientation:n.orientation,side:r}}c(Yr,"rerouteLane");function qr(t,e,o,n,r,i,a){let s=ue(e),d=ao(s.points,n),u=s.segments[o];if(!u||u.orientation==="Z")return null;let l=s.points[o],f=s.points[o+1],p=i.get(t.id)?.nodeId,h=new Set;if(p&&h.add(p),u.orientation==="H"){let R=Math.min(l.x,f.x),E=Math.max(l.x,f.x),S=Math.max(R,n.left-me),M=Math.min(E,n.right+me);if(M-S<=ee)return U(r.metrics,"rerouteRejectedDegenerateSpan"),null;for(let I of Un(u,n,r,h,a)){U(r.metrics,"rerouteCandidatesEvaluated");let x=ue([...s.points.slice(0,o),l,{x:S,y:u.a.y},{x:S,y:I},{x:M,y:I},{x:M,y:u.a.y},f,...s.points.slice(o+2)]).points;if(Re(x,n)&&!Xn(x,n,d)){U(r.metrics,"rerouteRejectedBlockedRect");continue}if(Re(x,n)&&U(r.metrics,"reroutePartialProgressCandidates"),Lt(x,r,h)){U(r.metrics,"rerouteRejectedProtectedObstacle");continue}if(!lt(t.id,x,i)){U(r.metrics,"rerouteRejectedLabelAnchor");continue}return x}return null}let g=Math.min(l.y,f.y),b=Math.max(l.y,f.y),m=Math.max(g,n.top-me),y=Math.min(b,n.bottom+me);if(y-m<=ee)return U(r.metrics,"rerouteRejectedDegenerateSpan"),null;for(let R of Un(u,n,r,h,a)){U(r.metrics,"rerouteCandidatesEvaluated");let E=ue([...s.points.slice(0,o),l,{x:u.a.x,y:m},{x:R,y:m},{x:R,y},{x:u.a.x,y},f,...s.points.slice(o+2)]).points;if(Re(E,n)&&!Xn(E,n,d)){U(r.metrics,"rerouteRejectedBlockedRect");continue}if(Re(E,n)&&U(r.metrics,"reroutePartialProgressCandidates"),Lt(E,r,h)){U(r.metrics,"rerouteRejectedProtectedObstacle");continue}if(!lt(t.id,E,i)){U(r.metrics,"rerouteRejectedLabelAnchor");continue}return E}return null}c(qr,"rerouteSegmentAroundRect");function mt(t,e,o,n,r,i){let a=Le(t,r,o);for(let s=0;s<8;s++){if(!Re(a,e))return a;let d=ue(a),u=d.points.findIndex((f,p)=>p===d.points.length-1?!1:Ne(f,d.points[p+1],e));if(u<0)return a;let l=qr(t,d.points,u,e,o,n,i);if(!l)return null;a=l}return Re(a,e)?null:a}c(mt,"rerouteForeignEdgeAroundRect");function Qr(t,e,o,n,r){let i=Le(t,r,o),a=i;for(let s=0;s<2;s++){let d=!1;for(let u of e){if(!Re(a,u.rect))continue;U(o.metrics,"individualLabelRerouteAttempts"),r.set(t.id,a);let l=mt(t,u.rect,o,n,r);if(!l)return r.set(t.id,i),null;U(o.metrics,"individualLabelRerouteSuccesses"),a=l,d=!0}if(e.every(u=>!Re(a,u.rect)))return a;if(!d)break}return r.set(t.id,i),null}c(Qr,"rerouteForeignEdgeAroundLabels");function Zr(t,e,o,n,r){if(!Q(e.width)||!Q(e.height))return null;let i=Le(t,void 0,n),a=o.segment;for(let s of oo(t,e,a,n)){let d=At(Wr(a,e),a.orientation==="H"?(Math.min(a.a.x,a.b.x)+Math.max(a.a.x,a.b.x))/2:(Math.min(a.a.y,a.b.y)+Math.max(a.a.y,a.b.y))/2);for(let u of d){let l=Xr(i,o.segmentIndex,a,e,u,s,t.start===t.end);if(!l||t.start===t.end&&Yn(l)||Lt(l,n,new Set([e.id])))continue;let f=a.orientation==="H"?{x:u,y:s}:{x:s,y:u},p=pt(e,f),h=new Map([[t.id,l]]),g=on(n,p,t.id,h).map(m=>n.edgeById.get(m)).filter(m=>m!==void 0),b=!0;for(let m of g){let y=mt(m,p,n,r,h);if(!y){b=!1;break}h.set(m.id,y)}if(b&&ft(p,t,e.id,n,h)){ze(n,t,l);for(let[m,y]of h){if(m===t.id)continue;let R=n.edgeById.get(m);R&&ze(n,R,y)}return f}}}return null}c(Zr,"labelPlacementWithExtendedDetour");function Jr(t,e,o,n,r){if(!Q(e.width)||!Q(e.height))return null;let i=Le(t,void 0,n),a=o.segment;for(let s of oo(t,e,a,n)){let d=Hr(t,e,a,s,n);for(let u of d){let l=Ur(i,o.segmentIndex,a,e,u,s);if(!l||t.start===t.end&&Yn(l)||Lt(l,n,new Set([e.id])))continue;let f=a.orientation==="H"?{x:u,y:s}:{x:s,y:u},p=pt(e,f),h=new Map([[t.id,l]]),g=on(n,p,t.id,h).map(m=>n.edgeById.get(m)).filter(m=>m!==void 0),b=!0;for(let m of g){let y=mt(m,p,n,r,h);if(!y){b=!1;break}h.set(m.id,y)}if(b&&ft(p,t,e.id,n,h)){ze(n,t,l);for(let[m,y]of h){if(m===t.id)continue;let R=n.edgeById.get(m);R&&ze(n,R,y)}return f}}}return null}c(Jr,"labelPlacementWithDetour");function ei(t){return!!(t&&ce(t))}c(ei,"isPreparedLabelNode");function so(t,e,o){let n=t.points?.find(s=>Number.isFinite(s.x)&&Number.isFinite(s.y)),r=t.start?o.get(t.start):void 0,i=t.end?o.get(t.end):void 0,a=n??(r&&Jn(r)?r:void 0)??i;if(a&&Number.isFinite(a.x)&&Number.isFinite(a.y)){e.x=a.x,e.y=a.y;return}delete e.x,delete e.y}c(so,"parkDegenerateLabel");function ti(t,e,o){let n;for(let r of ue(t.points??[]).segments)r.orientation!=="Z"&&(!n||Ze(r)>Ze(n))&&(n=r);if(!n){so(t,e,o);return}e.x=(n.a.x+n.b.x)/2,e.y=(n.a.y+n.b.y)/2}c(ti,"placeLabelAtRouteMidpoint");function ni(t){return`${jn}${t.start??""}-${t.end??""}-${t.id}`}c(ni,"preferredGridLabelNodeId");function oi(t,e){let o=t.labelNodeId??ni(t),n=o,r=1;for(;;){if(!e.has(n))return n;n=`${o}-${r}`,r+=1}}c(oi,"ensureUniqueGridLabelNodeId");function co(t){let e=new Map;for(let n of t.nodes)e.set(n.id,n);let o=[];for(let n of t.edges){if(!n.label||n.label.length===0)continue;let r=n.start?e.get(n.start):void 0,i=n.labelNodeId?e.get(n.labelNodeId):void 0;if(ei(i)){i.label=n.label,i.labelStyle=Array.isArray(n.labelStyle)?n.labelStyle[0]:n.labelStyle??"",i.edgeStart=n.start??"",i.edgeEnd=n.end??"",r?.dir&&(i.dir=r.dir),n.label=void 0,n.text=void 0;continue}let a=oi(n,e),s={id:a,label:n.label,edgeStart:n.start??"",edgeEnd:n.end??"",shape:"labelRect",width:0,height:0,isEdgeLabel:!0,isDummy:!0,isGroup:!1,parentId:void 0,labelStyle:Array.isArray(n.labelStyle)?n.labelStyle[0]:n.labelStyle??"",...r?.dir?{dir:r.dir}:{}};o.push(s),e.set(a,s),n.labelNodeId=a,n.label=void 0,n.text=void 0}t.nodes.push(...o)}c(co,"prepareGridLayout");function uo(t,e,o,n={}){let r=new Map;for(let y of t.nodes)r.set(y.id,y);let i=t.nodes.filter(ce),a=new Map(i.map(y=>[y.id,{x:y.x,y:y.y}]));for(let y of i)delete y.x,delete y.y;let s=t.edges.map((y,R)=>{if(!y.labelNodeId)return null;let E=r.get(y.labelNodeId);return E?!Q(E.width)||!Q(E.height)?(U(e,"unmeasuredLabelsSkipped"),null):{edge:y,labelNode:E,sourceIndex:R}:null}).filter(y=>y!==null);if(s.length===0)return;let d=new Map(t.edges.map(y=>[y.id,y.points?.map(R=>({...R}))])),u=c(()=>{for(let y of t.edges){let R=d.get(y.id);R?y.points=R.map(E=>({...E})):delete y.points}},"restoreBaseEdgePoints"),l=c(()=>{u();for(let y of i){let R=a.get(y.id);R&&Number.isFinite(R.x)&&Number.isFinite(R.y)?(y.x=R.x,y.y=R.y):(delete y.x,delete y.y)}},"restoreBaseState"),f=s.filter(({edge:y,labelNode:R})=>ue(y.points??[]).segments.length>0?!0:(so(y,R,r),U(e,"degenerateLabelRoutesSkipped"),!1)).sort((y,R)=>y.sourceIndex-R.sourceIndex||z(y.edge.id,R.edge.id));if(f.length===0)return;let p=c(()=>{u();for(let{edge:y,labelNode:R}of f)ti(y,R,r)},"placeLabelsAtRouteMidpoints"),h=n.maxIndexWork??Hn,g=Number.isFinite(h)&&h>=0?Math.floor(h):Hn,b=new qt(g,e),m=[];for(let y=1;y<=2;y++){U(e,"labelPasses");let R=Dr(t,b,e),E=new Map,S=new Map,M=new Map(t.edges.map(x=>[x.id,Le(x,void 0,R).map(vt)])),I=new Map;try{for(let{edge:C,labelNode:v}of f){let A=[..._r(C,R)].sort((N,W)=>{let q=Ze(W.segment)-Ze(N.segment);return Math.abs(q)>ee?q:N.order-W.order}),O=null;for(let N of A)if(O=jr(C,v,N.segment,R),O)break;if(!O){for(let N of A)if(O=zr(C,v,N.segment,R,E),O)break}if(y===2&&!O){for(let N of A)if(O=Jr(C,v,N,R,E),O)break}if(y===2&&!O){for(let N of A)if(O=Zr(C,v,N,R,E),O)break}if(!O)throw new Pt({edgeId:C.id,labelNodeId:v.id,labelWidth:v.width,labelHeight:v.height,pointCount:C.points?.length??0,points:C.points?.map(N=>({...N}))??[],segments:A.map(({segment:N})=>({orientation:N.orientation,length:Ze(N),a:{...N.a},b:{...N.b}}))});v.x=O.x,v.y=O.y;let _={edgeId:C.id,nodeId:v.id,center:O,rect:pt(v,O)};E.set(C.id,_),Tr(R,_)}let x=[...E.values()];U(e,"frozenReservations",x.length),o&&(o.labelOverlayBuilds++,o.labelOverlayVertices+=x.length*4);for(let C of t.edges){let v=M.get(C.id)??[],A=Le(C,void 0,R);JSON.stringify(A)!==JSON.stringify(v)&&I.set(C.id,A.map(vt)),ze(R,C,v.map(vt))}for(let[C,v]of I){if(!E.has(C))continue;let A=R.edgeById.get(C);A&&ze(R,A,v)}let w=new Set(I.keys());for(let C of t.edges){let v=E.get(C.id),A=Le(C,void 0,R);for(let O of x)O.edgeId!==C.id&&Re(A,O.rect)&&w.add(C.id);v&&!lt(C.id,A,E)&&w.add(C.id)}let G=new Map;for(let C of t.edges){if(!w.has(C.id))continue;let v=Le(C,void 0,R),A=new Map([[C.id,v]]),O=x.filter(_=>_.edgeId!==C.id&&Re(v,_.rect));if(O.length>0){let _=O.slice(1).reduce((ne,Z)=>({cx:(Math.min(ne.left,Z.rect.left)+Math.max(ne.right,Z.rect.right))/2,cy:(Math.min(ne.top,Z.rect.top)+Math.max(ne.bottom,Z.rect.bottom))/2,left:Math.min(ne.left,Z.rect.left),right:Math.max(ne.right,Z.rect.right),top:Math.min(ne.top,Z.rect.top),bottom:Math.max(ne.bottom,Z.rect.bottom)}),{...O[0].rect}),N=Yr(v,_,O),W=N?G.get(N.key)??0:0,q=(W+2)*12,H={cx:_.cx,cy:_.cy,left:_.left-q,right:_.right+q,top:_.top-q,bottom:_.bottom+q},Y=mt(C,H,R,E,A,N??void 0),be=Y!==null;if(Y??=Qr(C,O,R,E,A),Y)v=Y,A.set(C.id,v),N&&be&&G.set(N.key,W+1);else{U(e,"labelOverlapFallbacks");for(let ne of O){if(!Re(v,ne.rect))continue;let Z=S.get(ne.edgeId)??new Set;Z.has(C.id)||U(e,"labelOverlapReservations"),Z.add(C.id),S.set(ne.edgeId,Z)}}}if(!lt(C.id,v,E))throw D("GRID_ROUTE_NOT_FOUND",`Label anchor was lost for edge "${C.id}"`,{edgeId:C.id,pass:y});E.has(C.id)&&U(e,"preservedAnchors"),ze(R,C,v),U(e,"impactedEdgeReroutes")}for(let{edge:C,labelNode:v}of f){let A=E.get(C.id);if(!A||!lt(C.id,Le(C,void 0,R),E)||!ft(A.rect,C,v.id,R,void 0,S.get(C.id)))throw D("GRID_ROUTE_NOT_FOUND",`Label pass ${y} did not validate`,{edgeId:C.id,pass:y})}return}catch(x){if(x instanceof Ot){p(),U(e,"indexWorkLimitFallbacks"),o&&(o.resourceLimitFallbacks++,o.fallbackReasons.label_index_work_cap++);return}if(!(x instanceof Pt))throw l(),U(e,"rollbacks"),x;m.push(x),u();for(let{labelNode:w}of f)delete w.x,delete w.y}}p(),U(e,"placementSearchFallbacks"),o&&o.fallbackReasons.label_placement_exhausted++,Be.warn("[grid]","Label placement search exhausted; using route midpoints. Labels may overlap.",{reason:"label_placement_exhausted",edgeIds:f.map(({edge:y})=>y.id),failures:m.map(({details:y})=>y)})}c(uo,"positionGridEdgeLabels");function an(t){return t.isGroup?t:void 0}c(an,"asGroup");function lo(t){let e=new Map,o=new Map,n=new Set;for(let u of t){if(u.id===F)throw D("GRID_INVALID_CONTAINMENT",`Node id "${F}" is reserved for the grid layout root`,{traversedIds:[u.id]});if(e.has(u.id))throw D("GRID_INVALID_CONTAINMENT",`Duplicate node id "${u.id}"`,{traversedIds:[u.id]});if(e.set(u.id,u),ce(u)){n.add(u.id);continue}let l=an(u);l&&o.set(l.id,l)}let r=new Map;r.set(F,[]);for(let u of t){if(n.has(u.id))continue;let l=u.parentId??F;if(l!==F&&!o.get(l))throw D("GRID_INVALID_CONTAINMENT",`Missing parent "${l}" for "${u.id}"`,{traversedIds:[u.id,l]});r.has(l)||r.set(l,[]),r.get(l).push(u)}let i=new Map,a=[],s=c(u=>(r.get(u)??[]).map(l=>an(l)).filter(l=>l!==void 0),"directChildGroups"),d=c(u=>{let l=i.get(u)??0;if(l===1)throw D("GRID_INVALID_CONTAINMENT",`Containment cycle at "${u}"`,{traversedIds:[u]});if(l===2)return;i.set(u,1);let f=[{groupId:u,childGroups:s(u),nextChildIndex:0}];for(;f.length>0;){let p=f[f.length-1],h=p.childGroups[p.nextChildIndex];if(h){p.nextChildIndex+=1;let b=i.get(h.id)??0;if(b===1)throw D("GRID_INVALID_CONTAINMENT",`Containment cycle at "${h.id}"`,{traversedIds:[...f.map(m=>m.groupId),h.id]});if(b===2)continue;i.set(h.id,1),f.push({groupId:h.id,childGroups:s(h.id),nextChildIndex:0});continue}f.pop(),i.set(p.groupId,2);let g=o.get(p.groupId);g&&a.push(g)}},"visit");for(let u of r.get(F)??[]){let l=an(u);l&&d(l.id)}for(let u of o.keys())(i.get(u)??0)===0&&d(u);return{nodeById:e,groupById:o,childrenByParent:r,rootChildren:r.get(F)??[],postOrderGroups:a,helperNodeIds:n}}c(lo,"buildGridForest");function Te(t,e,o){let n=e,r=new Set;for(;n?.parentId;){if(n.parentId===t)return!0;if(r.has(n.parentId))return!1;r.add(n.parentId),n=o.get(n.parentId)}return!1}c(Te,"isAncestorGroup");function go(t,e,o){let n=new Map,r=new Map;for(let i of t.edges){if(!i.start||!i.end||!i.labelNodeId)continue;o&&o.labelSpacingEdgesExamined++;let a=e.nodeById.get(i.labelNodeId),s=e.nodeById.get(i.start),d=e.nodeById.get(i.end);if(!a||!s||!d)continue;if(i.start===i.end){let p=Yt(a,"H");p&&r.set(i.id,p);continue}let u=s.parentId??F,l=d.parentId??F;if(u!==l)continue;let f=n.get(u);f?f.push({edge:i,labelNode:a}):n.set(u,[{edge:i,labelNode:a}])}return{byContainer:n,selfLoops:r}}c(go,"buildGridLabelRequirements");function ri(t,e,o){let n=new Map,r=0;for(let i of t)n.set(i,r),r+=(e.get(i)??0)+o;return n}c(ri,"trackOrigins");function po(t,e,o,n,r){let i=ri(o,n,r),a=i.get(t),s=n.get(t);if(a===void 0||s===void 0)return!1;let d=a+s/2,u=d-e/2,l=d+e/2;return o.every(f=>{if(f===t)return!0;let p=i.get(f),h=p+(n.get(f)??0);return l<=p||u>=h})}c(po,"crossAxisFits");function ii(t){let e=new Map;for(let o of t.values())for(let n of o.items)e.set(n.item.id,n);return e}c(ii,"placementByNodeId");function fo(t,e,o){let n=Math.min(t,e),r=Math.max(t,e),i=o.indexOf(n);return i>=0&&o[i+1]===r}c(fo,"areConsecutive");function mo(t,e,o){t.set(e,Math.max(t.get(e)??0,o))}c(mo,"addGapRequirement");function ho(t,e,o,n,r,i,a,s,d){let u=new Map,l=new Map,f=ii(e),p=c((h,g)=>Yt(h.labelNode,g),"requirementFor");for(let h of t){let g=h.edge.start?f.get(h.edge.start):void 0,b=h.edge.end?f.get(h.edge.end):void 0;if(!(!g||!b)){if(g.row===b.row&&fo(g.column,b.column,i)){let m=p(h,"H");m&&po(g.row,m.cross,r,o,a)&&(d&&d.labelSpacingEligibleEdges++,mo(l,Math.min(g.column,b.column),m.requiredAlongSpan));continue}if(g.column===b.column&&fo(g.row,b.row,r)){let m=p(h,"V");m&&po(g.column,m.cross,i,n,s)&&(d&&d.labelSpacingEligibleEdges++,mo(u,Math.min(g.row,b.row),m.requiredAlongSpan))}}}return{rowGapAfter:u,columnGapAfter:l}}c(ho,"deriveGridContainerLabelGaps");var sn="[grid]",ai=new Set(["basis","bumpX","bumpY","cardinal","catmullRom","linear","monotoneX","monotoneY","natural","step","stepAfter","stepBefore","rounded"]);function dn(t){return t!==null&&typeof t=="object"&&!Array.isArray(t)}c(dn,"isObjectRecord");function yo(t){if(!dn(t))return{};let e={};return Object.hasOwn(t,"row")&&(e.row=t.row),Object.hasOwn(t,"column")&&(e.column=t.column),Kt(t.horizontalAlign)&&(e.horizontalAlign=t.horizontalAlign),Ut(t.verticalAlign)&&(e.verticalAlign=t.verticalAlign),e}c(yo,"ownPlacementFrom");function bo(t){let e=new Map,o=0;for(let n of t)e.has(n.id)||e.set(n.id,o++);return e}c(bo,"buildGridSourceOrder");function xo(t){let e=dn(t.config?.grid)?t.config.grid:{},o=dn(e.placements)?e.placements:{},n=new Map;for(let r of Object.keys(o)){let i=o[r],a=Array.isArray(i)?i:[i];n.set(r,a.map(s=>yo(s)))}return{placements:n,columns:typeof e.columns=="number"&&Number.isFinite(e.columns)&&e.columns>=0?Math.trunc(e.columns):fe.columns,rowGap:typeof e.rowGap=="number"&&Number.isFinite(e.rowGap)&&e.rowGap>=0?e.rowGap:fe.rowGap,columnGap:typeof e.columnGap=="number"&&Number.isFinite(e.columnGap)&&e.columnGap>=0?e.columnGap:fe.columnGap,cellGap:typeof e.cellGap=="number"&&Number.isFinite(e.cellGap)&&e.cellGap>=0?e.cellGap:fe.cellGap,containerPadding:typeof e.containerPadding=="number"&&Number.isFinite(e.containerPadding)&&e.containerPadding>=0?e.containerPadding:fe.containerPadding,titleGap:typeof e.titleGap=="number"&&Number.isFinite(e.titleGap)&&e.titleGap>=0?e.titleGap:fe.titleGap,horizontalAlign:Kt(e.horizontalAlign)?e.horizontalAlign:fe.horizontalAlign,verticalAlign:Ut(e.verticalAlign)?e.verticalAlign:fe.verticalAlign,curve:typeof e.curve=="string"&&ai.has(e.curve)?e.curve:fe.curve,edgeCornerRadius:Nn(e.edgeCornerRadius)}}c(xo,"readGridConfig");function Ro(t,e){let o=new Map;for(let n of t){let r=n.placementId??n.id;o.set(r,(o.get(r)??0)+1)}for(let[n,r]of e.placements){let i=o.get(n)??0;if(i===0){Be.warn(sn,`Ignoring grid placement for unknown target "${n}"`);continue}r.length>i&&Be.warn(sn,`Ignoring ${r.length-i} extra grid placement occurrence(s) for target "${n}"; found ${i} matching node(s)`)}}c(Ro,"validateGridPlacementMap");function si(t){return(e,o)=>{let n=t.get(e.id)??Number.MAX_SAFE_INTEGER,r=t.get(o.id)??Number.MAX_SAFE_INTEGER;return n!==r?n-r:z(e.id,o.id)}}c(si,"itemComparator");function di(t,e,o,n){let r=t.placementId??t.id,i=o.placements.get(r)?.[n]??{},a=yo(t.metadata);for(let u of["row","column","horizontalAlign","verticalAlign"])Object.hasOwn(i,u)&&Object.hasOwn(a,u)&&i[u]!==a[u]&&Be.debug(sn,"metadata overrides placement map",{nodeId:t.id,field:u,metadata:a[u],config:i[u]});let s=Object.hasOwn(a,"row")?a.row:i.row,d=Object.hasOwn(a,"column")?a.column:i.column;return Xt(r,"row",s,t.id),Xt(r,"column",d,t.id),{item:t,row:s??0,column:d??0,horizontalAlign:a.horizontalAlign??i.horizontalAlign??o.horizontalAlign,verticalAlign:a.verticalAlign??i.verticalAlign??o.verticalAlign,sourceOrder:e.get(t.id)??Number.MAX_SAFE_INTEGER,explicitRow:s!==void 0,explicitColumn:d!==void 0,explicitCell:s!==void 0&&d!==void 0}}c(di,"resolveItemPlacement");function ci(t,e,o){let n=o;for(;e.has(`${t}:${n}`);)n++;return n}c(ci,"nextFreeColumn");function ui(t,e,o){let n=o;for(;e.has(`${n}:${t}`);)n++;return n}c(ui,"nextFreeRow");function li(t,e,o){let n=o;for(;;){let r=Math.floor(n/e)+1,i=n%e+1;if(n++,!t.has(`${r}:${i}`))return{row:r,column:i,nextIndex:n}}}c(li,"nextAutoCell");function Io(t,e,o){let n=[...t].sort(si(e)),r=new Map,i=n.map(p=>{let h=p.placementId??p.id,g=r.get(h)??0;return r.set(h,g+1),di(p,e,o,g)}),a=new Map,s=new Set;for(let p of i){if(!p.explicitCell)continue;let h=`${p.row}:${p.column}`;s.add(h);let g=a.get(h);if(g){if(g.verticalAlign!==p.verticalAlign)throw D("GRID_CELL_ALIGNMENT_CONFLICT",`Conflicting vertical alignment in cell (${p.row}, ${p.column})`,{containerId:p.item.parentId??"root",row:p.row,column:p.column,itemIds:[...g.items.map(b=>b.item.id),p.item.id],values:[...g.items.map(b=>b.verticalAlign),p.verticalAlign]});g.items.push(p)}else a.set(h,{row:p.row,column:p.column,items:[p],width:0,height:0,verticalAlign:p.verticalAlign})}let d=o.columns>0?o.columns:Math.max(1,Math.ceil(Math.sqrt(n.length))),u=0,l=new Map,f=new Map;for(let p of i){if(p.explicitCell)continue;if(p.explicitRow){let g=ci(p.row,s,l.get(p.row)??1);p.column=g,l.set(p.row,g+1)}else if(p.explicitColumn){let g=ui(p.column,s,f.get(p.column)??1);p.row=g,f.set(p.column,g+1)}else{let g=li(s,d,u);p.row=g.row,p.column=g.column,u=g.nextIndex}let h=`${p.row}:${p.column}`;s.add(h),a.set(h,{row:p.row,column:p.column,items:[p],width:0,height:0,verticalAlign:p.verticalAlign})}return{placements:i,cells:a}}c(Io,"resolveGridPlacements");var le=class extends Error{constructor(o,n,r){super(n);this.reason=o;this.searchStateScope=r;this.name="GridRoutingResourceLimitError"}static{c(this,"GridRoutingResourceLimitError")}};var j=6,$e=24,pi=5e4,fi=4,Ae=64*1024*1024,cn=class{constructor(e,o){this.bands=e;this.strictBoundaryIntervals=o;this.byLow=new Map;this.byHigh=new Map;this.coordinateCount=new Set(e.flatMap(({low:r,high:i})=>[r,i])).size;let n=e.reduce((r,i)=>r+i.intervals.length,0);for(let r of o.values())n+=r.length;this.intervalCount=n;for(let r of e)this.byLow.set(r.low,r),this.byHigh.set(r.high,r);Object.freeze(this.bands),Object.freeze(this)}static{c(this,"CompressedIntervalIndex")}containingBand(e){let o=0,n=this.bands.length-1;for(;o<=n;){let r=o+n>>>1,i=this.bands[r];if(e<=i.low)n=r-1;else if(e>=i.high)o=r+1;else return i}}overlaps(e,o,n){let r=0,i=e.length;for(;r<i;){let a=r+i>>>1;e[a][1]<=o?r=a+1:i=a}return r<e.length&&e[r][0]<n}intersects(e,o,n){let r=Math.min(o,n),i=Math.max(o,n),a=this.byHigh.get(e),s=this.byLow.get(e),d=a||s?void 0:this.containingBand(e);if(d)return this.overlaps(d.intervals,r,i);if(!a||!s)return!1;let u=0,l=0;for(;u<a.intervals.length&&l<s.intervals.length;){let f=Math.max(r,a.intervals[u][0],s.intervals[l][0]),p=Math.min(i,a.intervals[u][1],s.intervals[l][1]);if(f<p)return!0;a.intervals[u][1]<s.intervals[l][1]?u++:l++}return!1}contains(e,o){let n=this.strictBoundaryIntervals.get(e)??this.containingBand(e)?.intervals;if(!n)return!1;let r=0,i=n.length;for(;r<i;){let a=r+i>>>1;n[a][0]<o?r=a+1:i=a}return r>0&&o<n[r-1][1]}nearestBoundary(e,o,n){let r=this.strictBoundaryIntervals.get(e)??this.containingBand(e)?.intervals;if(!r)return;if(n<0){let s=0,d=r.length;for(;s<d;){let u=s+d>>>1;r[u][1]<=o?s=u+1:d=u}return s>0?r[s-1][1]:void 0}let i=0,a=r.length;for(;i<a;){let s=i+a>>>1;r[s][0]<o?i=s+1:a=s}return i<r.length?r[i][0]:void 0}},Go,et=class{constructor(e){this.backing=e;this[Go]="ImmutableMap";Object.freeze(this)}static{c(this,"ImmutableMap")}get size(){return this.backing.size}entries(){return this.backing.entries()}forEach(e,o){for(let[n,r]of this.backing)e.call(o,r,n,this)}get(e){return this.backing.get(e)}has(e){return this.backing.has(e)}keys(){return this.backing.keys()}values(){return this.backing.values()}[(Go=Symbol.toStringTag,Symbol.iterator)](){return this.entries()}},vo,Bt=class{constructor(e,o){this.base=e;this.overrides=o;this[vo]="OverlayMap"}static{c(this,"OverlayMap")}get size(){let e=0;for(let o of this.overrides.keys())this.base.has(o)||e++;return this.base.size+e}materialize(){return new Map([...this.base,...this.overrides])}entries(){return this.materialize().entries()}forEach(e,o){for(let[n,r]of this)e.call(o,r,n,this)}get(e){return this.overrides.get(e)??this.base.get(e)}has(e){return this.overrides.has(e)||this.base.has(e)}keys(){return this.materialize().keys()}values(){return this.materialize().values()}[(vo=Symbol.toStringTag,Symbol.iterator)](){return this.entries()}};function mi(t,e){return new Proxy([],{get(o,n){if(n==="length")return t.length+e.length;if(n===Symbol.iterator)return function*(){yield*t,yield*e};if(typeof n=="string"&&/^\d+$/.test(n)){let r=Number(n);return r<t.length?t[r]:e[r-t.length]}return Reflect.get(Array.prototype,n)}})}c(mi,"overlayArray");function Eo(t,e,o){return new Proxy([],{get(n,r){if(r==="length")return o;if(r===Symbol.iterator)return function*(){for(let i=0;i<o;i++)yield e.get(i)??t[i]};if(typeof r=="string"&&/^\d+$/.test(r)){let i=Number(r);return e.get(i)??t[i]}return Reflect.get(Array.prototype,r)}})}c(Eo,"overrideArray");var gt=class{constructor(e){this.base=e;this.addedVertices=[];this.adjacencyOverrides=new Map;this.searchAdjacencyOverrides=new Map;this.pointOverrides=new Map;this.undoLog=[];this.resetCount=0}static{c(this,"EndpointOverlayScratch")}reset(e){if(e!==this.base)throw new Error("Endpoint overlay scratch cannot be shared across base topologies");for(let o of this.undoLog)this.adjacencyOverrides.delete(o),this.searchAdjacencyOverrides.delete(o);this.undoLog.length=0,this.addedVertices.length=0,this.pointOverrides.clear(),this.resetCount++}setArcs(e,o){this.adjacencyOverrides.has(e)||this.undoLog.push(e),this.adjacencyOverrides.set(e,o),this.searchAdjacencyOverrides.set(e,Lo(o))}};function He(t){return Object.is(t,-0)?0:t}c(He,"normalizeCoordinate");function Nt(t){if(![t.left,t.right,t.top,t.bottom].every(Number.isFinite))throw new Error("Grid routing geometry must contain only finite coordinates");let o=He(Math.min(t.left,t.right)),n=He(Math.max(t.left,t.right)),r=He(Math.min(t.top,t.bottom)),i=He(Math.max(t.top,t.bottom));if(o===n||r===i)throw new Error("Grid routing rectangles must have positive area");return{left:o,right:n,top:r,bottom:i}}c(Nt,"normalizeRect");function gi(t,e){let o=Nt(t.bounds),n={id:t.id,left:Math.max(e.left,o.left-j),right:Math.min(e.right,o.right+j),top:Math.max(e.top,o.top-j),bottom:Math.min(e.bottom,o.bottom+j)};return n.left<n.right&&n.top<n.bottom?n:void 0}c(gi,"inflateAndClip");function Po(t){let e=[...t].sort((n,r)=>n.low-r.low||n.high-r.high||z(n.id,r.id)),o=[];for(let n of e){let r=o.at(-1);if(!r||n.low>r.high){o.push({...n});continue}r.high=Math.max(r.high,n.high),z(n.id,r.id)<0&&(r.id=n.id)}return o}c(Po,"mergeObstacleIntervals");function hi(t){let e=new Map;for(let a of t){let s=e.get(a.left)??{starts:[],ends:[]};s.starts.push(a),e.set(a.left,s);let d=e.get(a.right)??{starts:[],ends:[]};d.ends.push(a),e.set(a.right,d)}let o=[...e.keys()].sort((a,s)=>a-s),n=new Set,r=[],i=new Map;for(let a=0;a<o.length-1;a++){let s=o[a],d=o[a+1],u=e.get(s);for(let f of u.ends)n.delete(f);for(let f of u.starts)n.add(f);let l=new Map;for(let f of Po([...n].map(p=>({low:p.top,high:p.bottom,id:p.id})))){let p=`${f.low}:${f.high}`,h=i.get(p);if(h)h.right=d,z(f.id,h.id)<0&&(h.id=f.id);else{let g={id:f.id,left:s,right:d,top:f.low,bottom:f.high};r.push(g),l.set(p,g);continue}l.set(p,h)}i=l}return r.sort((a,s)=>a.top-s.top||a.left-s.left||a.bottom-s.bottom||a.right-s.right||z(a.id,s.id))}c(hi,"unionObstacles");function ge(t){return`${He(t.x)}:${He(t.y)}`}c(ge,"routingPointKey");function _t(t){return t==="left"?0:t==="right"?1:t==="top"?2:t==="bottom"?3:4}c(_t,"topologySideOrdinal");function ln(t,e){return z(t.kind,e.kind)||_t(t.side)-_t(e.side)||t.point.x-e.point.x||t.point.y-e.point.y||z(t.ownerId??"",e.ownerId??"")}c(ln,"vertexRecordOrder");function pn(t){let e=new Map;for(let o of[...t].sort(ln)){let n={...o,point:{x:He(o.point.x),y:He(o.point.y)}},r=ge(n.point);e.has(r)||e.set(r,n)}return[...e.values()]}c(pn,"canonicalRecords");function Co(t,e,o="corner"){return[{point:{x:t.left,y:t.top},kind:o,ownerId:e},{point:{x:t.right,y:t.top},kind:o,ownerId:e},{point:{x:t.left,y:t.bottom},kind:o,ownerId:e},{point:{x:t.right,y:t.bottom},kind:o,ownerId:e}]}c(Co,"cornerRecords");function yi(t,e){let o=t.coordinate??(t.side==="left"?e.left:t.side==="right"?e.right:t.side==="top"?e.top:e.bottom);return(t.side==="left"||t.side==="right"?[{x:o,y:t.low},{x:o,y:t.high}]:[{x:t.low,y:o},{x:t.high,y:o}]).map(r=>({point:r,kind:"portal",ownerId:t.ownerId,side:t.side}))}c(yi,"portalRecords");function Oo(t,e,o,n,r){if(e==="left"||e==="right")return{x:n.nearestBoundary(t.y,t.x,e==="left"?-1:1)??(e==="left"?o.left:o.right),y:t.y};let i=r.nearestBoundary(t.x,t.y,e==="top"?-1:1)??(e==="top"?o.top:o.bottom);return{x:t.x,y:i}}c(Oo,"rayHit");function So(t,e,o){let n=[...new Set(t.flatMap(f=>e==="H"?[f.top,f.bottom]:[f.left,f.right]))].sort((f,p)=>f-p),r=[],i=new Map,a=new Map,s=new Map;for(let f of t){let p=e==="H"?f.top:f.left,h=e==="H"?f.bottom:f.right,g=a.get(p)??[];g.push(f),a.set(p,g);let b=s.get(h)??[];b.push(f),s.set(h,b)}let d=new Set,u=c(()=>Po([...d].map(f=>e==="H"?{low:f.left,high:f.right,id:f.id}:{low:f.top,high:f.bottom,id:f.id})),"activeIntervals"),l=c(f=>{let p=o.intervalCount+f.length;return Ke(un(0,0,t.length,p),o.maxEstimatedBytes,"estimated_memory_cap"),o.intervalCount=p,Object.freeze(f.map(h=>Object.freeze([h.low,h.high])))},"freezeIntervals");for(let f=0;f<n.length;f++){let p=n[f];for(let b of s.get(p)??[])d.delete(b);i.set(p,l(u()));for(let b of a.get(p)??[])d.add(b);let h=n[f+1];if(h===void 0)continue;let g=u();g.length>0&&r.push({low:p,high:h,intervals:l(g)})}return new cn(Object.freeze(r),new et(i))}c(So,"createIntervalIndex");function un(t,e,o,n){return t*64+e*56+o*48+n*24}c(un,"estimatedTopologyBytes");function Ke(t,e,o){if(t>e)throw new le(o,`Grid routing ${o} exceeded: ${t} > ${e}`)}c(Ke,"enforceCap");function Mo(t,e){let o=new Map;for(let n of t){let r=e==="H"?n.point.y:n.point.x,i=o.get(r)??[];i.push(n),o.set(r,i)}for(let n of o.values())n.sort((r,i)=>e==="H"?r.point.x-i.point.x||r.id-i.id:r.point.y-i.point.y||r.id-i.id),Object.freeze(n);return new et(o)}c(Mo,"indexVertexLines");function Lo(t){return Object.freeze(t.map(e=>Object.freeze({to:e.to,orientationOrdinal:e.orientation==="H"?1:2,length:e.length,boundaryTransitions:e.kind==="portal"?1:0})))}c(Lo,"searchArcs");function bi(t,e,o,n){let r=pn(t).sort(ln),i=n.maxVertices??pi;Ke(r.length,i,"vertex_cap");let a=r.map((p,h)=>Object.freeze({id:h,...p,point:Object.freeze({...p.point})})),s=new Map(a.map(p=>[p.id,[]])),d=c((p,h,g)=>{let b=Math.abs(p.point.x-h.point.x)+Math.abs(p.point.y-h.point.y);if(b===0)return;let m=g==="H"?Math.min(p.point.x,h.point.x):Math.min(p.point.y,h.point.y),y=m+b;for(let[R,E]of[[p,h],[h,p]])s.get(R.id).push({from:R.id,to:E.id,orientation:g,length:b,kind:"visibility",intervalStart:m,intervalEnd:y})},"addArcPair");for(let p of["H","V"]){let h=new Map;for(let g of a){let b=p==="H"?g.point.y:g.point.x,m=h.get(b)??[];m.push(g),h.set(b,m)}for(let[g,b]of h){b.sort((m,y)=>p==="H"?m.point.x-y.point.x||m.id-y.id:m.point.y-y.point.y||m.id-y.id);for(let m=1;m<b.length;m++){let y=b[m-1],R=b[m],E=p==="H"?y.point.x:y.point.y,S=p==="H"?R.point.x:R.point.y;(p==="H"?e:o).intersects(g,E,S)||d(y,R,p)}}}let u={visibility:0,terminal:1,portal:2,lane:3},l=0,f=new Map;for(let p of a){let h=s.get(p.id);h.sort((b,m)=>u[b.kind]-u[m.kind]||z(b.orientation,m.orientation)||a[b.to].point.x-a[m.to].point.x||a[b.to].point.y-a[m.to].point.y||b.to-m.to);let g=h.filter((b,m)=>m===0||b.to!==h[m-1].to||b.orientation!==h[m-1].orientation||b.kind!==h[m-1].kind);l+=g.length,f.set(p.id,Object.freeze(g.map(b=>Object.freeze(b))))}return Ke(l,n.maxAdjacencyEntries??i*fi,"adjacency_cap"),{vertices:Object.freeze(a),adjacency:new et(f),adjacencyEntries:l}}c(bi,"buildVisibilityGraph");function Ue(t,e,o){let n=Nt(e),r=o?Nt(o):void 0,i=Math.max(n.top+j,(r?.bottom??-1/0)+j),a=n.bottom-j,s=n.left+j,d=n.right-j,u=[];return i<=a&&u.push({ownerId:t,side:"left",low:i,high:a},{ownerId:t,side:"right",low:i,high:a}),s<=d&&(u.push({ownerId:t,side:"bottom",low:s,high:d}),(!r||r.bottom+j<=n.top)&&u.push({ownerId:t,side:"top",low:s,high:d})),u}c(Ue,"derivePortalRanges");function fn(t,e,o,n,r){let i=Ue(t,e,o).find(f=>f.side===n);if(!i||r<i.low||r>i.high)throw new Error(`Illegal ${n} portal for "${t}" at ${r}`);let a=n==="left"?e.left:n==="right"?e.right:n==="top"?e.top:e.bottom,s=n==="left"||n==="top"?a+j:a-j,d=n==="left"||n==="top"?a-j:a+j,u=n==="left"||n==="right"?{x:s,y:r}:{x:r,y:s},l=n==="left"||n==="right"?{x:d,y:r}:{x:r,y:d};return{ownerId:t,side:n,tangentialCoordinate:r,interior:u,exterior:l,transition:{from:u,to:l,orientation:n==="left"||n==="right"?"H":"V",length:j*2,kind:"portal"}}}c(fn,"buildPairedPortal");function Ao(t,e={}){let o=Nt(t.bounds),n=[...t.obstacles,...t.titleExclusions??[]].map(m=>gi(m,o)).filter(m=>!!m),r=hi(n),i=[...t.portalRanges??[]].sort((m,y)=>_t(m.side)-_t(y.side)||m.low-y.low||m.high-y.high||z(m.ownerId,y.ownerId)),a=pn([...Co(o,t.containerId),...r.flatMap(m=>Co(m,m.id)),...i.flatMap(m=>yi(m,o))]),s={intervalCount:0,maxEstimatedBytes:e.caps?.maxEstimatedBytes??Ae};Ke(un(0,0,r.length,0),s.maxEstimatedBytes,"estimated_memory_cap");let d=So(r,"H",s),u=So(r,"V",s),l=[],f=["left","right","top","bottom"];for(let m of a)for(let y of f){let R=Oo(m.point,y,o,d,u);(R.x!==m.point.x||R.y!==m.point.y)&&l.push({point:R,kind:"projection",side:y})}let{vertices:p,adjacency:h,adjacencyEntries:g}=bi([...a,...l],d,u,e.caps??{}),b=un(p.length,g,r.length,d.intervalCount+u.intervalCount);return Ke(b,e.caps?.maxEstimatedBytes??Ae,"estimated_memory_cap"),e.metrics&&(e.metrics.containersBuilt++,e.metrics.baseTopologyBuilds++,e.metrics.baseVertices+=p.length,e.metrics.baseAdjacencyEntries+=g,e.metrics.buildSweepEvents+=a.length*4+r.length*4,e.metrics.estimatedBytes+=b),Object.freeze({containerId:t.containerId,bounds:Object.freeze(o),obstacles:Object.freeze(r.map(m=>Object.freeze(m))),vertices:Object.freeze(p),pointVertexIds:new et(new Map(p.map(({id:m,point:y})=>[ge(y),m]))),horizontalVertexLines:Mo(p,"H"),verticalVertexLines:Mo(p,"V"),adjacency:h,adjacencyByVertex:Object.freeze(p.map(({id:m})=>h.get(m)??[])),searchAdjacencyByVertex:Object.freeze(p.map(({id:m})=>Lo(h.get(m)??[]))),adjacencyEntries:g,horizontalIntervals:d,verticalIntervals:u,portalRanges:Object.freeze(i.map(m=>Object.freeze(m))),seedCount:a.length,estimatedBytes:b})}c(Ao,"buildContainerRoutingTopology");function wo(t,e){return e.contains(t.y,t.x)}c(wo,"pointInsideObstacle");function Tt(t,e,o,n,r,i=[]){let a=r??new gt(t);a.reset(t);let s=[{point:e,kind:"endpoint",ownerId:"source"},{point:o,kind:"endpoint",ownerId:"target"},...i.filter(I=>!wo(I,t.horizontalIntervals)).map(I=>({point:I,kind:"lane"}))];for(let I of[{x:e.x,y:o.y},{x:o.x,y:e.y}])wo(I,t.horizontalIntervals)||s.push({point:I,kind:"projection"});for(let I of s.slice(0,2))for(let x of["left","right","top","bottom"]){let w=Oo(I.point,x,t.bounds,t.horizontalIntervals,t.verticalIntervals);(w.x!==I.point.x||w.y!==I.point.y)&&s.push({point:w,kind:"projection",side:x})}let d=pn(s).filter(({point:I})=>!t.pointVertexIds.has(ge(I))).sort(ln);Ke(d.length,32,"vertex_cap"),a.addedVertices.push(...d.map((I,x)=>Object.freeze({id:t.vertices.length+x,...I,point:Object.freeze({...I.point})})));let u=mi(t.vertices,a.addedVertices),l=c(I=>I<t.vertices.length?t.vertices[I]:a.addedVertices[I-t.vertices.length],"vertexAt"),f=new Map,p=c(I=>{let x=f.get(I);return x||(x=[...t.adjacency.get(I)??[]],f.set(I,x)),x},"mutableArcs");for(let I of a.addedVertices)f.set(I.id,[]);let h=new Set(a.addedVertices.map(({id:I})=>I)),g=new Set,b=c((I,x,w)=>{let G=Math.abs(I.point.x-x.point.x)+Math.abs(I.point.y-x.point.y);if(G===0)return;let C=w==="H"?Math.min(I.point.x,x.point.x):Math.min(I.point.y,x.point.y),v=C+G;for(let[A,O]of[[I,x],[x,I]]){let _=`${A.id}:${O.id}:${w}:visibility`;g.has(_)||(t.adjacency.get(A.id)??[]).some(N=>N.to===O.id&&N.orientation===w&&N.kind==="visibility")||(g.add(_),p(A.id).push({from:A.id,to:O.id,orientation:w,length:G,kind:"visibility",intervalStart:C,intervalEnd:v}))}},"connect");for(let I of["H","V"]){let x=I==="H"?t.horizontalVertexLines:t.verticalVertexLines,w=new Set(a.addedVertices.map(({point:G})=>I==="H"?G.y:G.x));for(let G of w){let C=[...x.get(G)??[],...a.addedVertices.filter(({point:v})=>(I==="H"?v.y:v.x)===G)];C.sort((v,A)=>I==="H"?v.point.x-A.point.x||v.id-A.id:v.point.y-A.point.y||v.id-A.id);for(let v=1;v<C.length;v++){let A=C[v-1],O=C[v];if(!h.has(A.id)&&!h.has(O.id))continue;let _=I==="H"?A.point.x:A.point.y,N=I==="H"?O.point.x:O.point.y;(I==="H"?t.horizontalIntervals:t.verticalIntervals).intersects(G,_,N)||b(A,O,I)}}}for(let[I,x]of f){x.sort((G,C)=>z(G.kind,C.kind)||z(G.orientation,C.orientation)||l(G.to).point.x-l(C.to).point.x||l(G.to).point.y-l(C.to).point.y||G.to-C.to);let w=Object.freeze(x.map(G=>Object.freeze(G)));a.setArcs(I,w)}let m=t.adjacencyEntries+g.size;Ke(m,t.adjacencyEntries+64,"adjacency_cap");let y=d.length,R=t.estimatedBytes+y*64+(m-t.adjacencyEntries)*56;n&&(n.endpointOverlayBuilds++,n.endpointOverlayVertices+=y,n.estimatedBytes=Math.max(n.estimatedBytes,R));let E=t.vertices.length+a.addedVertices.length,S=Eo(t.adjacencyByVertex,a.adjacencyOverrides,E),M=Eo(t.searchAdjacencyByVertex,a.searchAdjacencyOverrides,E);for(let I of a.addedVertices)a.pointOverrides.set(ge(I.point),I.id);return Object.freeze({...t,vertices:u,pointVertexIds:new Bt(t.pointVertexIds,a.pointOverrides),adjacency:new Bt(t.adjacency,a.adjacencyOverrides),adjacencyByVertex:S,searchAdjacencyByVertex:M,adjacencyEntries:m,estimatedBytes:R,vertexCount:E,getVertex:l,getSearchArcs:c(I=>a.searchAdjacencyOverrides.get(I)??t.searchAdjacencyByVertex[I]??[],"getSearchArcs")})}c(Tt,"buildEndpointRoutingOverlay");var yt=4,X=20,tt=8;function xt(t){return!!t.groupTitleRect}c(xt,"ownerGroupTitle");function Xe(t,e){return JSON.stringify([t,e])}c(Xe,"ownerSideKey");function bn(t,e){let o=k(t),n=e.x-o.cx,r=e.y-o.cy,i=t.isGroup&&xt(t);return Math.abs(n)>=Math.abs(r)?n>=0?"right":"left":r>=0?"bottom":i?n>=0?"right":"left":"top"}c(bn,"preferredSide");function mn(t,e){let o=[],n=t.parentId??F,r=new Set;for(;o.push(n),!(n===F||r.has(n));)r.add(n),n=e.get(n)?.parentId??F;return o}c(mn,"containerChain");function xi(t,e,o){if(t.isGroup&&Te(t.id,e,o))return t.parentId??F;if(e.isGroup&&Te(e.id,t,o))return e.parentId??F;let n=new Set(mn(e,o));for(let r of mn(t,o))if(n.has(r))return r;return F}c(xi,"commonContainerId");function gn(t,e){return e==="left"||e==="right"?t.cy:t.cx}c(gn,"oppositeCoordFor");function bt(t,e){return e==="left"||e==="right"?{x:e==="left"?t.left:t.right,y:t.cy}:{x:t.cx,y:e==="top"?t.top:t.bottom}}c(bt,"sideMidpoint");function hn(t,e){return e==="left"||e==="right"?t.y:t.x}c(hn,"tangentialCoordinate");function Bo(t,e,o,n,r,i){let a=[],s=k(t),d=k(e),u={x:e.x??0,y:e.y??0},l=t;for(;;){let f=l.parentId??F,p=n.get(f),h={x:l.x??0,y:l.y??0},g=u.x-h.x,b=u.y-h.y,m=b<0&&Math.abs(g)<=1&&!!((l.isGroup?l.groupTitleRect:void 0)??p?.groupTitleRect),y=[];Math.abs(Math.abs(g)-Math.abs(b))<=1&&Math.abs(g)>1&&y.push(g>=0?"right":"left",b>=0?"bottom":"top");let R=!!((l.isGroup?l.groupTitleRect:void 0)??p?.groupTitleRect),E=(m?["left","right"]:y).filter(M=>M!=="top"||!R),S=bn(l,u);if(S==="top"&&p?.groupTitleRect&&(S=g>=0?"right":"left"),a.push({ownerId:l.id,side:S,demandKey:JSON.stringify([r,i,l.id,S]),oppositeCoord:gn(d,S),preferredCoord:gn(s,S),compactPortal:l.id!==t.id,ambiguousSides:E.length>1?E:void 0,inner:a.length?{demandKey:a[a.length-1].demandKey,side:a[a.length-1].side}:void 0}),l.isGroup&&l.id===o&&Te(l.id,e,n))return{chain:a,finalKind:"boundary"};if(f===o)return{chain:a,finalKind:"item"};if(!p?.isGroup)throw D("GRID_INVALID_CONTAINMENT",`Missing parent group "${f}"`,{traversedIds:a.map(M=>M.ownerId)});l=p}}c(Bo,"buildEndpointPlan");function Ri(t){let e=new Map;for(let n of t){if(!n.start||!n.end)continue;let r=n.start<n.end?`${n.start}|${n.end}`:`${n.end}|${n.start}`;e.has(r)||e.set(r,[]),e.get(r).push(n)}let o=new Map;for(let[n,r]of e)r.sort((i,a)=>z(`${i.start}|${i.end}`,`${a.start}|${a.end}`)||z(i.id,a.id)),r.forEach((i,a)=>o.set(i.id,{index:a,size:r.length,offset:(a-(r.length-1)/2)*tt,pairKey:n}));return o}c(Ri,"buildPairLanes");function Ii(t,e){let o=new Map,n=c((a,s)=>Xe(a,s),"reservationKey");for(let a of t)for(let s of[...a.source.chain,...a.target.chain])if(!s.ambiguousSides){let d=n(s.ownerId,s.side);o.set(d,(o.get(d)??0)+1)}let r=c((a,s,d)=>{let u=s.chain.filter(({ambiguousSides:p})=>p);if(u.length===0)return;let f=["right","bottom","left","top"].filter(p=>u.every(h=>h.ambiguousSides?.includes(p))&&u.every(h=>{let g=e.forest.nodeById.get(h.ownerId);return g!==void 0&&Se(g,p)!==void 0})).sort((p,h)=>u.reduce((g,b)=>g+(o.get(n(b.ownerId,p))??0),0)-u.reduce((g,b)=>g+(o.get(n(b.ownerId,h))??0),0)||+(p!==u[0].side)-+(h!==u[0].side))[0];if(f){for(let p of u){p.side=f;let h=n(p.ownerId,f);o.set(h,(o.get(h)??0)+1);let g=e.forest.nodeById.get(p.ownerId);g&&(p.preferredCoord=gn(k(g),f))}for(let[p,h]of s.chain.entries())h.demandKey=JSON.stringify([a.edge.id,d,h.ownerId,h.side]),h.inner=p>0?{demandKey:s.chain[p-1].demandKey,side:s.chain[p-1].side}:void 0}},"resolveEndpoint"),i=t.flatMap(a=>[{plan:a,endpoint:a.source,role:"source"},{plan:a,endpoint:a.target,role:"target"}]).sort((a,s)=>z(`${a.plan.edge.start??""}|${a.plan.edge.end??""}|${a.plan.edge.id}|${a.role}`,`${s.plan.edge.start??""}|${s.plan.edge.end??""}|${s.plan.edge.id}|${s.role}`));for(let{plan:a,endpoint:s,role:d}of i)r(a,s,d);for(let a of t){let s=a.source.chain.at(-1),d=a.target.chain.at(-1),u=s?e.forest.nodeById.get(s.ownerId):void 0,l=d?e.forest.nodeById.get(d.ownerId):void 0;if(!s||!d||!u||!l)continue;let f=bt(k(u),s.side),p=bt(k(l),d.side);for(let h of a.source.chain)h.oppositeCoord=hn(p,h.side);for(let h of a.target.chain)h.oppositeCoord=hn(f,h.side)}}c(Ii,"resolveAmbiguousEndpointSides");function Ei(t,e){let o=e.forest.nodeById,n=Ri(t.edges),r=t.edges.map(i=>{let a=i.start?o.get(i.start):void 0,s=i.end?o.get(i.end):void 0;if(!a||!s)throw D("GRID_MISSING_ENDPOINT",`Missing endpoint for edge "${i.id}"`,{edgeId:i.id,start:i.start,end:i.end});let d=xi(a,s,o),u=n.get(i.id)??{index:0,size:1,offset:0,pairKey:De(i)};return{edge:i,lcaContainerId:d,source:Bo(a,s,d,o,i.id,"source"),target:Bo(s,a,d,o,i.id,"target"),laneIndex:u.index,laneOffset:u.offset,bundleSize:u.size,pairKey:u.pairKey}});return Ii(r,e),r}c(Ei,"collectRoutePlans");function xn(t,e,o){let n=[];for(let[u,l]of t.entries())n.push(u===0?l:Math.max(l,n[u-1]+yt));for(let u=n.length-1;u>=0;u--)n[u]=Math.min(n[u],u===n.length-1?o:n[u+1]-yt);let r=t.reduce((u,l)=>u+l,0)/t.length,i=n.reduce((u,l)=>u+l,0)/n.length,a=e-n[0],s=o-n[n.length-1],d=Ce(r-i,a,s);return n.map(u=>u+d)}c(xn,"assignCompactPortalCoordinates");function Ci(t,e,o){let n=e.forest.nodeById,r=new Map(t.map(l=>[l.edge.id,l])),i=new Map;for(let l of t)if(l.edge.start!==l.edge.end)for(let f of[...l.source.chain,...l.target.chain])i.has(f.demandKey)||i.set(f.demandKey,{ownerId:f.ownerId,side:f.side,edgeId:l.edge.id,demandKey:f.demandKey,oppositeCoord:f.oppositeCoord,preferredCoord:f.preferredCoord,compactPortal:f.compactPortal,inner:f.inner});let a=new Map;for(let l of i.values()){let f=Xe(l.ownerId,l.side),p=a.get(f)??{ownerId:l.ownerId,side:l.side,demands:[]};p.demands.push(l),a.set(f,p)}let s=c(l=>{let f=n.get(l);return f?mn(f,n).length:0},"depthOf"),d=[...a.values()].sort((l,f)=>Number(l.demands.every(p=>p.compactPortal))-Number(f.demands.every(p=>p.compactPortal))||s(f.ownerId)-s(l.ownerId)),u=new Map;for(let{ownerId:l,side:f,demands:p}of d){let h=n.get(l);if(!h)continue;let g=k(h),b=f==="left"||f==="right"?g.top+j:g.left+j,m=f==="left"||f==="right"?g.bottom-j:g.right-j;(f==="left"||f==="right")&&h.groupTitleRect&&(b=Math.max(b,h.groupTitleRect.bottom+j));let y=Math.max(0,m-b);p.sort((I,x)=>I.oppositeCoord-x.oppositeCoord||z(I.edgeId,x.edgeId));let R=p.map(I=>r.get(I.edgeId)),E=new Set(R.map(I=>I?.pairKey)),S=h.isGroup&&p.every(({compactPortal:I})=>I);if(!(S&&p.length===1)&&E.size===1&&R.every(I=>I&&I.bundleSize===p.length&&o.get(I.edge.start)===I.bundleSize&&o.get(I.edge.end)===I.bundleSize)){let I=(b+m)/2,x=R.map(w=>I+w.laneOffset);if(x.every(w=>w>=b&&w<=m)){p.forEach((w,G)=>u.set(w.demandKey,x[G]));continue}}if(S){let I=e.containers.get(h.id),x=f==="left"||f==="right"?I?.horizontalCorridors:I?.verticalCorridors,w=c(C=>{let v=C.inner?i.get(C.inner.demandKey):void 0,A=v?n.get(v.ownerId):void 0,O=v?u.get(v.demandKey):void 0;if(!v||!A||O===void 0)return C.preferredCoord;let _=bt(k(A),v.side);return v.side==="left"||v.side==="right"?_.y=O:_.x=O,hn(_,f)},"innerCoordinate"),G=c(C=>{let v=C.inner?u.get(C.inner.demandKey):void 0,A=C.inner!==void 0&&v!==void 0&&(C.inner.side==="left"||C.inner.side==="right")==(f==="left"||f==="right"),O=Ce(A?v:C.preferredCoord,b,m),_=x?.reduce((N,W)=>Math.abs(W-O)<Math.abs(N-O)?W:N,x[0]);return _!==void 0&&Math.abs(_-O)<=1?_:O},"preferredCoordinate");if(p.length===1){u.set(p[0].demandKey,G(p[0]));continue}if(y>=yt*(p.length-1)){p.sort((A,O)=>w(A)-w(O)||A.oppositeCoord-O.oppositeCoord||z(A.edgeId,O.edgeId));let C=p.map(G),v=xn(C,b,m);p.forEach((A,O)=>u.set(A.demandKey,v[O]));continue}}if(p.length===1||y<=0){u.set(p[0].demandKey,(b+m)/2);continue}let M=y/(p.length-1);p.forEach((I,x)=>{u.set(I.demandKey,b+M*x)})}for(let l of t){if(l.bundleSize!==1)continue;let f=l.source.chain.at(-1),p=l.target.chain.at(-1);if(!f||!p)continue;let h=c(R=>R==="left"||R==="right","verticalSide");if(h(f.side)!==h(p.side))continue;let g=u.get(f.demandKey),b=u.get(p.demandKey);if(g===void 0||b===void 0||Math.abs(g-b)>1)continue;let m=[...l.source.chain,...l.target.chain];if(m.some(R=>h(R.side)!==h(f.side)))continue;let y=[g,b].find(R=>m.every(E=>{let S=n.get(E.ownerId),M=S?Se(S,E.side):void 0;return M!==void 0&&R>=M.low-1&&R<=M.high+1}));y!==void 0&&m.forEach(R=>u.set(R.demandKey,y))}return u}c(Ci,"assignDemandCoordinates");function ke(t){return t==="right"?0:t==="bottom"?1:t==="left"?2:3}c(ke,"sideOrder");function Se(t,e){let o=k(t);if(e==="top"&&t.groupTitleRect)return;let n=e==="left"||e==="right"?o.top+j:o.left+j,r=e==="left"||e==="right"?o.bottom-j:o.right-j;return(e==="left"||e==="right")&&t.groupTitleRect&&(n=Math.max(n,t.groupTitleRect.bottom+j)),n<=r?{low:n,high:r}:void 0}c(Se,"sideInterval");function De(t){let e=t.start??"",o=t.end??"";return e<o?`${e}|${o}`:`${o}|${e}`}c(De,"endpointPairKey");function To(t,e,o){let n=k(t.opposite),r=k(e.opposite),i=o==="left"||o==="right"?n.cy:n.cx,a=o==="left"||o==="right"?r.cy:r.cx;return i-a||z(De(t.plan.edge),De(e.plan.edge))||z(`${t.plan.edge.start}|${t.plan.edge.end}`,`${e.plan.edge.start}|${e.plan.edge.end}`)||z(t.plan.edge.id,e.plan.edge.id)||z(t.role,e.role)}c(To,"compareEndpointDemands");function Si(t){return`${t.plan.edge.id}:${t.role}`}c(Si,"endpointDemandKey");function Mi(t,e){return z(De(t.plan.edge),De(e.plan.edge))||z(`${t.plan.edge.start}|${t.plan.edge.end}`,`${e.plan.edge.start}|${e.plan.edge.end}`)||z(t.plan.edge.id,e.plan.edge.id)||z(t.role,e.role)}c(Mi,"compareEndpointDemandIdentity");function yn(t,e,o){let n=Se(t,e);if(!n||o.length===0)return new Map;let r=[...o].sort((s,d)=>To(s,d,e));if(r.length===1){let s=k(t),d=e==="left"||e==="right"?s.cy:s.cx;return new Map([[r[0],Ce(d,n.low,n.high)]])}if((n.high-n.low)/(r.length-1)<yt)return new Map(r.map((s,d)=>[s,n.low+d*(n.high-n.low)/(r.length-1)]));let i=r.map(({opposite:s})=>{let d=k(s),u=e==="left"||e==="right"?d.cy:d.cx;return Ce(u,n.low,n.high)}),a=xn(i,n.low,n.high);return new Map(r.map((s,d)=>[s,a[d]]))}c(yn,"preferredEndpointCoordinates");function Do(t,e){let o=Se(t,e);return o?1+Math.floor((o.high-o.low)/yt):0}c(Do,"endpointSideCapacity");function No(t,e){let o=k(e.opposite),n=bn(t,{x:o.cx,y:o.cy});return["right","bottom","left","top"].filter(r=>Do(t,r)>0).sort((r,i)=>{if(r===n)return-1;if(i===n)return 1;let a=k(t),s=bt(a,r),d=bt(a,i),u=Math.abs(s.x-o.cx)+Math.abs(s.y-o.cy),l=Math.abs(d.x-o.cx)+Math.abs(d.y-o.cy);return u-l||ke(r)-ke(i)})}c(No,"endpointSidePreferences");function ht(t,e){let o=k(t),n=k(e.opposite);return Math.abs(o.cx-n.cx)<=1||Math.abs(o.cy-n.cy)<=1}c(ht,"isAxisAlignedDemand");function wi(t,e,o,n){let r=Se(t,e);if(!r)return;let i=[...o],a=new Map;for(let s of n){let d=k(s.opposite),u=Ce(e==="left"||e==="right"?d.cy:d.cx,r.low,r.high),l=[u,r.low,r.high,...i.flatMap(f=>[f-$e,f+$e])].filter(f=>f>=r.low-1&&f<=r.high+1&&i.every(p=>Math.abs(f-p)>=$e-1)).sort((f,p)=>Math.abs(f-u)-Math.abs(p-u)||f-p);if(l.length===0)return;a.set(s,l[0]),i.push(l[0])}return a}c(wi,"placePortsBesideFixed");function Gi(t,e){let o=new Map,n=new Map(["right","bottom","left","top"].map(m=>[m,Do(t,m)])),r=[...e].sort(Mi),i=r.filter(m=>ht(t,m)),a=r.filter(m=>!ht(t,m)),s=new Set,d=new Map,u=new Map,l=c(m=>(o.get(m)?.length??0)<(n.get(m)??0),"hasCapacity"),f=c((m,y)=>{let R=o.get(y)??[];R.push(m),o.set(y,R)},"record"),p=c(m=>{let y=No(t,m).find(l);y&&(f(m,y),s.add(y))},"assignAligned"),h=c((m,y)=>{let R=u.get(y)??new Map,E=wi(t,y,[...d.get(y)??[],...R.values()],[m]);return E?(R.set(m,E.get(m)),u.set(y,R),f(m,y),!0):!1},"tryShareReservedSide"),g=c(m=>{let y=No(t,m).filter(l),R=y.find(M=>!s.has(M));if(y.find(M=>(R===void 0||y.indexOf(M)<y.indexOf(R))&&s.has(M)&&h(m,M)))return;let S=R??y[0];S&&f(m,S)},"assignUnaligned");for(let m of i)p(m);for(let[m,y]of o)d.set(m,[...yn(t,m,y).values()]);for(let m of a)g(m);let b=new Map;for(let[m,y]of o){let R=Se(t,m);if(!R)continue;let E=u.get(m),S=E?y.filter(x=>!E.has(x)):y,M=new Map([...yn(t,m,S),...E??[]]),I=[...y].sort((x,w)=>To(x,w,m));for(let[x,w]of I.entries()){let G=M.get(w)??(I.length===1?(R.low+R.high)/2:R.low+x*(R.high-R.low)/(I.length-1));b.set(Si(w),{side:m,coordinate:G})}}return b}c(Gi,"allocateEndpointSlots");function _o(t,e,o,n,r,i){let a=e==="source"?t.edge.start:t.edge.end,s=e==="source"?t.edge.end:t.edge.start,d=i.forest.nodeById.get(a),u=i.forest.nodeById.get(s);if(!d||!u)throw D("GRID_MISSING_ENDPOINT",`Missing endpoint for edge "${t.edge.id}"`);let l=k(u),f=[],p=new Set,h=n.get(a)?.get(`${t.edge.id}:${e}`),g=r.has(a)&&h!==void 0,b=c((y,R,E)=>{let S=Se(d,y);if(!S||R<S.low||R>S.high)return;let M=k(d),I=y==="left"||y==="right"?{x:y==="left"?M.left:M.right,y:R}:{x:R,y:y==="top"?M.top:M.bottom},x=`${y}:${ge(I)}`;p.has(x)||(p.add(x),f.push({ownerId:a,side:y,port:I,connect:{x:I.x+(y==="left"?-X:y==="right"?X:0),y:I.y+(y==="top"?-X:y==="bottom"?X:0)},rank:E,onAssignedSide:g?y===h.side:void 0}))},"addCandidate");if(h&&t.bundleSize===1&&b(h.side,h.coordinate,-2),t.bundleSize>1){let y=bn(d,{x:l.cx,y:l.cy}),R=k(d),E=y==="left"||y==="right"?R.cy:R.cx;b(y,E+t.laneOffset,-1)}let m=[...o.get(a)??[]];for(let y of["right","bottom","left","top"]){if(!Se(d,y))continue;let E=yn(d,y,m),S=m.find(G=>G.plan===t&&G.role===e),M=S?E.get(S):void 0;if(M===void 0)continue;let I=k(d),x=y==="left"||y==="right"?{x:y==="left"?I.left:I.right,y:M}:{x:M,y:y==="top"?I.top:I.bottom},w=Math.abs(x.x-l.cx)+Math.abs(x.y-l.cy);b(y,M,w)}return f.sort((y,R)=>y.rank-R.rank||ke(y.side)-ke(R.side)),f.forEach((y,R)=>{y.rank=R}),f}c(_o,"endpointCandidates");function ko(t,e){let o=Ei(t,e),n=new Map,r=new Map;for(let g of o){let b=De(g.edge);n.set(b,(n.get(b)??0)+1),g.edge.start&&r.set(g.edge.start,(r.get(g.edge.start)??0)+1),g.edge.end&&g.edge.end!==g.edge.start&&r.set(g.edge.end,(r.get(g.edge.end)??0)+1)}let i=o.filter(g=>g.edge.start!==g.edge.end&&g.source.chain.length===1&&g.target.chain.length===1&&g.source.finalKind==="item"&&g.target.finalKind==="item"),a=new Map;for(let g of i){let b=e.forest.nodeById.get(g.edge.start),m=e.forest.nodeById.get(g.edge.end);if(!b||!m)continue;let y=a.get(b.id)??[];y.push({plan:g,role:"source",opposite:m}),a.set(b.id,y);let R=a.get(m.id)??[];R.push({plan:g,role:"target",opposite:b}),a.set(m.id,R)}let s=new Map([...a].map(([g,b])=>{let m=e.forest.nodeById.get(g);return[g,m?Gi(m,b):new Map]})),d=new Set,u=new Set;for(let[g,b]of a){let m=e.forest.nodeById.get(g);if(!m)continue;let y=b.some(E=>ht(m,E)),R=b.some(E=>!ht(m,E));if(y&&R){u.add(g);for(let E of b)ht(m,E)||d.add(E.plan.edge.id)}}let l=new Map(i.map(g=>[g.edge.id,{sources:_o(g,"source",a,s,u,e),targets:_o(g,"target",a,s,u,e)}])),f=new Map([...l].map(([g,b])=>[g,b.sources.length*b.targets.length])),p=c(g=>{let b=e.forest.nodeById.get(g.edge.start),m=e.forest.nodeById.get(g.edge.end);return g.edge.start===g.edge.end?0:b?.isGroup&&m&&Te(b.id,m,e.forest.nodeById)||m?.isGroup&&b&&Te(m.id,b,e.forest.nodeById)?1:(b?.parentId??F)!==(m?.parentId??F)?2:n.get(De(g.edge))>1?3:4},"routeClass"),h=[...o].sort((g,b)=>{let m=De(g.edge),y=De(b.edge),R=`${g.edge.start??""}|${g.edge.end??""}`,E=`${b.edge.start??""}|${b.edge.end??""}`,S=g.source.chain.length+g.target.chain.length-2,M=b.source.chain.length+b.target.chain.length-2;return p(g)-p(b)||M-S||(f.get(g.edge.id)??0)-(f.get(b.edge.id)??0)||z(m,y)||z(R,E)||z(g.edge.id,b.edge.id)});return{plans:o,eligiblePlans:i,eligibleIds:new Set(i.map(({edge:g})=>g.id)),orderedPlans:h,demandCoords:Ci(h,e,r),endpointCandidatesByEdge:l,endpointIncidentCounts:r,coordinatedEndpointIds:d}}c(ko,"prepareEdgeRoutes");function Fo(t){if(!(t instanceof Error)||!("details"in t))return!1;let e=t.details;return typeof e=="object"&&e!==null&&"reason"in e&&e.reason==="pair-separation"}c(Fo,"isPairSeparationError");function ve(t){let e=k(t);return{left:e.left,right:e.right,top:e.top,bottom:e.bottom}}c(ve,"routerRect");function jo(t){if(t.length<2||t.some(({x:r,y:i})=>!Number.isFinite(r)||!Number.isFinite(i)))return;let e=[{...t[0]}];for(let r=1;r<t.length;r++){let i=t[r],a=e.at(-1);if(!(Math.abs(i.x-a.x)<=1&&Math.abs(i.y-a.y)<=1)){e.push({...i});continue}if(r===t.length-1){for(;e.length>1;){let d=e.at(-2);if(Math.abs(i.x-d.x)>1||Math.abs(i.y-d.y)>1)break;e.pop()}if(e.length===1)return;e[e.length-1]={...i}}}if(e.length<2)return;let o=[];for(let r=0;r<e.length-1;r++){let i=e[r],a=e[r+1],s=Math.abs(a.x-i.x);if(Math.abs(a.y-i.y)<=1)o.push("H");else if(s<=1)o.push("V");else return}let n=e.map(r=>({...r}));for(let r=0;r<o.length;){let i=o[r],a=r+1;for(;a<o.length&&o[a]===i;)a++;let s=i==="H"?"y":"x";if(r===0&&a===e.length-1&&e[r][s]!==e[a][s])return;let d=a===e.length-1?e[a][s]:e[r][s];for(let u=r;u<=a;u++){if(Math.abs(e[u][s]-d)>1)return;n[u][s]=d}r=a}return n}c(jo,"canonicalizeOrthogonalPolyline");function Dt(t,e,o){return t.y===e.y?t.y>o.top&&t.y<o.bottom&&Math.max(t.x,e.x)>o.left&&Math.min(t.x,e.x)<o.right:t.x===e.x?t.x>o.left&&t.x<o.right&&Math.max(t.y,e.y)>o.top&&Math.min(t.y,e.y)<o.bottom:!0}c(Dt,"segmentEntersRect");function zo(t){let e=ve(t);return{left:e.left-j,right:e.right+j,top:e.top-j,bottom:e.bottom+j}}c(zo,"inflatedRect");function Ie(t,e,o,n,r){let i=jo(t);if(!i)return!1;let a=V(i);if(a.points.length<2||a.segments.length>1&&(Math.abs(a.segments[0].a.x-a.segments[0].b.x)+Math.abs(a.segments[0].a.y-a.segments[0].b.y)<X||Math.abs(a.segments.at(-1).a.x-a.segments.at(-1).b.x)+Math.abs(a.segments.at(-1).a.y-a.segments.at(-1).b.y)<X))return!1;let s=(r.forest.childrenByParent.get(n)??[]).filter(d=>!ce(d));for(let d of s){let u=zo(d);for(let l=0;l<a.points.length-1;l++){if(d.id===e.id&&l===0||d.id===o.id&&l===a.points.length-2){if(Dt(a.points[l],a.points[l+1],ve(d)))return!1;continue}if(Dt(a.points[l],a.points[l+1],u))return!1}}return!0}c(Ie,"validateSameContainerRoute");function Ve(t,e,o,n,r){let i=jo(t);if(!i)return!1;let a=V(i);if(a.points.length<2)return!1;let s=(r.forest.childrenByParent.get(n)??[]).filter(d=>!ce(d));for(let d of s){let u=zo(d);for(let l=0;l<a.points.length-1;l++){if(d.id===e&&l===0||d.id===o&&l===a.points.length-2){if(Dt(a.points[l],a.points[l+1],ve(d)))return!1;continue}if(Dt(a.points[l],a.points[l+1],u))return!1}}return!0}c(Ve,"validateContainerSegment");function Vo(t){return t.orientation==="H"?{low:Math.min(t.a.x,t.b.x),high:Math.max(t.a.x,t.b.x)}:{low:Math.min(t.a.y,t.b.y),high:Math.max(t.a.y,t.b.y)}}c(Vo,"segmentSpan");function nt(t){let e=V([...t]).segments.filter(n=>n.orientation!=="Z").map(n=>({a:{...n.a},b:{...n.b},orientation:n.orientation}));if(e.length===0)return[];if(e.length===1&&Math.abs(e[0].a.x-e[0].b.x)+Math.abs(e[0].a.y-e[0].b.y)<=X*2)return[];let o=c((n,r)=>{if(n.orientation==="H"){let i=Math.sign(n.b.x-n.a.x);r?n.a.x+=i*X:n.b.x-=i*X}else{let i=Math.sign(n.b.y-n.a.y);r?n.a.y+=i*X:n.b.y-=i*X}},"trim");return o(e[0],!0),o(e[e.length-1],!1),e.filter(({a:n,b:r})=>n.x!==r.x||n.y!==r.y)}c(nt,"nonterminalSegments");function Ho(t,e){if(t.orientation!==e.orientation)return!1;let o=Vo(t),n=Vo(e),r=Math.max(0,Math.min(o.high,n.high)-Math.max(o.low,n.low));if(r<=0)return!1;let i=t.orientation==="H"?Math.abs(t.a.y-e.a.y):Math.abs(t.a.x-e.a.x);return i===0&&r>=tt||i>0&&i<tt}c(Ho,"pairSegmentsConflict");function he(t,e){let o=V([...t]).points,n=[o[0],o.at(-1)],r=nt(o);return e.every(i=>{let a=V([...i]).points,s=[a[0],a.at(-1)];return n.some(d=>s.some(u=>d.x===u.x&&d.y===u.y))?!1:r.every(d=>nt(a).every(u=>!Ho(d,u)))})}c(he,"routeSatisfiesPairConstraints");function kt(t,e){let o=V([...t]).points,n=[o[0],o.at(-1)];return e.every(r=>{let i=V([...r]).points,a=[i[0],i.at(-1)];return!n.some(s=>a.some(d=>s.x===d.x&&s.y===d.y))})}c(kt,"routeHasDistinctPairPorts");function Vt(t,e,o){let n=t.y===e.y?"H":"V",r={a:t,b:e,orientation:n};return o.every(i=>nt(i).every(a=>!Ho(r,a)))}c(Vt,"pairArcAllowed");function $o(t,e){let o=t.side==="left"||t.side==="right"?"H":"V",n=e.side==="left"||e.side==="right"?"H":"V",r=Math.abs(t.connect.x-e.connect.x),i=Math.abs(t.connect.y-e.connect.y),a;if(r===0&&i===0)a=+(o!==n);else if(r===0||i===0){let s=r===0?"V":"H";a=+(o!==s)+ +(n!==s)}else a=Math.min(+(o!=="H")+1+ +(n!=="V"),+(o!=="V")+1+ +(n!=="H"));return[X*2+r+i,a]}c($o,"endpointPairLowerBound");function vi(t,e){if(t.orientation!==e.orientation)return!1;let o=t.orientation==="H",n=c(s=>o?Math.min(s.a.x,s.b.x):Math.min(s.a.y,s.b.y),"lowOf"),r=c(s=>o?Math.max(s.a.x,s.b.x):Math.max(s.a.y,s.b.y),"highOf"),i=Math.min(r(t),r(e))-Math.max(n(t),n(e)),a=Math.abs(o?t.a.y-e.a.y:t.a.x-e.a.x);return i>0&&a<$e}c(vi,"segmentsTooClose");var We=class{constructor(){this.lines={H:new Map,V:new Map};this.sortedCoordinates={H:void 0,V:void 0};this.log=[]}static{c(this,"RouteOccupancyIndex")}get size(){return this.log.length}add(e,o){for(let n of nt(e)){let r=n.orientation,i=r==="H"?n.a.y:n.a.x,a=this.lines[r].get(i);a?a.push({segment:n,pairKey:o}):(this.lines[r].set(i,[{segment:n,pairKey:o}]),this.sortedCoordinates[r]=void 0),this.log.push({orientation:r,coordinate:i})}}truncate(e){for(;this.log.length>e;){let{orientation:o,coordinate:n}=this.log.pop();this.lines[o].get(n)?.pop()}}conflictsWithRoute(e,o){return nt(e).some(n=>this.conflicts(n,o))}conflictsWithArc(e,o,n){let r=e.y===o.y?"H":"V";return this.conflicts({a:e,b:o,orientation:r},n)}conflicts(e,o){let n=e.orientation;if(n!=="H"&&n!=="V")return!1;let r=n==="H"?e.a.y:e.a.x,i=this.coordinatesFor(n),a=0,s=i.length;for(;a<s;){let d=a+s>>1;i[d]<=r-$e?a=d+1:s=d}for(let d=a;d<i.length;d++){let u=i[d];if(u>=r+$e)break;for(let l of this.lines[n].get(u))if(l.pairKey!==o&&vi(e,l.segment))return!0}return!1}coordinatesFor(e){return this.sortedCoordinates[e]??=[...this.lines[e].keys()].sort((o,n)=>o-n)}};var Me=24,ye=18,Pe=14,Ko=4,Xo=18,Ft=4,Pi=64,Oi=4;function Li(t,e,o,n){if(!e.conflictsWithRoute(t,o))return[...t];let r=V([...t]).points;for(let i=1;i<=Oi;i++)for(let a of[1,-1]){let s=a*i*tt;for(let d=1;d<r.length-2;d++){let u=r[d].x===r[d+1].x,l=r.map((f,p)=>p===d||p===d+1?u?{x:f.x+s,y:f.y}:{x:f.x,y:f.y+s}:f);if(!e.conflictsWithRoute(l,o)&&n(l))return l}}}c(Li,"separateRouteFromOccupied");function Ai(t){let e=c((o,n)=>Math.abs(o.x-n.x)+Math.abs(o.y-n.y),"stub");return e(t[0],t[1])>=X&&e(t.at(-1),t.at(-2))>=X}c(Ai,"terminalStubsAreLongEnough");function In(t,e,o,n,r=3){let i=[...n.filter(d=>d>=e&&d<=o).sort((d,u)=>Math.abs(d-t)-Math.abs(u-t)||d-u),(e+o)/2,e,o],a=new Set([t]),s=[];for(let d of i)if(!a.has(d)&&(a.add(d),s.push(d),s.length===r))break;return s}c(In,"boundedAlternativePortalCoordinates");function Rt(t,e){return Math.abs(t.x-e.x)<=1e-6&&Math.abs(t.y-e.y)<=1e-6}c(Rt,"equalPoint");function En(t,e){return t.x===e.x||t.y===e.y}c(En,"areExactlyAxisAligned");function Rn(t,e,o,n){let r=[...t].sort((i,a)=>Math.abs(i-e)+Math.abs(i-o)-(Math.abs(a-e)+Math.abs(a-o))||i-a);return r[Math.min(n,Math.max(0,r.length-1))]??(e+o)/2}c(Rn,"chooseIntermediateCoordinate");function Bi(t){let e=[];for(let o of t)(e.length===0||!Rt(e[e.length-1],o))&&e.push(o);return e}c(Bi,"dedupeConsecutive");function Ye(t,e,o,n,r){let i=e.containers.get(t);if(!i)throw D("GRID_ROUTE_NOT_FOUND",`Missing container layout "${t}"`);let a=[o.port,o.connect];if(o.orientation==="V"&&n.orientation==="V"){if(!Rt(o.connect,n.connect)&&Math.abs(o.connect.x-n.connect.x)>1e-6){let s=Rn(i.horizontalCorridors,o.connect.y,n.connect.y,r);a.push({x:o.connect.x,y:s},{x:n.connect.x,y:s})}else if(!Rt(o.connect,n.connect)&&Math.abs(o.connect.y-n.connect.y)>1e-6&&r>0){let s=i.verticalCorridors.filter(u=>Math.abs(u-o.connect.x)>1e-6),d=Rn(s.length>0?s:i.verticalCorridors,o.connect.x,n.connect.x,r-1);a.push({x:d,y:o.connect.y},{x:d,y:n.connect.y})}}else if(o.orientation==="H"&&n.orientation==="H"){if(!Rt(o.connect,n.connect)&&Math.abs(o.connect.y-n.connect.y)>1e-6){let s=Rn(i.verticalCorridors,o.connect.x,n.connect.x,r);a.push({x:s,y:o.connect.y},{x:s,y:n.connect.y})}}else o.orientation==="V"&&n.orientation==="H"?a.push({x:o.connect.x,y:n.connect.y}):a.push({x:n.connect.x,y:o.connect.y});return a.push(n.connect,n.port),V(Bi(a)).points}c(Ye,"routeWithinContainer");function Et(t,e,o,n,r,i,a){let s=r.forest.nodeById.get(t),d=r.itemMeta.get(t);if(!s||!d||d.containerId!==n)throw D("GRID_ROUTE_NOT_FOUND",`Missing item metadata for "${t}" in "${n}"`);let u=k(s),l=a??i.get(o)??(e==="left"||e==="right"?u.cy:u.cx);switch(e){case"left":return{port:{x:u.left,y:l},connect:{x:d.leftCorridorX,y:l},orientation:"V",side:e};case"right":return{port:{x:u.right,y:l},connect:{x:d.rightCorridorX,y:l},orientation:"V",side:e};case"top":return{port:{x:l,y:u.top},connect:{x:l,y:d.topCorridorY},orientation:"H",side:e};case"bottom":return{port:{x:l,y:u.bottom},connect:{x:l,y:d.bottomCorridorY},orientation:"H",side:e}}}c(Et,"itemAttachment");function Wo(t,e,o,n,r){let i=n.forest.nodeById.get(t),a=n.containers.get(t);if(!i||!a)throw D("GRID_ROUTE_NOT_FOUND",`Missing boundary metadata for "${t}"`);let s=k(i),d=r.get(o)??(e==="left"||e==="right"?s.cy:s.cx);switch(e){case"left":return{port:{x:s.left,y:d},connect:{x:a.verticalCorridors[0],y:d},orientation:"V",side:e};case"right":return{port:{x:s.right,y:d},connect:{x:a.verticalCorridors[a.verticalCorridors.length-1],y:d},orientation:"V",side:e};case"top":return{port:{x:d,y:s.top},connect:{x:d,y:a.horizontalCorridors[0]},orientation:"H",side:e};case"bottom":return{port:{x:d,y:s.bottom},connect:{x:d,y:a.horizontalCorridors[a.horizontalCorridors.length-1]},orientation:"H",side:e}}}c(Wo,"boundaryAttachment");function Ni(t){return{x:(t.interior.x+t.exterior.x)/2,y:(t.interior.y+t.exterior.y)/2}}c(Ni,"portalBoundaryPoint");function ot(t,e){return{ownerId:t.ownerId,port:Ni(t),connect:e?t.interior:t.exterior,orientation:t.side==="left"||t.side==="right"?"V":"H",side:t.side}}c(ot,"portalAttachment");function Cn(t,e,o,n,r){let i=n.forest.nodeById.get(t);if(!i?.isGroup)throw D("GRID_ROUTE_NOT_FOUND",`Missing group endpoint "${t}"`);let a=k(i),s=r.get(o)??(e==="left"||e==="right"?a.cy:a.cx),d=e==="left"||e==="right"?{x:e==="left"?a.left:a.right,y:s}:{x:s,y:e==="top"?a.top:a.bottom};return{ownerId:t,port:d,connect:{x:d.x+(e==="left"?X:e==="right"?-X:0),y:d.y+(e==="top"?X:e==="bottom"?-X:0)},orientation:e==="left"||e==="right"?"V":"H",side:e}}c(Cn,"groupBoundaryEndpointAttachment");function Yo(t){return[...t].reverse()}c(Yo,"reversePoints");function qo(t){let e=[];for(let o of t)for(let n of o)(e.length===0||!Rt(e[e.length-1],n))&&e.push(n);return V(e).points}c(qo,"combinePointChains");function _i(t,e,o,n){let r=k(t),i=Pe+(o+1)*Xo,a=o*Ft;switch(e){case"right":{let s=n-ye/2-a,d=n+ye/2+a;return V([{x:r.right,y:s},{x:r.right+Pe,y:s},{x:r.right+i,y:s},{x:r.right+i,y:d},{x:r.right+Pe,y:d},{x:r.right,y:d}]).points}case"left":{let s=n-ye/2-a,d=n+ye/2+a;return V([{x:r.left,y:s},{x:r.left-Pe,y:s},{x:r.left-i,y:s},{x:r.left-i,y:d},{x:r.left-Pe,y:d},{x:r.left,y:d}]).points}case"top":{let s=n-ye/2-a,d=n+ye/2+a;return V([{x:s,y:r.top},{x:s,y:r.top-Pe},{x:s,y:r.top-i},{x:d,y:r.top-i},{x:d,y:r.top-Pe},{x:d,y:r.top}]).points}case"bottom":{let s=n-ye/2-a,d=n+ye/2+a;return V([{x:s,y:r.bottom},{x:s,y:r.bottom+Pe},{x:s,y:r.bottom+i},{x:d,y:r.bottom+i},{x:d,y:r.bottom+Pe},{x:d,y:r.bottom}]).points}}}c(_i,"routeSelfLoop");function Sn(t,e,o,n,r){let i=k(t),a=Pe+(o+1)*Xo,s=o*Ft,d=ye+s*2,u=e==="left"||e==="right",l=Math.max(d,(u?r.cross:r.along)+_e*2+Wt),f=Math.max(a,(u?r.along:r.cross)/2+_e),p=u?[i.top,i.bottom]:[i.left,i.right],h=n-l/2,g=n+l/2;if(h-p[0]>=Ko&&p[1]-g>=Ko)switch(e){case"right":return[V([{x:i.right,y:h},{x:i.right+f,y:h},{x:i.right+f,y:g},{x:i.right,y:g}]).points];case"left":return[V([{x:i.left,y:h},{x:i.left-f,y:h},{x:i.left-f,y:g},{x:i.left,y:g}]).points];case"top":return[V([{x:h,y:i.top},{x:h,y:i.top-f},{x:g,y:i.top-f},{x:g,y:i.top}]).points];case"bottom":return[V([{x:h,y:i.bottom},{x:h,y:i.bottom+f},{x:g,y:i.bottom+f},{x:g,y:i.bottom}]).points]}let b=n-ye/2-s,m=n+ye/2+s;if(b<p[0]||m>p[1])return[];let y=n-l/2,R=n+l/2,E=Math.max(Pe,X),S=Math.max(f,E+(u?r.along:r.cross)/2+_e);switch(e){case"right":return[V([{x:i.right,y:b},{x:i.right+E,y:b},{x:i.right+E,y},{x:i.right+S,y},{x:i.right+S,y:R},{x:i.right+E,y:R},{x:i.right+E,y:m},{x:i.right,y:m}]).points];case"left":return[V([{x:i.left,y:b},{x:i.left-E,y:b},{x:i.left-E,y},{x:i.left-S,y},{x:i.left-S,y:R},{x:i.left-E,y:R},{x:i.left-E,y:m},{x:i.left,y:m}]).points];case"top":return[V([{x:b,y:i.top},{x:b,y:i.top-E},{x:y,y:i.top-E},{x:y,y:i.top-S},{x:R,y:i.top-S},{x:R,y:i.top-E},{x:m,y:i.top-E},{x:m,y:i.top}]).points];case"bottom":return[V([{x:b,y:i.bottom},{x:b,y:i.bottom+E},{x:y,y:i.bottom+E},{x:y,y:i.bottom+S},{x:R,y:i.bottom+S},{x:R,y:i.bottom+E},{x:m,y:i.bottom+E},{x:m,y:i.bottom}]).points]}}c(Sn,"labelAwareSelfLoopCandidates");function Mn(t,e,o){return(t.isGroup&&xt(t)?["left","right","bottom"]:["left","right","top","bottom"]).sort((r,i)=>{let a=(e.get(Xe(t.id,r))??0)+(o.get(`${t.id}:${r}`)??0),s=(e.get(Xe(t.id,i))??0)+(o.get(`${t.id}:${i}`)??0);return a-s})}c(Mn,"orderedSelfLoopSides");function Ti(t,e){let o=[];for(let n of e.forest.nodeById.values())if(n.id!==t.id&&!n.isGroup&&n.x!==void 0&&n.y!==void 0&&o.push(k(n)),n.id!==t.id&&n.groupTitleRect){let{left:r,right:i,top:a,bottom:s}=n.groupTitleRect;o.push({cx:(r+i)/2,cy:(a+s)/2,left:r,right:i,top:a,bottom:s})}return o}c(Ti,"selfLoopObstacles");function Qo(t,e,o,n,r,i){let a=k(t),s=Ti(t,n);for(let d of Mn(t,e,o)){let u=`${t.id}:${d}`,l=o.get(u)??0,f=d==="left"||d==="right"?a.cy:a.cx,p=r?Sn(t,d,l,f,r):[_i(t,d,l,f)];r&&i&&(i.labelAwareSelfLoopCandidates+=p.length);for(let h of p)if(!s.some(g=>Re(h,g)))return{points:h,side:d,index:l}}throw D("GRID_ROUTE_NOT_FOUND",`No obstacle-clear self-loop route for "${t.id}"`,{nodeId:t.id})}c(Qo,"routeObstacleClearSelfLoop");function Zo(t){if(t.containers.has(F))return;let e=0,o=0,n=0,r=0,i=!0;for(let d of t.itemMeta.values())d.containerId===F&&(i?(e=d.cellLeft,o=d.cellLeft+d.cellWidth,n=d.cellTop,r=d.cellTop+d.cellHeight,i=!1):(e=Math.min(e,d.cellLeft),o=Math.max(o,d.cellLeft+d.cellWidth),n=Math.min(n,d.cellTop),r=Math.max(r,d.cellTop+d.cellHeight)));let a=new Set([n-Me,r+Me]),s=new Set([e-Me,o+Me]);for(let d of t.itemMeta.values())d.containerId===F&&(a.add(d.topCorridorY),a.add(d.bottomCorridorY),s.add(d.leftCorridorX),s.add(d.rightCorridorX));t.containers.set(F,{id:F,left:e-Me,top:n-Me,width:o-e+Me*2,height:r-n+Me*2,contentLeft:e,contentTop:n,contentRight:o,contentBottom:r,verticalCorridors:[...s].sort((d,u)=>d-u),horizontalCorridors:[...a].sort((d,u)=>d-u)})}c(Zo,"rootContainerMeta");function Di(t,e){let o=e.containers.get(t);if(!o)throw D("GRID_ROUTE_NOT_FOUND",`Missing container layout "${t}"`);if(t===F){let i=[...e.itemMeta.values()].filter(({containerId:a})=>a===F);return i.length>0?{left:Math.min(...i.map(({cellLeft:a})=>a))-Me,right:Math.max(...i.map(({cellLeft:a,cellWidth:s})=>a+s))+Me,top:Math.min(...i.map(({cellTop:a})=>a))-Me,bottom:Math.max(...i.map(({cellTop:a,cellHeight:s})=>a+s))+Me}:{left:o.left,right:o.left+o.width,top:o.top,bottom:o.top+o.height}}let n=e.forest.nodeById.get(t);if(!n)throw D("GRID_ROUTE_NOT_FOUND",`Missing container owner "${t}"`);let r=ve(n);return{left:r.left+j,right:r.right-j,top:r.top+j,bottom:r.bottom-j}}c(Di,"containerBounds");function Uo(t,e,o){return(e==="left"?t.left:e==="right"?t.right:e==="top"?t.top:t.bottom)+(e==="left"||e==="top"?1:-1)*j*(o?1:-1)}c(Uo,"portalCoordinate");function ki(t,e){let o=[];if(t!==F){let n=e.forest.nodeById.get(t);if(n){let r=ve(n);o.push(...Ue(t,r,n.groupTitleRect).map(i=>({...i,coordinate:Uo(r,i.side,!0)})))}}for(let n of e.forest.childrenByParent.get(t)??[]){if(!n.isGroup)continue;let r=ve(n);o.push(...Ue(n.id,r,n.groupTitleRect).map(i=>({...i,coordinate:Uo(r,i.side,!1)})))}return o}c(ki,"containerPortalRanges");function Jo(t,e,o,n){let r={topologies:new Map,fallbackContainers:new Map,searchBudget:{expandedStates:0},searchBudgetWarningEmitted:!1,baseEstimatedBytes:0,metrics:o},i=0;for(let a of[...new Set(t)].sort()){let s=(e.forest.childrenByParent.get(a)??[]).filter(u=>!ce(u)),d=e.forest.nodeById.get(a)?.groupTitleRect;try{let u=Ao({containerId:a,bounds:Di(a,e),obstacles:s.map(f=>({id:f.id,bounds:ve(f)})),titleExclusions:d?[{id:`${a}:title`,bounds:{...d}}]:void 0,portalRanges:ki(a,e)},{caps:n.topologyCaps}),l=n.topologyCaps?.maxEstimatedBytes??Ae;if(i+u.estimatedBytes>l){r.fallbackContainers.set(a,"estimated_memory_cap");continue}i+=u.estimatedBytes,r.baseEstimatedBytes=i,r.topologies.set(a,u),o&&(o.containersBuilt++,o.baseTopologyBuilds++,o.baseVertices+=u.vertices.length,o.baseAdjacencyEntries+=u.adjacencyEntries,o.buildSweepEvents+=u.seedCount*4+u.obstacles.length*4,o.estimatedBytes=i)}catch(u){if(!(u instanceof le))throw u;r.fallbackContainers.set(a,u.reason)}}return r}c(Jo,"buildRoutingContext");function wn(t,e,o,n,r,i,a){let s=r();if(a&&a.compatibilitySegments++,Ve(s,o.ownerId,n.ownerId,e,i))return s;throw a&&(a.compatibilityValidationFailures++,a.routesImpossible++),D("GRID_ROUTE_NOT_FOUND",`Invalid compatibility route for "${t}"`,{edgeId:t,containerId:e})}c(wn,"validatedCompatibilitySegment");function It(t,e,o){let n=t.source.chain.at(-1),r=t.target.chain.at(-1),i=c((a,s)=>({ownerId:s.ownerId,...a.finalKind==="boundary"?Wo(t.lcaContainerId,s.side,s.demandKey,e,o):Et(s.ownerId,s.side,s.demandKey,t.lcaContainerId,e,o)}),"finalAttachment");return{start:i(t.source,n),end:i(t.target,r)}}c(It,"compatibilityLcaAttachments");function Gn(t,e,o){return t.chain.slice(0,-1).map((n,r)=>{let i=t.chain[r+1];return{containerId:i.ownerId,start:{ownerId:n.ownerId,...Et(n.ownerId,n.side,n.demandKey,i.ownerId,e,o)},end:{ownerId:i.ownerId,...Wo(i.ownerId,i.side,i.demandKey,e,o)}}})}c(Gn,"compatibilityHierarchyAttachments");function Vi(t,e,o){let n=c((i,a,s)=>Ve(Ye(i,e,a,s,t.laneIndex),a.ownerId,s.ownerId,i,e),"segmentIsValid");for(let i of[t.source,t.target])for(let{containerId:a,start:s,end:d}of Gn(i,e,o))if(!n(a,s,d))return!1;let r=It(t,e,o);return n(t.lcaContainerId,r.start,r.end)}c(Vi,"compatibilityPlanIsValid");function er(t,e,o,n){let{plans:r,eligiblePlans:i,eligibleIds:a,demandCoords:s,endpointCandidatesByEdge:d,endpointIncidentCounts:u,coordinatedEndpointIds:l}=t,f=c(x=>(u.get(x.edge.start)??0)===x.bundleSize&&(u.get(x.edge.end)??0)===x.bundleSize,"hasIsolatedEndpoints"),p=c(x=>{let w=e.forest.nodeById.get(x.edge.start),G=e.forest.nodeById.get(x.edge.end);return!!(w&&G&&(w.isGroup&&Te(w.id,G,e.forest.nodeById)||G.isGroup&&Te(G.id,w,e.forest.nodeById)))},"hasAncestorEndpoint"),h=new Map,g=new Map,b=r.length<=Pi,m=n.topologyCaps===void 0&&n.searchCaps===void 0&&n.onDualRouteComparison===void 0;for(let x of m?i:[]){if(l.has(x.edge.id))continue;let w=d.get(x.edge.id);if(x.bundleSize!==1||!w?.sources.length||!w.targets.length)continue;let G=e.forest.nodeById.get(x.edge.start),C=e.forest.nodeById.get(x.edge.end);if(!G||!C)continue;let v=It(x,e,s),A=Ye(x.lcaContainerId,e,v.start,v.end,x.laneIndex),O=V(A),_=O.points[0],N=O.points.at(-1);if(!_||!N)continue;let W=w.sources.find(({port:K})=>K.x===_.x&&K.y===_.y),q=w.targets.find(({port:K})=>K.x===N.x&&K.y===N.y);if(!W||!q)continue;o&&o.compatibilityFastPathAttempts++;let H=Math.abs(_.x-N.x)+Math.abs(_.y-N.y);if(!Ie(A,G,C,x.lcaContainerId,e)){o&&o.compatibilityFastPathValidationFailures++;continue}if(Fe(O.points)!==H){o&&o.compatibilityFastPathNonMinimalRoutes++;continue}let Y=g.get(x.lcaContainerId);if(Y||(Y=new We,g.set(x.lcaContainerId,Y)),Y.conflictsWithRoute(O.points,x.pairKey)&&b)continue;let Z=Li(O.points,Y,x.pairKey,K=>Ai(K)&&Fe(K)===H&&Ie(K,G,C,x.lcaContainerId,e))??A;Y.add(Z,x.pairKey),h.set(x.edge.id,Z)}let y=c(x=>!a.has(x.edge.id)&&x.edge.start!==x.edge.end&&(x.bundleSize>1||f(x)||!Vi(x,e,s)),"sparseCandidate"),R=new Map,E=new Set;if(b)for(let x of t.orderedPlans){if(a.has(x.edge.id)||x.edge.start===x.edge.end||p(x)||y(x))continue;let w=It(x,e,s),G=Ye(x.lcaContainerId,e,w.start,w.end,x.laneIndex),C=R.get(x.lcaContainerId);C||(C=new We,R.set(x.lcaContainerId,C)),C.conflictsWithRoute(G,x.pairKey)?E.add(x.edge.id):C.add(G,x.pairKey)}let S=new Set([...r.filter(y).map(({edge:x})=>x.id),...E]),M=new Set([...S,...r.filter(p).map(({edge:x})=>x.id)]),I=r.filter(x=>a.has(x.edge.id)&&!h.has(x.edge.id)||M.has(x.edge.id)||x.edge.start===x.edge.end).flatMap(x=>[x.lcaContainerId,...x.source.chain.slice(1).map(({ownerId:w})=>w),...x.target.chain.slice(1).map(({ownerId:w})=>w)]);return{overlapAvoidanceEnabled:b,compatibilityFastRoutes:h,sparseHierarchyIds:S,sparseLcaIds:M,routedContainerIds:I,routeOccupancy:g}}c(er,"prepareRoutingModes");var tr={containersBuilt:"cumulative",baseTopologyBuilds:"cumulative",baseVertices:"cumulative",baseAdjacencyEntries:"cumulative",buildSweepEvents:"cumulative",endpointOverlayBuilds:"cumulative",endpointOverlayVertices:"cumulative",hierarchyPortalPairs:"transactional",hierarchyPortalTransitionLength:"transactional",hierarchyPortalAlternativeAttempts:"cumulative",hierarchyPortalAlternativeSelections:"transactional",hierarchyBoundaryTransitions:"transactional",labelOverlayBuilds:"cumulative",labelOverlayVertices:"cumulative",labelSpacingEdgesExamined:"cumulative",labelSpacingEligibleEdges:"cumulative",labelSpacingBoundariesExpanded:"cumulative",labelSpacingPixelsAdded:"cumulative",labelAwareSelfLoopCandidates:"cumulative",labelAwareSelfLoopsCommitted:"transactional",labelAwareSelfLoopFallbacks:"cumulative",searches:"cumulative",expandedStates:"cumulative",maxOpenSet:"cumulative",routesFound:"transactional",routesImpossible:"transactional",resourceLimitFallbacks:"cumulative",fallbackReasons:"cumulative",fallbackValidationFailures:"cumulative",compatibilitySegments:"transactional",compatibilityFastPathAttempts:"cumulative",compatibilityFastPaths:"cumulative",compatibilityFastPathValidationFailures:"cumulative",compatibilityFastPathNonMinimalRoutes:"cumulative",compatibilityRecoveries:"transactional",compatibilityValidationFailures:"cumulative",bundleSeparationRelaxations:"transactional",routeLength:"transactional",bendCount:"transactional",crossingCount:"transactional",sharedLength:"transactional",estimatedBytes:"cumulative",searchWorkspaceBytes:"cumulative",routeOrder:"transactional",routes:"transactional"},Fi=Object.keys(tr).filter(t=>tr[t]==="transactional"&&t!=="routeOrder"&&t!=="routes");function or(t){return{routeOrderLength:t.routeOrder.length,routesLength:t.routes.length,values:Object.fromEntries(Fi.map(e=>[e,t[e]]))}}c(or,"createGridRoutingInstrumentationCheckpoint");function rr(t,e){t.routeOrder.length=e.routeOrderLength,t.routes.length=e.routesLength,Object.assign(t,e.values)}c(rr,"restoreGridRoutingInstrumentationCheckpoint");function nr(t,e,o,n){return Math.max(0,Math.min(Math.max(t,e),Math.max(o,n))-Math.max(Math.min(t,e),Math.min(o,n)))}c(nr,"intervalOverlap");function ji(t,e){let o=V(t).segments,n=V(e).segments,r=0;for(let i of o)for(let a of n)i.orientation===a.orientation&&(i.orientation==="H"&&i.a.y===a.a.y?r+=nr(i.a.x,i.b.x,a.a.x,a.b.x):i.orientation==="V"&&i.a.x===a.a.x&&(r+=nr(i.a.y,i.b.y,a.a.y,a.b.y)));return r}c(ji,"sharedSegmentLength");function vn(t,e,o,n,r=0,i=0){let a=V(o),s=0,d=0;for(let l of n){let f=V(l).segments;for(let p of a.segments)for(let h of f)Mt(p,h)&&s++;d+=ji(a.points,l)}let u={edgeId:e,routeOrder:t.routes.length,laneOffset:i,routeLength:Fe(a.points),bendCount:a.bends,crossingCount:s,sharedLength:d,boundaryTransitionCount:r,routeSignature:JSON.stringify(a.points)};t.routeOrder.push(e),t.routes.push(u),t.routesFound++,t.routeLength+=u.routeLength,t.bendCount+=u.bendCount,t.crossingCount+=u.crossingCount,t.sharedLength+=u.sharedLength,t.hierarchyBoundaryTransitions+=r}c(vn,"recordGridRoute");var dr=1e5,cr=2e6,Ct=class{constructor(){this.stateVertices=new Int32Array(256);this.orientations=new Uint8Array(256);this.gLengths=new Float64Array(256);this.gBends=new Float64Array(256);this.gBoundaryTransitions=new Float64Array(256);this.fLengths=new Float64Array(256);this.fBends=new Float64Array(256);this.predecessors=new Int32Array(256);this.depths=new Int32Array(256);this.ancestor4=new Int32Array(256);this.best=new Int32Array(256);this.heapChildren=new Int32Array(256);this.heapSiblings=new Int32Array(256);this.heapPairs=[];this.touchedBestIndices=[];this.best.fill(-1)}static{c(this,"RouterSearchWorkspace")}reset(e){let o=e*3;if(this.best.length<o)this.best=new Int32Array(o),this.best.fill(-1);else for(let n of this.touchedBestIndices)this.best[n]=-1;this.touchedBestIndices.length=0,this.heapPairs.length=0}setBest(e,o){this.best[e]<0&&this.touchedBestIndices.push(e),this.best[e]=o}ensureStateCapacity(e){if(this.stateVertices.length>=e)return;let o=Math.max(e,this.stateVertices.length*2);this.stateVertices=we(this.stateVertices,o),this.orientations=we(this.orientations,o),this.gLengths=we(this.gLengths,o),this.gBends=we(this.gBends,o),this.gBoundaryTransitions=we(this.gBoundaryTransitions,o),this.fLengths=we(this.fLengths,o),this.fBends=we(this.fBends,o),this.predecessors=we(this.predecessors,o),this.depths=we(this.depths,o),this.ancestor4=we(this.ancestor4,o),this.heapChildren=we(this.heapChildren,o),this.heapSiblings=we(this.heapSiblings,o)}estimatedBytes(){return this.stateVertices.byteLength+this.orientations.byteLength+this.gLengths.byteLength+this.gBends.byteLength+this.gBoundaryTransitions.byteLength+this.fLengths.byteLength+this.fBends.byteLength+this.predecessors.byteLength+this.depths.byteLength+this.ancestor4.byteLength+this.best.byteLength+this.heapChildren.byteLength+this.heapSiblings.byteLength+(this.heapPairs.length+this.touchedBestIndices.length)*8}};function St(t){let e=t.caps?.maxExpandedStates??dr,o=t.caps?.maxInvocationExpandedStates??cr,n=t.budget?.expandedStates??t.metrics?.expandedStates??0;if(!(e>0&&n<o))throw new le("search_state_cap","Grid routing search-state cap exceeded",n>=o?"invocation":"edge")}c(St,"assertSearchBudgetAvailable");function we(t,e){let o=new t.constructor(e);return o.set(t),o}c(we,"growTypedArray");function rt(t,e){for(let o=0;o<4;o++){if(t[o]<e[o])return-1;if(t[o]>e[o])return 1}return 0}c(rt,"compareTupleCost");function ir(t,e,o,n){return t===0&&e===0?o&&n?+(o!==n):0:t===0?(o?+(o!=="V"):0)+(n?+(n!=="V"):0):e===0?(o?+(o!=="H"):0)+(n?+(n!=="H"):0):Math.min((o?+(o!=="H"):0)+1+(n?+(n!=="V"):0),(o?+(o!=="V"):0)+1+(n?+(n!=="H"):0))}c(ir,"minimumBends");function jt(t){return t===void 0?0:t==="H"?1:2}c(jt,"orientationOrdinal");function ar(t,e,o){return o==="left"?t.y===e.y&&e.x>t.x:o==="right"?t.y===e.y&&e.x<t.x:o==="top"?t.x===e.x&&e.y>t.y:t.x===e.x&&e.y<t.y}c(ar,"movesTowardOwner");function zi(t,e){return t*3+jt(e)}c(zi,"stateIndex");function Hi(t){let e=[];for(let o of t){let n=e.at(-1);if(n&&n.x===o.x&&n.y===o.y)continue;let r=e.at(-2);r&&n&&(r.x===n.x&&n.x===o.x||r.y===n.y&&n.y===o.y)?e[e.length-1]={...o}:e.push({...o})}return e}c(Hi,"normalizePoints");function sr(t,e,o,n,r){let i=[],a=t;for(;a>=0;)i.push(e[a]),a=o[a];return i.reverse(),{points:Hi(i.map(s=>r(s).point)),vertexIds:i,cost:n}}c(sr,"reconstruct");function $i(t){for(let e of t.vertices){if(e.id<0||t.vertices[e.id]!==e||!Number.isFinite(e.point.x)||!Number.isFinite(e.point.y))throw new Error("Malformed grid routing vertex");for(let o of t.adjacency.get(e.id)??[]){let n=t.vertices[o.to],r=n&&(o.orientation==="H"?e.point.y===n.point.y:e.point.x===n.point.x),i=n?Math.abs(e.point.x-n.point.x)+Math.abs(e.point.y-n.point.y):Number.NaN;if(o.from!==e.id||!r||!Number.isFinite(o.length)||o.length<=0||o.length!==i)throw new Error("Malformed grid routing arc")}}}c($i,"validateSearchTopology");function Ki(t,e,o,n){let r=c(L=>{let B=t.getVertex?.(L)??t.vertices[L];if(!B)throw new Error(`Missing grid routing vertex ${L}`);return B},"vertexAt"),i=r(e),a=r(o);n.topologyValidated||$i(t);let s=n.metrics;s&&s.searches++,St(n);let d=n.workspace??new Ct;d.reset(t.vertexCount??t.vertices.length);let u=d.stateVertices,l=d.orientations,f=d.gLengths,p=d.gBends,h=d.gBoundaryTransitions,g=d.fLengths,b=d.fBends,m=d.predecessors,y=d.depths,R=d.ancestor4,E=d.heapChildren,S=d.heapSiblings,M=d.best,I=d.heapPairs,x=1,w=n.endpointCandidateRank??0,G=n.heuristic!=="zero";function C(L,B){return u[L]-u[B]||l[L]-l[B]||m[L]-m[B]||L-B}c(C,"nonCostOrder");function v(L,B){return g[L]-g[B]||b[L]-b[B]||h[L]-h[B]||(n.queueOrder==="reverse"?-C(L,B):C(L,B))}c(v,"compareStates");function A(L,B){if(L===B)return 0;if(L<0)return-1;if(B<0)return 1;let se=y[L],de=y[B],J=L,$=B;for(;y[J]>y[$];)J=m[J];for(;y[$]>y[J];)$=m[$];if(J===$)return se-de;for(;R[J]>=0&&R[$]>=0&&R[J]!==R[$];)J=R[J],$=R[$];for(;m[J]!==m[$];)J=m[J],$=m[$];return u[J]-u[$]||l[J]-l[$]}c(A,"compareChains");let O=-1,_=0;function N(L,B){return L<0?B:B<0?L:v(L,B)<=0?(S[B]=E[L],E[L]=B,L):(S[L]=E[B],E[B]=L,B)}c(N,"heapMeld");let W=c(L=>{E[L]=-1,S[L]=-1,O=N(O,L),_++},"heapPush"),q=c(()=>{if(O<0)return;let L=O;I.length=0;let B=E[L];for(;B>=0;){let de=S[B];S[B]=-1,I.push(B),B=de}for(let de=0;de+1<I.length;de+=2)I[de>>>1]=N(I[de],I[de+1]);let se=Math.ceil(I.length/2);for(I.length%2!==0&&(I[se-1]=I[I.length-1]),O=-1;se>0;)O=N(I[--se],O);return _--,L},"heapPop"),H=0,Y=n.caps?.maxExpandedStates??dr,be=n.caps?.maxInvocationExpandedStates??cr,ne=n.budget?.expandedStates??s?.expandedStates??0,Z=c(()=>{let L=d.estimatedBytes(),B=(n.estimatedBytesBase??0)+L;if(s&&(s.searchWorkspaceBytes=Math.max(s.searchWorkspaceBytes,L),s.estimatedBytes=Math.max(s.estimatedBytes,B)),B>(n.caps?.maxEstimatedBytes??Number.POSITIVE_INFINITY))throw new le("estimated_memory_cap","Grid routing search workspace memory cap exceeded")},"recordWorkspaceBytes");Z();let K=c(()=>{n.budget&&(n.budget.expandedStates+=H),s&&(s.expandedStates+=H)},"commitExpandedStates"),xe=n.initialLength??0;u[0]=e,l[0]=jt(n.initialOrientation),f[0]=xe,p[0]=0,h[0]=0;let ie=Math.abs(i.point.x-a.point.x),pe=Math.abs(i.point.y-a.point.y);g[0]=xe+(G?ie+pe:0),b[0]=G?ir(ie,pe,n.initialOrientation,n.targetOrientation):0,m[0]=-1,y[0]=1,R[0]=-1,d.setBest(zi(e,n.initialOrientation),0),W(0),s&&(s.maxOpenSet=Math.max(s.maxOpenSet,_));let te=-1,P,T=c(L=>{let B=[f[L]+(n.targetLength??0),p[L]+(n.targetOrientation!==void 0&&l[L]!==0&&jt(n.targetOrientation)!==l[L]?1:0),h[L],w];(!P||rt(B,P)<0||rt(B,P)===0&&A(L,te)<0)&&(te=L,P=B)},"considerGoal"),ae=c((L,B,se,de,J,$)=>{let Ge=r(se).point;if(n.arcAllowed&&!n.arcAllowed(B,Ge)||n.initialSide&&m[L]<0&&ar(B,Ge,n.initialSide)||n.targetSide&&se===o&&ar(a.point,B,n.targetSide))return;let at=se*3+de,Oe=M[at],st=f[L]+J,dt=p[L]+(l[L]!==0&&l[L]!==de?1:0),ct=h[L]+$,ut=Oe>=0?st-f[Oe]||dt-p[Oe]||ct-h[Oe]:-1;if(Oe>=0&&(ut>0||ut===0&&A(L,m[Oe])>=0))return;let Ee=x++;x>u.length&&(d.ensureStateCapacity(x),u=d.stateVertices,l=d.orientations,f=d.gLengths,p=d.gBends,h=d.gBoundaryTransitions,g=d.fLengths,b=d.fBends,m=d.predecessors,y=d.depths,R=d.ancestor4,E=d.heapChildren,S=d.heapSiblings,Z()),u[Ee]=se,l[Ee]=de,f[Ee]=st,p[Ee]=dt,h[Ee]=ct;let On=Math.abs(Ge.x-a.point.x),Ln=Math.abs(Ge.y-a.point.y);g[Ee]=st+(G?On+Ln:0),b[Ee]=dt+(G?ir(On,Ln,de===1?"H":"V",n.targetOrientation):0),m[Ee]=L,y[Ee]=y[L]+1;let An=m[L],Bn=An>=0?m[An]:-1;R[Ee]=Bn>=0?m[Bn]:-1,d.setBest(at,Ee),W(Ee)},"relaxArc");for(;_>0;){let L=O;if(P&&(g[L]-P[0]||b[L]-P[1]||h[L]-P[2]||w-P[3])>0)break;let B=q();if(M[u[B]*3+l[B]]!==B)continue;if(H>=Y||ne+H>=be){if(te>=0&&P)return s&&n.recordOutcome!==!1&&s.routesFound++,K(),sr(te,u,m,P,r);K();let $=ne+H>=be?"invocation":"edge";throw new le("search_state_cap","Grid routing search-state cap exceeded",$)}if(H++,u[B]===o){T(B);continue}let se=u[B],de=r(se).point,J=t.getSearchArcs?.(se)??t.searchAdjacencyByVertex?.[se];if(J)for(let $ of J)ae(B,de,$.to,$.orientationOrdinal,$.length,$.boundaryTransitions);else for(let $ of t.adjacencyByVertex?.[se]??t.adjacency.get(se)??[])ae(B,de,$.to,jt($.orientation),$.length,$.kind==="portal"?1:0);s&&(s.maxOpenSet=Math.max(s.maxOpenSet,_))}if(te<0||!P){K(),s&&n.recordOutcome!==!1&&s.routesImpossible++;return}return s&&n.recordOutcome!==!1&&s.routesFound++,K(),sr(te,u,m,P,r)}c(Ki,"search");function zt(t,e,o,n={}){return Ki(t,e,o,n)}c(zt,"findShortestRoute");var ur="grid-router";function Ui(t,e,o,n,r){t.metrics&&(t.metrics.resourceLimitFallbacks++,t.metrics.fallbackReasons[e]++),Be.debug(ur,"GRID_ROUTING_RESOURCE_FALLBACK",{edgeId:o,containerId:n,reason:e}),e==="search_state_cap"&&r==="invocation"&&!t.searchBudgetWarningEmitted&&(t.searchBudgetWarningEmitted=!0,Be.warn(ur,"GRID_ROUTING_INVOCATION_SEARCH_BUDGET_EXHAUSTED",{edgeId:o,containerId:n,expandedStates:t.searchBudget.expandedStates}))}c(Ui,"recordFallback");function it({context:t,reason:e,edgeId:o,containerId:n,searchStateScope:r,route:i,isValid:a,includeContainerIdInError:s}){let d=i();if(Ui(t,e,o,n,r),a(d))return d;throw t.metrics&&t.metrics.fallbackValidationFailures++,D("GRID_ROUTE_NOT_FOUND",`Invalid legacy fallback for "${o}"`,{edgeId:o,...s?{containerId:n}:{},reason:e})}c(it,"runValidatedFallback");function lr(t,e,o,n,r,i,a,s,d,u,l,f,p){let h=s.fallbackContainers.get(t.lcaContainerId);if(h)return it({context:s,reason:h,edgeId:t.edge.id,containerId:t.lcaContainerId,route:i,isValid:c(S=>Ie(S,n,r,t.lcaContainerId,a)&&(t.bundleSize<=1||he(S,f)),"isValid")});let g=s.topologies.get(t.lcaContainerId);if(!g)throw D("GRID_ROUTE_NOT_FOUND",`Missing routing topology "${t.lcaContainerId}"`);if(e.length===0||o.length===0)throw s.metrics&&s.metrics.routesImpossible++,D("GRID_ROUTE_NOT_FOUND",t.bundleSize>1?`No distinct lane route for "${t.edge.id}"`:`No legal endpoint candidates for "${t.edge.id}"`,{edgeId:t.edge.id});let b,m,y=e.flatMap(S=>o.map(M=>{let I=S.rank*o.length+M.rank,[x,w]=$o(S,M);return{sourceCandidate:S,targetCandidate:M,pairRank:I,lowerCost:[x,w,0,I]}})).sort((S,M)=>rt(S.lowerCost,M.lowerCost)||ke(S.sourceCandidate.side)-ke(M.sourceCandidate.side)||ke(S.targetCandidate.side)-ke(M.targetCandidate.side)),R=c((S,M="all")=>{let I=t.bundleSize>1||S?(x,w)=>(t.bundleSize<=1||Vt(x,w,f))&&!(S&&p.index.conflictsWithArc(x,w,p.pairKey)):void 0;for(let{sourceCandidate:x,targetCandidate:w,pairRank:G,lowerCost:C}of y)if(!(M==="preferred"&&G!==0)&&!(M==="assigned-side"&&(x.onAssignedSide===!1||w.onAssignedSide===!1))){if(b&&rt(b.result.cost,C)<=0)break;try{St({metrics:s.metrics,caps:l.searchCaps,budget:s.searchBudget});let v=Tt(g,x.connect,w.connect,s.metrics,u,t.bundleSize>1&&(x.connect.x===w.connect.x||x.connect.y===w.connect.y)?[{x:(x.connect.x+w.connect.x)/2,y:(x.connect.y+w.connect.y)/2}]:[]),A=s.baseEstimatedBytes+v.estimatedBytes-g.estimatedBytes;if(A>(l.topologyCaps?.maxEstimatedBytes??Ae))throw new le("estimated_memory_cap","Grid routing estimated_memory_cap exceeded");s.metrics&&(s.metrics.estimatedBytes=Math.max(s.metrics.estimatedBytes,A));let O=v.pointVertexIds.get(ge(x.connect)),_=v.pointVertexIds.get(ge(w.connect));if(O===void 0||_===void 0)throw D("GRID_ROUTE_NOT_FOUND",`Endpoint projection is missing from the routing overlay for "${t.edge.id}"`,{edgeId:t.edge.id,containerId:t.lcaContainerId,source:x.connect,target:w.connect});let N=x.side==="left"||x.side==="right"?"H":"V",W=w.side==="left"||w.side==="right"?"H":"V",q=zt(v,O,_,{metrics:s.metrics,caps:{...l.searchCaps,maxEstimatedBytes:l.searchCaps?.maxEstimatedBytes??l.topologyCaps?.maxEstimatedBytes??Ae},endpointCandidateRank:G,recordOutcome:!1,budget:s.searchBudget,initialOrientation:N,initialLength:X,targetOrientation:W,targetLength:X,topologyValidated:!0,workspace:d,estimatedBytesBase:A,arcAllowed:I});if(!q)continue;let H=V([x.port,...q.points,w.port]).points;if(!Ie(H,n,r,t.lcaContainerId,a)||t.bundleSize>1&&!he(H,f)||S&&p.index.conflictsWithRoute(H,p.pairKey))continue;(!b||rt(q.cost,b.result.cost)<0)&&(b={result:q,points:H},m=void 0)}catch(v){if(!(v instanceof le))throw v;if(b)break;m=v;break}}},"searchPairs"),E=p!==void 0&&p.index.size>0;if(p?.prioritizeEndpointCandidates&&(R(E,"preferred"),R(E,"assigned-side")),b||R(E),!b&&!m&&p!==void 0&&p.index.size>0&&R(!1),m)return it({context:s,reason:m.reason,edgeId:t.edge.id,containerId:t.lcaContainerId,searchStateScope:m.searchStateScope,route:i,isValid:c(S=>Ie(S,n,r,t.lcaContainerId,a)&&(t.bundleSize<=1||he(S,f)),"isValid")});if(!b)throw s.metrics&&s.metrics.routesImpossible++,D("GRID_ROUTE_NOT_FOUND",t.bundleSize>1?`No distinct lane route for "${t.edge.id}"`:`No cell-aware route for "${t.edge.id}"`,{edgeId:t.edge.id});if(l.onDualRouteComparison){let S=i(),M=V(b.points),I=V(S);l.onDualRouteComparison({edgeId:t.edge.id,sparse:{valid:!0,length:Fe(M.points),bends:M.bends},legacy:{valid:Ie(S,n,r,t.lcaContainerId,a),length:Fe(I.points),bends:I.bends}})}return b.points}c(lr,"sparseSameContainerRoute");function Xi(t,e,o){let n=Se(t,e);if(!n||n.high-n.low<ye)return;let r=k(t),i=(n.low+n.high)/2,a=o*Ft,s=[i-ye/2-a,i+ye/2+a];if(s[0]<n.low||s[1]>n.high)return;let d=c(u=>{let l=e==="left"||e==="right"?{x:e==="left"?r.left:r.right,y:u}:{x:u,y:e==="top"?r.top:r.bottom};return{ownerId:t.id,side:e,port:l,connect:{x:l.x+(e==="left"?-X:e==="right"?X:0),y:l.y+(e==="top"?-X:e==="bottom"?X:0)},rank:0}},"candidate");return{start:d(s[0]),target:d(s[1])}}c(Xi,"selfLoopAttachments");function pr(t,e,o,n,r,i,a,s,d,u){let l=e.parentId??F,f=o.labelRequirements.selfLoops.get(t.edge.id),p=n.fallbackContainers.get(l),h=c(()=>Qo(e,a,s,o,f,n.metrics),"legacyRoute");if(p){let m=it({context:n,reason:p,edgeId:t.edge.id,containerId:l,route:h,isValid:c(y=>Ie(y.points,e,e,l,o)&&he(y.points,d),"isValid")});return f&&n.metrics&&n.metrics.labelAwareSelfLoopsCommitted++,m}let g=n.topologies.get(l);if(!g||!i)throw D("GRID_ROUTE_NOT_FOUND",`Missing routing topology "${l}"`);let b=Mn(e,a,s);if(f){for(let m of b){let y=s.get(`${e.id}:${m}`)??0,R=k(e),E=m==="left"||m==="right"?R.cy:R.cx,S=Sn(e,m,y,E,f);n.metrics&&(n.metrics.labelAwareSelfLoopCandidates+=S.length);for(let M of S)if(Ie(M,e,e,l,o)&&he(M,d))return n.metrics&&n.metrics.labelAwareSelfLoopsCommitted++,{points:M,side:m,index:y}}n.metrics&&n.metrics.labelAwareSelfLoopFallbacks++}for(let m of b){let y=s.get(`${e.id}:${m}`)??0,R=Xi(e,m,y);if(!R)continue;let{start:E,target:S}=R;try{St({metrics:n.metrics,caps:u.searchCaps,budget:n.searchBudget});let M=Tt(g,E.connect,S.connect,n.metrics,i,[{x:(E.connect.x+S.connect.x)/2,y:(E.connect.y+S.connect.y)/2}]),I=M.pointVertexIds.get(ge(E.connect)),x=M.pointVertexIds.get(ge(S.connect));if(I===void 0||x===void 0)throw D("GRID_ROUTE_NOT_FOUND",`Self-loop endpoint projection is missing from the routing overlay for "${t.edge.id}"`,{edgeId:t.edge.id,containerId:l,source:E.connect,target:S.connect,side:m});let w=m==="left"||m==="right"?"H":"V",G=zt(M,I,x,{metrics:n.metrics,caps:{...u.searchCaps,maxEstimatedBytes:u.searchCaps?.maxEstimatedBytes??u.topologyCaps?.maxEstimatedBytes??Ae},recordOutcome:!1,budget:n.searchBudget,initialOrientation:w,initialSide:m,initialLength:X,targetOrientation:w,targetSide:m,targetLength:X,topologyValidated:!0,workspace:r,estimatedBytesBase:n.baseEstimatedBytes+M.estimatedBytes-g.estimatedBytes,arcAllowed:c((v,A)=>Vt(v,A,d),"arcAllowed")});if(!G)continue;let C=V([E.port,...G.points,S.port]).points;if(Ie(C,e,e,l,o)&&he(C,d))return{points:C,side:m,index:y}}catch(M){if(!(M instanceof le))throw M;let I=it({context:n,reason:M.reason,edgeId:t.edge.id,containerId:l,searchStateScope:M.searchStateScope,route:h,isValid:c(x=>Ie(x.points,e,e,l,o)&&he(x.points,d),"isValid")});return f&&n.metrics&&n.metrics.labelAwareSelfLoopsCommitted++,I}}throw n.metrics&&n.metrics.routesImpossible++,D("GRID_ROUTE_NOT_FOUND",`No distinct self-loop route for "${t.edge.id}"`,{edgeId:t.edge.id,nodeId:e.id})}c(pr,"sparseSelfLoopRoute");function Pn(t,e,o,n,r,i,a,s,d,u,l=[],f=()=>[],p=()=>[],h=!1,g){let b=a.fallbackContainers.get(e);if(b)return it({context:a,reason:b,edgeId:t,containerId:e,route:r,isValid:c(I=>Ve(I,o.ownerId,n.ownerId,e,i)&&he(I,l),"isValid"),includeContainerIdInError:!0});let m=a.topologies.get(e);if(!m||!d)throw D("GRID_ROUTE_NOT_FOUND",`Missing routing topology "${e}"`);let y=[{start:o,end:n,select:[]}],R=!1,E=c(()=>{if(R)return;R=!0;let I=p(),x=f();y.push(...x.map(w=>({start:o,end:w.attachment,select:[w.select]})),...I.map(w=>({start:w.attachment,end:n,select:[w.select]})),...I.flatMap(w=>x.map(G=>({start:w.attachment,end:G.attachment,select:[w.select,G.select]}))))},"appendAlternatives"),S=g!==void 0&&g.index.size>0?[!0,!1]:[!1];try{for(let I of S)for(let[x,w]of y.entries()){let G=w.start,C=w.end;if(x>0&&a.metrics&&a.metrics.hierarchyPortalAlternativeAttempts++,En(G.connect,C.connect)){let H=V([G.port,G.connect,C.connect,C.port]).points;if(Ve(H,G.ownerId,C.ownerId,e,i)&&he(H,l)&&!(I&&g.index.conflictsWithRoute(H,g.pairKey))){for(let Y of w.select)Y();return x>0&&a.metrics&&a.metrics.hierarchyPortalAlternativeSelections++,H}}St({metrics:a.metrics,caps:u.searchCaps,budget:a.searchBudget});let A=Tt(m,G.connect,C.connect,a.metrics,d),O=a.baseEstimatedBytes+A.estimatedBytes-m.estimatedBytes;if(O>(u.topologyCaps?.maxEstimatedBytes??Ae))throw new le("estimated_memory_cap","Grid routing estimated_memory_cap exceeded");let _=A.pointVertexIds.get(ge(G.connect)),N=A.pointVertexIds.get(ge(C.connect));if(_===void 0||N===void 0){x===0&&E();continue}let W=zt(A,_,N,{metrics:a.metrics,caps:{...u.searchCaps,maxEstimatedBytes:u.searchCaps?.maxEstimatedBytes??u.topologyCaps?.maxEstimatedBytes??Ae},recordOutcome:!1,budget:a.searchBudget,initialOrientation:G.side==="left"||G.side==="right"?"H":"V",initialSide:G.side,initialLength:Math.abs(G.port.x-G.connect.x)+Math.abs(G.port.y-G.connect.y),targetOrientation:C.side==="left"||C.side==="right"?"H":"V",targetSide:C.side,targetLength:Math.abs(C.port.x-C.connect.x)+Math.abs(C.port.y-C.connect.y),topologyValidated:!0,workspace:s,estimatedBytesBase:O,arcAllowed:l.length>0||I?(H,Y)=>(l.length===0||Vt(H,Y,l))&&!(I&&g.index.conflictsWithArc(H,Y,g.pairKey)):void 0});if(!W){x===0&&E();continue}let q=V([G.port,...W.points,C.port]).points;if(!Ve(q,G.ownerId,C.ownerId,e,i)||!he(q,l)||I&&g.index.conflictsWithRoute(q,g.pairKey)){x===0&&E();continue}for(let H of w.select)H();return x>0&&a.metrics&&a.metrics.hierarchyPortalAlternativeSelections++,q}}catch(I){if(!(I instanceof le))throw I;return it({context:a,reason:I.reason,edgeId:t,containerId:e,searchStateScope:I.searchStateScope,route:r,isValid:c(x=>Ve(x,o.ownerId,n.ownerId,e,i)&&he(x,l),"isValid"),includeContainerIdInError:!0})}let M=r();if(Ve(M,o.ownerId,n.ownerId,e,i)&&(he(M,l)||h&&kt(M,l)))return a.metrics&&(a.metrics.compatibilitySegments++,a.metrics.compatibilityRecoveries++),M;throw D("GRID_ROUTE_NOT_FOUND",`No hierarchy route for "${t}" in "${e}" from ${ge(o.connect)} to ${ge(n.connect)}`,{edgeId:t,containerId:e})}c(Pn,"sparseContainerSegment");function Wi(t,e,o,n,r,i,a){let s=t[0].pairKey,d=e.get(s),u=[...new Set(t.flatMap(h=>[...h.source.chain,...h.target.chain].map(({demandKey:g})=>g)))],l=new Map,f=new Map;for(let h of u){let g=o.get(h);g&&l.set(h,g);let b=n.get(h);b!==void 0&&f.set(h,b)}let p=a.get(t[0].lcaContainerId);return{pairKey:s,occupancy:p?{index:p,size:p.size}:void 0,pairRoutes:d?[...d]:void 0,demandKeys:u,pairedPortals:l,demandCoords:f,edges:t.map(({edge:h})=>({edge:h,points:h.points,curve:h.curve,cornerRadius:h.cornerRadius})),instrumentedRoutesLength:r?.length,metrics:i?or(i):void 0}}c(Wi,"createBundleCheckpoint");function Yi(t,e,o,n,r,i){t.occupancy?.index.truncate(t.occupancy.size),t.pairRoutes?e.set(t.pairKey,[...t.pairRoutes]):e.delete(t.pairKey);for(let a of t.demandKeys){let s=t.pairedPortals.get(a);s?o.set(a,s):o.delete(a);let d=t.demandCoords.get(a);d!==void 0?n.set(a,d):n.delete(a)}for(let a of t.edges)a.edge.points=a.points,a.edge.curve=a.curve,a.edge.cornerRadius=a.cornerRadius;r&&t.instrumentedRoutesLength!==void 0&&(r.length=t.instrumentedRoutesLength),i&&t.metrics&&rr(i,t.metrics)}c(Yi,"restoreBundleCheckpoint");var Ht=class{constructor(e,o,n,r){this.layout=e;this.result=o;this.metrics=n;this.options=r;this.searchWorkspace=new Ct;this.pairedPortals=new Map;this.ownerSideCounts=new Map;this.selfLoopCounts=new Map;this.pairRoutes=new Map;this.routed=!1}static{c(this,"GridEdgeRoutingSession")}route(){if(this.routed)throw new Error("Grid edge routing session can only be run once");this.routed=!0,this.prepare();let e=new Map;for(let o of this.prepared.orderedPlans){let n=e.get(o.pairKey)??[];n.push(o),e.set(o.pairKey,n)}for(let o of e.values())this.routeBundle(o)}prepare(){Zo(this.result),this.prepared=ko(this.layout,this.result),this.modes=er(this.prepared,this.result,this.metrics,this.options),this.routingContext=Jo(this.modes.routedContainerIds,this.result,this.metrics,this.options),this.endpointOverlayScratch=new Map([...this.routingContext.topologies].map(([o,n])=>[o,new gt(n)])),this.instrumentedRoutes=this.metrics?[]:void 0;let e=new Set;for(let o of this.prepared.orderedPlans)if(o.edge.start!==o.edge.end)for(let n of[...o.source.chain,...o.target.chain]){if(e.has(n.demandKey))continue;e.add(n.demandKey);let r=Xe(n.ownerId,n.side);this.ownerSideCounts.set(r,(this.ownerSideCounts.get(r)??0)+1)}}pairedPortal(e){let o=this.pairedPortals.get(e.demandKey);if(o)return o;let n=this.result.forest.nodeById.get(e.ownerId);if(!n?.isGroup)throw D("GRID_ROUTE_NOT_FOUND",`Missing portal owner "${e.ownerId}"`);let r=ve(n),i=this.prepared.demandCoords.get(e.demandKey)??(e.side==="left"||e.side==="right"?(r.top+r.bottom)/2:(r.left+r.right)/2),a=Ue(e.ownerId,r,n.groupTitleRect).find(({side:d})=>d===e.side);if(!a)throw D("GRID_ROUTE_NOT_FOUND",`No legal ${e.side} portal for "${e.ownerId}"`);let s=fn(e.ownerId,r,n.groupTitleRect?{...n.groupTitleRect}:void 0,e.side,Ce(i,a.low,a.high));return this.pairedPortals.set(e.demandKey,s),this.metrics&&(this.metrics.hierarchyPortalPairs++,this.metrics.hierarchyPortalTransitionLength+=s.transition.length),s}alternativePairedPortals(e,o=!0){let n=this.pairedPortal(e),r=this.result.forest.nodeById.get(e.ownerId);if(!r?.isGroup)return[];let i=ve(r),a=Ue(e.ownerId,i,r.groupTitleRect).find(({side:u})=>u===e.side);if(!a)return[];let s=this.result.containers.get(r.id),d=e.side==="left"||e.side==="right"?s?.horizontalCorridors:s?.verticalCorridors;return In(n.tangentialCoordinate,a.low,a.high,d??[]).map(u=>{let l=fn(e.ownerId,i,r.groupTitleRect?{...r.groupTitleRect}:void 0,e.side,u);return{attachment:ot(l,o),select:c(()=>{this.pairedPortals.set(e.demandKey,l),this.prepared.demandCoords.set(e.demandKey,u)},"select")}})}itemSegmentAttachment(e,o){return{ownerId:e.ownerId,...Et(e.ownerId,e.side,e.demandKey,o,this.result,this.prepared.demandCoords)}}alternativeItemAttachments(e,o,n){let r=this.itemSegmentAttachment(o,n),i=this.result.forest.nodeById.get(o.ownerId);return i?["right","bottom","left","top"].filter(a=>a!==r.side&&!(a==="top"&&xt(i))).map(a=>{let s=Se(i,a);if(!s)return;let d=k(i),u=a==="left"||a==="right"?d.cy:d.cx,l=Math.max(s.low,Math.min(s.high,u+e.laneOffset));return{ownerId:o.ownerId,...Et(o.ownerId,a,o.demandKey,n,this.result,this.prepared.demandCoords,l)}}).filter(a=>a!==void 0).slice(0,3).map(a=>({attachment:a,select:c(()=>{},"select")})):[]}routePlan(e,o=!0){let n=this.result,r=this.routingContext,i=this.searchWorkspace,a=this.endpointOverlayScratch,s=this.prepared.demandCoords,d=this.ownerSideCounts,u=this.selfLoopCounts,l=this.pairRoutes,f=this.instrumentedRoutes,p=this.metrics,h=this.options,g=this.prepared.eligibleIds,b=this.prepared.endpointCandidatesByEdge,m=this.modes.compatibilityFastRoutes,y=this.modes.sparseHierarchyIds,R=this.modes.sparseLcaIds,E=e.edge,S=E.start?n.forest.nodeById.get(E.start):void 0,M=E.end?n.forest.nodeById.get(E.end):void 0;if(!S||!M)throw D("GRID_MISSING_ENDPOINT",`Missing endpoint for edge "${E.id}"`,{edgeId:E.id});if(S.id===M.id){let K=l.get(e.pairKey)??[],{points:xe,side:ie,index:pe}=pr(e,S,n,r,i,a.get(S.parentId??F),d,u,K,h),te=`${S.id}:${ie}`;u.set(te,pe+1),E.points=xe,E.curve=n.config.curve,E.cornerRadius=n.config.edgeCornerRadius,K.push(xe),l.set(e.pairKey,K),p&&f&&(vn(p,E.id,xe,f,0,e.laneOffset),f.push(xe));return}let I=e.source.chain[e.source.chain.length-1],x=e.target.chain[e.target.chain.length-1],w=R.has(E.id),G=w?void 0:It(e,n,s),C=G?G.start:e.source.finalKind==="boundary"?Cn(I.ownerId,I.side,I.demandKey,n,s):e.source.chain.length>1?ot(this.pairedPortal(I),!1):this.itemSegmentAttachment(I,e.lcaContainerId),v=G?G.end:e.target.finalKind==="boundary"?Cn(x.ownerId,x.side,x.demandKey,n,s):e.target.chain.length>1?ot(this.pairedPortal(x),!1):this.itemSegmentAttachment(x,e.lcaContainerId),A,O=c(()=>A??=Ye(e.lcaContainerId,n,C,v,e.laneIndex),"legacyRoute"),_=m.get(E.id),N=_?(p&&p.compatibilityFastPaths++,_):g.has(E.id)?lr(e,b.get(E.id).sources,b.get(E.id).targets,S,M,O,n,r,i,a.get(e.lcaContainerId),h,l.get(e.pairKey)??[],{index:this.occupancyFor(e.lcaContainerId),pairKey:e.pairKey,prioritizeEndpointCandidates:this.prepared.coordinatedEndpointIds.has(E.id)}):w?Pn(E.id,e.lcaContainerId,C,v,O,n,r,i,a.get(e.lcaContainerId),h,l.get(e.pairKey)??[],()=>e.target.finalKind==="item"&&e.target.chain.length>1?this.alternativePairedPortals(x,!1):[],()=>e.source.finalKind==="item"&&e.source.chain.length>1?this.alternativePairedPortals(I,!1):[],e.bundleSize>1,this.modes.overlapAvoidanceEnabled?{index:this.occupancyFor(e.lcaContainerId),pairKey:e.pairKey}:void 0):wn(E.id,e.lcaContainerId,C,v,O,n,p),W=c((K,xe)=>{if(!y.has(E.id)){for(let{containerId:ie,start:pe,end:te}of Gn(K,n,s))xe.push(wn(E.id,ie,pe,te,()=>Ye(ie,n,pe,te,e.laneIndex),n,p));return}for(let ie=0;ie<K.chain.length-1;ie++){let pe=K.chain[ie],te=K.chain[ie+1],P=ie===0?this.itemSegmentAttachment(pe,te.ownerId):ot(this.pairedPortal(pe),!1),T=ot(this.pairedPortal(te),!0);xe.push(Pn(E.id,te.ownerId,P,T,()=>Ye(te.ownerId,n,P,T,e.laneIndex),n,r,i,a.get(te.ownerId),h,l.get(e.pairKey)??[],()=>this.alternativePairedPortals(te),()=>ie===0?this.alternativeItemAttachments(e,pe,te.ownerId):[],e.bundleSize>1))}},"routeHierarchyChain"),q=[],H=[];W(e.source,q),W(e.target,H);let Y=qo([...q,N,...H.reverse().map(K=>Yo(K))]),be=l.get(e.pairKey)??[],ne=he(Y,be),Z=!ne&&e.bundleSize>1&&e.source.chain.length+e.target.chain.length>2&&kt(Y,be);if(e.bundleSize>1&&!ne&&(!Z||!o))throw p&&p.routesImpossible++,D("GRID_ROUTE_NOT_FOUND",`No distinct lane route for "${E.id}"`,{edgeId:E.id,reason:Z?"pair-separation":"no-distinct-route"});if(Z&&p&&p.bundleSeparationRelaxations++,E.points=Y,E.curve=n.config.curve,E.cornerRadius=n.config.edgeCornerRadius,be.push(Y),l.set(e.pairKey,be),_||this.occupancyFor(e.lcaContainerId).add(N,e.pairKey),p&&f){let K=e.source.chain.length+e.target.chain.length-2;vn(p,E.id,Y,f,K,e.laneOffset),f.push(Y)}}occupancyFor(e){let o=this.modes.routeOccupancy.get(e);return o||(o=new We,this.modes.routeOccupancy.set(e,o)),o}createBundleCheckpoint(e){return this.occupancyFor(e[0].lcaContainerId),Wi(e,this.pairRoutes,this.pairedPortals,this.prepared.demandCoords,this.instrumentedRoutes,this.metrics,this.modes.routeOccupancy)}restoreBundleCheckpoint(e){Yi(e,this.pairRoutes,this.pairedPortals,this.prepared.demandCoords,this.instrumentedRoutes,this.metrics)}routeBundle(e){if(!(e.length>1&&e.length<=8&&e.every(({edge:i})=>i.start!==i.end))){for(let i of e)this.routePlan(i);return}let n=this.createBundleCheckpoint(e),r;try{for(let i of e)this.routePlan(i,!1);return}catch(i){r=i,this.restoreBundleCheckpoint(n)}if(!Fo(r))throw r;for(let i of e)this.routePlan(i)}};function fr(t,e,o,n={}){new Ht(t,e,o,n).route()}c(fr,"routeGridEdges");var qi=24,$t=20;function Qi(t,e){return!t.label||!t.labelBBox||!(t.labelBBox.height>0)?0:t.labelBBox.height+e}c(Qi,"titleBandFor");function Zi(t){if(!Q(t.width)||!Q(t.height))throw D("GRID_MISSING_MEASUREMENT",`Node "${t.id}" must have width and height greater than 0 (width: ${t.width}, height: ${t.height})`,{nodeId:t.id,width:t.width,height:t.height})}c(Zi,"ensureMeasuredNode");function mr(t){return Zi(t),{width:t.width,height:t.height}}c(mr,"measureChild");function gr(t,e,o,n,r){let{forest:i,config:a,sourceOrder:s}=e,d=t===F?void 0:i.groupById.get(t),u=d?Math.max(d.padding??0,a.containerPadding):0,l=d?Qi(d,a.titleGap):0,f=d?qi:0,p=d?u+l+f:0,h=(i.childrenByParent.get(t)??[]).filter(P=>!ce(P)),{cells:g}=Io(h,s,a),b=[...g.values()].sort((P,T)=>P.row-T.row||P.column-T.column),m=new Map,y=new Map,R=new Set,E=new Set;for(let P of b){let T=0,ae=0;for(let L of P.items){let B=mr(L.item);T=Math.max(T,B.width),ae+=B.height}P.items.length>1&&(ae+=a.cellGap*(P.items.length-1)),P.width=T,P.height=ae,R.add(P.row),E.add(P.column),m.set(P.column,Math.max(m.get(P.column)??0,T)),y.set(P.row,Math.max(y.get(P.row)??0,ae))}let S=[...R].sort((P,T)=>P-T),M=[...E].sort((P,T)=>P-T),I=ho(e.labelRequirements.byContainer.get(t)??[],g,y,m,S,M,a.rowGap,a.columnGap,r),x=c(P=>Math.max(a.rowGap,I.rowGapAfter.get(P)??0),"rowGapAfter"),w=c(P=>Math.max(a.columnGap,I.columnGapAfter.get(P)??0),"columnGapAfter");if(r){for(let P of I.rowGapAfter.values())P>a.rowGap&&(r.labelSpacingBoundariesExpanded++,r.labelSpacingPixelsAdded+=P-a.rowGap);for(let P of I.columnGapAfter.values())P>a.columnGap&&(r.labelSpacingBoundariesExpanded++,r.labelSpacingPixelsAdded+=P-a.columnGap)}let G=M.reduce((P,T,ae)=>P+(m.get(T)??0)+(ae<M.length-1?w(T):0),0),C=d?Math.max(G+(u+f)*2,(d.labelBBox?.width??0)+u*2):G,v=d?(C-G)/2:0,A=new Map,O=v;for(let[P,T]of M.entries())A.set(T,O),O+=(m.get(T)??0)+(P<M.length-1?w(T):0);let _=new Map,N=p;for(let[P,T]of S.entries())_.set(T,N),N+=(y.get(T)??0)+(P<S.length-1?x(T):0);let W=S.length?N-p:0,q=d?v-$t:-Math.max(a.columnGap/2,fe.containerPadding),H=d?v+G+$t:v+G+Math.max(a.columnGap/2,fe.containerPadding),Y=d?p-$t:-Math.max(a.rowGap/2,fe.containerPadding),be=d?p+W+$t:p+W+Math.max(a.rowGap/2,fe.containerPadding),ne=new Set([q,H]),Z=new Set([Y,be]),K=new Map,xe=new Map,ie=new Map,pe=new Map;M.length>0&&(K.set(M[0],q),xe.set(M.at(-1),H));for(let P=0;P<M.length-1;P++){let T=M[P],ae=M[P+1],L=(A.get(T)+(m.get(T)??0)+A.get(ae))/2;ne.add(L),xe.set(T,L),K.set(ae,L)}S.length>0&&(ie.set(S[0],Y),pe.set(S.at(-1),be));for(let P=0;P<S.length-1;P++){let T=S[P],ae=S[P+1],L=(_.get(T)+(y.get(T)??0)+_.get(ae))/2;Z.add(L),pe.set(T,L),ie.set(ae,L)}for(let P of b){let T=A.get(P.column)??v,ae=_.get(P.row)??p,L=m.get(P.column)??P.width,B=y.get(P.row)??P.height,se=P.verticalAlign==="top"?0:P.verticalAlign==="bottom"?1:.5,J=ae+(B-P.height)*se;for(let[$,Ge]of P.items.entries()){let{width:at,height:Oe}=mr(Ge.item),st=Ge.horizontalAlign==="left"?0:Ge.horizontalAlign==="right"?1:.5,dt=T+(L-at)*st;Ge.item.x=dt+at/2,Ge.item.y=J+Oe/2;let ct=$>0?J-a.cellGap/2:ie.get(P.row)??Y,ut=$<P.items.length-1?J+Oe+a.cellGap/2:pe.get(P.row)??be;$>0&&Z.add(ct),$<P.items.length-1&&Z.add(ut),n.set(Ge.item.id,{containerId:t,row:P.row,column:P.column,cellLeft:T,cellTop:ae,cellWidth:L,cellHeight:B,leftCorridorX:K.get(P.column)??q,rightCorridorX:xe.get(P.column)??H,topCorridorY:ct,bottomCorridorY:ut}),J+=Oe+a.cellGap}}let te=d?W+l+(u+f)*2:W;o.set(t,{id:t,left:0,top:0,width:C,height:te,contentLeft:v,contentTop:p,contentRight:d?v+G:G,contentBottom:d?p+W:W,verticalCorridors:[...ne].sort((P,T)=>P-T),horizontalCorridors:[...Z].sort((P,T)=>P-T)}),d&&(d.width=C,d.height=te,d.x=C/2,d.y=te/2,l>0?d.groupTitleRect={left:0,right:C,top:0,bottom:u+l}:delete d.groupTitleRect)}c(gr,"layoutContainer");function Ji(t,e,o){return{left:t.left+e,right:t.right+e,top:t.top+o,bottom:t.bottom+o}}c(Ji,"translateRect");function ea(t){let{forest:e,containers:o,itemMeta:n}=t,r=[{containerId:F,offsetX:0,offsetY:0,children:e.childrenByParent.get(F)??[],nextChildIndex:0,entered:!1}];for(;r.length>0;){let i=r[r.length-1];if(!i.entered){i.entered=!0;let d=o.get(i.containerId);d&&(d.left+=i.offsetX,d.top+=i.offsetY,d.contentLeft+=i.offsetX,d.contentRight+=i.offsetX,d.contentTop+=i.offsetY,d.contentBottom+=i.offsetY,d.verticalCorridors=d.verticalCorridors.map(u=>u+i.offsetX),d.horizontalCorridors=d.horizontalCorridors.map(u=>u+i.offsetY))}let a=i.children[i.nextChildIndex];if(!a){r.pop();continue}if(i.nextChildIndex+=1,ce(a))continue;let s=n.get(a.id);s&&(s.cellLeft+=i.offsetX,s.cellTop+=i.offsetY,s.leftCorridorX+=i.offsetX,s.rightCorridorX+=i.offsetX,s.topCorridorY+=i.offsetY,s.bottomCorridorY+=i.offsetY),a.x=(a.x??0)+i.offsetX,a.y=(a.y??0)+i.offsetY,a.isGroup&&(a.groupTitleRect&&(a.groupTitleRect=Ji(a.groupTitleRect,a.x-(a.width??0)/2,a.y-(a.height??0)/2)),r.push({containerId:a.id,offsetX:a.x-(a.width??0)/2,offsetY:a.y-(a.height??0)/2,children:e.childrenByParent.get(a.id)??[],nextChildIndex:0,entered:!1}))}}c(ea,"materializeAbsoluteGeometry");function ta(t,e){let o=new Map(t.nodes.map(r=>[r.id,r]));for(let r of e.nodes){let i=o.get(r.id);i&&(r.x=i.x,r.y=i.y,r.width=i.width,r.height=i.height,i.groupTitleRect?r.groupTitleRect={...i.groupTitleRect}:delete r.groupTitleRect)}let n=new Map(t.edges.map(r=>[r.id,r]));for(let r of e.edges){let i=n.get(r.id);i&&(r.points=i.points?.map(a=>({...a})),r.curve=i.curve,r.cornerRadius=i.cornerRadius,r.portClipping="outline-orthogonal",r.skipCornerFix=i.curve==="linear")}}c(ta,"commitGridGeometry");function na(t){return{...t,nodes:t.nodes.map(e=>({...e,labelBBox:e.labelBBox?{...e.labelBBox}:e.labelBBox,groupTitleRect:e.groupTitleRect?{...e.groupTitleRect}:e.groupTitleRect})),edges:t.edges.map(e=>({...e,points:e.points?.map(o=>({...o}))}))}}c(na,"cloneGridLayoutData");function oa(t,e){let o=new Map(e.map(i=>[i.id,i])),n=c(i=>{let a=o.get(i.id);if(!a)throw new Error(`Missing committed grid node "${i.id}"`);return a},"originalNode"),r=c(i=>{let a=n(i);if(!a.isGroup)throw new Error(`Committed grid node "${i.id}" is no longer a group`);return a},"originalGroup");return{nodeById:o,groupById:new Map([...t.groupById].map(([i,a])=>[i,r(a)])),childrenByParent:new Map([...t.childrenByParent].map(([i,a])=>[i,a.map(n)])),rootChildren:t.rootChildren.map(n),postOrderGroups:t.postOrderGroups.map(r),helperNodeIds:new Set(t.helperNodeIds)}}c(oa,"rebindGridForest");function ra(t,e){let o=lo(t.nodes),n=xo(t),r=bo(t.nodes.filter(a=>!ce(a)));Ro(t.nodes.filter(a=>!ce(a)),n);let i={forest:o,config:n,containers:new Map,itemMeta:new Map,sourceOrder:r,labelRequirements:go(t,o,e.metrics)};for(let a of o.postOrderGroups)gr(a.id,i,i.containers,i.itemMeta,e.metrics);return gr(F,i,i.containers,i.itemMeta,e.metrics),ea(i),fr(t,i,e.metrics,e.routing),uo(t,void 0,e.metrics),i}c(ra,"runGridLayoutCoreInPlace");function hr(t,e={}){let o=t,n=na(o),r=ra(n,e);return ta(n,o),{...r,forest:oa(r.forest,o.nodes)}}c(hr,"runGridLayoutCore");function yr(t,{measure:e}){let o=Tn(t.config.grid?.lineHops);if(o===!1)return;let n=t.edges.filter(r=>Array.isArray(r.points)&&r.points.length>=2).map(r=>({id:r.id,points:r.points,curve:r.curve,cornerRadius:r.cornerRadius,arrowTypeStart:r.arrowTypeStart,arrowTypeEnd:r.arrowTypeEnd}));Dn(e.groups.edgePaths,n,{enabled:!0,jumpRadius:6,jumpStyle:o})}c(yr,"applyGridLineJumps");var hd=_n({prepareLayout:co,runLayoutCore:c(t=>hr(t),"runLayoutCore"),afterPaint:yr});export{co as prepareGridLayout,hd as render,hr as runGridLayoutCore};