import{n as e}from"./chunk-Y2CYZVJY-DsF7k-Jl.js";import{m as t}from"./src-BY_WSiz7.js";import{x as n}from"./chunk-I66GZJ75-DyIufBk3.js";import"./chunk-NSK5VX7P-C5rgTOqf.js";import"./chunk-4I5QYGJK-CqcwRbfk.js";import"./chunk-WRU74C26-BTfK14t9.js";import"./chunk-7BUUIJ7U-Bb538aSH.js";import{n as r}from"./chunk-UBXNYLIW-D4IRhP65.js";import{t as i}from"./graphlib-DS17s2tU.js";import{t as a}from"./dagre-Buvkdvvj.js";import{a as o,i as s,n as c,o as l,r as u,t as d}from"./chunk-RYQCIY6F-DCAUdfJM.js";import"./chunk-W5SLKNZC-Bwa0Erac.js";import{a as f,c as p,i as m,l as h,n as g,t as _,u as v}from"./chunk-QR6OTTB3-Gf6zEt7G.js";import{a as y,i as b,o as x,r as S,t as C}from"./chunk-7Z6QIM7H-C8-Xmgc1.js";var w=e((e,t,n)=>Math.max(t,Math.min(n,e)),`clamp`),T=e((e=`TB`)=>{switch(e){case`BT`:return`bottom`;case`LR`:return`right`;case`RL`:return`left`;default:return`top`}},`getDefaultSelfLoopSide`),E=e(e=>e===`flowchart`||e===`flowchart-v2`||e===`stateDiagram`,`shouldMergeSelfLoopSegments`),D=e((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=e((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=e((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=e((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=e(async(n,i,c,d,g,_)=>{t.warn(`Graph in recursive render:XAX`,l(i),g);let y=i.graph().rankdir;t.trace(`Dir in recursive render - dir:`,y);let C=n.insert(`g`).attr(`class`,`root`);i.nodes()?t.info(`Recursive render XXX`,i.nodes()):t.info(`No nodes found for`,i),i.edges().length>0&&t.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(c);await Promise.all(i.nodes().map(async function(e){let n=i.node(e);if(g!==void 0){let n=JSON.parse(JSON.stringify(g.clusterData));t.trace(`Setting data for parent cluster XXX
 Node.id = `,e,`
 data=`,n.height,`
Parent cluster`,g.height),i.setNode(g.id,n),i.parent(e)||(t.trace(`Setting parent`,e,g.id),i.setParent(e,g.id,n))}if(t.info(`(Insert) Node XXX`+e+`: `+JSON.stringify(i.node(e))),n?.clusterNode){t.info(`Cluster identified XBX`,e,n.width,i.node(e));let{ranksep:r,nodesep:a}=i.graph();n.graph.setGraph({...n.graph.graph(),ranksep:r+25,nodesep:a});let o=await j(O,n.graph,c,d,i.node(e),_),s=o.elem;v(n,s),n.diff=o.diff||0,t.info(`New compound node after recursive render XAX`,e,`width`,n.width,`height`,n.height),h(s,n)}else i.children(e).length>0?(t.trace(`Cluster - the non recursive path XBX`,e,n.id,n,n.width,`Graph:`,i),t.trace(s(n.id,i)),u.set(n.id,{id:s(n.id,i),node:n})):(t.trace(`Node - the non recursive path XAX`,e,O,i.node(e),y),await f(O,i.node(e),{config:_,dir:y}))})),await e(async()=>{let e=i.edges().map(async function(e){let n=i.edge(e.v,e.w,e.name);if(t.info(`Edge `+e.v+` -> `+e.w+`: `+JSON.stringify(e)),t.info(`Edge `+e.v+` -> `+e.w+`: `,e,` `,JSON.stringify(i.edge(e))),t.info(`Fix`,u,`ids:`,e.v,e.w,`Translating: `,u.get(e.v),u.get(e.w)),k&&n.selfLoop){if(n.selfLoop.order!==1)return;let e=n.id;n.id=n.selfLoop.id,await b(D,n),n.id=e;return}await b(D,n)});await Promise.all(e)},`processEdges`)(),t.info(`Graph before layout:`,JSON.stringify(l(i))),t.info(`############################################# XXX`),t.info(`###                Layout                 ### XXX`),t.info(`############################################# XXX`),a(i),t.info(`Graph after layout:`,JSON.stringify(l(i)));let M=0,{subGraphTitleTotalMargin:N}=r(_);await Promise.all(o(i).map(async function(e){let n=i.node(e);if(t.info(`Position XBX => `+e+`: (`+n.x,`,`+n.y,`) width: `,n.width,` height: `,n.height),n?.clusterNode)n.y+=N,t.info(`A tainted cluster node XBX1`,e,n.id,n.width,n.height,n.x,n.y,i.parent(e)),u.get(n.id).node=n,p(n);else if(i.children(e).length>0){t.info(`A pure cluster node XBX1`,e,n.id,n.x,n.y,n.width,n.height,i.parent(e)),n.height+=N,i.node(n.parentId);let r=n?.padding/2||0,a=n?.labelBBox?.height||0,o=a-r||0;t.debug(`OffsetY`,o,`labelHeight`,a,`halfPadding`,r),await m(w,n),u.get(n.id).node=n}else{let e=i.node(n.parentId);n.y+=N/2,t.info(`A regular node XBX1 - using the padding`,n.id,`parent`,n.parentId,n.width,n.height,n.x,n.y,`offsetY`,n.offsetY,`parent`,e,e?.offsetY,n),p(n)}}));let P=N/2;return A(i,P,{mergeSelfLoops:k}).forEach(function({edge:e,start:n,end:r}){t.info(`Edge `+n+` -> `+r+`: `+JSON.stringify(e),e),e.points.forEach(e=>e.y+=P);let a=i.node(n),o=i.node(r),s=S(T,e,u,c,a,o,d);x(e,s)}),i.nodes().forEach(function(e){let n=i.node(e);t.info(e,n.type,n.diff),n.isGroup&&(M=n.diff)}),t.warn(`Returning from recursive render XAX`,C,M),{elem:C,diff:M}},`recursiveRender`),M=e(async(e,r)=>{let a=new i({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=r.select(`g`);y(o,e.markers,e.type,e.diagramId),g(),C(),_(),c(),e.nodes.forEach(e=>{a.setNode(e.id,{...e}),e.parentId&&a.setParent(e.id,e.parentId)}),t.debug(`Edges:`,e.edges),e.edges.forEach(e=>{if(e.start===e.end){let t=e.start,n=t+`---`+t+`---1`,r=t+`---`+t+`---2`,i=a.node(t);a.setNode(n,{domId:n,id:n,parentId:i.parentId,labelStyle:``,label:``,padding:0,shape:`labelRect`,style:``,width:10,height:10}),a.setParent(n,i.parentId),a.setNode(r,{domId:r,id:r,parentId:i.parentId,labelStyle:``,padding:0,shape:`labelRect`,label:``,style:``,width:10,height:10}),a.setParent(r,i.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`,i.isGroup&&(s.fromCluster=t,l.toCluster=t),l.id=t+`-cyclic-special-2`,l.arrowTypeStart=`none`,a.setEdge(t,n,s,t+`-cyclic-special-0`),a.setEdge(n,r,c,t+`-cyclic-special-1`),a.setEdge(r,t,l,t+`-cyclic-special-2`)}else a.setEdge(e.start,e.end,{...e},e.id)}),t.warn(`Graph at first:`,JSON.stringify(l(a))),d(a),t.warn(`Graph after XAX:`,JSON.stringify(l(a)));let s=n();await j(o,a,e.type,e.diagramId,void 0,s)},`render`);export{A as getEdgesToRender,M as render};