import{aj as v,s as d,ak as x,al as b,am as h,an as L,Z as P,ao as g,ap as S,z as R,aq as M,ar as D}from"./index-uJX-4cHl.js";const w=e=>({name:e.tag,type:e.type,now:"",history:e.urlTestDelay>0?[{time:new Date().toISOString(),delay:e.urlTestDelay}]:[],extra:{},udp:!0,icon:""});let n=new Map,c=new Map,u=[],p="",l=null;const i=new Set,U=()=>{for(const e of i)clearTimeout(e.timer),e.resolve();i.clear()},k=e=>{for(const t of i)clearTimeout(t.timer),t.reject(e);i.clear()},C=e=>{let t;return{promise:new Promise((s,a)=>{const r=setTimeout(()=>{i.delete(t),a(new Error("sing-box URL test result timeout"))},Math.max(5e3,e)+1e3);t={resolve:s,reject:a,timer:r},i.add(t)}),cancel:()=>{clearTimeout(t.timer),i.delete(t)}}},f=()=>{const e={};for(const t of c.values())e[t.tag]=w(t);for(const t of n.values())for(const o of t.items)e[o.tag]||(e[o.tag]=w(o));for(const t of n.values())e[t.tag]={name:t.tag,type:t.type,now:t.selected,all:t.items.map(o=>o.tag),history:[],extra:{},udp:!0,icon:""};for(const t of n.values())for(const o of t.items){const s=e[o.tag];s&&!s.all?.length&&o.urlTestDelay>0&&(s.history=[{time:new Date().toISOString(),delay:o.urlTestDelay}])}for(const t of S.value){const o=e[t.name];o&&(o.icon=t.icon)}R.value=e,M.value=Array.from(n.values()).filter(t=>t.items.length).map(t=>t.tag),D.value=[]},E=()=>{u.forEach(e=>e.close()),u=[],k(new Error("sing-box proxy stream closed")),p="",l=null},y=()=>{E(),n=new Map,c=new Map},T=()=>{const e=P.value,t=d()?.client;if(!e||e.type!=="singbox"||!t){y();return}if(p===e.uuid&&u.length)return;y(),p=e.uuid;let o,s=!1;l=new Promise(a=>o=a),u=[g("groups",a=>{n=new Map;for(const r of a.group)n.set(r.tag,r);f(),s?U():(s=!0,o())}),g("outbounds",a=>{c=new Map;for(const r of a.outbounds)c.set(r.tag,r);f()})]},A=()=>y(),O=async()=>{T(),l&&await l,f()},W=async(e,t)=>{const o=d()?.client;if(!o)return;await o.selectOutbound({groupTag:e,outboundTag:t});const s=n.get(e);s&&(s.selected=t,f()),x.value&&b.value.filter(a=>h(a).includes(e)).forEach(a=>L(a.id))},m=async(e,t=v.value)=>{T(),l&&await l;const o=d()?.client;if(!o)return;const s=C(t);try{await Promise.all([o.uRLTest({outboundTag:e}),s.promise])}finally{s.cancel()}},j=async(e,t,o=v.value)=>{await m(e,o)},B=async e=>{await m(e)},q=async()=>{await Promise.allSettled(Array.from(n.keys()).map(e=>m(e)))};export{q as allProxiesLatencyTest,O as fetchProxies,W as handlerProxySelect,B as proxyGroupLatencyTest,j as proxyLatencyTest,A as resetProxies};
