import{al as q,am as L,an as E,aj as R,x as h,ao as _,ap as $,aq as H,ar as b,ak as x,ai as W,as as O,at as P,au as y,av as Y,aw as K,ad as U,ae as Q,af as V,ag as J,ax as X,ay as N,az as Z,aA as G,aB as C,aC as S,aD as T,aE as v,aF as ee,K as p,aG as te,aH as ae,aI as se,aJ as ne,aK as ie}from"./index-Bp7bQ1dR.js";class oe{value;next;constructor(t){this.value=t}}class re{#e;#t;#a;constructor(){this.clear()}enqueue(t){const a=new oe(t);this.#e?(this.#t.next=a,this.#t=a):(this.#e=a,this.#t=a),this.#a++}dequeue(){const t=this.#e;if(t)return this.#e=this.#e.next,this.#a--,this.#e||(this.#t=void 0),t.value}peek(){if(this.#e)return this.#e.value}clear(){this.#e=void 0,this.#t=void 0,this.#a=0}get size(){return this.#a}*[Symbol.iterator](){let t=this.#e;for(;t;)yield t.value,t=t.next}*drain(){for(;this.#e;)yield this.dequeue()}}function j(e){let t=!1;if(typeof e=="object"&&({concurrency:e,rejectOnClear:t=!1}=e),A(e),typeof t!="boolean")throw new TypeError("Expected `rejectOnClear` to be a boolean");const a=new re;let s=0;const l=()=>{s<e&&a.size>0&&(s++,a.dequeue().run())},u=()=>{s--,l()},d=async(n,i,o)=>{const r=(async()=>n(...o))();i(r);try{await r}catch{}u()},c=(n,i,o,r)=>{const m={reject:o};new Promise(M=>{m.run=M,a.enqueue(m)}).then(d.bind(void 0,n,i,r)),s<e&&l()},f=(n,...i)=>new Promise((o,r)=>{c(n,o,r,i)});return Object.defineProperties(f,{activeCount:{get:()=>s},pendingCount:{get:()=>a.size},clearQueue:{value(){if(!t){a.clear();return}const n=AbortSignal.abort().reason;for(;a.size>0;)a.dequeue().reject(n)}},concurrency:{get:()=>e,set(n){A(n),e=n,queueMicrotask(()=>{for(;s<e&&a.size>0;)l()})}},map:{async value(n,i){const o=Array.from(n,(r,m)=>this(i,r,m));return Promise.all(o)}}}),f}function A(e){if(!((Number.isInteger(e)||e===Number.POSITIVE_INFINITY)&&e>0))throw new TypeError("Expected `concurrency` to be a number from 1 and up")}let D=0;const w=async()=>{const e=Date.now();D=e;const[t,a]=await Promise.all([$(),H()]),s=t.data,l=a.data;if(D!==e)return;const u=s.proxies[b]?.all??[],d={},c=Object.values(l.providers).filter(n=>n.name!=="default"&&n.vehicleType!=="Compatible");for(const n of c)for(const i of n.proxies)i["provider-name"]||=n.name,d[i.name]=i;h.value={...d,...s.proxies},R.value=Object.values(s.proxies).filter(n=>n.all?.length&&n.name!==b).sort((n,i)=>{const o=u.indexOf(n.name),r=u.indexOf(i.name);return o===-1&&r===-1?0:o===-1?1:r===-1?-1:o-r}).map(n=>n.name),x.value=c;const f=[];Object.entries(h.value).forEach(([n,i])=>{const o=W.value.find(r=>r.name===n);o&&(h.value[n].icon=o.icon),O.value&&de(i)&&(P.value[n]=!0),i.type.toLowerCase()===y.Smart&&f.push(n)}),f.length>0&&Y(f)},he=async(e,t)=>{const a=h.value[e];a.type.toLowerCase()!==y.LoadBalance&&(a.now===t&&(await w(),a.now===t)||(await K(e,t),h.value[e].now=t,U.value&&Q.value.filter(s=>V(s).includes(e)).forEach(s=>J(s.id)),w()))},le=e=>{const t=h.value[e]?.["provider-name"];return t?x.value.some(a=>a.name===t)?t:"":x.value.find(a=>a.proxies.some(s=>s.name===e))?.name??""},B=(e,t,a)=>{if(!se.value){const s=le(e);if(s)return ne(s,e,t,a)}return ie(e,t,a)},k=async(e,t,a)=>{const s=T(e);if(O.value)try{const{data:l}=await B(s,S,2e3);P.value[s]=l.delay>v}catch{P.value[s]=!1}return await B(E.value?e:s,t,a)},g=(e,t)=>E.value?`${e}
@${t}`:e,ve=async(e,t=q.value,a=L.value)=>{const s=await k(e,t,a);await w(),s.status!==200&&p({content:"testFailedTip",params:{name:g(e,t)},type:"alert-error"})},F=(e,t)=>{const a=ae(e),s=new Date;a.push({time:s.toISOString(),delay:t})},I="testLatencyOneByOneWithTip",ce=j(5),z=async(e,t,a=q.value)=>{const s=t.length;let l=0,u=0;await Promise.allSettled(t.map(d=>ce(async()=>{const c=await k(d,a,Math.min(2e3,L.value));c.status!==200?(u++,F(d,v)):F(d,c.data.delay),l++,p({content:"testFinishedTip",key:I+e,params:{name:g(e,a),total:s.toString(),number:l.toString()},type:"alert-info",timeout:0})}))),p({content:"testFinishedResultTip",key:I+e,params:{name:g(e,a),total:s.toString(),success:`${s-u}`,failed:`${u}`},type:u?"alert-warning":"alert-success",timeout:3e3})},ue=async e=>{const t=h.value[e],a=t.all??[],s=X(e);if(N.value===Z.DASHBOARD&&[y.Selector,y.LoadBalance,y.Smart].includes(t.type.toLowerCase()))return t.fixed&&G(e),z(e,a,s);const l=Math.max(5e3,L.value);if(O.value)try{const{data:c}=await C(e,S,l);a?.forEach(f=>{P.value[T(f)]=c[f]>v})}catch{a?.forEach(c=>{P.value[T(c)]=!1})}await C(e,s,l),await w();const u=a.length,d=a.filter(c=>ee(c,e)===v).length;p({content:"testFinishedResultTip",key:I+e,params:{name:g(e,s),total:u.toString(),success:`${u-d}`,failed:`${d}`},type:d?"alert-warning":"alert-success",timeout:3e3})},ye=async()=>{if(E.value){const t=j(3);return await Promise.all(R.value.map(a=>t(async()=>{await ue(a)})))}const e=Object.keys(h.value).filter(t=>!_(t));return z("all",e)},de=e=>{const t=e.extra?.[S]?.history;return(te.last(t)?.delay??v)>v};export{ye as allProxiesLatencyTest,w as fetchProxies,he as handlerProxySelect,ue as proxyGroupLatencyTest,ve as proxyLatencyTest};
