(()=>{"use strict";window.newspackRAS=window.newspackRAS||[],window.newspackRAS.push((function(e){var t;t=function(){document.querySelectorAll(".newspack-registration").forEach((t=>{const s=t.querySelector("form");if(!s)return;let n=new FormData(s);const a=t.querySelector(".newspack-registration__response"),i=s.querySelector('button[type="submit"]'),o=document.createElement("span");o.classList.add("spinner");let r=t.querySelector(".newspack-registration__registration-success");s.startLoginFlow=()=>{a.classList.add("newspack-registration--hidden"),a.innerHTML="",i.disabled=!0,i.appendChild(o),t.classList.add("newspack-registration--in-progress")},s.endLoginFlow=(d=null,c=500,l=null)=>{let p;if(l?.existing_user&&(r=t.querySelector(".newspack-registration__login-success")),d){p=document.createElement("p"),p.textContent=d;const e=r.querySelector("p");e&&l?.sso&&e.replaceWith(p)}const g=200===c;if(t.classList.add("newspack-registration--"+(g?"success":"error")),g){if(r.classList.remove("newspack-registration--hidden"),l?.email&&(n=new FormData(s),e.setReaderEmail(l.email),e.setAuthenticated(l?.authenticated),l.authenticated)){const t={email:l.email},s=n.getAll("lists[]");n.has("newspack_popup_id")&&(t.newspack_popup_id=n.get("newspack_popup_id")),n.has("gate_post_id")&&(t.gate_post_id=n.get("gate_post_id")),l?.sso&&(t.sso=!0),s?.length&&e.dispatchActivity("newsletter_signup",{...t,newsletters_subscription_method:"reader-registration",lists:s}),l?.existing_user?e.dispatchActivity("reader_logged_in",{...t,login_method:l?.metadata?.login_method||"registration-block"}):e.dispatchActivity("reader_registered",{...t,registration_method:l?.metadata?.registration_method||"registration-block"})}s.remove()}else p&&(a.appendChild(p),a.classList.remove("newspack-registration--hidden"));i.contains(o)&&i.removeChild(o),i.disabled=!1,t.classList.remove("newspack-registration--in-progress")},s.addEventListener("submit",(e=>(e.preventDefault(),s.startLoginFlow(),s.npe?.value?(n=new FormData(s),n.has("npe")&&n.get("npe")?void fetch(s.getAttribute("action")||window.location.pathname,{method:"POST",headers:{Accept:"application/json"},body:n}).then((e=>{e.json().then((({message:t,data:n})=>s.endLoginFlow(t,e.status,n)))})).catch((e=>{s.endLoginFlow(e,400)})):s.endFlow("Please enter a vaild email address.",400)):s.endLoginFlow("Please enter a vaild email address.",400)))),e.on("reader",(({detail:{authenticated:e}})=>{e&&s.endLoginFlow(null,200)}))}))},"undefined"!=typeof document&&("complete"!==document.readyState&&"interactive"!==document.readyState?document.addEventListener("DOMContentLoaded",t):t())}))})();