import{C as e,En as t,M as n,On as r,S as i,_ as a,b as o,d as s,f as c,l,s as u,tn as d,u as f,v as p,w as m,y as h}from"./index-D6r_eLun.js";import{t as g}from"./graphlib-CZ6DcSg7.js";import{t as _}from"./dagre-MpneA_0c.js";import{a as v,i as y,n as b,o as x,r as S,t as C}from"./chunk-RYQCIY6F-8YBnVEJg.js";var w=r((e,t,n)=>Math.max(t,Math.min(n,e)),`clamp`),T=r((e=`TB`)=>{switch(e){case`BT`:return`bottom`;case`LR`:return`right`;case`RL`:return`left`;default:return`top`}},`getDefaultSelfLoopSide`),E=r(e=>e===`flowchart`||e===`flowchart-v2`||e===`stateDiagram`,`shouldMergeSelfLoopSegments`),D=r((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=r((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=r((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=r((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=r(async(a,s,u,d,p,g)=>{t.warn(`Graph in recursive render:XAX`,x(s),p);let b=s.graph().rankdir;t.trace(`Dir in recursive render - dir:`,b);let C=a.insert(`g`).attr(`class`,`root`);s.nodes()?t.info(`Recursive render XXX`,s.nodes()):t.info(`No nodes found for`,s),s.edges().length>0&&t.info(`Recursive edges`,s.edge(s.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(u);await Promise.all(s.nodes().map(async function(n){let r=s.node(n);if(p!==void 0){let e=JSON.parse(JSON.stringify(p.clusterData));t.trace(`Setting data for parent cluster XXX
 Node.id = `,n,`
 data=`,e.height,`
Parent cluster`,p.height),s.setNode(p.id,e),s.parent(n)||(t.trace(`Setting parent`,n,p.id),s.setParent(n,p.id,e))}if(t.info(`(Insert) Node XXX`+n+`: `+JSON.stringify(s.node(n))),r?.clusterNode){t.info(`Cluster identified XBX`,n,r.width,s.node(n));let{ranksep:i,nodesep:a}=s.graph();r.graph.setGraph({...r.graph.graph(),ranksep:i+25,nodesep:a});let o=await j(O,r.graph,u,d,s.node(n),g),c=o.elem;m(r,c),r.diff=o.diff||0,t.info(`New compound node after recursive render XAX`,n,`width`,r.width,`height`,r.height),e(c,r)}else s.children(n).length>0?(t.trace(`Cluster - the non recursive path XBX`,n,r.id,r,r.width,`Graph:`,s),t.trace(y(r.id,s)),S.set(r.id,{id:y(r.id,s),node:r})):(t.trace(`Node - the non recursive path XAX`,n,O,s.node(n),b),await o(O,s.node(n),{config:g,dir:b}))})),await r(async()=>{let e=s.edges().map(async function(e){let n=s.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(s.edge(e))),t.info(`Fix`,S,`ids:`,e.v,e.w,`Translating: `,S.get(e.v),S.get(e.w)),k&&n.selfLoop){if(n.selfLoop.order!==1)return;let e=n.id;n.id=n.selfLoop.id,await f(D,n),n.id=e;return}await f(D,n)});await Promise.all(e)},`processEdges`)(),t.info(`Graph before layout:`,JSON.stringify(x(s))),t.info(`############################################# XXX`),t.info(`###                Layout                 ### XXX`),t.info(`############################################# XXX`),_(s),t.info(`Graph after layout:`,JSON.stringify(x(s)));let M=0,{subGraphTitleTotalMargin:N}=n(g);await Promise.all(v(s).map(async function(e){let n=s.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,s.parent(e)),S.get(n.id).node=n,i(n);else if(s.children(e).length>0){t.info(`A pure cluster node XBX1`,e,n.id,n.x,n.y,n.width,n.height,s.parent(e)),n.height+=N,s.node(n.parentId);let r=n?.padding/2||0,i=n?.labelBBox?.height||0,a=i-r||0;t.debug(`OffsetY`,a,`labelHeight`,i,`halfPadding`,r),await h(w,n),S.get(n.id).node=n}else{let e=s.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),i(n)}}));let P=N/2;return A(s,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),c(e,l(T,e,S,u,s.node(n),s.node(r),d))}),s.nodes().forEach(function(e){let n=s.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=r(async(e,n)=>{let r=new g({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{}}),i=n.select(`g`);s(i,e.markers,e.type,e.diagramId),p(),u(),a(),b(),e.nodes.forEach(e=>{r.setNode(e.id,{...e}),e.parentId&&r.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`,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)}),t.warn(`Graph at first:`,JSON.stringify(x(r))),C(r),t.warn(`Graph after XAX:`,JSON.stringify(x(r)));let o=d();await j(i,r,e.type,e.diagramId,void 0,o)},`render`);export{A as getEdgesToRender,M as render};