text: !function(e,t){typeof exports==='object'&&typeof module!=='undefined'?module.exports=t():typeof define==='function'&&define.amd?define(t):(e.FormValidation=e.FormValidation||{}, e.FormValidation.plugins=e.FormValidation.plugins||{}, e.FormValidation.plugins.InvisibleRecaptcha=t())}(this,(function(){"use strict";var e=FormValidation.Plugin,t;!function(e){e.Invalid="Invalid";e.NotValidated="NotValidated";e.Valid="Valid";e.Validating="Validating"}(t||(t={}));var a=t,i=FormValidation.utils.fetch;class l extends e{constructor(e){super(e);this.widgetIds=new Map;this.captchaStatus=a.NotValidated;this.opts=Object.assign({},l.DEFAULT_OPTIONS,e);this.fieldResetHandler=this.onResetField.bind(this);this.preValidateFilter=this.preValidate.bind(this)}install(){this.core.on('core.field.reset',this.fieldResetHandler).registerFilter('validate-pre',this.preValidateFilter);let e=typeof window[l.LOADED_CALLBACK]==='undefined'?()=>{}:window[l.LOADED_CALLBACK];window[l.LOADED_CALLBACK]=()=>{e();let t={badge:this.opts.badge,'error-callback':()=>{this.captchaStatus=a.Invalid,this.core.updateFieldStatus(l.CAPTCHA_FIELD,a.Invalid)},'expired-callback':()=>{this.captchaStatus=a.NotValidated,this.core.updateFieldStatus(l.CAPTCHA_FIELD,a.NotValidated)},sitekey:this.opts.siteKey,size:this.opts.size},r=window.grecaptcha.render(this.opts.element,t);this.widgetIds.set(this.opts.element,r);this.core.addField(l.CAPTCHA_FIELD,{validators:{promise:{message:this.opts.message,promise:e=>{if(e.value===''){this.captchaStatus=a.Invalid;return Promise.resolve({valid:!1})}else if(this.opts.backendVerificationUrl===''){this.captchaStatus=a.Valid;return Promise.resolve({valid:!0})}else if(this.captchaStatus===a.Valid){return Promise.resolve({valid:!0})}else{return i(this.opts.backendVerificationUrl,{method:'POST',params:{[l.CAPTCHA_FIELD]:e.value}}).then(e=>{let t=`${e.success}`==='true';this.captchaStatus=t?a.Valid:a.Invalid;return Promise.resolve({meta:e,valid:t})}).catch(e=>{this.captchaStatus=a.NotValidated;return Promise.reject({valid:!1})})}}}}})};let t=this.getScript();if(!document.body.querySelector(`script[src="${t}"]`)){let e=document.createElement('script');e.type='text/javascript';e.async=!0;e.defer=!0;e.src=t;document.body.appendChild(e)}}uninstall(){this.core.off('core.field.reset',this.fieldResetHandler).deregisterFilter('validate-pre',this.preValidateFilter);this.widgetIds.clear();let e=this.getScript(),t=[].slice.call(document.body.querySelectorAll(`script[src="${e}"]`));t.forEach(e=>e.parentNode.removeChild(e));this.core.removeField(l.CAPTCHA_FIELD)}getScript(){let e=this.opts.language?`&hl=${this.opts.language}`:'';return`https://www.google.com/recaptcha/api.js?onload=${l.LOADED_CALLBACK}&render=explicit${e}`}preValidate(){if(this.widgetIds.has(this.opts.element)){let e=this.widgetIds.get(this.opts.element);return this.captchaStatus===a.Valid?Promise.resolve():new Promise((t,a)=>{window.grecaptcha.execute(e).then(()=>{this.timer&&clearTimeout(this.timer),this.timer=window.setTimeout(t,1e3)})})}else{return Promise.resolve()}}onResetField(e){if(e.field===l.CAPTCHA_FIELD&&this.widgetIds.has(this.opts.element)){let e=this.widgetIds.get(this.opts.element);window.grecaptcha.reset(e)}}}l.CAPTCHA_FIELD='g-recaptcha-response';l.DEFAULT_OPTIONS={badge:'bottomright',size:'invisible',backendVerificationUrl:''};l.LOADED_CALLBACK='___invisibleRecaptchaLoaded___';return l}))