(()=>{"use strict";let e;var t;t=function(){const t=new Event("newspack-newsletters-subscribe-success");document.querySelectorAll(".newspack-newsletters-subscribe").forEach(s=>{const n=s.querySelector("form");if(!n)return;const i=s.querySelector(".newspack-newsletters-subscribe__response"),o=s.querySelector(".newspack-newsletters-subscribe__message"),r=s.querySelector('input[type="email"]'),a=s.querySelector('button[type="submit"]'),c=document.createElement("span");c.classList.add("spinner"),n.endFlow=(e,d=500,u=!1,l={})=>{s.setAttribute("data-status",d);const p=document.createElement("p");if(r.removeAttribute("disabled"),a.removeChild(c),a.removeAttribute("disabled"),n.classList.remove("in-progress"),p.innerHTML=u?s.getAttribute("data-success-message"):e,o.appendChild(p),p.className=`message status-${d}`,200===d){s.replaceChild(i,n),n.dispatchEvent(t),function(e,t,s){const n=e.getAttribute("data-after-success-behavior");if(!n)return;const i=e.getAttribute("data-after-success-url");if("custom"===n&&!i)return;if(t.querySelector(".newspack-newsletters-subscribe__continue"))return;const o=e.getAttribute("data-after-success-label")||"Continue",r=document.createElement("button");r.type="button",r.textContent=o,r.className="newspack-newsletters-subscribe__continue "+(s?.className||"submit-button");const a=s?.getAttribute("style");a&&r.setAttribute("style",a),r.addEventListener("click",()=>{"custom"===n?window.location.href=i:"referrer"===n&&function(){const e=document.referrer;if(e)try{if(new URL(e).origin===window.location.origin)return void(window.location.href=e)}catch(e){}window.history.back()}()}),t.appendChild(r),r.focus()}(s,i,a),window.newspackRAS=window.newspackRAS||[];const e=new FormData(n).getAll("lists[]"),o={email:r.value};l?.newspack_popup_id&&(o.newspack_popup_id=l.newspack_popup_id),l?.gate_post_id&&(o.gate_post_id=l.gate_post_id),e.length&&u&&window.newspackRAS.push(function(t){t.dispatchActivity("newsletter_signup",{...o,lists:e,newsletters_subscription_method:l?.newsletters_subscription_method||"newsletters-subscription-block"})}),l?.registered&&window.newspackRAS.push(function(e){e.dispatchActivity("reader_registered",{...o,registration_method:l?.registration_method||"newsletters-subscription"})})}},n.addEventListener("submit",t=>{if(t.preventDefault(),o.innerHTML="",n.classList.add("in-progress"),a.disabled=!0,a.appendChild(c),!n.npe?.value)return n.endFlow(newspack_newsletters_subscribe_block.invalid_email,400);const s=new FormData(n);if(!s.has("npe")||!s.get("npe"))return n.endFlow(newspack_newsletters_subscribe_block.invalid_email,400);e&&s.set("newspack_newsletters_subscribe",e),r.setAttribute("disabled","true"),a.setAttribute("disabled","true");const i=()=>{fetch(n.getAttribute("action")||window.location.pathname,{method:"POST",headers:{Accept:"application/json"},body:s}).then(t=>{t.json().then(s=>{const{message:i,newspack_newsletters_subscribed:o,newspack_newsletters_subscribe:r,metadata:a}=s;e=r,n.endFlow(i,t.status,o,a),200===t.status&&s?.registered&&!0!==s?.verified&&s?.verification_nonce&&(window.newspackRAS=window.newspackRAS||[],window.newspackRAS.push(e=>{"function"==typeof e?.openVerificationModal&&e.openVerificationModal({email:s.email,verificationNonce:s.verification_nonce,onSendCode:()=>{"function"==typeof e?.openAuthModal&&e.openAuthModal({skipAuthenticatedCheck:!0,skipNewslettersSignup:!0,backButtonClosesModal:!0,initialState:"otp",closeOnSuccess:!0,onClose:null})}})}))})})},d=window.newspackReaderActivation;"function"==typeof d?.maybeConfirmRegistration?d.maybeConfirmRegistration({email:s.get("npe"),onProceed:i,onCancel:()=>{r.removeAttribute("disabled"),a.removeAttribute("disabled"),a.contains(c)&&a.removeChild(c),n.classList.remove("in-progress")}}):i()})})},"undefined"!=typeof document&&("complete"!==document.readyState&&"interactive"!==document.readyState?document.addEventListener("DOMContentLoaded",t):t())})();