(function(){"use strict";let m,u,v,w;onmessage=function(e){e.data.canvas&&(m=e.data.canvas,E()),e.data.opts&&(u=e.data.opts,E()),e.data.resolvedSeries&&(v=e.data.resolvedSeries,E())};function W(e,a){let s=!1;return()=>{s||(s=!0,setTimeout(()=>{s=!1,e()},a))}}let k=0;async function I(){if(!m||!u||!v)return;const{margins:e,canvasWidth:a,canvasHeight:s,visibleStartTimeSec:y,visibleEndTimeSec:f,minValue:t,maxValue:l}=u;m.width=a,m.height=s;const i=m.getContext("2d");if(!i)return;k+=1;const n=k;for(const c of w?[1,2]:[1]){if(n!==k)return;const o=Date.now();(c===2||!w)&&(w=H(v));const d=r=>({x:e.left+(r.x-y)/(f-y)*(a-e.left-e.right),y:s-e.bottom-(r.y-t)/(l-t)*(s-e.top-e.bottom)}),p=d({x:0,y:0}).y,h=w.map((r,b)=>({dimensionIndex:b,dimensionLabel:`${b}`,pixelTimes:r.times.map(T=>d({x:T,y:0}).x),pixelValues:r.type==="interval"?r.values:r.values.map(T=>d({x:0,y:T}).y),type:r.type,attributes:r.attributes}));C(i,{pixelZero:p,dimensions:h});const S=Date.now()-o;await A(S)}}const E=W(I,10),M=e=>{if(!u||u.hideLegend||!v)return;const{legendOpts:a,margins:s,canvasWidth:y}=u,f=v.filter(h=>h.title).filter(h=>h.type!=="interval");if(f.length===0)return;const{location:t}=a,l=18,i=12,n=50,c=200,o=10,d=20+f.length*l,p=t==="northwest"?{x:s.left+20,y:s.top+20,w:c,h:d}:t==="northeast"?{x:y-s.right-c-20,y:s.top+20,w:c,h:d}:void 0;p&&(e.fillStyle="white",e.strokeStyle="gray",e.lineWidth=1.5,e.fillRect(p.x,p.y,p.w,p.h),e.strokeRect(p.x,p.y,p.w,p.h),f.forEach((h,V)=>{const S=p.y+o+V*l,r={x:p.x+o,y:S,w:n,h:l},b={x:p.x+o+n+o,y:S,w:c-o-o-n-o,h:l},T=h.title||"untitled";if(e.fillStyle="black",e.font=`${i}px Arial`,e.fillText(T,b.x,b.y+b.h/2+i/2),h.type==="line")R(e,h.attributes),e.beginPath(),e.moveTo(r.x,r.y+r.h/2),e.lineTo(r.x+r.w,r.y+r.h/2),e.stroke(),e.setLineDash([]);else if(h.type==="marker"){D(e,h.attributes);const g=l*.3,L=h.attributes.shape??"circle",P={x:r.x+r.w/2,y:r.y+r.h/2};L==="circle"?(e.beginPath(),e.ellipse(P.x,P.y,g,g,0,0,2*Math.PI),e.fill()):L==="square"&&e.fillRect(P.x-g,P.y-g,g*2,g*2)}}))},C=(e,a)=>{if(!u)return;const{margins:s,canvasWidth:y,canvasHeight:f}=u;e.clearRect(0,0,y,f),e.save(),e.beginPath(),e.rect(s.left,s.top,y-s.left-s.right,f-s.top-s.bottom),e.clip(),a.dimensions.forEach(t=>{if(t.type==="line")R(e,t.attributes),e.beginPath(),t.pixelTimes.forEach((l,i)=>{const n=t.pixelValues[i];i===0?e.moveTo(l,n):e.lineTo(l,n)}),e.stroke(),e.setLineDash([]);else if(t.type==="marker"){D(e,t.attributes);const l=t.attributes.radius??2,i=t.attributes.shape??"circle";i==="circle"?t.pixelTimes.forEach((n,c)=>{e.beginPath(),e.ellipse(n,t.pixelValues[c],l,l,0,0,2*Math.PI),e.fill()}):i==="square"&&t.pixelTimes.forEach((n,c)=>{e.fillRect(n-l,t.pixelValues[c]-l,l*2,l*2)})}else if(t.type==="interval"){R(e,t.attributes),D(e,t.attributes);for(let l=0;l<t.pixelTimes.length-1;l++)if(t.pixelValues[l]===0){const i=t.pixelTimes[l],n=t.pixelTimes[l+1];e.fillRect(i,s.top,n-i,f-s.top-s.bottom-1)}}}),M(e),e.restore()},H=e=>{const a=[];if(!u)return a;const{visibleStartTimeSec:s,visibleEndTimeSec:y}=u;return s===void 0||y===void 0||e.forEach(f=>{const t=f.t,l=f.y;let i=t.flatMap((o,d)=>s<=o&&o<=y?d:[]);(i[0]||0)>0&&(i=[i[0]-1,...i]),(i[i.length-1]||t.length)<t.length-1&&i.push(i[i.length-1]+1);const n=i.map(o=>t[o]),c=i.map(o=>l[o]);a.push({type:f.type,times:n,values:c,attributes:f.attributes})}),a},R=(e,a)=>{e.strokeStyle=a.color??"black",e.lineWidth=a.width??1.1,a.dash&&e.setLineDash(a.dash)},D=(e,a)=>{e.fillStyle=a.color??"black"};function A(e){return new Promise(a=>{setTimeout(a,e)})}})();
