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