define(["knockout","pubsub","spinner","jquery","ccRestClient","ccConstants","storageApi","navigation","notifier","ccResourceLoader!global/frigelarGlobalUtils"],function(e,t,n,r,i,s,o,u,a,f){"use strict";var l,c,h,p,d="/ccstore/v1/",v={customSelector:".spinner-login-container"};return{WIDGET_ID:"frigelarLogin",showErrorMessage:e.observable(!1),validationErrorMsg:e.observable(""),passwordType:e.observable("password"),passwordRecoveryErrorMessage:e.observable(!1),isMobile:e.observable(!1),isOpen:e.observable(!1),recaptchaId:e.observable(!1),isLoadingPJ:e.observable(!1),showPreloader:e.observable(!1),showUnavailabilityAlert:e.observable(!1),showRestrictionAlert:e.observable(!1),showSuccessPJAlert:e.observable(!1),passwordExpired:e.observable(!1),loginValidated:e.observable(!1),allowCloseModal:e.observable(!1),onLoad:function(e){c=e,h=!1,r.Topic(t.topicNames.USER_LOGIN_SUCCESSFUL).subscribe(function(e){h&&c.user().adapter.loadJSON(s.ENDPOINT_UPDATE_PROFILE,s.ENDPOINT_UPDATE_PROFILE,p,function(e){r.Topic(t.topicNames.USER_PROFILE_UPDATE_SUCCESSFUL).publish(e)},function(e){r.Topic(t.topicNames.USER_PROFILE_UPDATE_FAILURE).publish(e)})}),e.checkResponsiveFeatures(),r.Topic("OPEN_LOGIN_MODAL").subscribe(e.openLogin.bind(e)),r.Topic(t.topicNames.USER_LOGIN_SUCCESSFUL).subscribe(function(t){e.spinnerDestroy(),e.closeModal(),r("body").removeClass("modal-open")}),r.Topic(t.topicNames.USER_LOGIN_FAILURE).subscribe(function(t){e.spinnerDestroy(),t.error=="unauthorized_client"?e.showLoginErrorMessage("Credenciais incorretas!"):e.showLoginErrorMessage("Credenciais incorretas!")}),r.Topic(t.topicNames.USER_PASSWORD_EXPIRED).subscribe(function(){e.spinnerDestroy(),e.passwordExpired(!0),window.location.pathname.includes("checkout")||e.switchContent("forgot-password")}),r.Topic("CLOSE_LOGIN_MODAL").subscribe(e.closeModal.bind(e)),r.Topic("RECAPTCHA_LOGIN_RESPONSE").subscribe(function(t){e.doLogin_v3(t)}),r.Topic(t.topicNames.PAGE_PARAMETERS).subscribe(function(){this.parameters.loggedIn=="false"&&!e.user().loggedIn()&&setTimeout(function(){e.openLogin()},0)}),r.Topic(t.topicNames.PAGE_READY).subscribe(function(){try{var t=new URLSearchParams(window.location.search);t.get("loggedIn")=="false"&&!e.user().loggedIn()&&!e.isOpen()&&setTimeout(function(){e.openLogin()},0)}catch(n){}})},redirectHome:function(){c.user().loggedIn()&&(u.goTo("/home"),r("body").removeClass("modal-open"))},validateUserDataPJ_sync:function(e,t){setTimeout(function(){f.isUserPJ(e).value()?(e.spinnerDestroy(),e.isLoadingPJ(!0),e.showPreloader(!0),f.populateAddressPJ(e,!0,"modal").then(function(t){var n=t&&t==="200";n?e.showSuccessPJAlert(!0):(t==="404"||t==="400"?e.showRestrictionAlert(!0):e.showUnavailabilityAlert(!0),e.validateUserDataPJ_async(e)),e.showPreloader(!1),f.populateUserPJAzyxLastUpdate(n)}).catch(function(){e.showPreloader(!1),e.showUnavailabilityAlert(!0),e.validateUserDataPJ_async(e),f.populateUserPJAzyxLastUpdate(!1)})):(t.get("loggedIn")=="false"&&t.get("page")&&window.location.assign(decodeURI(t.get("page"))),e.closeModal())},1500)},validateUserDataPJ_async:function(e){f.isUserPJ(e).value()&&f.populateAddressPJ(e,!1,"modal").then(function(e){var t=e&&e==="200";f.populateUserPJAzyxLastUpdate(t)}).catch(function(){f.populateUserPJAzyxLastUpdate(!1)})},createLoginSpinner:function(){try{Array.from(r(".spinner-login-container")[0].classList).indexOf("hidden")!==-1&&(r(".spinner-login-container").removeClass("hidden"),r(".address-info > .obs").addClass("hidden"),r(".content-wrapper").addClass("hidden"),n.create(v))}catch(e){}},spinnerDestroy:function(){n.destroy(v),r(".spinner-login-container").addClass("hidden")},beforeAppear:function(){var e=this;e.isOpen(!1),r.Topic(t.topicNames.PAGE_READY).subscribe(function(){try{var t=new URLSearchParams(window.location.search);t.get("loggedIn")=="false"&&!e.user().loggedIn()&&!e.isOpen()&&setTimeout(function(){if(r(".modal-backdrop").length===0&&t.get("page").includes("orderDetails")){var e="home?loggedIn=false&",n=window.location.href.replace(e,"");r('
').appendTo(document.body),r("#loginModal").hasClass("in")||(r("#loginModal").addClass("in"),r("#loginModal").show())}},0)}catch(n){}})},closeModal:function(){this.spinnerDestroy(),this.clearErrorMessage(),this.allowCloseModal(!1),r("#loginModal").modal("hide"),this.isOpen(!1);var e=o.getInstance().getItem("productToAddToWishlist");e=="undefined"&&!this.user().loggedIn()&&o.getInstance().setItem("productToAddToWishlist","null"),this.passwordExpired(!1)},openLogin:function(){var e=this;this.switchContent("login-form"),r("#email").val(""),r("#password").val(""),r("#password-recovery-email").val(""),r("#loginModal").modal(),setTimeout(function(){var e=r("#loginModal.modal.fade.right.in");e.unbind("click")},500),this.isOpen(!0);const t=document.getElementById("loginModal");t.addEventListener("click",function(t){e.allowCloseModal()==0&&t.target.id==="loginModal"&&e.closeModal()})},validateEmptyLogin:function(){var e=document.getElementById("email").value,t=document.getElementById("password").value,n=!e&&!t||e.length<=0&&t.length<=0;return n?(this.spinnerDestroy(),this.showLoginErrorMessage("Preencha os campos abaixo."),!1):!0},doLogin_v3:function(e){var n=this;n.createLoginSpinner(),n.clearErrorMessage(),n.allowCloseModal(!0),n.validateEmptyLogin();if(n.user().validateLogin()==0){n.spinnerDestroy(),this.showLoginErrorMessage("Preencha os campos abaixo.");return}if(e.googleTokenAllow===!1)a.sendMessage("reCAPTCHAError","Ops! Ocorreu um erro ao verificar a solicitação.","error",!0),n.spinnerDestroy();else if(e.googleTokenAllow===!0){if(n.user().validateLogin())return r.Topic(t.topicNames.USER_LOGIN_SUBMIT).publishWith(this.user(),[{message:"success"}]),r.Topic(t.topicNames.USER_LOGIN_FAILURE).subscribe(function(e){n.spinnerDestroy(),e.error=="unauthorized_client"?n.showLoginErrorMessage("Credenciais incorretas!"):n.showLoginErrorMessage("Credenciais incorretas!")}),!0;this.spinnerDestroy(),this.showLoginErrorMessage("Credenciais incorretas!")}else r.Topic("GOOGLE_RECAPTCHA_GET_TOKEN").publish(n.setReCaptchaToken)},doFacebookLogin:function(){var e=this;o.getInstance().removeItem("idSelectedAddress"),o.getInstance().removeItem("CEP-PDP");var t=e.site(),n=t.extensionSiteSettings.frigelarSiteCustomConfigurations.apiBaseUrl,s=!0;if(document.location.href.indexOf("dev-")!=-1||document.location.href.indexOf("tst-")!=-1)s=!1;var u=t.siteInfo.id,a=n+"/social-login/auth/facebook-login?siteId="+u;s&&(a=n.replace("api","social-login-svc")+"/auth/facebook-login?siteId="+u);var f=document.location.href,l=f.split("/");l=l.slice(3,l.length);var c="/"+l.join("/");o.getInstance().setSessionCookieItem("SSO_LOGIN_IN_PROGRESS",!0),r.Topic("FRIGELAR_SSO_LOGIN_STARTED").publish(),i.setSessionStoredValue("SSO_RETURN_URL",c),window.open(a,"_blank")},doGoogleLogin:function(){var e=this;o.getInstance().removeItem("idSelectedAddress"),o.getInstance().removeItem("CEP-PDP");var t=e.site(),n=t.extensionSiteSettings.frigelarSiteCustomConfigurations.apiBaseUrl,s=!0;if(document.location.href.indexOf("dev-")!=-1||document.location.href.indexOf("tst-")!=-1)s=!1;var u=t.siteInfo.id,a=n+"/social-login/auth/google-login?siteId="+u;s&&(a=n.replace("api","social-login-svc")+"/auth/google-login?siteId="+u);var f=document.location.href,l=f.split("/");l=l.slice(3,l.length);var c="/"+l.join("/");o.getInstance().setSessionCookieItem("SSO_LOGIN_IN_PROGRESS",!0),r.Topic("FRIGELAR_SSO_LOGIN_STARTED").publish(),i.setSessionStoredValue("SSO_RETURN_URL",c),window.open(a,"_blank")},clearErrorMessage:function(){this.showErrorMessage(!1);var e=document.getElementById("validation-box");this.validationErrorMsg(""),e&&(e.style.maxHeight="0",e.style.opacity=0);var t=document.getElementById("password-recovery-validation-box");t&&(t.style.maxHeight="0",t.style.opacity=0),this.passwordRecoveryErrorMessage(!1)},showLoginErrorMessage:function(e){this.showErrorMessage(!0);var t=document.getElementById("validation-box");t&&(this.validationErrorMsg(e),t.style.padding="14px",t.style.marginTop="16px",t.style.maxHeight="40px",t.style.opacity=1)},resetPassword:function(e){var t=this,n=/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,r=document.getElementById("password-recovery-email").value;this.clearErrorMessage();if(n.test(r))i.request("resetPassword",{login:r},function(e){if(t.passwordExpired()){t.openLogin(),t.passwordExpired(!1);return}t.switchContent("password-recovery-success")},function(e){console.error("Erro!",e)});else{var s=document.getElementById("password-recovery-validation-box");s.style.padding="14px",s.style.marginTop="16px",s.style.maxHeight="40px",s.style.opacity=1,this.passwordRecoveryErrorMessage(!0)}},switchContent:function(e){this.clearErrorMessage(),Array.isArray(e)&&(e=e[0]);var t=document.getElementById(e),n=document.getElementsByClassName("body-content");for(var r=0;r