(()=>{var w=class{rootPackageName;graph;constructor(e,t){this.graph=new Map;let n=i=>(this.graph.has(i)||this.graph.set(i,{name:i,parents:new Map,children:new Map}),this.graph.get(i));for(let[i,g]of Object.entries(e)){let c=n(i),o=g.map(n);for(let a of o)a.parents.set(c.name,c),c.children.set(a.name,a)}if(!this.graph.has(t))throw new Error(`Provided dataset doesn't have root package ${t}`);this.rootPackageName=t}get root(){return this.graph.get(this.rootPackageName)}getPackage(e){let t=this.graph.get(e);if(!t)throw new Error(`Unable to find package ${e}`);return t}};var f=class{packageName;parentsDepth;childrenDepth;registry;constructor(e){this.registry=e,this.packageName=e.rootPackageName,this.parentsDepth=1,this.childrenDepth=1}get package(){return this.registry.getPackage(this.packageName)}};var y={root:["1","2","3"],"1":["10","11","12"],"2":["12","13"],"3":["14","11"],"11":["100","101"],"13":["101","102"],"101":["1001","1002"]};var x=class{fontSize;boxSize;pageSize;constructor(e){this.fontSize=e.fontSize,this.boxSize=e.boxSize,this.pageSize=e.pageSize}make(e){let t=B(e.package,e.parentsDepth,e.childrenDepth),n=t.length,i=Math.max(...t.map(h=>h.length)),g=new Map,c=[],o=(this.pageSize.height-n*this.boxSize)/(n+1),a=o;for(let h of t){let u=(this.pageSize.width-h.length*this.boxSize)/(h.length+1),m=u;for(let p of h){let d=this.makeCapture(m,a,p.name);c.push({x:m,y:a,root:p.name===e.packageName,capture:d,packageName:p.name}),g.set(p.name,{x:m,y:a}),m+=this.boxSize+u}a+=this.boxSize+o}let l=V(e.package,e.parentsDepth,e.childrenDepth),s=[];for(let[h,u]of l)for(let m of u){let p=g.get(h);if(!p)throw new Error(`Can't find coordinates for rect ${h}`);let d=g.get(m);if(!d)throw new Error(`Can't find coordinates for rect ${m}`);let M={from:{x:p.x+this.boxSize/2,y:p.y+this.boxSize},to:{x:d.x+this.boxSize/2,y:d.y-4}};s.push(M)}return{boxes:c,arrows:s}}makeCapture(e,t,n){let i=this.fontSize.width*n.length,g=(this.boxSize-i)/2,c=(this.boxSize-this.fontSize.height)/2;return{x:e+g,y:t+c+this.fontSize.height-4,text:n}}};function B(r,e,t){function n(a,l){let s=a[a.length-1],h=new Map;for(let m of s)for(let[p,d]of m[l])h.set(d.name,d);let u=[...h.values()];a.push(u)}function i(a,l){let s=[[r]];for(;a>0&&s[s.length-1].length!==0;)n(s,l),a-=1;return s[s.length-1].length===0&&s.pop(),s.slice(1)}let g=i(e,"parents").reverse(),c=i(t,"children");return[...g,[r],...c]}function V(r,e,t){let n=new Map;function i(o,a){n.has(o)||n.set(o,new Set),n.get(o).add(a)}function g(o,a){if(a!==0)for(let[l,s]of o.parents)i(s.name,o.name),g(s,a-1)}function c(o,a){if(a!==0)for(let[l,s]of o.children)i(o.name,s.name),c(s,a-1)}return g(r,e),c(r,t),n}function b(r){let e=r.width.baseVal.value,t=r.height.baseVal.value;return{width:e,height:t}}function E(r){let{width:e,height:t}=b(r),n=`0 0 ${e} ${t}`;r.setAttribute("viewBox",n)}function v(r){let e=document.querySelector(r);if(!e)throw new Error(`Can't find element with selector ${r}`);let t=e.textContent;if(!t)throw new Error(`Element with selector ${r} doesn't have text`);let{width:n,height:i}=e.getBoundingClientRect();return{width:n/t.length,height:i}}function z(r){let e=document.createElementNS("http://www.w3.org/2000/svg","g");return e.setAttribute("id",r.id),e}function P(r){let e=document.createElementNS("http://www.w3.org/2000/svg","text");return e.setAttribute("x",r.x.toString()),e.setAttribute("y",r.y.toString()),e.textContent=r.text,e.classList.add("package-name"),e}function C(r){let e=document.createElementNS("http://www.w3.org/2000/svg","rect");return e.setAttribute("x",r.x.toString()),e.setAttribute("y",r.y.toString()),e.classList.add("box"),e}function N(r){let e=document.createElementNS("http://www.w3.org/2000/svg","line");return e.setAttribute("x1",r.x1.toString()),e.setAttribute("y1",r.y1.toString()),e.setAttribute("x2",r.x2.toString()),e.setAttribute("y2",r.y2.toString()),e.classList.add("arrow"),e}var D="tree",T=100,S=class{registry;state;svg;treeElement;grid;constructor(e,t){this.svg=document.querySelector("svg"),E(this.svg);let n=b(this.svg),i=v("#one-line-text-sample");this.registry=e,this.state=t,this.treeElement=document.getElementById("tree"),this.grid=new x({fontSize:i,boxSize:T,pageSize:n})}wire(){this.draw(),window.addEventListener("click",e=>{let n=e.target.dataset.package;n&&n!==this.state.packageName&&(this.state.packageName=n,this.redraw())}),window.addEventListener("keydown",e=>{e.ctrlKey&&e.key==="f"&&console.log("search")})}redraw(){this.clear(),this.draw()}clear(){let e=z({id:D});this.treeElement.remove(),this.svg.appendChild(e),this.treeElement=e}draw(){let{boxes:e,arrows:t}=this.grid.make(this.state);e.forEach(n=>this.drawBox(n)),t.forEach(n=>this.drawArrow(n))}drawBox(e){let t=C({x:e.x,y:e.y});t.dataset.package=e.packageName;let n=e.root?"blue-box":"green-box";t.classList.add(n),this.treeElement.appendChild(t);let i=P({x:e.capture.x,y:e.capture.y,text:e.capture.text});i.dataset.package=e.packageName,this.treeElement.appendChild(i)}drawArrow(e){let t=N({x1:e.from.x,y1:e.from.y,x2:e.to.x,y2:e.to.y});this.treeElement.appendChild(t)}};var A=window.data;A===void 0&&console.log("window.data is not defined, using default data...");var G=new w(A||y,"root"),k=new f(G);k.childrenDepth=2;k.parentsDepth=2;var R=new S(G,k);R.wire();})();