!function(){"use strict";let e=null,t=null,s=!0;const a=new Map;let r=0,n=null,o=null;async function c(e){const s={};t&&(s.Authorization=`Bearer ${t}`);const a=new AbortController,r=setTimeout(()=>a.abort(),15e3);try{return await fetch(e,{headers:s,signal:a.signal})}finally{clearTimeout(r)}}async function l(){if(e)try{const t=`${e.apiBaseUrl}/api/stats`,s=await c(t);if(!s.ok)throw new Error(`API error: ${s.status}`);const a=await s.json();self.postMessage({type:"stats",data:a,timestamp:Date.now()})}catch(t){self.postMessage({type:"error",source:"stats",error:t instanceof Error?t.message:"Unknown error"})}}async function i(){if(e&&s)try{const t=`${e.apiBaseUrl}/api/recent_packets?limit=100`,s=await c(t);if(!s.ok)throw new Error(`API error: ${s.status}`);const n=await s.json(),o=n.data??n.packets??[],l=[];for(const e of o){const t=e.packet_hash;if(t&&!a.has(t)){a.set(t,e),l.push(e);const s=e.timestamp??0;s>r&&(r=s)}}a.size>75e3&&function(){const e=Array.from(a.values()).sort((e,t)=>(e.timestamp??0)-(t.timestamp??0)),t=e.length-75e3;for(let s=0;s<t;s++){const t=e[s];t.packet_hash&&a.delete(t.packet_hash)}}(),l.length>0&&self.postMessage({type:"packets",newPackets:l,totalCount:a.size,newestTimestamp:r})}catch(t){self.postMessage({type:"error",source:"packets",error:t instanceof Error?t.message:"Unknown error"})}}function p(){n&&(clearInterval(n),n=null),o&&(clearInterval(o),o=null)}self.onmessage=c=>{const f=c.data;switch(f.type){case"init":e=f.config,t=f.config.authToken??null,e&&(p(),n=setInterval(l,e.statsIntervalMs),o=setInterval(i,e.packetsIntervalMs),l(),i()),self.postMessage({type:"ready"});break;case"setAuth":t=f.token;break;case"setLiveMode":s=f.enabled;break;case"forceRefresh":l(),i();break;case"clearCache":a.clear(),r=0,self.postMessage({type:"packetsReset",packets:[],newestTimestamp:0});break;case"stop":p()}}}();
