import{bZ as x,b3 as E,bg as q,bi as S,W,g3 as j}from"./index-DE_m8aQ1.js";function M(s,w={}){const{key:b="fallback",name:h="Fallback",rank:f=!1,shouldThrow:k=v,retryCount:m,retryDelay:T}=w;return({chain:i,pollingInterval:_=4e3,timeout:o,...g})=>{let c=s,d=()=>{};const C=x({key:b,name:h,async request({method:t,params:e}){let l;const p=async(a=0)=>{const u=c[a]({...g,chain:i,retryCount:0,timeout:o});try{const n=await u.request({method:t,params:e});return d({method:t,params:e,response:n,transport:u,status:"success"}),n}catch(n){if(d({error:n,method:t,params:e,transport:u,status:"error"}),k(n)||a===c.length-1||(l??(l=c.slice(a+1).some(r=>{const{include:y,exclude:R}=r({chain:i}).config.methods||{};return y?y.includes(t):R?!R.includes(t):!0})),!l))throw n;return p(a+1)}};return p()},retryCount:m,retryDelay:T,type:"fallback"},{onResponse:t=>d=t,transports:c.map(t=>t({chain:i,retryCount:0}))});if(f){const t=typeof f=="object"?f:{};D({chain:i,interval:t.interval??_,onTransports:e=>c=e,ping:t.ping,sampleCount:t.sampleCount,timeout:t.timeout,transports:c,weights:t.weights})}return C}}function v(s){return!!("code"in s&&typeof s.code=="number"&&(s.code===E.code||s.code===q.code||s.code===S.code||W.nodeMessage.test(s.message)||s.code===5e3))}function D({chain:s,interval:w=4e3,onTransports:b,ping:h,sampleCount:f=10,timeout:k=1e3,transports:m,weights:T={}}){const{stability:i=.7,latency:_=.3}=T,o=[],g=async()=>{const c=await Promise.all(m.map(async t=>{const e=t({chain:s,retryCount:0,timeout:k}),l=Date.now();let p,a;try{await(h?h({transport:e}):e.request({method:"net_listening"})),a=1}catch{a=0}finally{p=Date.now()}return{latency:p-l,success:a}}));o.push(c),o.length>f&&o.shift();const d=Math.max(...o.map(t=>Math.max(...t.map(({latency:e})=>e)))),C=m.map((t,e)=>{const l=o.map(r=>r[e].latency),a=1-l.reduce((r,y)=>r+y,0)/l.length/d,u=o.map(r=>r[e].success),n=u.reduce((r,y)=>r+y,0)/u.length;return n===0?[0,e]:[_*a+i*n,e]}).sort((t,e)=>e[0]-t[0]);b(C.map(([,t])=>m[t])),await j(w),g()};g()}export{M as f,v as s};
