import{r as e,j as t}from"./vendor-react-Dp9pL_Wn.js";import{fL as r,H$ as n}from"./c4b350ac1-KLFM4jkb.js";const i={},c={attestation:i,getAttestation:()=>i,mintAttestation:()=>Promise.resolve(i),setHcaptchaToken:()=>{},reset:()=>{},registerWidgetReset:()=>{},registerTokenMinter:()=>{},setChallengeOpen:()=>{}},o=e.createContext(c);function a({children:r,mintTimeoutMs:n=3e3}){const[c,a]=e.useState(void 0),s=e.useRef(void 0),u=e.useRef(void 0),d=e.useRef(void 0),l=e.useRef(!1),h=e.useCallback(e=>{s.current=e,a(e)},[]),v=e.useCallback(()=>s.current?{hcaptcha_token:s.current}:i,[]),p=e.useCallback(e=>{u.current=e},[]),f=e.useCallback(e=>{d.current=e},[]),m=e.useCallback(e=>{l.current=e},[]),w=e.useCallback(async()=>{if(s.current)return{hcaptcha_token:s.current};const e=d.current;if(!e)return v();let t;try{const r=await Promise.race([e().catch(()=>{}),new Promise(e=>{t=setTimeout(()=>{l.current||e(void 0)},n)})]);return r?{hcaptcha_token:r}:v()}finally{clearTimeout(t)}},[v,n]),k=e.useCallback(()=>{s.current=void 0,a(void 0),u.current?.()},[]),b=e.useMemo(()=>({attestation:c?{hcaptcha_token:c}:i,getAttestation:v,mintAttestation:w,setHcaptchaToken:h,reset:k,registerWidgetReset:p,registerTokenMinter:f,setChallengeOpen:m}),[c,v,w,h,k,p,f,m]);return t.jsx(o.Provider,{value:b,children:r})}function s(){return e.useContext(o)}let u;function d(){return"undefined"!=typeof window&&window.hcaptcha?Promise.resolve(window.hcaptcha):(u??=(e="__antClientAttestationHcaptchaInvisibleReady",t="client-attestation-hcaptcha-invisible-script","undefined"==typeof window?Promise.reject(new Error("ssr")):window.hcaptcha?Promise.resolve(window.hcaptcha):new Promise((r,n)=>{let i=!1;const c=e=>{i||(i=!0,clearTimeout(o),e())},o=setTimeout(()=>{c(()=>{document.getElementById(t)?.remove(),n(new Error("script_load_timeout"))})},1e4);window[e]=()=>{c(()=>{window.hcaptcha?r(window.hcaptcha):n(new Error("hcaptcha_onload_without_api"))})};const a=document.createElement("script");a.id=t,a.src=`https://js.hcaptcha.com/1/api.js?render=explicit&recaptchacompat=off&onload=${e}`,a.async=!0,a.defer=!0,a.onerror=()=>{c(()=>{a.remove(),n(new Error("script_load_error"))})},document.head.appendChild(a)})).catch(e=>{throw u=void 0,e}),u);var e,t}function l({sitekeyStateOverride:i}={}){const{setHcaptchaToken:c,registerWidgetReset:o,registerTokenMinter:a,setChallengeOpen:u}=s(),l=e.useRef(null),h=e.useRef(void 0),v=e.useRef({}),p=e.useRef([]),f=e.useRef(void 0),m=e.useRef(void 0),w=r();let k;return n(),k=void 0!==i?"":w?"pending":"",e.useEffect(()=>(a(()=>{if(m.current)return m.current;const e=(()=>{const e=v.current;return e.ready?Promise.resolve(e.ready):e.off?Promise.resolve(void 0):new Promise(e=>{p.current.push(e)})})().then(e=>new Promise(t=>{if(e){f.current=t;try{e.api.execute(e.id)}catch{f.current=void 0,t(void 0)}}else t(void 0)}));return m.current=e.finally(()=>{m.current=void 0}),m.current}),()=>{a(void 0),f.current?.(void 0),f.current=void 0}),[]),e.useEffect(()=>{if("pending"===k)return;const e=e=>{v.current=e?{ready:e}:{off:!0},p.current.splice(0).forEach(t=>t(e))};if(""===k)return e(void 0),()=>{v.current={}};const t=k;let r,n=!1;const i=()=>{void 0!==r&&(clearTimeout(r),r=void 0)},a=e=>{i(),f.current?.(e),f.current=void 0};return d().then(s=>{if(n||!l.current)return void(n||e(void 0));let d=!1;const v=()=>{n||c(void 0)},p=s.render(l.current,{sitekey:t,size:"invisible",callback:e=>{n||(u(!1),c(e),a(e))},"expired-callback":()=>{v(),a(void 0)},"chalexpired-callback":()=>{u(!1),v(),a(void 0)},"error-callback":()=>{d=!0,u(!1),v(),a(void 0)},"open-callback":()=>{n||u(!0)},"close-callback":()=>{if(n)return;i();const e=f.current;r=setTimeout(()=>{r=void 0,n||f.current===e&&(u(!1),a(void 0))},2e3)}});void 0!==p?(h.current=p,n||d?n||e(void 0):(o(()=>{try{a(void 0),s.reset(p)}catch{}}),e({api:s,id:p}))):e(void 0)}).catch(()=>{n||e(void 0)}),()=>{n=!0,v.current={},a(void 0),u(!1),c(void 0),o(void 0);const e=h.current;if(void 0!==e){try{window.hcaptcha?.remove(e)}catch{}h.current=void 0}}},[k]),""===k||"pending"===k?null:t.jsx("div",{ref:l,"aria-hidden":!0,style:{position:"absolute",width:0,height:0,overflow:"hidden"},"data-client-attestation":"hcaptcha-invisible"})}function h(e){return e.hcaptcha_token?e:void 0}export{a as C,l as H,h as c,s as u};
