import{Fn as e,Qr as t,Rr as n,Un as r,Yr as i,hr as a,o,t as s,tr as c}from"./KXnyWfWg.js";import{t as l}from"./4So2ULil.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=c(`connections`,()=>{let o=l(),c=s(),u=t([]),d=t([]),f=t([]),m=t(null),h=i(!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=e=>{m.value=e;let t=e?.connections,n=e?.uploadTotal||0,r=e?.downloadTotal||0;if((n<C||r<w)&&(A(),j(),o.clearChartHistory()),C=n,w=r,!t||t.length===0)return;let i=D(t,d.value),a=new Set(i.map(e=>e.id));if(c.enableDataUsageTracking&&F(i),N(a),O(i),!h.value){let e=k(a,u.value);d.value=i,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=a(()=>{let e={};return d.value.forEach(t=>{t.chains.forEach(n=>{e[n]||(e[n]=0),e[n]+=t.downloadSpeed})}),e}),L=e();return n(()=>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};