import{$n as e,Ir as t,Nn as n,Vn as r,Xr as i,o as a,pr as o,qr as s,t as c}from"./ChQ2IioC.js";import{t as l}from"./DswraVc3.js";var u=`metacubexd_db`,d=`data_usage_logs`,f=1;const p=new class{db=null;async open(){return this.db?this.db:typeof indexedDB>`u`?Promise.reject(Error(`IndexedDB is not available`)):new Promise((e,t)=>{let n=indexedDB.open(u,f);n.onupgradeneeded=e=>{let t=e.target.result;if(!t.objectStoreNames.contains(d)){let e=t.createObjectStore(d,{keyPath:`id`,autoIncrement:!0});e.createIndex(`timestamp`,`timestamp`,{unique:!1}),e.createIndex(`sourceIP`,`sourceIP`,{unique:!1}),e.createIndex(`host`,`host`,{unique:!1}),e.createIndex(`outbound`,`outbound`,{unique:!1}),e.createIndex(`process`,`process`,{unique:!1})}},n.onsuccess=t=>{this.db=t.target.result,e(this.db)},n.onerror=e=>{t(e.target.error)}})}async addLogs(e){let t=await this.open();return new Promise((n,r)=>{let i=t.transaction([d],`readwrite`),a=i.objectStore(d);e.forEach(e=>a.add(e)),i.oncomplete=()=>n(),i.onerror=e=>r(i.error)})}async query(e,t){let n=await this.open();return new Promise((r,i)=>{let a=n.transaction([d],`readonly`).objectStore(d).index(`timestamp`),o=IDBKeyRange.bound(e,t),s=a.openCursor(o),c=[];s.onsuccess=e=>{let t=e.target.result;t?(c.push({...t.value,inboundUser:t.value.inboundUser||`Unknown`}),t.continue()):r(c)},s.onerror=e=>i(s.error)})}async clearAll(){let e=await this.open();return new Promise((t,n)=>{let r=e.transaction([d],`readwrite`).objectStore(d).clear();r.onsuccess=()=>t(),r.onerror=e=>n(r.error)})}async cleanup(e){let t=await this.open();return new Promise((n,r)=>{let i=t.transaction([d],`readwrite`).objectStore(d),a=i.index(`timestamp`),o=IDBKeyRange.upperBound(e),s=a.openKeyCursor(o);s.onsuccess=e=>{let t=e.target.result;t?(i.delete(t.primaryKey),t.continue()):n()},s.onerror=e=>r(s.error)})}},m=e(`connections`,()=>{let e=l(),a=c(),u=i([]),d=i([]),f=i([]),m=i(null),h=s(!1),g=r(`traffic_data_retention`,-1),_=new Map,v=null,y=e=>`${e.timestamp}\x1F${e.sourceIP}\x1F${e.host}\x1F${e.outbound}\x1F${e.process}\x1F${e.inboundUser}`,b=async()=>{if(_.size===0)return;let e=Array.from(_.values());_.clear();try{await p.addLogs(e),g.value>0&&await p.cleanup(Date.now()-g.value)}catch(e){console.error(`[Data Usage] Failed to flush logs to IndexedDB`,e)}},x=()=>{if(v)return;let e=3e4-Date.now()%3e4||3e4;v=setTimeout(async()=>{await b(),v=null,_.size>0&&x()},e)},S=r(`dataUsageBaseline`,{upload:0,download:0}),C=0,w=0,T=new Map,E=!1,D=(e,t)=>{let n=new Map;return t.forEach(e=>n.set(e.id,e)),e.map(e=>{let t=n.get(e.id);return!t||typeof t.download!=`number`||typeof t.upload!=`number`?{...e,downloadSpeed:0,uploadSpeed:0}:{...e,downloadSpeed:e.download-t.download,uploadSpeed:e.upload-t.upload}})},O=e=>{let t=new Set,n=[];for(let r of e)t.has(r.id)||(t.add(r.id),n.push(r));for(let e of u.value)t.has(e.id)||(t.add(e.id),n.push(e));let r=e.length+200;u.value=r>0&&n.length>r?n.slice(-r):n},k=(e,t)=>t.filter(t=>!e.has(t.id)),A=()=>{T.clear(),S.value={upload:0,download:0},E=!1,console.log(`[Data Usage] Connection tracking reset due to service restart`)},j=async()=>{_.clear(),T.clear(),await p.clearAll()},M=async(e,t)=>{console.warn(`[Data Usage] removeDataUsageEntry is not supported in the new log-based model`)},N=e=>{T.forEach((t,n)=>{e.has(n)||T.delete(n)})},P=t=>{m.value=t;let n=t?.connections,r=t?.uploadTotal||0,i=t?.downloadTotal||0;if((r<C||i<w)&&(A(),j(),e.clearChartHistory()),C=r,w=i,!n||n.length===0)return;let o=D(n,d.value),s=new Set(o.map(e=>e.id));if(a.enableDataUsageTracking&&F(o),N(s),O(o),!h.value){let e=k(s,u.value);d.value=o,f.value=e.slice(-200)}},F=e=>{E||=!0;let t=Date.now(),n=Math.floor(t/6e4)*6e4,r=!1;e.forEach(e=>{let t=e.upload||0,i=e.download||0,a=0,o=0,s=T.get(e.id);if(s?(a=Math.max(0,t-s.upload),o=Math.max(0,i-s.download)):(a=t,o=i),T.set(e.id,{upload:t,download:i}),a===0&&o===0)return;r=!0;let c={timestamp:n,sourceIP:e.metadata.sourceIP||`Inner`,host:e.metadata.host||e.metadata.destinationIP,process:e.metadata.process||`Unknown`,outbound:e.chains[0]??`DIRECT`,inboundUser:e.metadata.inboundUser||e.metadata.inboundIP||e.metadata.inboundName||e.metadata.type||`Unknown`,upload:a,download:o},l=y(c),u=_.get(l);u?(u.upload+=a,u.download+=o):_.set(l,c)}),r&&x()},I=o(()=>{let e={};return d.value.forEach(t=>{t.chains.forEach(n=>{e[n]||(e[n]=0),e[n]+=t.downloadSpeed})}),e}),L=n();return t(()=>L.selectedEndpoint,()=>{C=0,w=0,T.clear()}),{allConnections:u,activeConnections:d,closedConnections:f,latestConnectionMsg:m,paused:h,speedGroupByName:I,updateFromWsMsg:P,clearDataUsage:j,removeDataUsageEntry:M,restructRawMsgToConnection:D}});export{p as n,m as t};