document.addEventListener("DOMContentLoaded",function(){class l{constructor(){var r,o;this.formatErrorFields(),this.signupUrl=((o=(r=window.fluentcart_checkout_info)==null?void 0:r.rest)==null?void 0:o.url)+"/user/register",this.translate=window.fluentcart.$t}formatErrorFields(){document.querySelectorAll("[data-fluent-cart-checkout-page-form-section]").forEach(o=>{o.querySelectorAll("[data-fluent-cart-checkout-page-form-error]").forEach(e=>{const n=e.cloneNode(!0);e.remove(),o.appendChild(n)})})}init(){this.form=document.querySelector("[data-fluent-cart-registration-form]"),this.form&&this.form.addEventListener("submit",r=>{r.preventDefault();const o=this.form.querySelector("button");o.textContent=this.translate("Loading..."),o.disabled=!0,this.handleRegistration()})}handleRegistration(){const r={};this.form.querySelectorAll("input, select, textarea").forEach(n=>{n.name&&(r[n.name]=n.value)});const t=this.form.querySelector("button.fluent-cart-registration-button");t.textContent=this.translate("Please wait..."),t.disabled=!0,this.removeValidationErrors();const e=new XMLHttpRequest;e.open("POST",this.signupUrl,!0),e.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8"),e.onload=()=>{var n,s,i;if(e.status>=200&&e.status<300){const a=JSON.parse(e.responseText);t.textContent=this.translate("Redirecting please wait..."),new Toastify({text:(n=a.data)==null?void 0:n.message,duration:300,className:"info",style:{background:"linear-gradient(to right, #00b09b, #96c93d)"}}).showToast(),window.location.href=(s=a.data)==null?void 0:s.redirect_url}else{t.disabled=!1,t.textContent=this.translate("Register");let a;try{a=JSON.parse(e.responseText)}catch{a=null}if(a){const c=a??{};this.showValidationErrors(c),a.hasOwnProperty("data")&&new Toastify({text:((i=a.data)==null?void 0:i.message)??this.translate("Something went wrong"),className:"warning",style:{background:"linear-gradient(to right, #ff8a23, #ffc051)"}}).showToast()}}},e.onerror=function(){t.disabled=!1,t.textContent=this.translate("Submit"),new Toastify({text:this.translate("Network error occurred"),className:"error",style:{background:"linear-gradient(to right, #ff5f6d, #ffc371)"}}).showToast()},e.send(new URLSearchParams(r).toString())}removeValidationErrors(){document.querySelectorAll("[data-fluent-cart-checkout-page-form-input-wrapper]").forEach(t=>{t.classList.remove("has-error")}),document.querySelectorAll("[data-fluent-cart-checkout-page-form-error]").forEach(t=>{t.innerHTML=""})}showValidationErrors(r){if(typeof r=="object")for(const o in r){const t=document.getElementById(o);if(!t)continue;const e=t.closest("[data-fluent-cart-checkout-page-form-input-wrapper]");e&&e.classList.add("has-error");const n=r[o];if(typeof n=="object"){let s="",i=0;const a=Object.entries(n).length;for(const d in n)s+=n[d],i!==a-1&&(s+="<br>"),i++;const c=document.querySelector("[data-fluent-cart-checkout-page-form-error][for='"+t.name+"']");c&&(c.innerHTML=s)}}}}new l().init()});
