import{ct as e}from"./CcHu_7lQ.js";import{D as t,F as n}from"#entry";import{t as r}from"./CL4OkYTX.js";import{c as i,d as a,f as o,h as s,i as c,p as l,s as u,u as d}from"./D_PBZH73.js";import{t as f}from"./adkIU2uk.js";var p=n(`proxies`,()=>{let n=r(),p=f(),m=e([]),h=e([]),g=e({}),_=e({}),v=e({}),y=e({}),b=e({}),x=e({}),S=e(!1),C=t(`collapsedMap`,{}),w=(e,t=!0)=>{let r=e.extra||{},i=Object.keys(r).reduce((e,t)=>{let i=r[t],a=i?.history?.at(-1)?.delay??n.latencyQualityMap.NOT_CONNECTED;return e.allTestUrlLatency[t]=a,e.allTestUrlLatencyHistory[t]=i?.history,e},{allTestUrlLatency:{},allTestUrlLatencyHistory:{}});if(t){let t=e.testUrl||n.urlForLatencyTest;if(!(t in i.allTestUrlLatency)){let r=e.history?.at(-1)?.delay??n.latencyQualityMap.NOT_CONNECTED;i.allTestUrlLatency[t]=r,i.allTestUrlLatencyHistory[t]=e.history}}return i},T=e=>{let t={..._.value},r={...g.value};e.forEach(e=>{let{allTestUrlLatency:i,allTestUrlLatencyHistory:a}=w(e),{udp:o,xudp:s,type:c,now:l,name:u,tfo:d,provider:f=``}=e,p=`alive`in e?e.alive:void 0;t[e.name]={alive:p,udp:o,xudp:s,type:c,latency:l,latencyTestHistory:a,name:u,tfo:d,provider:f},(Object.values(i).some(e=>e!==n.latencyQualityMap.NOT_CONNECTED)||!r[e.name])&&(r[e.name]=i)}),_.value=t,g.value=r},E=async()=>{let[{providers:e},{proxies:t}]=await Promise.all([i(),u()]),n=Object.values(t).map(t=>{if(t.all?.length&&!t.testUrl){let{testUrl:n,timeout:r}=e?.[t.name]||{};return{...t,testUrl:n,timeout:r}}return t}),r=[...t.GLOBAL?.all??[],`GLOBAL`],a=Object.values(n).filter(e=>e.all?.length).sort((e,t)=>r.indexOf(e.name)-r.indexOf(t.name)),o=Object.values(e).filter(e=>e.name!==`default`&&e.vehicleType!==`Compatible`),s=[...n,...o.flatMap(e=>e.proxies.filter(e=>!(e.name in t)).map(t=>({...t,provider:e.name})))];m.value=a,h.value=o,T(s)},D=async(e,t)=>{if(await l(e.name,t),await E(),n.autoCloseConns){let t=p.restructRawMsgToConnection(p.latestConnectionMsg?.connections??[],[]);if(t.length>0){let n=t.filter(({chains:t})=>t.includes(e.name)).map(({id:e})=>c(e));await Promise.allSettled(n)}}},O=e=>{let t=_.value[e];if(!e||!t)return e;let n=new Set;for(;t&&t.latency&&t.latency!==t.name;){if(n.has(t.name))return t.name;n.add(t.name);let e=_.value[t.latency];if(!e)return t.name;t=e}return t?.name??e};return{proxies:m,proxyProviders:h,latencyMap:g,proxyNodeMap:_,proxyLatencyTestingMap:v,proxyGroupLatencyTestingMap:y,proxyProviderLatencyTestingMap:b,updatingMap:x,isAllProviderUpdating:S,collapsedMap:C,fetchProxies:E,selectProxyInGroup:D,getNowProxyNodeName:O,getLatencyByName:(e,t)=>{let r=t||n.urlForLatencyTest,i=g.value,a=O(e),o=i[a]?.[r],s=i[e]?.[r];if(o!=null)return o;if(s!=null)return s;let c=i[a];if(c&&Object.keys(c).length>0){let e=Object.keys(c),t=c[r]==null?e[0]:r;if(t)return c[t]}let l=i[e];if(l&&Object.keys(l).length>0){let e=Object.keys(l);if(e[0])return l[e[0]]}return n.latencyQualityMap.NOT_CONNECTED},getLatencyHistoryByName:(e,t)=>{let r=_.value[e],i=O(e),a=_.value[i],o=t||n.urlForLatencyTest,s=a?.latencyTestHistory[o]||r?.latencyTestHistory[o];if(s&&s.length)return s;let c=a?.latencyTestHistory||{},l=Object.keys(c)[0];if(l){let e=c[l];if(e&&e.length)return e}let u=r?.latencyTestHistory||{},d=Object.keys(u)[0];if(d){let e=u[d];if(e&&e.length)return e}return[]},isProxyGroup:e=>{let t=_.value[e];return t?[`direct`,`reject`,`loadbalance`].includes(t.type.toLowerCase())||!!t.latency:!1},proxyLatencyTest:async(e,t,r,i)=>{let o=O(e);v.value[o]=!0;try{let e=r||n.urlForLatencyTest,s=g.value?.[o]||{},{delay:c}=await a(o,t,e,i??n.latencyTestTimeoutDuration);s[e]=c,g.value={...g.value,[o]:s}}catch{let e=r||n.urlForLatencyTest,t=g.value?.[o]||{};t[e]=n.latencyQualityMap.NOT_CONNECTED,g.value={...g.value,[o]:t}}finally{v.value[o]=!1}},proxyGroupLatencyTest:async e=>{y.value[e]=!0;try{let t=m.value.find(t=>t.name===e);await d(e,t?.testUrl||n.urlForLatencyTest,t?.timeout??n.latencyTestTimeoutDuration),await E()}finally{y.value[e]=!1}},updateProviderByProviderName:async e=>{x.value[e]=!0;try{await s(e)}catch{}try{await E()}finally{x.value[e]=!1}},updateAllProvider:async()=>{S.value=!0;try{await Promise.allSettled(h.value.map(e=>s(e.name))),await E()}finally{S.value=!1}},proxyProviderLatencyTest:async e=>{b.value[e]=!0;try{await o(e),await E()}finally{b.value[e]=!1}}}});export{p as t};