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