import{t as e}from"./graphlib.D9QTypQK.js";import{a as t,i as n,n as r,o as i,r as a,t as o}from"./chunk-RYQCIY6F.CLMTjyXb.js";import{t as s}from"./dagre.DGETeYl4.js";import{n as c}from"./chunk-Y2CYZVJY.DsF7k-Jl.js";import{t as l}from"./src.DMVfaFne.js";import{x as u}from"./chunk-I66GZJ75.DE9AwV3O.js";import"./chunk-NSK5VX7P.Bdf5aQAc.js";import"./chunk-4I5QYGJK.hg4ofZMO.js";import"./chunk-WRU74C26.mA3yoLIh.js";import"./chunk-7BUUIJ7U.CAhmNhj7.js";import{n as d}from"./chunk-UBXNYLIW.BHK08rsA.js";import"./chunk-W5SLKNZC.NO1AxUsq.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.DsE7Dzlj.js";import{a as y,i as b,o as x,r as S,t as C}from"./chunk-7Z6QIM7H.BtklZ3Wo.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(e,r,o,u,g,_)=>{l.warn(`Graph in recursive render:XAX`,i(r),g);let y=r.graph().rankdir;l.trace(`Dir in recursive render - dir:`,y);let C=e.insert(`g`).attr(`class`,`root`);r.nodes()?l.info(`Recursive render XXX`,r.nodes()):l.info(`No nodes found for`,r),r.edges().length>0&&l.info(`Recursive edges`,r.edge(r.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(o);await Promise.all(r.nodes().map(async function(e){let t=r.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),r.setNode(g.id,t),r.parent(e)||(l.trace(`Setting parent`,e,g.id),r.setParent(e,g.id,t))}if(l.info(`(Insert) Node XXX`+e+`: `+JSON.stringify(r.node(e))),t?.clusterNode){l.info(`Cluster identified XBX`,e,t.width,r.node(e));let{ranksep:n,nodesep:i}=r.graph();t.graph.setGraph({...t.graph.graph(),ranksep:n+25,nodesep:i});let a=await j(O,t.graph,o,u,r.node(e),_),s=a.elem;v(t,s),t.diff=a.diff||0,l.info(`New compound node after recursive render XAX`,e,`width`,t.width,`height`,t.height),h(s,t)}else r.children(e).length>0?(l.trace(`Cluster - the non recursive path XBX`,e,t.id,t,t.width,`Graph:`,r),l.trace(n(t.id,r)),a.set(t.id,{id:n(t.id,r),node:t})):(l.trace(`Node - the non recursive path XAX`,e,O,r.node(e),y),await f(O,r.node(e),{config:_,dir:y}))})),await c(async()=>{let e=r.edges().map(async function(e){let t=r.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(r.edge(e))),l.info(`Fix`,a,`ids:`,e.v,e.w,`Translating: `,a.get(e.v),a.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(i(r))),l.info(`############################################# XXX`),l.info(`###                Layout                 ### XXX`),l.info(`############################################# XXX`),s(r),l.info(`Graph after layout:`,JSON.stringify(i(r)));let M=0,{subGraphTitleTotalMargin:N}=d(_);await Promise.all(t(r).map(async function(e){let t=r.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,r.parent(e)),a.get(t.id).node=t,p(t);else if(r.children(e).length>0){l.info(`A pure cluster node XBX1`,e,t.id,t.x,t.y,t.width,t.height,r.parent(e)),t.height+=N,r.node(t.parentId);let n=t?.padding/2||0,i=t?.labelBBox?.height||0,o=i-n||0;l.debug(`OffsetY`,o,`labelHeight`,i,`halfPadding`,n),await m(w,t),a.get(t.id).node=t}else{let e=r.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(r,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 i=r.node(t),s=r.node(n),c=S(T,e,a,o,i,s,u);x(e,c)}),r.nodes().forEach(function(e){let t=r.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(t,n)=>{let a=new e({multigraph:!0,compound:!0}).setGraph({rankdir:t.direction,nodesep:t.config?.nodeSpacing||t.config?.flowchart?.nodeSpacing||t.nodeSpacing,ranksep:t.config?.rankSpacing||t.config?.flowchart?.rankSpacing||t.rankSpacing,marginx:8,marginy:8}).setDefaultEdgeLabel(function(){return{}}),s=n.select(`g`);y(s,t.markers,t.type,t.diagramId),g(),C(),_(),r(),t.nodes.forEach(e=>{a.setNode(e.id,{...e}),e.parentId&&a.setParent(e.id,e.parentId)}),l.debug(`Edges:`,t.edges),t.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)}),l.warn(`Graph at first:`,JSON.stringify(i(a))),o(a),l.warn(`Graph after XAX:`,JSON.stringify(i(a)));let c=u();await j(s,a,t.type,t.diagramId,void 0,c)},`render`);export{A as getEdgesToRender,M as render};