@hcaptcha/react-hcaptcha
Version:
A React library for hCaptcha
5 lines (4 loc) • 20.2 kB
JavaScript
var ae=Object.defineProperty;var B=Object.getOwnPropertySymbols;var oe=Object.prototype.hasOwnProperty,ce=Object.prototype.propertyIsEnumerable;var D=(t,e,n)=>e in t?ae(t,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[e]=n,$=(t,e)=>{for(var n in e||(e={}))oe.call(e,n)&&D(t,n,e[n]);if(B)for(var n of B(e))ce.call(e,n)&&D(t,n,e[n]);return t};var F=(t,e,n)=>new Promise((r,i)=>{var a=s=>{try{h(n.next(s))}catch(c){i(c)}},o=s=>{try{h(n.throw(s))}catch(c){i(c)}},h=s=>s.done?r(s.value):Promise.resolve(s.value).then(a,o);h((n=n.apply(t,e)).next())});import{useContext as le}from"react";import{createContext as he}from"react";var I=he();var N=()=>le(I);import re,{useEffect as Fe,useRef as Ne,useState as U}from"react";import*as R from"react";var ue=Object.defineProperty,de=Object.defineProperties,pe=Object.getOwnPropertyDescriptors,A=Object.getOwnPropertySymbols,J=Object.prototype.hasOwnProperty,K=Object.prototype.propertyIsEnumerable,q=(t,e,n)=>e in t?ue(t,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[e]=n,g=(t,e)=>{for(var n in e||(e={}))J.call(e,n)&&q(t,n,e[n]);if(A)for(var n of A(e))K.call(e,n)&&q(t,n,e[n]);return t},G=(t,e)=>de(t,pe(e)),fe=(t,e)=>{var n={};for(var r in t)J.call(t,r)&&e.indexOf(r)<0&&(n[r]=t[r]);if(t!=null&&A)for(var r of A(t))e.indexOf(r)<0&&K.call(t,r)&&(n[r]=t[r]);return n},l=(t,e,n)=>(q(t,typeof e!="symbol"?e+"":e,n),n),C=(t,e,n)=>new Promise((r,i)=>{var a=s=>{try{h(n.next(s))}catch(c){i(c)}},o=s=>{try{h(n.throw(s))}catch(c){i(c)}},h=s=>s.done?r(s.value):Promise.resolve(s.value).then(a,o);h((n=n.apply(t,e)).next())}),me="hCaptcha-script",j="hCaptchaOnLoad",W="script-error",b="@hCaptcha/loader";function ge(t){return Object.entries(t).filter(([,e])=>e||e===!1).map(([e,n])=>`${encodeURIComponent(e)}=${encodeURIComponent(String(n))}`).join("&")}function X(t){let e=t&&t.ownerDocument||document,n=e.defaultView||e.parentWindow||window;return{document:e,window:n}}function Q(t){return t||document.head}function ye(t){var e;t.setTag("source",b),t.setTag("url",document.URL),t.setContext("os",{UA:navigator.userAgent}),t.setContext("browser",g({},ve())),t.setContext("device",G(g({},be()),{screen_width_pixels:screen.width,screen_height_pixels:screen.height,language:navigator.language,orientation:((e=screen.orientation)==null?void 0:e.type)||"Unknown",processor_count:navigator.hardwareConcurrency,platform:navigator.platform}))}function ve(){var t,e,n,r,i,a;let o=navigator.userAgent,h,s;return o.indexOf("Firefox")!==-1?(h="Firefox",s=(t=o.match(/Firefox\/([\d.]+)/))==null?void 0:t[1]):o.indexOf("Edg")!==-1?(h="Microsoft Edge",s=(e=o.match(/Edg\/([\d.]+)/))==null?void 0:e[1]):o.indexOf("Chrome")!==-1&&o.indexOf("Safari")!==-1?(h="Chrome",s=(n=o.match(/Chrome\/([\d.]+)/))==null?void 0:n[1]):o.indexOf("Safari")!==-1&&o.indexOf("Chrome")===-1?(h="Safari",s=(r=o.match(/Version\/([\d.]+)/))==null?void 0:r[1]):o.indexOf("Opera")!==-1||o.indexOf("OPR")!==-1?(h="Opera",s=(i=o.match(/(Opera|OPR)\/([\d.]+)/))==null?void 0:i[2]):o.indexOf("MSIE")!==-1||o.indexOf("Trident")!==-1?(h="Internet Explorer",s=(a=o.match(/(MSIE |rv:)([\d.]+)/))==null?void 0:a[2]):(h="Unknown",s="Unknown"),{name:h,version:s}}function xe(t){return new Promise(e=>setTimeout(e,t))}function be(){let t=navigator.userAgent,e;t.indexOf("Win")!==-1?e="Windows":t.indexOf("Mac")!==-1?e="Mac":t.indexOf("Linux")!==-1?e="Linux":t.indexOf("Android")!==-1?e="Android":t.indexOf("like Mac")!==-1||t.indexOf("iPhone")!==-1||t.indexOf("iPad")!==-1?e="iOS":e="Unknown";let n;return/Mobile|iPhone|iPod|Android/i.test(t)?n="Mobile":/Tablet|iPad/i.test(t)?n="Tablet":n="Desktop",{model:e,family:e,device:n}}var we=class Y{constructor(e){l(this,"_parent"),l(this,"breadcrumbs",[]),l(this,"context",{}),l(this,"extra",{}),l(this,"tags",{}),l(this,"request"),l(this,"user"),this._parent=e}get parent(){return this._parent}child(){return new Y(this)}setRequest(e){return this.request=e,this}removeRequest(){return this.request=void 0,this}addBreadcrumb(e){return typeof e.timestamp>"u"&&(e.timestamp=new Date().toISOString()),this.breadcrumbs.push(e),this}setExtra(e,n){return this.extra[e]=n,this}removeExtra(e){return delete this.extra[e],this}setContext(e,n){return typeof n.type>"u"&&(n.type=e),this.context[e]=n,this}removeContext(e){return delete this.context[e],this}setTags(e){return this.tags=g(g({},this.tags),e),this}setTag(e,n){return this.tags[e]=n,this}removeTag(e){return delete this.tags[e],this}setUser(e){return this.user=e,this}removeUser(){return this.user=void 0,this}toBody(){let e=[],n=this;for(;n;)e.push(n),n=n.parent;return e.reverse().reduce((r,i)=>{var a;return r.breadcrumbs=[...(a=r.breadcrumbs)!=null?a:[],...i.breadcrumbs],r.extra=g(g({},r.extra),i.extra),r.contexts=g(g({},r.contexts),i.context),r.tags=g(g({},r.tags),i.tags),i.user&&(r.user=i.user),i.request&&(r.request=i.request),r},{breadcrumbs:[],extra:{},contexts:{},tags:{},request:void 0,user:void 0})}clear(){this.breadcrumbs=[],this.context={},this.tags={},this.user=void 0}},Ee=/^\s*at (?:(.*?) ?\()?((?:file|https?|blob|chrome-extension|address|native|eval|webpack|<anonymous>|[-a-z]+:|.*bundle|\/).*?)(?::(\d+))?(?::(\d+))?\)?\s*$/i,Ce=/^\s*(.*?)(?:\((.*?)\))?(?:^|@)?((?:file|https?|blob|chrome|webpack|resource|moz-extension).*?:\/.*?|\[native code\]|[^@]*(?:bundle|\d+\.js))(?::(\d+))?(?::(\d+))?\s*$/i,Re=/^\s*at (?:((?:\[object object\])?.+) )?\(?((?:file|ms-appx|https?|webpack|blob):.*?):(\d+)(?::(\d+))?\)?\s*$/i,_e=/^(?:(\w+):)\/\/(?:(\w+)(?::(\w+))?@)([\w.-]+)(?::(\d+))?\/(.+)/,S="?",z="An unknown error occurred",Oe="0.0.4";function Pe(t){for(let e=0;e<t.length;e++)t[e]=Math.floor(Math.random()*256);return t}function p(t){return(t+256).toString(16).substring(1)}function Ie(){let t=Pe(new Array(16));return t[6]=t[6]&15|64,t[8]=t[8]&63|128,p(t[0])+p(t[1])+p(t[2])+p(t[3])+"-"+p(t[4])+p(t[5])+"-"+p(t[6])+p(t[7])+"-"+p(t[8])+p(t[9])+"-"+p(t[10])+p(t[11])+p(t[12])+p(t[13])+p(t[14])+p(t[15])}var Se=[[Ee,"chrome"],[Re,"winjs"],[Ce,"gecko"]];function ke(t){var e,n,r,i;if(!t.stack)return null;let a=[],o=(r=(n=(e=t.stack).split)==null?void 0:n.call(e,`
`))!=null?r:[];for(let h=0;h<o.length;++h){let s=null,c=null,u=null;for(let[y,v]of Se)if(c=y.exec(o[h]),c){u=v;break}if(!(!c||!u)){if(u==="chrome")s={filename:(i=c[2])!=null&&i.startsWith("address at ")?c[2].substring(11):c[2],function:c[1]||S,lineno:c[3]?+c[3]:null,colno:c[4]?+c[4]:null};else if(u==="winjs")s={filename:c[2],function:c[1]||S,lineno:+c[3],colno:c[4]?+c[4]:null};else if(u==="gecko")h===0&&!c[5]&&t.columnNumber!==void 0&&a.length>0&&(a[0].column=t.columnNumber+1),s={filename:c[3],function:c[1]||S,lineno:c[4]?+c[4]:null,colno:c[5]?+c[5]:null};else continue;!s.function&&s.lineno&&(s.function=S),a.push(s)}}return a.length?a.reverse():null}function je(t){let e=ke(t);return{type:t.name,value:t.message,stacktrace:{frames:e!=null?e:[]}}}function Ae(t){let e=_e.exec(t),n=e?e.slice(1):[];if(n.length!==6)throw new Error("Invalid DSN");let r=n[5].split("/"),i=r.slice(0,-1).join("/");return n[0]+"://"+n[3]+(n[4]?":"+n[4]:"")+(i?"/"+i:"")+"/api/"+r.pop()+"/envelope/?sentry_version=7&sentry_key="+n[1]+(n[2]?"&sentry_secret="+n[2]:"")}function qe(t,e,n){var r,i;let a=g({event_id:Ie().replaceAll("-",""),platform:"javascript",sdk:{name:"@hcaptcha/sentry",version:Oe},environment:e,release:n,timestamp:Date.now()/1e3},t.scope.toBody());if(t.type==="exception"){a.message=(i=(r=t.error)==null?void 0:r.message)!=null?i:"Unknown error",a.fingerprint=[a.message];let o=[],h=t.error;for(let s=0;s<5&&h&&(o.push(je(h)),!(!h.cause||!(h.cause instanceof Error)));s++)h=h.cause;a.exception={values:o.reverse()}}return t.type==="message"&&(a.message=t.message,a.level=t.level),a}function Le(t){if(t instanceof Error)return t;if(typeof t=="string")return new Error(t);if(typeof t=="object"&&t!==null&&!Array.isArray(t)){let n=t,{message:r}=n,i=fe(n,["message"]),a=new Error(typeof r=="string"?r:z);return Object.assign(a,i)}let e=new Error(z);return Object.assign(e,{cause:t})}function Me(t,e,n){return C(this,null,function*(){var r,i;try{if(typeof fetch<"u"&&typeof AbortSignal<"u"){let a;if(n){let s=new AbortController;a=s.signal,setTimeout(()=>s.abort(),n)}let o=yield fetch(t,G(g({},e),{signal:a})),h=yield o.text();return{status:o.status,body:h}}return yield new Promise((a,o)=>{var h,s;let c=new XMLHttpRequest;if(c.open((h=e==null?void 0:e.method)!=null?h:"GET",t),c.onload=()=>a({status:c.status,body:c.responseText}),c.onerror=()=>o(new Error("XHR Network Error")),e==null?void 0:e.headers)for(let[u,y]of Object.entries(e.headers))c.setRequestHeader(u,y);if(n){let u=setTimeout(()=>{c.abort(),o(new Error("Request timed out"))},n);c.onloadend=()=>{clearTimeout(u)}}c.send((s=e==null?void 0:e.body)==null?void 0:s.toString())})}catch(a){return{status:0,body:(i=(r=a==null?void 0:a.toString)==null?void 0:r.call(a))!=null?i:"Unknown error"}}})}var m,L=(m=class{constructor(t){l(this,"apiURL"),l(this,"dsn"),l(this,"environment"),l(this,"release"),l(this,"sampleRate"),l(this,"debug"),l(this,"_scope"),l(this,"shouldBuffer",!1),l(this,"bufferlimit",20),l(this,"buffer",[]);var e,n,r,i,a;this.environment=t.environment,this.release=t.release,this.sampleRate=(e=t.sampleRate)!=null?e:1,this.debug=(n=t.debug)!=null?n:!1,this._scope=(r=t.scope)!=null?r:new we,this.apiURL=Ae(t.dsn),this.dsn=t.dsn,this.shouldBuffer=(i=t.buffer)!=null?i:!1,this.bufferlimit=(a=t.bufferLimit)!=null?a:20}static init(t){m._instance||(m._instance=new m(t))}static get instance(){if(!m._instance)throw new Error("Sentry has not been initialized");return m._instance}log(...t){this.debug&&console.log(...t)}get scope(){return this._scope}static get scope(){return m.instance.scope}withScope(t){let e=this._scope.child();t(e)}static withScope(t){m.instance.withScope(t)}captureException(t,e){this.captureEvent({type:"exception",level:"error",error:Le(t),scope:e!=null?e:this._scope})}static captureException(t,e){m.instance.captureException(t,e)}captureMessage(t,e="info",n){this.captureEvent({type:"message",level:e,message:t,scope:n!=null?n:this._scope})}static captureMessage(t,e="info",n){m.instance.captureMessage(t,e,n)}captureEvent(t){if(Math.random()>=this.sampleRate){this.log("Dropped event due to sample rate");return}if(this.shouldBuffer){if(this.buffer.length>=this.bufferlimit)return;this.buffer.push(t)}else this.sendEvent(t)}sendEvent(t,e=5e3){return C(this,null,function*(){try{this.log("Sending sentry event",t);let n=qe(t,this.environment,this.release),r={event_id:n.event_id,dsn:this.dsn},i={type:"event"},a=JSON.stringify(r)+`
`+JSON.stringify(i)+`
`+JSON.stringify(n),o=yield Me(this.apiURL,{method:"POST",headers:{"Content-Type":"application/x-sentry-envelope"},body:a},e);this.log("Sentry response",o.status),o.status!==200&&(console.log(o.body),console.error("Failed to send event to Sentry",o))}catch(n){console.error("Failed to send event",n)}})}flush(t=5e3){return C(this,null,function*(){try{this.log("Flushing sentry events",this.buffer.length);let e=this.buffer.splice(0,this.buffer.length).map(n=>this.sendEvent(n,t));yield Promise.all(e),this.log("Flushed all events")}catch(e){console.error("Failed to flush events",e)}})}static flush(t=5e3){return m.instance.flush(t)}static reset(){m._instance=void 0}},l(m,"_instance"),m),Te="https://d233059272824702afc8c43834c4912d@sentry.hcaptcha.com/6",Ue="2.3.0",He="production";function Be(t=!0){if(!t)return V();L.init({dsn:Te,release:Ue,environment:He});let e=L.scope;return ye(e),V(e)}function V(t=null){return{addBreadcrumb:e=>{t&&t.addBreadcrumb(e)},captureRequest:e=>{t&&t.setRequest(e)},captureException:e=>{t&&L.captureException(e,t)}}}function De({scriptLocation:t,query:e,loadAsync:n=!0,crossOrigin:r="anonymous",apihost:i="https://js.hcaptcha.com",cleanup:a=!1,secureApi:o=!1,scriptSource:h=""}={},s){let c=Q(t),u=X(c);return new Promise((y,v)=>{let d=u.document.createElement("script");d.id=me,h?d.src=`${h}?onload=${j}`:o?d.src=`${i}/1/secure-api.js?onload=${j}`:d.src=`${i}/1/api.js?onload=${j}`,d.crossOrigin=r,d.async=n;let w=(x,E)=>{try{!o&&a&&c.removeChild(d),E(x)}catch(P){v(P)}};d.onload=x=>w(x,y),d.onerror=x=>{s&&s(d.src),w(x,v)},d.src+=e!==""?`&${e}`:"",c.appendChild(d)})}var k=[];function $e(t={cleanup:!1},e){try{e.addBreadcrumb({category:b,message:"hCaptcha loader params",data:t});let n=Q(t.scriptLocation),r=X(n),i=k.find(({scope:o})=>o===r.window);if(i)return e.addBreadcrumb({category:b,message:"hCaptcha already loaded"}),i.promise;let a=new Promise((o,h)=>C(this,null,function*(){try{r.window[j]=()=>{e.addBreadcrumb({category:b,message:"hCaptcha script called onload function"}),o(r.window.hcaptcha)};let s=ge({custom:t.custom,render:t.render,sentry:t.sentry,assethost:t.assethost,imghost:t.imghost,reportapi:t.reportapi,endpoint:t.endpoint,host:t.host,recaptchacompat:t.recaptchacompat,hl:t.hl,uj:t.uj});yield De(g({query:s},t),c=>{e.captureRequest({url:c,method:"GET"})}),e.addBreadcrumb({category:b,message:"hCaptcha loaded",data:i})}catch(s){e.addBreadcrumb({category:b,message:"hCaptcha failed to load"});let c=k.findIndex(u=>u.scope===r.window);c!==-1&&k.splice(c,1),h(new Error(W))}}));return k.push({promise:a,scope:r.window}),a}catch(n){return e.captureException(n),Promise.reject(new Error(W))}}function Z(t,e,n=0){return C(this,null,function*(){var r,i;let a=(r=t.maxRetries)!=null?r:2,o=(i=t.retryDelay)!=null?i:1e3,h=n<a?"Retry loading hCaptcha Api":"Exceeded maximum retries";try{return yield $e(t,e)}catch(s){return e.addBreadcrumb({category:b,message:h}),n>=a?(e.captureException(s),Promise.reject(s)):(e.addBreadcrumb({category:b,message:`Waiting ${o}ms before retry attempt ${n+1}`}),yield xe(o),n+=1,Z(t,e,n))}})}function ee(){return C(this,arguments,function*(t={}){let e=Be(t.sentry);return yield Z(t,e)})}function M(t){let e=t&&t.ownerDocument||document,n=e.defaultView||e.parentWindow||window;return{document:e,window:n}}function T(t){return t||document.head}var O=class extends R.Component{constructor(e){super(e),this._hcaptcha=void 0,this.renderCaptcha=this.renderCaptcha.bind(this),this.resetCaptcha=this.resetCaptcha.bind(this),this.removeCaptcha=this.removeCaptcha.bind(this),this.isReady=this.isReady.bind(this),this._onReady=null,this.loadCaptcha=this.loadCaptcha.bind(this),this.handleOnLoad=this.handleOnLoad.bind(this),this.handleSubmit=this.handleSubmit.bind(this),this.handleExpire=this.handleExpire.bind(this),this.handleError=this.handleError.bind(this),this.handleOpen=this.handleOpen.bind(this),this.handleClose=this.handleClose.bind(this),this.handleChallengeExpired=this.handleChallengeExpired.bind(this),this.ref=R.createRef(),this.apiScriptRequested=!1,this.sentryHub=null,this.captchaId="",this._pendingExecute=null,this.state={isApiReady:!1,isRemoved:!1,elementId:e.id}}componentDidMount(){let e=T(this.props.scriptLocation),n=M(e);if(this._hcaptcha=n.window.hcaptcha||void 0,typeof this._hcaptcha!="undefined"){this.setState({isApiReady:!0},()=>{this.renderCaptcha()});return}this.loadCaptcha()}componentWillUnmount(){let e=this._hcaptcha,n=this.captchaId;if(this._cancelPendingExecute("react-component-unmounted"),!!this.isReady())try{e.reset(n),e.remove(n)}catch(r){}finally{this.captchaId="",this._onReady=null}}shouldComponentUpdate(e,n){return!(this.state.isApiReady!==n.isApiReady||this.state.isRemoved!==n.isRemoved)}componentDidUpdate(e){["sitekey","size","theme","tabindex","languageOverride","endpoint"].every(i=>e[i]===this.props[i])||this.removeCaptcha(()=>{this.renderCaptcha()})}loadCaptcha(){if(this.apiScriptRequested)return;let{apihost:e,assethost:n,endpoint:r,host:i,imghost:a,languageOverride:o,reCaptchaCompat:h,reportapi:s,sentry:c,custom:u,loadAsync:y,scriptLocation:v,scriptSource:d,secureApi:w,cleanup:x=!0,userJourneys:E}=this.props;ee({render:"explicit",apihost:e,assethost:n,endpoint:r,hl:o,host:i,imghost:a,recaptchacompat:h===!1?"off":null,reportapi:s,sentry:c,custom:u,loadAsync:y,scriptLocation:v,scriptSource:d,secureApi:w,cleanup:x,uj:E!==void 0?E:!1}).then(this.handleOnLoad,this.handleError).catch(this.handleError),this.apiScriptRequested=!0}renderCaptcha(e){let{onReady:n}=this.props,{isApiReady:r}=this.state,i=this.captchaId;if(!r||i)return;let a=Object.assign({"open-callback":this.handleOpen,"close-callback":this.handleClose,"error-callback":this.handleError,"chalexpired-callback":this.handleChallengeExpired,"expired-callback":this.handleExpire,callback:this.handleSubmit},this.props,{hl:this.props.hl||this.props.languageOverride,languageOverride:void 0}),h=this._hcaptcha.render(this.ref.current,a);this.captchaId=h,this.setState({isRemoved:!1},()=>{e&&e(),n&&n(),this._onReady&&this._onReady(h)})}resetCaptcha(){let e=this._hcaptcha,n=this.captchaId;this.isReady()&&(e.reset(n),this._cancelPendingExecute("hcaptcha-reset"))}removeCaptcha(e){let n=this._hcaptcha,r=this.captchaId;this._cancelPendingExecute("hcaptcha-removed"),this.isReady()&&this.setState({isRemoved:!0},()=>{this.captchaId="",n.remove(r),e&&e()})}handleOnLoad(){this.setState({isApiReady:!0},()=>{let e=T(this.props.scriptLocation),n=M(e);this._hcaptcha=n.window.hcaptcha,this.renderCaptcha(()=>{let{onLoad:r}=this.props;r&&r()})})}handleSubmit(e){let{onVerify:n}=this.props,{isRemoved:r}=this.state,i=this._hcaptcha,a=this.captchaId;if(typeof i=="undefined"||r)return;let o=i.getResponse(a),h=i.getRespKey(a);n&&n(o,h)}handleExpire(){let{onExpire:e}=this.props,n=this._hcaptcha,r=this.captchaId;this.isReady()&&(n.reset(r),e&&e())}handleError(e){let{onError:n}=this.props,r=this._hcaptcha,i=this.captchaId;this.isReady()&&r.reset(i),n&&n(e)}isReady(){let{isApiReady:e,isRemoved:n}=this.state;return e&&!n}_cancelPendingExecute(e){if(!this._pendingExecute)return;let n=this._pendingExecute;this._pendingExecute=null;let r=new Error(e);n.reject(r)}handleOpen(){!this.isReady()||!this.props.onOpen||this.props.onOpen()}handleClose(){!this.isReady()||!this.props.onClose||this.props.onClose()}handleChallengeExpired(){!this.isReady()||!this.props.onChalExpired||this.props.onChalExpired()}execute(e=null){e=typeof e=="object"?e:null;try{let n=this._hcaptcha,r=this.captchaId;if(e&&e.async&&this._pendingExecute&&this._cancelPendingExecute("hcaptcha-execute-replaced"),!this.isReady())return e&&e.async?new Promise((a,o)=>{this._pendingExecute={resolve:a,reject:o},this._onReady=h=>{if(this._pendingExecute)try{let s=n.execute(h,e);s&&typeof s.then=="function"?s.then(c=>{this._pendingExecute=null,a(c)}).catch(c=>{this._pendingExecute=null,o(c)}):(this._pendingExecute=null,o(new Error("hcaptcha-execute-no-promise")))}catch(s){this._pendingExecute=null,o(s)}}}):(this._onReady=a=>{n.execute(a,e)},null);let i=n.execute(r,e);return e&&e.async&&i&&typeof i.then=="function"?new Promise((a,o)=>{this._pendingExecute={resolve:a,reject:o},i.then(h=>{this._pendingExecute=null,a(h)}).catch(h=>{this._pendingExecute=null,o(h)})}):i}catch(n){return e&&e.async?Promise.reject(n):null}}close(){let e=this._hcaptcha,n=this.captchaId;if(this._cancelPendingExecute("hcaptcha-closed"),!!this.isReady())return e.close(n)}setData(e){let n=this._hcaptcha,r=this.captchaId;this.isReady()&&(e&&typeof e!="object"&&(e=null),n.setData(r,e))}getResponse(){return this._hcaptcha.getResponse(this.captchaId)}getRespKey(){return this._hcaptcha.getRespKey(this.captchaId)}render(){let{elementId:e}=this.state;return R.createElement("div",{ref:this.ref,id:e})}};var te=O;function ne({sitekey:t=null,size:e="normal",theme:n="light",rqdata:r=null,languageOverride:i=null,onVerify:a,onError:o,children:h}){let s=Ne(null),[c,u]=U(!1),[y,v]=U(null),[d,w]=U(null),x=()=>{u(!0)},E=f=>{w(f),o&&o(f)},P=()=>{v(null)},se=f=>{v(f),a&&a(f)},ie=(...mt)=>F(this,[...mt],function*(f={}){try{if(!c)throw new Error("hCaptcha not ready");y&&H();let{response:_}=yield s.current.execute($({async:!0},f.rqdata?{rqdata:f.rqdata}:{}));return v(_),_}catch(_){w(_),o&&o(_)}}),H=()=>{var f;(f=s==null?void 0:s.current)==null||f.resetCaptcha()};return Fe(()=>{var f;r&&((f=s==null?void 0:s.current)==null||f.setData(r))},[r]),re.createElement(I.Provider,{value:{sitekey:t,error:d,token:y,ready:c,executeInstance:ie,resetInstance:H}},h,re.createElement(te,{sitekey:t,size:e,theme:n,languageOverride:i,onReady:x,onVerify:se,onExpire:P,onError:E,ref:s}))}export{ne as HCaptchaProvider,N as useHCaptcha};