import{d3 as x,aL as V,br as N,d4 as X,b4 as Z}from"./Co0rzPff.js";import{M as i}from"./1TRkdg3x.js";class c extends Error{constructor(n,r,y,o,R){let u=n,h="GET",l="";if(y instanceof x){const a=n.match(/\[([A-Z]+)\]\s+"([^"]+)":/);a&&(h=a[1],l=a[2].replace(/https?:\/\/[^/]+\/[^/]+\/v\d+\//,""));const s=(()=>{if(!r)return"Unknown Error";switch(r){case 400:return"Bad Request";case 401:return"Unauthorized";case 403:return"Forbidden";case 404:return"Not Found";case 408:return"Request Timeout";case 429:return"Too Many Requests";case 500:return"Internal Server Error";case 502:return"Bad Gateway";case 503:return"Service Unavailable";case 504:return"Gateway Timeout";default:return`HTTP ${r}`}})();u=`[${h}] ${s} (${r}) while fetching ${l}${o?` in ${o}`:""}`}else u=`${n}${r?` (${r})`:""}${o?` in ${o}`:""}`;super(u),this.statusCode=r,this.originalError=y,this.module=o,this.v1Error=R,this.name="ModrinthServersFetchError"}}async function ee(v,n={},r,y){var k,F,q,B,_,G,O;const o=Z(),u=(k=(await V()).value)==null?void 0:k.token;if(!u&&!n.bypassAuth){const e=new i("[Modrinth Servers] Cannot fetch without auth",1e4);throw new c("Missing auth token",401,e,r)}const{method:h="GET",contentType:l="application/json",body:a,version:s=0,override:d,retry:M=h==="GET"?3:0}=n,U=`${r||"default"}_${v}`,$=N(`fetch_failures_${U}`,()=>0),E=N(`last_failure_${U}`,()=>0),b=Date.now();if($.value>=3&&b-E.value<3e4){const e=new i("[Modrinth Servers] Circuit breaker open - too many recent failures",503);throw new c("Service temporarily unavailable",503,e,r)}b-E.value>3e4&&($.value=0);const g=(F=o.public.pyroBaseUrl)==null?void 0:F.replace(/\/$/,"");if(!g){const e=new i("[Modrinth Servers] Cannot fetch without base url. Make sure to set a PYRO_BASE_URL in environment variables",10001);throw new c("Configuration error: Missing PYRO_BASE_URL",500,e,r)}const H=`v${s}`;let f=d==null?void 0:d.url;f&&f.includes("v0")&&s!==0&&(f=f.replace("v0",H));const L=f?`https://${f}/${v.replace(/^\//,"")}`:s===0?`${g}/modrinth/v${s}/${v.replace(/^\//,"")}`:`${g}/v${s}/${v.replace(/^\//,"")}`,m={"User-Agent":"Modrinth/1.0 (https://test.kalataka.ru)","X-Archon-Request":"true",Vary:"Accept, Origin"};n.bypassAuth||(m.Authorization=`Bearer ${(d==null?void 0:d.token)??u}`,m["Access-Control-Allow-Headers"]="Authorization"),l!=="none"&&(m["Content-Type"]=l),typeof window<"u"&&(m.Origin=window.location.origin);let w=0;const S=(typeof M=="boolean"?M?3:1:M)+1;let p=null;for(;w=500&&($.value++,E.value=b);let P;(_=e.data)!=null&&_.error&&((G=e.data)!=null&&G.description)&&(P={context:y,...e.data});const C={400:"Bad Request",401:"Unauthorized",403:"Forbidden",404:"Not Found",405:"Method Not Allowed",408:"Request Timeout",429:"You're making requests too quickly. Please wait a moment and try again.",500:"Internal Server Error",502:"Bad Gateway",503:"Service Unavailable",504:"Gateway Timeout"},D=t&&t in C?C[t]:`HTTP Error: ${t||"unknown"} ${j}`,I=t?[408,429].includes(t):!1,J=t&&t>=500&&t<600&&h==="GET"&&w===1;if(!(I||J)||w>=S){console.error("Fetch error:",e);const A=new i(`[Modrinth Servers] ${e.message}`,t,e);throw new c(`[Modrinth Servers] ${D}`,t,A,r,P)}const K=t&&t>=500?5e3:1e3,z=Math.min(K*Math.pow(2,w-1)+Math.random()*1e3,15e3);console.warn(`Retrying request in ${z}ms (attempt ${w}/${S-1})`),await new Promise(A=>setTimeout(A,z));continue}console.error("Unexpected fetch error:",e);const T=new i("[Modrinth Servers] An unexpected error occurred during the fetch operation.",void 0,e);throw new c("Unexpected error during fetch operation",void 0,T,r)}if(console.error("All retry attempts failed:",p),p instanceof x){const e=(O=p.response)==null?void 0:O.status,T=new i("Maximum retry attempts reached",e,p);throw new c("Maximum retry attempts reached",e,T,r)}const Y=new i("Maximum retry attempts reached",void 0,p||void 0);throw new c("Maximum retry attempts reached",void 0,Y,r)}export{c as M,ee as u};