(()=>{"use strict";function e(e){"undefined"!=typeof document&&("complete"!==document.readyState&&"interactive"!==document.readyState?document.addEventListener("DOMContentLoaded",e):e())}function t(e){let t;const a=new IntersectionObserver(c=>{c.forEach(c=>{c.isIntersecting?t||(t=setTimeout(()=>{e(),a.unobserve(c.target)},250)):t&&(clearTimeout(t),t=!1)})},{threshold:.1});return a}function a(e,t="submit"){if(e){const a=newspack_recaptcha_data?.site_key;return grecaptcha.execute(a,{action:t}).then(t=>{e.value=t})}}function c(e){const t=parseInt(e.getAttribute("data-recaptcha-widget-id"));isNaN(t)||grecaptcha.reset(t)}window.newspack_grecaptcha=window.newspack_grecaptcha||{destroy:function(e=[]){(e.length?e:[...document.querySelectorAll("form[data-newspack-recaptcha]")]).forEach(e=>{!function(e){const t=e.querySelector('input[name="g-recaptcha-response"]');t&&t.parentElement.removeChild(t)}(e)})},render:d,version:newspack_recaptcha_data.version};const r="v2"===newspack_recaptcha_data.version.substring(0,2),n="v3"===newspack_recaptcha_data.version,o=newspack_recaptcha_data.site_key,i="v2_invisible"===newspack_recaptcha_data.version;function s(e,a=null,r=null){e.removeAttribute("data-recaptcha-validated");const n={sitekey:o,size:i?"invisible":"normal",isolated:!0},d=()=>{e.removeAttribute("data-recaptcha-validated");const t=parseInt(e.getAttribute("data-recaptcha-retry-count"))||0;if(t<3)c(e),grecaptcha.execute(e.getAttribute("data-recaptcha-widget-id")),e.setAttribute("data-recaptcha-retry-count",t+1);else{const t=wp.i18n.__("There was an error connecting with reCAPTCHA. Please reload the page and try again.","newspack-plugin");r?r(t):function(e,t){const a=document.createElement("p");a.textContent=t;const c=document.createElement("div");c.classList.add("newspack-recaptcha-error"),c.appendChild(a),e.parentElement.classList.contains("newspack-newsletters-subscribe")?e.append(c):(c.classList.add("newspack-ui__notice","newspack-ui__notice--error"),e.insertBefore(c,e.firstChild))}(e,t)}};if(jQuery&&(jQuery(document).on("updated_checkout",()=>s(e,a,r)),jQuery(document.body).on("checkout_error",()=>s(e,a,r))),e.hasAttribute("data-recaptcha-widget-id"))return void c(e);const u=document.createElement("div");u.classList.add("grecaptcha-container"),document.body.append(u);const p=grecaptcha.render(u,{...n,callback:t=>{a?.();let r=e.querySelector('[name="g-recaptcha-response"]');r||(r=document.createElement("input"),r.type="hidden",r.name="g-recaptcha-response",e.appendChild(r)),r.value=t,e.setAttribute("data-recaptcha-validated","1");const n=e.querySelector("#place_order");n?n.click():e.requestSubmit(e.querySelector('input[type="submit"], button[type="submit"]')),c(e)},"error-callback":d,"expired-callback":d});e.setAttribute("data-recaptcha-widget-id",p),(()=>{e.removeAttribute("data-submit-button-click"),t(()=>s(e,a,r)).observe(e,{attributes:!0});const c=t=>{e.hasAttribute("data-recaptcha-validated")||e.hasAttribute("data-skip-recaptcha")?e.removeAttribute("data-recaptcha-validated"):(t.preventDefault(),t.stopImmediatePropagation(),function(e){const t=e.querySelectorAll(".newspack-recaptcha-error");for(const e of t)e.parentElement.removeChild(e)}(e),grecaptcha.execute(p))};e.addEventListener("submit",c,!0);const n=e.querySelector("#place_order_clone");n&&n.addEventListener("click",e=>{e.preventDefault(),e.stopImmediatePropagation(),c(e)},!0)})()}function d(c=[],o=null,i=null){if(!grecaptcha)return e(()=>grecaptcha.ready(()=>d(c,o,i)));(c.length?c:[...document.querySelectorAll("form[data-newspack-recaptcha],form#add_payment_method,form.checkout")]).forEach(e=>{t(()=>{r&&s(e,o,i),n&&function(e){let t=e.querySelector('input[name="g-recaptcha-response"]');if(!t){t=document.createElement("input"),t.type="hidden",t.name="g-recaptcha-response",e.appendChild(t);const c=e.getAttribute("data-newspack-recaptcha")||"submit";a(t,c),setInterval(()=>a(t,c),3e4),jQuery&&(jQuery(document).on("updated_checkout",()=>a(t,c)),jQuery(document.body).on("checkout_error",()=>a(t,c)))}}(e)}).observe(e,{attributes:!0})})}e(function(){grecaptcha.ready(d)})})();