import{a as R,b as D,d as _,e as x,f as k,g as F,h as G,i as O,j as U,k as A,l as $,m as H}from"./chunk-TCQ7DWI4.min.js";import{a as P}from"./chunk-6WVHDLRC.min.js";import{a as J,b as L}from"./chunk-N4KMO2SW.min.js";import{c as T,k as B,p as v}from"./chunk-Q6G2VLU2.min.js";import{Da as n,Ga as y,Za as N,h as C}from"./chunk-PBNKOM7K.min.js";function b(e){var t={options:{directed:e.isDirected(),multigraph:e.isMultigraph(),compound:e.isCompound()},nodes:Q(e),edges:Y(e)};return v(e.graph())||(t.value=T(e.graph())),t}function Q(e){return B(e.nodes(),function(t){var i=e.node(t),r=e.parent(t),s={v:t};return v(i)||(s.value=i),v(r)||(s.parent=r),s})}function Y(e){return B(e.edges(),function(t){var i=e.edge(t),r={v:t.v,w:t.w};return v(t.name)||(r.name=t.name),v(i)||(r.value=i),r})}var f={},m={},M={},I=()=>{m={},M={},f={}},S=(e,t)=>(n.trace("In isDecendant",t," ",e," = ",m[t].includes(e)),!!m[t].includes(e)),tt=(e,t)=>(n.info("Decendants of ",t," is ",m[t]),n.info("Edge is ",e),e.v===t||e.w===t?!1:m[t]?m[t].includes(e.v)||S(e.v,t)||S(e.w,t)||m[t].includes(e.w):(n.debug("Tilt, ",t,",not in decendants"),!1)),V=(e,t,i,r)=>{n.warn("Copying children of ",e,"root",r,"data",t.node(e),r);let s=t.children(e)||[];e!==r&&s.push(e),n.warn("Copying (nodes) clusterId",e,"nodes",s),s.forEach(a=>{if(t.children(a).length>0)V(a,t,i,r);else{let h=t.node(a);n.info("cp ",a," to ",r," with parent ",e),i.setNode(a,h),r!==t.parent(a)&&(n.warn("Setting parent",a,t.parent(a)),i.setParent(a,t.parent(a))),e!==r&&a!==e?(n.debug("Setting parent",a,e),i.setParent(a,e)):(n.info("In copy ",e,"root",r,"data",t.node(e),r),n.debug("Not Setting parent for node=",a,"cluster!==rootId",e!==r,"node!==clusterId",a!==e));let l=t.edges(a);n.debug("Copying Edges",l),l.forEach(d=>{n.info("Edge",d);let w=t.edge(d.v,d.w,d.name);n.info("Edge data",w,r);try{tt(d,r)?(n.info("Copying as ",d.v,d.w,w,d.name),i.setEdge(d.v,d.w,w,d.name),n.info("newGraph edges ",i.edges(),i.edge(i.edges()[0]))):n.info("Skipping copy of edge ",d.v,"-->",d.w," rootId: ",r," clusterId:",e)}catch(g){n.error(g)}})}n.debug("Removing node",a),t.removeNode(a)})},j=(e,t)=>{let i=t.children(e),r=[...i];for(let s of i)M[s]=e,r=[...r,...j(s,t)];return r},E=(e,t)=>{n.trace("Searching",e);let i=t.children(e);if(n.trace("Searching children of id ",e,i),i.length<1)return n.trace("This is a valid node",e),e;for(let r of i){let s=E(r,t);if(s)return n.trace("Found replacement for",e," => ",s),s}},p=e=>!f[e]||!f[e].externalConnections?e:f[e]?f[e].id:e,et=(e,t)=>{if(!e||t>10){n.debug("Opting out, no graph ");return}else n.debug("Opting in, graph ");e.nodes().forEach(function(i){e.children(i).length>0&&(n.warn("Cluster identified",i," Replacement id in edges: ",E(i,e)),m[i]=j(i,e),f[i]={id:E(i,e),clusterData:e.node(i)})}),e.nodes().forEach(function(i){let r=e.children(i),s=e.edges();r.length>0?(n.debug("Cluster identified",i,m),s.forEach(a=>{if(a.v!==i&&a.w!==i){let h=S(a.v,i),l=S(a.w,i);h^l&&(n.warn("Edge: ",a," leaves cluster ",i),n.warn("Decendants of XXX ",i,": ",m[i]),f[i].externalConnections=!0)}})):n.debug("Not a cluster ",i,m)}),e.edges().forEach(function(i){let r=e.edge(i);n.warn("Edge "+i.v+" -> "+i.w+": "+JSON.stringify(i)),n.warn("Edge "+i.v+" -> "+i.w+": "+JSON.stringify(e.edge(i)));let s=i.v,a=i.w;if(n.warn("Fix XXX",f,"ids:",i.v,i.w,"Translating: ",f[i.v]," --- ",f[i.w]),f[i.v]&&f[i.w]&&f[i.v]===f[i.w]){n.warn("Fixing and trixing link to self - removing XXX",i.v,i.w,i.name),n.warn("Fixing and trixing - removing XXX",i.v,i.w,i.name),s=p(i.v),a=p(i.w),e.removeEdge(i.v,i.w,i.name);let h=i.w+"---"+i.v;e.setNode(h,{domId:h,id:h,labelStyle:"",labelText:r.label,padding:0,shape:"labelRect",style:""});let l=JSON.parse(JSON.stringify(r)),d=JSON.parse(JSON.stringify(r));l.label="",l.arrowTypeEnd="none",d.label="",l.fromCluster=i.v,d.toCluster=i.v,e.setEdge(s,h,l,i.name+"-cyclic-special"),e.setEdge(h,a,d,i.name+"-cyclic-special")}else(f[i.v]||f[i.w])&&(n.warn("Fixing and trixing - removing XXX",i.v,i.w,i.name),s=p(i.v),a=p(i.w),e.removeEdge(i.v,i.w,i.name),s!==i.v&&(r.fromCluster=i.v),a!==i.w&&(r.toCluster=i.w),n.warn("Fix Replacing with XXX",s,a,i.name),e.setEdge(s,a,r,i.name))}),n.warn("Adjusted Graph",b(e)),W(e,0),n.trace(f)},W=(e,t)=>{if(n.warn("extractor - ",t,b(e),e.children("D")),t>10){n.error("Bailing out");return}let i=e.nodes(),r=!1;for(let s of i){let a=e.children(s);r=r||a.length>0}if(!r){n.debug("Done, no node has children",e.nodes());return}n.debug("Nodes = ",i,t);for(let s of i)if(n.debug("Extracting node",s,f,f[s]&&!f[s].externalConnections,!e.parent(s),e.node(s),e.children("D")," Depth ",t),!f[s])n.debug("Not a cluster",s,t);else if(!f[s].externalConnections&&e.children(s)&&e.children(s).length>0){n.warn("Cluster without external connections, without a parent and with children",s,t);let h=e.graph().rankdir==="TB"?"LR":"TB";f[s]&&f[s].clusterData&&f[s].clusterData.dir&&(h=f[s].clusterData.dir,n.warn("Fixing dir",f[s].clusterData.dir,h));let l=new J({multigraph:!0,compound:!0}).setGraph({rankdir:h,nodesep:50,ranksep:50,marginx:8,marginy:8}).setDefaultEdgeLabel(function(){return{}});n.warn("Old graph before copy",b(e)),V(s,e,l,s),e.setNode(s,{clusterNode:!0,id:s,clusterData:f[s].clusterData,labelText:f[s].labelText,graph:l}),n.warn("New graph after copy node: (",s,")",b(l)),n.debug("Old graph after copy",b(e))}else n.warn("Cluster ** ",s," **not meeting the criteria !externalConnections:",!f[s].externalConnections," no parent: ",!e.parent(s)," children ",e.children(s)&&e.children(s).length>0,e.children("D"),t),n.debug(f);i=e.nodes(),n.warn("New list of nodes",i);for(let s of i){let a=e.node(s);n.warn(" Now next level",s,a),a.clusterNode&&W(a.graph,t+1)}},q=(e,t)=>{if(t.length===0)return[];let i=Object.assign(t);return t.forEach(r=>{let s=e.children(r),a=q(e,s);i=[...i,...a]}),i},it=e=>q(e,e.children()),nt=(e,t)=>{n.info("Creating subgraph rect for ",t.id,t);let i=e.insert("g").attr("class","cluster"+(t.class?" "+t.class:"")).attr("id",t.id),r=i.insert("rect",":first-child"),s=y(N().flowchart.htmlLabels),a=i.insert("g").attr("class","cluster-label"),h=t.labelType==="markdown"?P(a,t.labelText,{style:t.labelStyle,useHtmlLabels:s}):a.node().appendChild(D(t.labelText,t.labelStyle,void 0,!0)),l=h.getBBox();if(y(N().flowchart.htmlLabels)){let o=h.children[0],u=C(h);l=o.getBoundingClientRect(),u.attr("width",l.width),u.attr("height",l.height)}let d=0*t.padding,w=d/2,g=t.width<=l.width+d?l.width+d:t.width;t.width<=l.width+d?t.diff=(l.width-t.width)/2-t.padding/2:t.diff=-t.padding/2,n.trace("Data ",t,JSON.stringify(t)),r.attr("style",t.style).attr("rx",t.rx).attr("ry",t.ry).attr("x",t.x-g/2).attr("y",t.y-t.height/2-w).attr("width",g).attr("height",t.height+d),s?a.attr("transform","translate("+(t.x-l.width/2)+", "+(t.y-t.height/2)+")"):a.attr("transform","translate("+t.x+", "+(t.y-t.height/2)+")");let c=r.node().getBBox();return t.width=c.width,t.height=c.height,t.intersect=function(o){return x(t,o)},i},st=(e,t)=>{let i=e.insert("g").attr("class","note-cluster").attr("id",t.id),r=i.insert("rect",":first-child"),s=0*t.padding,a=s/2;r.attr("rx",t.rx).attr("ry",t.ry).attr("x",t.x-t.width/2-a).attr("y",t.y-t.height/2-a).attr("width",t.width+s).attr("height",t.height+s).attr("fill","none");let h=r.node().getBBox();return t.width=h.width,t.height=h.height,t.intersect=function(l){return x(t,l)},i},rt=(e,t)=>{let i=e.insert("g").attr("class",t.classes).attr("id",t.id),r=i.insert("rect",":first-child"),s=i.insert("g").attr("class","cluster-label"),a=i.append("rect"),h=s.node().appendChild(D(t.labelText,t.labelStyle,void 0,!0)),l=h.getBBox();if(y(N().flowchart.htmlLabels)){let o=h.children[0],u=C(h);l=o.getBoundingClientRect(),u.attr("width",l.width),u.attr("height",l.height)}l=h.getBBox();let d=0*t.padding,w=d/2,g=t.width<=l.width+t.padding?l.width+t.padding:t.width;t.width<=l.width+t.padding?t.diff=(l.width+t.padding*0-t.width)/2:t.diff=-t.padding/2,r.attr("class","outer").attr("x",t.x-g/2-w).attr("y",t.y-t.height/2-w).attr("width",g+d).attr("height",t.height+d),a.attr("class","inner").attr("x",t.x-g/2-w).attr("y",t.y-t.height/2-w+l.height-1).attr("width",g+d).attr("height",t.height+d-l.height-3),s.attr("transform","translate("+(t.x-l.width/2)+", "+(t.y-t.height/2-t.padding/3+(y(N().flowchart.htmlLabels)?5:3))+")");let c=r.node().getBBox();return t.height=c.height,t.intersect=function(o){return x(t,o)},i},at=(e,t)=>{let i=e.insert("g").attr("class",t.classes).attr("id",t.id),r=i.insert("rect",":first-child"),s=0*t.padding,a=s/2;r.attr("class","divider").attr("x",t.x-t.width/2-a).attr("y",t.y-t.height/2).attr("width",t.width+s).attr("height",t.height+s);let h=r.node().getBBox();return t.width=h.width,t.height=h.height,t.diff=-t.padding/2,t.intersect=function(l){return x(t,l)},i},ct={rect:nt,roundedWithTitle:rt,noteGroup:st,divider:at},z={},ot=(e,t)=>{n.trace("Inserting cluster");let i=t.shape||"rect";z[t.id]=ct[i](e,t)},lt=()=>{z={}},K=async(e,t,i,r)=>{n.info("Graph in recursive render: XXX",b(t),r);let s=t.graph().rankdir;n.trace("Dir in recursive render - dir:",s);let a=e.insert("g").attr("class","root");t.nodes()?n.info("Recursive render XXX",t.nodes()):n.info("No nodes found for",t),t.edges().length>0&&n.trace("Recursive edges",t.edge(t.edges()[0]));let h=a.insert("g").attr("class","clusters"),l=a.insert("g").attr("class","edgePaths"),d=a.insert("g").attr("class","edgeLabels"),w=a.insert("g").attr("class","nodes");await Promise.all(t.nodes().map(async function(c){let o=t.node(c);if(r!==void 0){let u=JSON.parse(JSON.stringify(r.clusterData));n.info("Setting data for cluster XXX (",c,") ",u,r),t.setNode(r.id,u),t.parent(c)||(n.trace("Setting parent",c,r.id),t.setParent(c,r.id,u))}if(n.info("(Insert) Node XXX"+c+": "+JSON.stringify(t.node(c))),o&&o.clusterNode){n.info("Cluster identified",c,o.width,t.node(c));let u=await K(w,o.graph,i,t.node(c)),X=u.elem;_(o,X),o.diff=u.diff||0,n.info("Node bounds (abc123)",c,o,o.width,o.x,o.y),F(X,o),n.warn("Recursive render complete ",X,o)}else t.children(c).length>0?(n.info("Cluster - the non recursive path XXX",c,o.id,o,t),n.info(E(o.id,t)),f[o.id]={id:E(o.id,t),node:o}):(n.info("Node - the non recursive path",c,o.id,o),await k(w,t.node(c),s))})),t.edges().forEach(function(c){let o=t.edge(c.v,c.w,c.name);n.info("Edge "+c.v+" -> "+c.w+": "+JSON.stringify(c)),n.info("Edge "+c.v+" -> "+c.w+": ",c," ",JSON.stringify(t.edge(c))),n.info("Fix",f,"ids:",c.v,c.w,"Translateing: ",f[c.v],f[c.w]),A(d,o)}),t.edges().forEach(function(c){n.info("Edge "+c.v+" -> "+c.w+": "+JSON.stringify(c))}),n.info("#############################################"),n.info("###                Layout                 ###"),n.info("#############################################"),n.info(t),L(t),n.info("Graph after layout:",b(t));let g=0;return it(t).forEach(function(c){let o=t.node(c);n.info("Position "+c+": "+JSON.stringify(t.node(c))),n.info("Position "+c+": ("+o.x,","+o.y,") width: ",o.width," height: ",o.height),o&&o.clusterNode?O(o):t.children(c).length>0?(ot(h,o),f[o.id].node=o):O(o)}),t.edges().forEach(function(c){let o=t.edge(c);n.info("Edge "+c.v+" -> "+c.w+": "+JSON.stringify(o),o);let u=H(l,c,o,f,i,t);$(o,u)}),t.nodes().forEach(function(c){let o=t.node(c);n.info(c,o.type,o.diff),o.type==="group"&&(g=o.diff)}),{elem:a,diff:g}},xt=async(e,t,i,r,s)=>{R(e,i,r,s),G(),U(),lt(),I(),n.warn("Graph at first:",b(t)),et(t),n.warn("Graph after:",b(t)),await K(e,t,r)};export{xt as a};
//# sourceMappingURL=chunk-F5HZMA2O.min.js.map
