jQuery(document).ready(function(){
var cfturnstileWooCheckoutAttempted=false;
jQuery(document.body).on('submit', 'form.checkout', function(){
cfturnstileWooCheckoutAttempted=true;
});
if(jQuery('.woocommerce-error, .woocommerce-NoticeGroup-checkout .woocommerce-error').length){
setTimeout(turnstileWooCheckoutReset, 50);
}
jQuery(document.body).on('update_checkout updated_checkout applied_coupon_in_checkout removed_coupon_in_checkout', function(){
if(jQuery('#cf-turnstile-woo-checkout').is(':empty')){
turnstileWooCheckoutReset();
return;
}
if(cfturnstileWooCheckoutAttempted&&jQuery('.woocommerce-error, .woocommerce-NoticeGroup-checkout .woocommerce-error').length){
setTimeout(turnstileWooCheckoutReset, 50);
cfturnstileWooCheckoutAttempted=false;
}});
jQuery(document.body).on('checkout_error', function(){
setTimeout(turnstileWooCheckoutReset, 50);
cfturnstileWooCheckoutAttempted=false;
});
});
function turnstileWooCheckoutReset(){
if(typeof turnstile==='undefined'){
return;
}
var el=document.getElementById('cf-turnstile-woo-checkout');
if(!el){
return;
}
if(!el.innerHTML||el.innerHTML.trim()===''){
try {
turnstile.render(el);
} catch (e){
try { turnstile.render('#cf-turnstile-woo-checkout'); } catch (e2){}}
return;
}
try {
if(typeof turnstile.reset==='function'){
turnstile.reset(el);
return;
}} catch (e){}
try {
turnstile.remove(el);
} catch (e){
try { turnstile.remove('#cf-turnstile-woo-checkout'); } catch (e2){}}
try {
turnstile.render(el);
} catch (e){
try { turnstile.render('#cf-turnstile-woo-checkout'); } catch (e2){}}
}
jQuery('.showlogin').on('click', function(){
turnstile.remove('.sct-woocommerce-login');
turnstile.render('.sct-woocommerce-login');
});
document.addEventListener('DOMContentLoaded', function(){
if(typeof wp!=='undefined'&&wp.data&&typeof turnstile!=='undefined'){
function setTurnstileExtensionData(token){
var dispatch=wp.data.dispatch('wc/store/checkout');
if(typeof dispatch.setExtensionData==='function'){
dispatch.setExtensionData('simple-cloudflare-turnstile', { token: token });
}else if(typeof dispatch.__internalSetExtensionData==='function'){
dispatch.__internalSetExtensionData('simple-cloudflare-turnstile', { token: token });
}}
function cfturnstileWooBlockCheckoutRender(){
var turnstileItem=document.getElementById('cf-turnstile-woo-checkout');
if(!turnstileItem) return;
if(turnstileItem.getAttribute('data-sct-init')==='true'&&turnstileItem.hasChildNodes()){
try {
turnstile.reset(turnstileItem);
setTurnstileExtensionData('');
return;
} catch (e){}}
try { turnstile.remove(turnstileItem); } catch (e){}
try {
turnstile.render(turnstileItem, {
sitekey: turnstileItem.dataset.sitekey,
callback: setTurnstileExtensionData,
'expired-callback': function(){ setTurnstileExtensionData(''); }});
turnstileItem.setAttribute('data-sct-init', 'true');
} catch (e){}}
var cfturnstileWooBlockClickTimer=null;
jQuery(document.body).on('click', '.wc-block-components-checkout-place-order-button', function(){
if(cfturnstileWooBlockClickTimer){
clearTimeout(cfturnstileWooBlockClickTimer);
}
cfturnstileWooBlockClickTimer=setTimeout(function(){
cfturnstileWooBlockCheckoutRender();
}, 1250);
});
var unsubscribe=wp.data.subscribe(function(){
const turnstileItem=document.getElementById('cf-turnstile-woo-checkout');
if(turnstileItem&&(turnstileItem.innerHTML.trim()===''||turnstileItem.getAttribute('data-sct-init')!=='true')){
cfturnstileWooBlockCheckoutRender();
}}, 'wc/store/cart');
}});