const translate=(t,a)=>`translate(${t}, ${a})`,yLabels=["5 μs","10 μs","25 μs","50 μs","75 μs","100 μs","250 μs","500 μs","750 μs","1.0 ms","2.5 ms","5.0 ms","7.5 ms","10 ms","+Inf"],drawHeatmap=(t,a,e={})=>{void 0===e.margin&&(e.margin={});const r={top:e.margin.top||5,right:e.margin.right||45,bottom:e.margin.bottom||20,left:e.margin.left||27},n=e.height||340,s=e.colorScheme||d3.interpolateInferno,i=Math.min(15,Math.ceil(a.length/20)),o=[],d=[],l=[],p=[],c=[];for(let t=0;t<a.length;t++){const e=a[t];d.push(e.avgTime),l.push({x:t,c:e.clients}),e.skipped&&c.push(t);const r=new Date(e.ts);if(t%i==0){const a=String(r.getHours()).padStart(2,"0"),e=String(r.getMinutes()).padStart(2,"0");o.push({x:t,t:`${a}:${e}`})}for(let a=0;a<15;a++){const r="number"==typeof e.buckets[a]?e.buckets[a]:0;p.push({x:t,y:a,freq:r})}}const m=t.offsetWidth,g=d3.scaleSequential(s).domain([0,1]),h=d3.create("svg").attr("viewBox",[0,0,m,n]),f=d3.scaleBand().domain(d3.range(a.length)).range([r.left,m-r.right]),k=d3.axisBottom(f).tickValues(o.map((t=>t.x))).tickFormat(((t,a)=>o[a].t));h.append("g").attr("id","timeAxis").attr("transform",translate(0,n-r.bottom)).attr("id","x-axis").attr("class","axis").call(k);const x=d3.scaleBand().domain(d3.range(yLabels.length)).range([n-r.bottom,r.top]),u=d3.axisRight(x).tickFormat(((t,a)=>""+yLabels[a]));h.append("g").attr("id","tickBucketsAxis").attr("class","axis").attr("transform",translate(m-r.right-3,0)).call(u),h.append("g").attr("id","heatmap").selectAll("rect").data(p).enter().append("rect").filter((t=>"number"==typeof t.freq)).attr("x",((t,a)=>f(t.x))).attr("y",((t,a)=>x(t.y))).attr("width",f.bandwidth()).attr("height",x.bandwidth()).attr("fill",(t=>g(t.freq))).attr("stroke",(t=>g(t.freq)));const b=Math.round(x.bandwidth()/2),y=d3.max(l.map((t=>t.c))),w=d3.scaleLinear().domain([0,y]).range([n-r.bottom-b,r.top+b]),L=d3.axisLeft(w).tickFormat((t=>t)).tickValues(y>7?null:d3.range(y+1));h.append("g").attr("id","clientAxis").attr("class","axis").attr("transform",translate(r.left-1.5,0)).call(L);const S=d3.line().defined((t=>!isNaN(t.c))).x((t=>f(t.x)+2)).y((t=>w(t.c))),q=h.append("g").attr("id","clientsLine");q.append("path").datum(l).attr("fill","none").attr("stroke","black").attr("stroke-width",6).attr("stroke-linejoin","round").attr("stroke-linecap","round").attr("d",S),q.append("path").datum(l).attr("fill","none").attr("stroke"," rgb(204, 203, 203)").attr("stroke-width",2).attr("stroke-linejoin","round").attr("stroke-linecap","round").attr("d",S),h.append("g").attr("id","skipLines").selectAll(".link").data(c).enter().append("line").style("stroke","gold").attr("stroke-dasharray",6).attr("x1",(t=>f(t))).attr("x2",(t=>f(t))).attr("y1",n-r.bottom).attr("y2",r.top),t.innerHTML="",t.append(h.node())};export{drawHeatmap};