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