document.addEventListener("DOMContentLoaded",function(){class i{constructor(){var r,e;this.loginUrl=((e=(r=window.fluentcart_checkout_info)==null?void 0:r.rest)==null?void 0:e.url)+"/user/login",this.nonce=window.fluentcart_checkout_info.rest.nonce,this.translate=window.fluentcart.$t}init(){this.form=document.querySelector("[data-fluent-cart-login-form]"),this.form&&(this.submitButton=this.form.querySelector("button.fluent-cart-login-button"),this.enableLiveErrorClearing(),this.form.addEventListener("submit",r=>{r.preventDefault(),this.handleLogin()}))}getFormData(){const r={};return this.form.querySelectorAll("input, select, textarea").forEach(t=>{t.name&&(t.type==="checkbox"?r[t.name]=t.checked?"on":"":r[t.name]=t.value.trim())}),r}clearErrors(){this.form.querySelectorAll("[data-fluent-cart-checkout-page-form-error]").forEach(e=>{e.textContent="";const t=e.closest("[data-fluent-cart-checkout-page-form-input-wrapper]");t&&t.classList.remove("has-error")})}showFieldError(r,e){const t=this.form.querySelector(`[data-fluent-cart-checkout-page-form-error][for="${r}"]`);if(t){t.textContent=e;const s=t.closest("[data-fluent-cart-checkout-page-form-input-wrapper]");s&&s.classList.add("has-error")}else this.showToast(e,"warning")}enableLiveErrorClearing(){this.form.querySelectorAll("input").forEach(e=>{e.addEventListener("input",()=>{const t=e.closest("[data-fluent-cart-checkout-page-form-input-wrapper]"),s=this.form.querySelector(`[data-fluent-cart-checkout-page-form-error][for="${e.name}"]`);t&&t.classList.remove("has-error"),s&&(s.textContent="")})})}validate(r){this.clearErrors();let e=!0;return r.user_login||(this.showFieldError("user_login",this.translate("Username or Email is required")),e=!1),r.password||(this.showFieldError("password",this.translate("Password is required")),e=!1),e}handleLogin(){const r=this.getFormData();if(!this.validate(r))return;this.submitButton.disabled=!0,this.submitButton.textContent=this.translate("Please wait...");const e=new XMLHttpRequest;e.open("POST",this.loginUrl,!0),e.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8"),e.setRequestHeader("X-WP-Nonce",this.nonce),e.onload=()=>{var s,o,a;let t=null;try{t=JSON.parse(e.responseText)}catch{}if(e.status>=200&&e.status<300&&(t!=null&&t.success))this.showToast(((s=t.data)==null?void 0:s.message)||this.translate("Login successful"),"success"),this.submitButton.textContent=this.translate("Redirecting..."),window.location.href=(o=t.data)==null?void 0:o.redirect_url;else{this.submitButton.disabled=!1,this.submitButton.textContent=this.translate("Login");const n=((a=t==null?void 0:t.data)==null?void 0:a.message)||this.translate("Login failed. Please try again.");this.showToast(n,"warning")}},e.onerror=()=>{this.submitButton.disabled=!1,this.submitButton.textContent=this.translate("Login"),this.showToast(this.translate("Network error occurred"),"error")},e.send(new URLSearchParams(r).toString())}showToast(r,e="info"){let t,s;const o=r||this.translate("Login failed."),a=document.createElement("div");switch(a.innerHTML=o,e){case"success":t="linear-gradient(to right, #00b09b, #96c93d)",s="#fff";break;case"warning":t="linear-gradient(to right, #ff8a23, #ffc051)",s="#fff";break;case"error":t="linear-gradient(to right, #ff5f6d, #ffc371)",s="#fff";break;default:t="linear-gradient(to right, #4e54c8, #8f94fb)",s="#fff"}new Toastify({node:a,text:r,duration:3e3,className:e,style:{background:t,color:s}}).showToast()}}new i().init()});
