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