(function(){"use strict";let u,d,o,v;onmessage=function(e){e.data.canvas&&(u=e.data.canvas,m()),e.data.opts&&(d=e.data.opts,m()),e.data.plotData&&(o=e.data.plotData,m())};function g(e,r){let s=!1;return()=>{s||(s=!0,setTimeout(()=>{s=!1,e()},r))}}let h=0;async function T(){if(!u||!d||!o)return;const{margins:e,canvasWidth:r,canvasHeight:s,visibleStartTimeSec:t,visibleEndTimeSec:n,hoveredUnitId:f,selectedUnitIds:a}=d;u.width=r,u.height=s;const i=u.getContext("2d");if(!i)return;h+=1;const k=h,w=o.plots.length,I=c=>s-e.bottom-(c+.5-0)/(w-0)*(s-e.top-e.bottom);for(const c of o?[1,2]:[1]){if(k!==h)return;const D=Date.now();(c===2||!v)&&(v=b(o));const E=l=>e.left+(l-t)/(n-t)*(r-e.left-e.right),W=o.plots.map((l,A)=>({y:I(A),x:l.spikeTimesSec.map(C=>E(C)),unitId:l.unitId,color:l.color,hovered:l.unitId===f,selected:a.includes(l.unitId)}));y(i,W);const p=Date.now()-D;await S(p)}}const m=g(T,10),y=(e,r)=>{if(!d)return;const{margins:s,canvasWidth:t,canvasHeight:n}=d;e.clearRect(0,0,t,n);const f=n/r.length;for(const a of[1,2,3])r.forEach(i=>{(a===1&&f>=10||a===2&&i.selected||a===3&&i.hovered)&&(e.fillStyle=a===1?i.color:a===2?"black":i.color,e.textAlign="right",e.textBaseline="middle",e.font=`${a>1?"bold ":""}12px Arial`,e.fillText(i.unitId+"",s.left-4,i.y),(a===3||a===2&&i.hovered)&&(e.textAlign="left",e.textBaseline="middle",e.font=`${a>1?"bold ":""}12px Arial`,e.fillText(i.unitId+"",t-s.right+4,i.y)))});e.save(),e.beginPath(),e.rect(s.left,s.top,t-s.left-s.right,n-s.top-s.bottom),e.clip();for(const a of[1,2])r.forEach(i=>{a===2&&i.hovered&&(e.strokeStyle="yellow",e.lineWidth=3,e.beginPath(),e.moveTo(0,i.y),e.lineTo(t,i.y),e.stroke(),e.strokeStyle="gray",e.lineWidth=1,e.beginPath(),e.moveTo(0,i.y),e.lineTo(t,i.y),e.stroke()),a===1&&i.selected&&(e.strokeStyle="lightblue",e.lineWidth=3,e.beginPath(),e.moveTo(0,i.y),e.lineTo(t,i.y),e.stroke())});r.forEach(a=>{e.strokeStyle=a.color,e.lineWidth=3,e.beginPath(),a.x.forEach(i=>{e.moveTo(i-2,a.y),e.lineTo(i+2,a.y)}),e.stroke()}),e.restore()},b=e=>{if(!d)return;const{visibleStartTimeSec:r,visibleEndTimeSec:s}=d;if(r===void 0||s===void 0)return;const t=e.plots.map(n=>({...n,spikeTimesSec:n.spikeTimesSec.filter(f=>r<=f&&f<=s)}));return{...e,plots:t}};function S(e){return new Promise(r=>{setTimeout(r,e)})}})();
