import{Y as G,Q as K,r as T,H as $}from"./DgRzpAjP.js";import{e as P}from"./DE0mmtVA.js";import{u as H}from"./n1hvS8Bl.js";var W=typeof global=="object"&&global&&global.Object===Object&&global,Q=typeof self=="object"&&self&&self.Object===Object&&self,V=W||Q||Function("return this")(),C=V.Symbol,M=Object.prototype,X=M.hasOwnProperty,Y=M.toString,v=C?C.toStringTag:void 0;function z(o){var c=X.call(o,v),d=o[v];try{o[v]=void 0;var i=!0}catch{}var l=Y.call(o);return i&&(c?o[v]=d:delete o[v]),l}var J=Object.prototype,Z=J.toString;function ee(o){return Z.call(o)}var te="[object Null]",oe="[object Undefined]",U=C?C.toStringTag:void 0;function ne(o){return o==null?o===void 0?oe:te:U&&U in Object(o)?z(o):ee(o)}function ae(o){return o!=null&&typeof o=="object"}var se="[object Number]";function B(o){return typeof o=="number"||ae(o)&&ne(o)==se}const re="metacubexd_db",g="data_usage_logs",ce=1;class le{db=null;async open(){return this.db?this.db:new Promise((c,d)=>{const i=indexedDB.open(re,ce);i.onupgradeneeded=l=>{const u=l.target.result;if(!u.objectStoreNames.contains(g)){const r=u.createObjectStore(g,{keyPath:"id",autoIncrement:!0});r.createIndex("timestamp","timestamp",{unique:!1}),r.createIndex("sourceIP","sourceIP",{unique:!1}),r.createIndex("host","host",{unique:!1}),r.createIndex("outbound","outbound",{unique:!1}),r.createIndex("process","process",{unique:!1})}},i.onsuccess=l=>{this.db=l.target.result,c(this.db)},i.onerror=l=>{d(l.target.error)}})}async addLogs(c){const d=await this.open();return new Promise((i,l)=>{const u=d.transaction([g],"readwrite"),r=u.objectStore(g);c.forEach(p=>r.add(p)),u.oncomplete=()=>i(),u.onerror=p=>l(u.error)})}async query(c,d){const i=await this.open();return new Promise((l,u)=>{const S=i.transaction([g],"readonly").objectStore(g).index("timestamp"),m=IDBKeyRange.bound(c,d),h=S.openCursor(m),b=[];h.onsuccess=y=>{const f=y.target.result;f?(b.push(f.value),f.continue()):l(b)},h.onerror=y=>u(h.error)})}async clearAll(){const c=await this.open();return new Promise((d,i)=>{const r=c.transaction([g],"readwrite").objectStore(g).clear();r.onsuccess=()=>d(),r.onerror=p=>i(r.error)})}async cleanup(c){const d=await this.open();return new Promise((i,l)=>{const r=d.transaction([g],"readwrite").objectStore(g),p=r.index("timestamp"),S=IDBKeyRange.upperBound(c),m=p.openKeyCursor(S);m.onsuccess=h=>{const b=h.target.result;b?(r.delete(b.primaryKey),b.continue()):i()},m.onerror=h=>l(m.error)})}}const O=new le,pe=G("connections",()=>{const o=H(),c=T([]),d=T([]),i=T([]),l=T(null),u=T(!1),r=[];let p=null;const S=async()=>{if(r.length===0)return;const n=[...r];r.length=0;try{await O.addLogs(n);const a=Date.now()-720*60*60*1e3;await O.cleanup(a)}catch(a){console.error("[Data Usage] Failed to flush logs to IndexedDB",a)}},m=()=>{p||(p=setTimeout(async()=>{await S(),p=null},5e3))},h=K("dataUsageBaseline",{upload:0,download:0});let b=0,y=0;const f=new Map,E=(n,a)=>{const t=new Map;return a.forEach(e=>t.set(e.id,e)),n.map(e=>{const s=t.get(e.id);if(!s||!B(s.download)||!B(s.upload)){const w=e;return{...e,downloadSpeed:w.downloadSpeed??0,uploadSpeed:w.uploadSpeed??0}}return{...e,downloadSpeed:e.download-s.download,uploadSpeed:e.upload-s.upload}})},N=n=>{const a=new Set,t=[];for(const s of c.value)a.has(s.id)||(a.add(s.id),t.push(s));for(const s of n)a.has(s.id)||(a.add(s.id),t.push(s));const e=n.length+P;c.value=e>0&&t.length>e?t.slice(-e):t},_=(n,a)=>{const t=new Set(n.map(e=>e.id));return a.filter(e=>!t.has(e.id))},q=()=>{f.clear(),h.value={upload:0,download:0},console.log("[Data Usage] Connection tracking reset due to service restart")},x=async()=>{r.length=0,await O.clearAll(),f.clear()},R=async(n,a)=>{console.warn("[Data Usage] removeDataUsageEntry is not supported in the new log-based model")},A=n=>{const a=n?new Set(n.map(t=>t.id)):new Set(c.value.map(t=>t.id));f.forEach((t,e)=>{a.has(e)||f.delete(e)})},L=n=>{l.value=n;const a=n?.connections,t=n?.uploadTotal||0,e=n?.downloadTotal||0;if((t<b||e<y)&&(q(),x(),o.clearChartHistory()),b=t,y=e,!a||a.length===0)return;const s=E(a,d.value);if(k(s),A(s),N(s),!u.value){const w=_(s,c.value);d.value=s,i.value=w.slice(-P)}},k=n=>{const a=Date.now();let t=!1;n.forEach(e=>{const s=e.upload||0,w=e.download||0;let D=0,j=0;const I=f.get(e.id);I?(D=Math.max(0,s-I.upload),j=Math.max(0,w-I.download)):(D=s,j=w),f.set(e.id,{upload:s,download:w}),!(D===0&&j===0)&&(t=!0,r.push({timestamp:a,sourceIP:e.metadata.sourceIP||"Inner",host:e.metadata.host||e.metadata.destinationIP,process:e.metadata.process||"Unknown",outbound:(e.chains&&e.chains.length>0?e.chains[0]:"DIRECT")||"DIRECT",upload:D,download:j}))}),t&&m()},F=$(()=>{const n={};return d.value.forEach(a=>{a.chains.forEach(t=>{n[t]||(n[t]=0),n[t]+=a.downloadSpeed})}),n});return{allConnections:c,activeConnections:d,closedConnections:i,latestConnectionMsg:l,paused:u,speedGroupByName:F,updateFromWsMsg:L,clearDataUsage:x,removeDataUsageEntry:R,restructRawMsgToConnection:E}});export{ne as b,O as d,ae as i,V as r,pe as u};
