import{s as e}from"./DrKWWDpL.js";import{t}from"./DYP3EJMY.js";import{r as n}from"./DOqzs8kl2.js";import{i as r,n as i,t as a}from"./ByMOJ7vP.js";var o=e(t(),1);function s(e){return e.replace(/[^a-zA-Z0-9_-]/g,`_`)}function c(e,t){return(0,o.join)((0,o.join)(n(),`rate-limit-state`),`${s(e)}-${s(t)}.json`)}function l(e){let t=t=>e instanceof Headers?e.get(t):e[t]??null,n=t(`retry-after`);if(n){let e=Number(n);if(Number.isFinite(e)&&e>0)return Date.now()+e*1e3;let t=Date.parse(n);if(Number.isFinite(t)&&t>Date.now())return t}let r=t(`x-ratelimit-reset-requests`);if(r){let e=Number(r);if(Number.isFinite(e)&&e>Date.now()/1e3)return e*1e3}let i=t(`x-ratelimit-reset`);if(i){let e=Number(i);if(Number.isFinite(e)&&e>Date.now()/1e3)return e*1e3;let t=Date.parse(i);if(Number.isFinite(t)&&t>Date.now())return t}return null}async function u(e,t,a){let s=Date.now(),u=(a?.defaultCooldownSeconds??300)*1e3,d,f;if(a?.headers){let e=l(a.headers);e!==null&&e>s?(d=e,f=`header`):(d=s+u,f=`default`)}else d=s+u,f=`default`;let p=c(e,t),m=(0,o.join)(n(),`rate-limit-state`);(0,o.existsSync)(m)||(0,o.mkdirSync)(m,{recursive:!0});let h={resetAt:d,recordedAt:s,source:f};await i(p,()=>{r(p,h)})}async function d(e,t){let n=a(c(e,t));if(!n)return null;let r=(n.resetAt-Date.now())/1e3;return r>0?r:null}export{u as n,d as t};