import{o as e}from"./rolldown-runtime-iuO8RvI4.js";import{Mi as t,ki as r}from"./vendor-frame-CwiylG4Y.js";var n=e(t());function a(e,t){return"undefined"==typeof window?Promise.reject(new Error("ssr")):window.hcaptcha?Promise.resolve(window.hcaptcha):new Promise((r,n)=>{let a=!1;const o=e=>{a||(a=!0,clearTimeout(s),e())},s=setTimeout(()=>{o(()=>{document.getElementById(t)?.remove(),n(new Error("script_load_timeout"))})},1e4);window[e]=()=>{o(()=>{window.hcaptcha?r(window.hcaptcha):n(new Error("hcaptcha_onload_without_api"))})};const c=document.createElement("script");c.id=t,c.src=`https://js.hcaptcha.com/1/api.js?render=explicit&recaptchacompat=off&onload=${e}`,c.async=!0,c.defer=!0,c.onerror=()=>{o(()=>{c.remove(),n(new Error("script_load_error"))})},document.head.appendChild(c)})}var o=r(),s=15e3,c={},i=(0,n.createContext)({attestation:c,getAttestation:()=>c,mintAttestation:()=>Promise.resolve(c),mintFreshAttestation:()=>Promise.resolve(c),mintFreshAttestationAlongside:async e=>[await e,c],setHcaptchaToken:()=>{},reset:()=>{},registerWidgetReset:()=>{},registerTokenMinter:()=>{},setChallengeOpen:()=>{}});function u({children:e,mintTimeoutMs:t=2e4}){const[r,a]=(0,n.useState)(void 0),s=(0,n.useRef)(void 0),u=(0,n.useRef)(void 0),l=(0,n.useRef)(void 0),h=(0,n.useRef)(!1),d=(0,n.useCallback)(e=>{s.current=e,a(e)},[]),m=(0,n.useCallback)(()=>s.current?{hcaptcha_token:s.current}:c,[]),p=(0,n.useCallback)(e=>{u.current=e},[]),w=(0,n.useCallback)(e=>{l.current=e},[]),k=(0,n.useCallback)(e=>{h.current=e},[]),f=(0,n.useCallback)(async()=>{if(s.current)return{hcaptcha_token:s.current};const e=l.current;if(!e)return m();const r=await async function(e,t,r){let n;try{return await Promise.race([e().catch(()=>{}),new Promise(e=>{n=setTimeout(()=>{t.current||e(void 0)},r)})])}finally{clearTimeout(n)}}(e,h,t);return r?(s.current=r,a(r),{hcaptcha_token:r}):m()},[m,t]),v=(0,n.useCallback)(()=>{s.current=void 0,a(void 0),u.current?.()},[]),C=(0,n.useCallback)(async()=>(v(),f()),[v,f]),g=(0,n.useCallback)(async e=>{const[t,r]=await Promise.all([e,C()]);let n=m();return r.hcaptcha_token&&!n.hcaptcha_token&&(n=await f()),[t,n]},[m,f,C]),_=(0,n.useMemo)(()=>({attestation:r?{hcaptcha_token:r}:c,getAttestation:m,mintAttestation:f,mintFreshAttestation:C,mintFreshAttestationAlongside:g,setHcaptchaToken:d,reset:v,registerWidgetReset:p,registerTokenMinter:w,setChallengeOpen:k}),[r,m,f,C,g,d,v,p,w,k]);return(0,o.jsx)(i.Provider,{value:_,children:e})}function l(){return(0,n.useContext)(i)}export{a as i,s as n,l as r,u as t};