import{t as e}from"./subsystem-kTFXkQM5.js";import{Ll as t,Rl as n}from"./auth-profiles-qXC1L8hs.js";import{r}from"./errors-DNQO70y-.js";import{RateLimitError as i}from"@buape/carbon";const a={attempts:3,minDelayMs:500,maxDelayMs:3e4,jitter:.1},o={attempts:3,minDelayMs:400,maxDelayMs:3e4,jitter:.1},s=/429|timeout|connect|reset|closed|unavailable|temporarily/i,c=e(`retry-policy`);function l(e){return e.shouldRetry?e.strictShouldRetry?e.shouldRetry:t=>e.shouldRetry?.(t)||s.test(r(t)):e=>s.test(r(e))}function u(e){if(!e||typeof e!=`object`)return;let t=`parameters`in e&&e.parameters&&typeof e.parameters==`object`?e.parameters.retry_after:`response`in e&&e.response&&typeof e.response==`object`&&`parameters`in e.response?e.response.parameters?.retry_after:`error`in e&&e.error&&typeof e.error==`object`&&`parameters`in e.error?e.error.parameters?.retry_after:void 0;return typeof t==`number`&&Number.isFinite(t)?t*1e3:void 0}function d(e){let r=t(a,{...e.configRetry,...e.retry});return(t,a)=>n(t,{...r,label:a,shouldRetry:e=>e instanceof i,retryAfterMs:e=>e instanceof i?e.retryAfter*1e3:void 0,onRetry:e.verbose?e=>{let t=e.label??`request`,n=Math.max(1,e.maxAttempts-1);c.warn(`discord ${t} rate limited, retry ${e.attempt}/${n} in ${e.delayMs}ms`)}:void 0})}function f(e){let i=t(o,{...e.configRetry,...e.retry}),a=l(e);return(t,o)=>n(t,{...i,label:o,shouldRetry:a,retryAfterMs:u,onRetry:e.verbose?e=>{let t=Math.max(1,e.maxAttempts-1);c.warn(`telegram send retry ${e.attempt}/${t} for ${e.label??o??`request`} in ${e.delayMs}ms: ${r(e.err)}`)}:void 0})}const p=new Map;function m(e,t){return`${e}:${t||`default`}`}function h(e,t){let n=m(e,t),r=p.get(n);if(r)return r;let i={inboundAt:null,outboundAt:null};return p.set(n,i),i}function g(e){let t=typeof e.at==`number`?e.at:Date.now(),n=e.accountId?.trim()||`default`,r=h(e.channel,n);e.direction===`inbound`&&(r.inboundAt=t),e.direction===`outbound`&&(r.outboundAt=t)}function _(e){let t=e.accountId?.trim()||`default`;return p.get(m(e.channel,t))??{inboundAt:null,outboundAt:null}}export{f as i,g as n,d as r,_ as t};
