import{a as Q}from"./chunk-D2TDZ4GK.js";import{a as M,b as S,c as q,d as G,e as z,f as K}from"./chunk-N5OZDRR5.js";import{D as F}from"./chunk-XE4UBALZ.js";import{c as Y,d as C,e as _,f as W,g as j}from"./chunk-RSYYWCFT.js";import"./chunk-AXDRLRJM.js";import{b as k,d as O,e as A,g as v,h as D,i as H,j as J}from"./chunk-QDZMYZBI.js";import"./chunk-A37R7TWI.js";import{a as T}from"./chunk-QYCUXD7R.js";import"./chunk-PRKFGJVH.js";import"./chunk-TZWSGC3H.js";import"./chunk-ZKLJWGVP.js";import"./chunk-55QKDJ5H.js";import"./chunk-2K35RNHN.js";import{_ as B}from"./chunk-E5GXGYZM.js";import{b as l}from"./chunk-ZZZJFKF2.js";import{a as w}from"./chunk-YUSHYV7C.js";import{a as I,b as R}from"./chunk-C6Q5SG76.js";var U=w((n,t,g)=>Math.max(t,Math.min(g,n)),"clamp"),V=w((n="TB")=>{switch(n){case"BT":return"bottom";case"LR":return"right";case"RL":return"left";default:return"top"}},"getDefaultSelfLoopSide"),ee=w(n=>n==="flowchart"||n==="flowchart-v2"||n==="stateDiagram","shouldMergeSelfLoopSegments"),te=w((n,t,g,m,c)=>{let i=[],s=new Set;if(g.forEach(({start:r,end:o})=>{r!==m&&s.add(r),o!==m&&s.add(o)}),s.forEach(r=>{let o=n.node(r);typeof o?.x=="number"&&typeof o?.y=="number"&&i.push(o)}),i.length===0&&g.forEach(({edge:r})=>{(r.points??[]).forEach(o=>{typeof o?.x=="number"&&typeof o?.y=="number"&&i.push(o)})}),i.length===0)return V(c);let f=i.reduce((r,o)=>({x:r.x+o.x/i.length,y:r.y+o.y/i.length}),{x:0,y:0}),h=f.x-t.x,a=f.y-t.y;return Math.abs(h)>Math.abs(a)?h>0?"right":"left":Math.abs(a)>0?a>0?"bottom":"top":V(c)},"getSelfLoopSide"),oe=w((n,t="top",g=0,m=0)=>{let c=n.x,i=n.y-g,s=n.width/2,f=n.height/2,h=Math.max(36,Math.min(100,n.width*.8)),a=U(Math.max(m,n.width*.35),36,h),r=U(Math.min(n.width,n.height)*.45,24,48);switch(t){case"bottom":{let o=i+f;return[{x:c-a/2,y:o},{x:c-a/2,y:o+r},{x:c+a/2,y:o+r},{x:c+a/2,y:o}]}case"right":{let o=c+s;return[{x:o,y:i-a/2},{x:o+r,y:i-a/2},{x:o+r,y:i+a/2},{x:o,y:i+a/2}]}case"left":{let o=c-s;return[{x:o,y:i-a/2},{x:o-r,y:i-a/2},{x:o-r,y:i+a/2},{x:o,y:i+a/2}]}default:{let o=i-f;return[{x:c-a/2,y:o},{x:c-a/2,y:o-r},{x:c+a/2,y:o-r},{x:c+a/2,y:o}]}}},"getSelfLoopPoints"),ne=w((n,t,g="top",m=0,c={})=>{let s=n.x,f=n.y-m,h=c.width??0,a=c.height??0;switch(g){case"bottom":return{x:s,y:Math.max(...t.map(r=>r.y))+a/2+4};case"right":return{x:Math.max(...t.map(r=>r.x))+h/2+4,y:f};case"left":return{x:Math.min(...t.map(r=>r.x))-h/2-4,y:f};default:return{x:s,y:Math.min(...t.map(r=>r.y))-a/2-4}}},"getSelfLoopLabelPosition"),ie=w((n,t=0,{mergeSelfLoops:g=!0}={})=>{let m=new Map,c=[],i=n.graph()?.rankdir;return n.edges().forEach(s=>{let f=n.edge(s);if(g&&f.selfLoop){let h=f.selfLoop.id;m.has(h)||m.set(h,[]),m.get(h).push({edge:f,start:s.v,end:s.w})}else c.push({edge:f,start:s.v,end:s.w})}),m.forEach(s=>{if(s.length!==3){s.forEach(L=>c.push(L));return}s.sort((L,d)=>L.edge.selfLoop.order-d.edge.selfLoop.order);let[f,h,a]=s,r=f.edge.originalEdge??h.edge.originalEdge??a.edge.originalEdge??h.edge,o=n.node(r.start);if(!o){s.forEach(L=>c.push(L));return}let u={width:h.edge.width,height:h.edge.height},y=te(n,o,s,r.start,i),X=oe(o,y,t,u.width??0),E=ne(o,X,y,t,u),b=R(I(I({},h.edge),r),{id:r.id,points:X,start:r.start,end:r.end,x:E.x,y:E.y,width:u.width,height:u.height,labelStyle:h.edge.labelStyle,fromCluster:f.edge.fromCluster??h.edge.fromCluster??a.edge.fromCluster,toCluster:f.edge.toCluster??h.edge.toCluster??a.edge.toCluster});delete b.selfLoop,delete b.originalEdge,c.push({edge:b,start:b.start,end:b.end})}),c},"getEdgesToRender"),Z=w(async(n,t,g,m,c,i)=>{l.warn("Graph in recursive render:XAX",M(t),c);let s=t.graph().rankdir;l.trace("Dir in recursive render - dir:",s);let f=n.insert("g").attr("class","root");t.nodes()?l.info("Recursive render XXX",t.nodes()):l.info("No nodes found for",t),t.edges().length>0&&l.info("Recursive edges",t.edge(t.edges()[0]));let h=f.insert("g").attr("class","clusters"),a=f.insert("g").attr("class","edgePaths"),r=f.insert("g").attr("class","edgeLabels"),o=f.insert("g").attr("class","nodes"),u=ee(g);await Promise.all(t.nodes().map(async function(d){let e=t.node(d);if(c!==void 0){let p=JSON.parse(JSON.stringify(c.clusterData));l.trace(`Setting data for parent cluster XXX
 Node.id = `,d,`
 data=`,p.height,`
Parent cluster`,c.height),t.setNode(c.id,p),t.parent(d)||(l.trace("Setting parent",d,c.id),t.setParent(d,c.id,p))}if(l.info("(Insert) Node XXX"+d+": "+JSON.stringify(t.node(d))),e?.clusterNode){l.info("Cluster identified XBX",d,e.width,t.node(d));let{ranksep:p,nodesep:x}=t.graph();e.graph.setGraph(R(I({},e.graph.graph()),{ranksep:p+25,nodesep:x}));let N=await Z(o,e.graph,g,m,t.node(d),i),P=N.elem;k(e,P),e.diff=N.diff||0,l.info("New compound node after recursive render XAX",d,"width",e.width,"height",e.height),D(P,e)}else t.children(d).length>0?(l.trace("Cluster - the non recursive path XBX",d,e.id,e,e.width,"Graph:",t),l.trace(G(e.id,t)),S.set(e.id,{id:G(e.id,t),node:e})):(l.trace("Node - the non recursive path XAX",d,o,t.node(d),s),await v(o,t.node(d),{config:i,dir:s}))})),await w(async()=>{let d=t.edges().map(async function(e){let p=t.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(t.edge(e))),l.info("Fix",S,"ids:",e.v,e.w,"Translating: ",S.get(e.v),S.get(e.w)),u&&p.selfLoop){if(p.selfLoop.order!==1)return;let x=p.id;p.id=p.selfLoop.id,await C(r,p),p.id=x;return}await C(r,p)});await Promise.all(d)},"processEdges")(),l.info("Graph before layout:",JSON.stringify(M(t))),l.info("############################################# XXX"),l.info("###                Layout                 ### XXX"),l.info("############################################# XXX"),Q(t),l.info("Graph after layout:",JSON.stringify(M(t)));let X=0,{subGraphTitleTotalMargin:E}=T(i);await Promise.all(K(t).map(async function(d){let e=t.node(d);if(l.info("Position XBX => "+d+": ("+e.x,","+e.y,") width: ",e.width," height: ",e.height),e?.clusterNode)e.y+=E,l.info("A tainted cluster node XBX1",d,e.id,e.width,e.height,e.x,e.y,t.parent(d)),S.get(e.id).node=e,J(e);else if(t.children(d).length>0){l.info("A pure cluster node XBX1",d,e.id,e.x,e.y,e.width,e.height,t.parent(d)),e.height+=E,t.node(e.parentId);let p=e?.padding/2||0,x=e?.labelBBox?.height||0,N=x-p||0;l.debug("OffsetY",N,"labelHeight",x,"halfPadding",p),await O(h,e),S.get(e.id).node=e}else{let p=t.node(e.parentId);e.y+=E/2,l.info("A regular node XBX1 - using the padding",e.id,"parent",e.parentId,e.width,e.height,e.x,e.y,"offsetY",e.offsetY,"parent",p,p?.offsetY,e),J(e)}}));let b=E/2;return ie(t,b,{mergeSelfLoops:u}).forEach(function({edge:d,start:e,end:p}){l.info("Edge "+e+" -> "+p+": "+JSON.stringify(d),d),d.points.forEach($=>$.y+=b);let x=t.node(e),N=t.node(p),P=W(a,d,S,g,x,N,m);_(d,P)}),t.nodes().forEach(function(d){let e=t.node(d);l.info(d,e.type,e.diff),e.isGroup&&(X=e.diff)}),l.warn("Returning from recursive render XAX",f,X),{elem:f,diff:X}},"recursiveRender"),xe=w(async(n,t)=>{let g=new F({multigraph:!0,compound:!0}).setGraph({rankdir:n.direction,nodesep:n.config?.nodeSpacing||n.config?.flowchart?.nodeSpacing||n.nodeSpacing,ranksep:n.config?.rankSpacing||n.config?.flowchart?.rankSpacing||n.rankSpacing,marginx:8,marginy:8}).setDefaultEdgeLabel(function(){return{}}),m=t.select("g");j(m,n.markers,n.type,n.diagramId),H(),Y(),A(),q(),n.nodes.forEach(i=>{g.setNode(i.id,I({},i)),i.parentId&&g.setParent(i.id,i.parentId)}),l.debug("Edges:",n.edges),n.edges.forEach(i=>{if(i.start===i.end){let s=i.start,f=s+"---"+s+"---1",h=s+"---"+s+"---2",a=g.node(s);g.setNode(f,{domId:f,id:f,parentId:a.parentId,labelStyle:"",label:"",padding:0,shape:"labelRect",style:"",width:10,height:10}),g.setParent(f,a.parentId),g.setNode(h,{domId:h,id:h,parentId:a.parentId,labelStyle:"",padding:0,shape:"labelRect",label:"",style:"",width:10,height:10}),g.setParent(h,a.parentId);let r=structuredClone(i),o=structuredClone(i),u=structuredClone(i),y=structuredClone(i);o.originalEdge=r,o.selfLoop={id:r.id,order:0},u.originalEdge=r,u.selfLoop={id:r.id,order:1},y.originalEdge=r,y.selfLoop={id:r.id,order:2},o.label="",o.arrowTypeEnd="none",o.endLabelLeft="",o.endLabelRight="",o.startLabelLeft="",o.id=s+"-cyclic-special-1",u.startLabelRight="",u.startLabelLeft="",u.endLabelLeft="",u.endLabelRight="",u.arrowTypeStart="none",u.arrowTypeEnd="none",u.id=s+"-cyclic-special-mid",y.label="",y.startLabelRight="",y.startLabelLeft="",y.arrowTypeStart="none",a.isGroup&&(o.fromCluster=s,y.toCluster=s),y.id=s+"-cyclic-special-2",y.arrowTypeStart="none",g.setEdge(s,f,o,s+"-cyclic-special-0"),g.setEdge(f,h,u,s+"-cyclic-special-1"),g.setEdge(h,s,y,s+"-cyclic-special-2")}else g.setEdge(i.start,i.end,I({},i),i.id)}),l.warn("Graph at first:",JSON.stringify(M(g))),z(g),l.warn("Graph after XAX:",JSON.stringify(M(g)));let c=B();await Z(m,g,n.type,n.diagramId,void 0,c)},"render");export{ie as getEdgesToRender,xe as render};
