angular.module("c8y.ui").controller("c8yLoginController",["$scope","$location","$window","c8yBase","c8yLoginService","gettextCatalog","c8ySettings","c8yLoginOptions","c8yConfig",function(n,t,o,r,s,a,e,i,c){var u,d,f=this,l=[],g=s.MESSAGES,p=s.FORMS;function h(e){return _.find(s.getLoginOptions(),function(n){return n.type.toLowerCase()===e})}function w(){l.length=0}function m(n){n=n.data.message;n&&$("remote_error",a.getString(n))}function v(n){var e=n.action;e?(E(e.form,e.keepCredentials),e.msg&&x.apply(this,e.msg)):m(n)}function y(n){return s.login(n.tenant,n.name,n.password,n.rememberMe).catch(v)}function P(){w(),L("password_reset_requested")}function R(){w(),E(p.LOGIN),L("password_changed")}function O(){$("phone_number_error")}function S(n){_.get(n,"config.url","").match(/user\/pin$/)&&403===n.status?$("tfa_pin_invalid"):m(n)}function C(){u=void 0}function E(n,e){f.user.name=f.user.name||r.getRuntimeOption("userPrefilled"),f.user.password=e?f.user.password:void 0,f.user.newPassword=e?f.user.newPassword:void 0,f.user.newPasswordConfirm=e?f.user.newPasswordConfirm:void 0,f.formToDisplay=n,w(),n===p.NEW_PASSWORD&&(T(),F())}function T(n){s.mustEnforcePasswordStrength(n).then(function(n){f.user.enforcePasswordStrength=n,A()})}function F(n){s.getPasswordMinGreenLength(n).then(function(n){f.user.minGreenLength=n,e.setUserPasswordValidationParams(n),f.passwordRegex=c.validation.password.pattern,g.ERROR.pattern_newPassword=c.validation.password.message,A()})}function L(n,e){x("success",g.SUCCESS,n,e)}function $(n,e){x("danger",g.ERROR,n,e)}function x(n,e,t,o){l.push({type:n,class:"alert-".concat(n),text:o||e[t]})}function A(n){var e=n;(e=e||A.formToValidate)&&(A.formToValidate=e,s.validateForm(e,f.user).then(function(){w(),_.forEach(e.$error,function(n,e){n&&"required"!==e&&$(e="pattern"===e?"".concat(e,"_").concat((n[0]||{}).$name||""):e)})}))}f.showTenant=s.showTenant(),f.FORMS=p,f.disabledForm=function(n){return u||n.$invalid},f.showForm=E,f.messages=l,f.login=function(n){w(),(u=y(n)).finally(C)},f.redirectOauth=function(){var n=f.oauthOptions.initRequest,e=encodeURIComponent(t.absUrl()),e="".concat(n.includes("?")?"&":"?","originUri=").concat(e);o.location.href="".concat(n).concat(e)},f.resetPassword=function(n){w(),(u=s.resetPassword(n).then(P).catch(m)).finally(C)},f.changePassword=function(n){w(),(u=s.changePassword(n).then(R).catch(function(n){(n=>(n=n.data.message,_.isEqual(n,"Error during password change : Token expired.")))(n)?(f.formToDisplay="password_reset",$("password_reset_token_expired")):m(n)})).finally(C)},f.savePhoneNumber=function(n){w(),(u=s.savePhoneNumber(f.user,n).then(_.partial(y,f.user)).catch(O)).finally(C)},f.verifyTFACode=function(n){w(),(u=s.verifyTFACode(n).catch(S)).finally(C)},f.resendTFASms=function(){w(),(u=s.resendTFASms().then(function(){L("resend_sms")}).catch(m)).finally(C)},f.fieldClass=function(n,e){return(e=(e||d)[n]||{}).$dirty&&e.$invalid?"has-error":""},f.fieldTooltip=function(n,e){var o,e=(e||d)[n]||{};return _.reduce((e=e.$error,o=n,_(e).map(function(n,e){var t=g.ERROR[e]||g.ERROR["".concat(e,"_").concat(o)];return n&&(a.getString(t)||e)}).filter(_.identity).value()),function(n,e){return n+(n?". ":"")+e},"")},f.setCurrentForm=function(n){d=n},f.validate=A,f.getEnforcePassword=T,f.getPasswordMinGreenLength=F,f.onTenantInputBlur=function(){f.getEnforcePassword(f.user.tenant),f.getPasswordMinGreenLength(f.user.tenant)},f.passwordRegex=c.validation.password.pattern,f.extra_link=r.appOption("login_extra_link"),f.showBasic=!1,f.user={},f.oauthOptions=void 0,w(),s.initializing.then(function(n){f.showBasic=!!h(i.loginOptions.BASIC.value),f.oauthOptions=h(i.loginOptions.OAUTH2.value),E(n)})}]);