import{t as e}from"./dagre-DhVYPhYs.js";import{t}from"./graphlib-DIH_KtCr.js";import{a as n,i as r,n as i,o as a,r as o,t as s}from"./chunk-RYQCIY6F-Bp_NzpzG.js";import{n as c}from"./chunk-Y2CYZVJY-DsF7k-Jl.js";import{t as l}from"./chunk-X3CZISLH-DX9Il8sT.js";import{x as u}from"./chunk-WYO6CB5R-BrkmBSXu.js";import"./chunk-ICXQ74PX-D_HF1znU.js";import"./chunk-C7G6YPKG-P3pgmzqd.js";import{n as d}from"./chunk-OGEWGWER-BlkcBkyz.js";import"./chunk-HOUHSVGY-DUkjS7OQ.js";import"./chunk-Q4XR5HBZ-BUs7C1TP.js";import{a as f,c as p,i as m,l as h,n as g,t as _,u as v}from"./chunk-ZGVPDNZ5-B4JvVAZ5.js";import"./chunk-7BUUIJ7U-Bb538aSH.js";import{a as y,i as b,o as x,r as S,t as C}from"./chunk-52WLFC77-BHGHKSq2.js";var w=c((e,t,n)=>Math.max(t,Math.min(n,e)),`clamp`),T=c((e=`TB`)=>{switch(e){case`BT`:return`bottom`;case`LR`:return`right`;case`RL`:return`left`;default:return`top`}},`getDefaultSelfLoopSide`),E=c(e=>e===`flowchart`||e===`flowchart-v2`||e===`stateDiagram`,`shouldMergeSelfLoopSegments`),D=c((e,t,n,r,i)=>{let a=[],o=new Set;if(n.forEach(({start:e,end:t})=>{e!==r&&o.add(e),t!==r&&o.add(t)}),o.forEach(t=>{let n=e.node(t);typeof n?.x==`number`&&typeof n?.y==`number`&&a.push(n)}),a.length===0&&n.forEach(({edge:e})=>{(e.points??[]).forEach(e=>{typeof e?.x==`number`&&typeof e?.y==`number`&&a.push(e)})}),a.length===0)return T(i);let s=a.reduce((e,t)=>({x:e.x+t.x/a.length,y:e.y+t.y/a.length}),{x:0,y:0}),c=s.x-t.x,l=s.y-t.y;return Math.abs(c)>Math.abs(l)?c>0?`right`:`left`:Math.abs(l)>0?l>0?`bottom`:`top`:T(i)},`getSelfLoopSide`),O=c((e,t=`top`,n=0,r=0)=>{let i=e.x,a=e.y-n,o=e.width/2,s=e.height/2,c=Math.max(36,Math.min(100,e.width*.8)),l=w(Math.max(r,e.width*.35),36,c),u=w(Math.min(e.width,e.height)*.45,24,48);switch(t){case`bottom`:{let e=a+s;return[{x:i-l/2,y:e},{x:i-l/2,y:e+u},{x:i+l/2,y:e+u},{x:i+l/2,y:e}]}case`right`:{let e=i+o;return[{x:e,y:a-l/2},{x:e+u,y:a-l/2},{x:e+u,y:a+l/2},{x:e,y:a+l/2}]}case`left`:{let e=i-o;return[{x:e,y:a-l/2},{x:e-u,y:a-l/2},{x:e-u,y:a+l/2},{x:e,y:a+l/2}]}default:{let e=a-s;return[{x:i-l/2,y:e},{x:i-l/2,y:e-u},{x:i+l/2,y:e-u},{x:i+l/2,y:e}]}}},`getSelfLoopPoints`),k=c((e,t,n=`top`,r=0,i={})=>{let a=e.x,o=e.y-r,s=i.width??0,c=i.height??0;switch(n){case`bottom`:return{x:a,y:Math.max(...t.map(e=>e.y))+c/2+4};case`right`:return{x:Math.max(...t.map(e=>e.x))+s/2+4,y:o};case`left`:return{x:Math.min(...t.map(e=>e.x))-s/2-4,y:o};default:return{x:a,y:Math.min(...t.map(e=>e.y))-c/2-4}}},`getSelfLoopLabelPosition`),A=c((e,t=0,{mergeSelfLoops:n=!0}={})=>{let r=new Map,i=[],a=e.graph()?.rankdir;return e.edges().forEach(t=>{let a=e.edge(t);if(n&&a.selfLoop){let e=a.selfLoop.id;r.has(e)||r.set(e,[]),r.get(e).push({edge:a,start:t.v,end:t.w})}else i.push({edge:a,start:t.v,end:t.w})}),r.forEach(n=>{if(n.length!==3){n.forEach(e=>i.push(e));return}n.sort((e,t)=>e.edge.selfLoop.order-t.edge.selfLoop.order);let[r,o,s]=n,c=r.edge.originalEdge??o.edge.originalEdge??s.edge.originalEdge??o.edge,l=e.node(c.start);if(!l){n.forEach(e=>i.push(e));return}let u={width:o.edge.width,height:o.edge.height},d=D(e,l,n,c.start,a),f=O(l,d,t,u.width??0),p=k(l,f,d,t,u),m={...o.edge,...c,id:c.id,points:f,start:c.start,end:c.end,x:p.x,y:p.y,width:u.width,height:u.height,labelStyle:o.edge.labelStyle,fromCluster:r.edge.fromCluster??o.edge.fromCluster??s.edge.fromCluster,toCluster:r.edge.toCluster??o.edge.toCluster??s.edge.toCluster};delete m.selfLoop,delete m.originalEdge,i.push({edge:m,start:m.start,end:m.end})}),i},`getEdgesToRender`),j=c(async(t,i,s,u,g,_)=>{l.warn(`Graph in recursive render:XAX`,a(i),g);let y=i.graph().rankdir;l.trace(`Dir in recursive render - dir:`,y);let C=t.insert(`g`).attr(`class`,`root`);i.nodes()?l.info(`Recursive render XXX`,i.nodes()):l.info(`No nodes found for`,i),i.edges().length>0&&l.info(`Recursive edges`,i.edge(i.edges()[0]));let w=C.insert(`g`).attr(`class`,`clusters`),T=C.insert(`g`).attr(`class`,`edgePaths`),D=C.insert(`g`).attr(`class`,`edgeLabels`),O=C.insert(`g`).attr(`class`,`nodes`),k=E(s);await Promise.all(i.nodes().map(async function(e){let t=i.node(e);if(g!==void 0){let t=JSON.parse(JSON.stringify(g.clusterData));l.trace(`Setting data for parent cluster XXX
 Node.id = `,e,`
 data=`,t.height,`
Parent cluster`,g.height),i.setNode(g.id,t),i.parent(e)||(l.trace(`Setting parent`,e,g.id),i.setParent(e,g.id,t))}if(l.info(`(Insert) Node XXX`+e+`: `+JSON.stringify(i.node(e))),t?.clusterNode){l.info(`Cluster identified XBX`,e,t.width,i.node(e));let{ranksep:n,nodesep:r}=i.graph();t.graph.setGraph({...t.graph.graph(),ranksep:n+25,nodesep:r});let a=await j(O,t.graph,s,u,i.node(e),_),o=a.elem;v(t,o),t.diff=a.diff||0,l.info(`New compound node after recursive render XAX`,e,`width`,t.width,`height`,t.height),h(o,t)}else i.children(e).length>0?(l.trace(`Cluster - the non recursive path XBX`,e,t.id,t,t.width,`Graph:`,i),l.trace(r(t.id,i)),o.set(t.id,{id:r(t.id,i),node:t})):(l.trace(`Node - the non recursive path XAX`,e,O,i.node(e),y),await f(O,i.node(e),{config:_,dir:y}))})),await c(async()=>{let e=i.edges().map(async function(e){let t=i.edge(e.v,e.w,e.name);if(l.info(`Edge `+e.v+` -> `+e.w+`: `+JSON.stringify(e)),l.info(`Edge `+e.v+` -> `+e.w+`: `,e,` `,JSON.stringify(i.edge(e))),l.info(`Fix`,o,`ids:`,e.v,e.w,`Translating: `,o.get(e.v),o.get(e.w)),k&&t.selfLoop){if(t.selfLoop.order!==1)return;let e=t.id;t.id=t.selfLoop.id,await b(D,t),t.id=e;return}await b(D,t)});await Promise.all(e)},`processEdges`)(),l.info(`Graph before layout:`,JSON.stringify(a(i))),l.info(`############################################# XXX`),l.info(`###                Layout                 ### XXX`),l.info(`############################################# XXX`),e(i),l.info(`Graph after layout:`,JSON.stringify(a(i)));let M=0,{subGraphTitleTotalMargin:N}=d(_);await Promise.all(n(i).map(async function(e){let t=i.node(e);if(l.info(`Position XBX => `+e+`: (`+t.x,`,`+t.y,`) width: `,t.width,` height: `,t.height),t?.clusterNode)t.y+=N,l.info(`A tainted cluster node XBX1`,e,t.id,t.width,t.height,t.x,t.y,i.parent(e)),o.get(t.id).node=t,p(t);else if(i.children(e).length>0){l.info(`A pure cluster node XBX1`,e,t.id,t.x,t.y,t.width,t.height,i.parent(e)),t.height+=N,i.node(t.parentId);let n=t?.padding/2||0,r=t?.labelBBox?.height||0,a=r-n||0;l.debug(`OffsetY`,a,`labelHeight`,r,`halfPadding`,n),await m(w,t),o.get(t.id).node=t}else{let e=i.node(t.parentId);t.y+=N/2,l.info(`A regular node XBX1 - using the padding`,t.id,`parent`,t.parentId,t.width,t.height,t.x,t.y,`offsetY`,t.offsetY,`parent`,e,e?.offsetY,t),p(t)}}));let P=N/2;return A(i,P,{mergeSelfLoops:k}).forEach(function({edge:e,start:t,end:n}){l.info(`Edge `+t+` -> `+n+`: `+JSON.stringify(e),e),e.points.forEach(e=>e.y+=P);let r=i.node(t),a=i.node(n);x(e,S(T,e,o,s,r,a,u))}),i.nodes().forEach(function(e){let t=i.node(e);l.info(e,t.type,t.diff),t.isGroup&&(M=t.diff)}),l.warn(`Returning from recursive render XAX`,C,M),{elem:C,diff:M}},`recursiveRender`),M=c(async(e,n)=>{let r=new t({multigraph:!0,compound:!0}).setGraph({rankdir:e.direction,nodesep:e.config?.nodeSpacing||e.config?.flowchart?.nodeSpacing||e.nodeSpacing,ranksep:e.config?.rankSpacing||e.config?.flowchart?.rankSpacing||e.rankSpacing,marginx:8,marginy:8}).setDefaultEdgeLabel(function(){return{}}),o=n.select(`g`);y(o,e.markers,e.type,e.diagramId),g(),C(),_(),i(),e.nodes.forEach(e=>{r.setNode(e.id,{...e}),e.parentId&&r.setParent(e.id,e.parentId)}),l.debug(`Edges:`,e.edges),e.edges.forEach(e=>{if(e.start===e.end){let t=e.start,n=t+`---`+t+`---1`,i=t+`---`+t+`---2`,a=r.node(t);r.setNode(n,{domId:n,id:n,parentId:a.parentId,labelStyle:``,label:``,padding:0,shape:`labelRect`,style:``,width:10,height:10}),r.setParent(n,a.parentId),r.setNode(i,{domId:i,id:i,parentId:a.parentId,labelStyle:``,padding:0,shape:`labelRect`,label:``,style:``,width:10,height:10}),r.setParent(i,a.parentId);let o=structuredClone(e),s=structuredClone(e),c=structuredClone(e),l=structuredClone(e);s.originalEdge=o,s.selfLoop={id:o.id,order:0},c.originalEdge=o,c.selfLoop={id:o.id,order:1},l.originalEdge=o,l.selfLoop={id:o.id,order:2},s.label=``,s.arrowTypeEnd=`none`,s.endLabelLeft=``,s.endLabelRight=``,s.startLabelLeft=``,s.id=t+`-cyclic-special-1`,c.startLabelRight=``,c.startLabelLeft=``,c.endLabelLeft=``,c.endLabelRight=``,c.arrowTypeStart=`none`,c.arrowTypeEnd=`none`,c.id=t+`-cyclic-special-mid`,l.label=``,l.startLabelRight=``,l.startLabelLeft=``,l.arrowTypeStart=`none`,a.isGroup&&(s.fromCluster=t,l.toCluster=t),l.id=t+`-cyclic-special-2`,l.arrowTypeStart=`none`,r.setEdge(t,n,s,t+`-cyclic-special-0`),r.setEdge(n,i,c,t+`-cyclic-special-1`),r.setEdge(i,t,l,t+`-cyclic-special-2`)}else r.setEdge(e.start,e.end,{...e},e.id)}),l.warn(`Graph at first:`,JSON.stringify(a(r))),s(r),l.warn(`Graph after XAX:`,JSON.stringify(a(r)));let c=u();await j(o,r,e.type,e.diagramId,void 0,c)},`render`);export{A as getEdgesToRender,M as render};