import{p as e}from"./bundle.CHKSNOJF.js";const w=3e4;function g({durationMs:o=w,onElapsed:u}={}){const[n,r]=e.useState("idle"),[f,a]=e.useState(0),c=e.useRef(null),t=e.useRef(!0),i=e.useRef(u);i.current=u;const s=e.useCallback(()=>{c.current&&(clearInterval(c.current),c.current=null)},[]);return e.useEffect(()=>(t.current=!0,()=>{t.current=!1,s()}),[s]),{send:e.useCallback(d=>{if(n!=="idle")return;r("sending");const R=new AbortController;return d().then(()=>{if(!t.current)return;const m=Date.now()+o;r("cooldown"),a(Math.ceil(o/1e3)),c.current=setInterval(()=>{if(!t.current)return;const l=Math.max(0,Math.ceil((m-Date.now())/1e3));a(l),l===0&&(s(),r("idle"),i.current?.())},1e3)}).catch(()=>{t.current&&r("idle")}),()=>{R.abort()}},[o,n,s]),isBusy:n!=="idle",secondsRemaining:n==="cooldown"?f:0}}export{g as u};
