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