/*! phoscon-app 2023-07-31 */

import{serviceSend}from"./settings-gateway-machine.js";import{states,events,PASSWORD_REGEXP,passwordError}from"./settings-gateway-constants.js";import{confirmOldPassword,sendChangePassword}from"./settings-gateway-functions.js";export var changePasswordViewHandlerFactory=view=>(ctx,activity)=>{let event=activity.type;event===states.GENERAL_CHANGE_PASSWORD?(view.show(),view.page1(),view.isValidInputOldPw()?serviceSend(states.GENERAL_CHANGE_PASSWORD_PAGE_1_VALID):serviceSend(states.GENERAL_CHANGE_PASSWORD_PAGE_1_INVALID)):event!==events.CANCEL_PRESSED&&event!==events.CLOSE_PRESSED||(view.hide(),serviceSend(states.IDLE))};export var changePasswordViewPage1ValidHandlerFactory=view=>(ctx,activity)=>{let event=activity.type;if(event===states.GENERAL_CHANGE_PASSWORD_PAGE_1_VALID)view.page1Valid();else if(event===events.PASSWORD_CHANGED)view.isValidInputOldPw()?serviceSend(states.GENERAL_CHANGE_PASSWORD_PAGE_1_VALID):serviceSend(states.GENERAL_CHANGE_PASSWORD_PAGE_1_INVALID);else if(event===events.NEXT_PRESSED){let pw=view.getPassword();confirmOldPassword(pw).then(()=>serviceSend(events.OLD_PW_SUCCESS)).catch(()=>serviceSend(events.OLD_PW_FAIL))}else event===events.OLD_PW_SUCCESS?serviceSend(states.GENERAL_CHANGE_PASSWORD_PAGE_2):event===events.OLD_PW_FAIL?view.showOldPwInfo(events.OLD_PW_FAIL):event===events.GENERAL_FAIL?view.showOldPwInfo(events.GENERAL_FAIL):event!==events.CANCEL_PRESSED&&event!==events.CLOSE_PRESSED||(view.hide(),serviceSend(states.IDLE))};export var changePasswordViewPage1InvalidHandlerFactory=view=>(ctx,activity)=>{let event=activity.type;event===states.GENERAL_CHANGE_PASSWORD_PAGE_1_INVALID?view.page1Invalid():event===events.PASSWORD_CHANGED?view.isValidInputOldPw()?serviceSend(states.GENERAL_CHANGE_PASSWORD_PAGE_1_VALID):serviceSend(states.GENERAL_CHANGE_PASSWORD_PAGE_1_INVALID):event!==events.CANCEL_PRESSED&&event!==events.CLOSE_PRESSED||(view.hide(),serviceSend(states.IDLE))};export var changePasswordViewPage2HandlerFactory=view=>(ctx,activity)=>{let event=activity.type;event===states.GENERAL_CHANGE_PASSWORD_PAGE_2?(view.page2(),isPasswordsEqualAndValid(view.getNewPasswords())):event!==events.CANCEL_PRESSED&&event!==events.CLOSE_PRESSED||(view.hide(),serviceSend(states.IDLE))};export var changePasswordViewPage2ValidHandlerFactory=view=>(ctx,activity)=>{let event=activity.type;if(event===states.GENERAL_CHANGE_PASSWORD_PAGE_2_VALID)view.page2Valid();else if(event===events.PASSWORD_CHANGED)isPasswordsEqualAndValid(view.getNewPasswords());else if(event===events.NEXT_PRESSED){let oldPw=view.getPassword(),newPw=view.getNewPasswords();sendChangePassword(oldPw,newPw.password1).then(()=>serviceSend(events.NEW_PW_SUCCESS)).catch(err=>{serviceSend(err)})}else event===events.NEW_PW_SUCCESS?serviceSend(states.GENERAL_CHANGE_PASSWORD_PAGE_SUCCESS):event===events.NEW_PW_FAIL?serviceSend(states.GENERAL_CHANGE_PASSWORD_PAGE_ERROR):event===events.GENERAL_FAIL?view.showNewPwInfo():event!==events.CANCEL_PRESSED&&event!==events.CLOSE_PRESSED||(view.hide(),serviceSend(states.IDLE))};export var changePasswordViewPage2InvalidHandlerFactory=view=>(ctx,activity)=>{let event=activity.type;if(event===states.GENERAL_CHANGE_PASSWORD_PAGE_2_INVALID){let tr=activity.pwFailTranslateId||void 0;view.page2Invalid(tr)}else event===events.PASSWORD_CHANGED?isPasswordsEqualAndValid(view.getNewPasswords()):event!==events.CANCEL_PRESSED&&event!==events.CLOSE_PRESSED||(view.hide(),serviceSend(states.IDLE))};function isPasswordsEqualAndValid({password1:password1,password2:password2}){if("string"!=typeof password1||"string"!=typeof password2)throw new TypeError;0===password1.length?serviceSend({type:states.GENERAL_CHANGE_PASSWORD_PAGE_2_INVALID,pwFailTranslateId:passwordError.PASSWORD_1_NOT_SET}):password1.length<8?serviceSend({type:states.GENERAL_CHANGE_PASSWORD_PAGE_2_INVALID,pwFailTranslateId:passwordError.PASSWORD_1_TOO_SHORT}):null===password1.match(PASSWORD_REGEXP)?serviceSend({type:states.GENERAL_CHANGE_PASSWORD_PAGE_2_INVALID,pwFailTranslateId:passwordError.PASSWORD_1_REGEX_FAIL}):0===password2.length?serviceSend({type:states.GENERAL_CHANGE_PASSWORD_PAGE_2_INVALID,pwFailTranslateId:passwordError.PASSWORD_2_NOT_SET}):serviceSend(password1!==password2?{type:states.GENERAL_CHANGE_PASSWORD_PAGE_2_INVALID,pwFailTranslateId:passwordError.PASSWORD_1_UNEQUALLY_2}:states.GENERAL_CHANGE_PASSWORD_PAGE_2_VALID)}export var changePasswordViewPageSuccessHandlerFactory=view=>(ctx,activity)=>{let event=activity.type;event===states.GENERAL_CHANGE_PASSWORD_PAGE_SUCCESS?view.pageSuccess():event!==events.NEXT_PRESSED&&event!==events.CLOSE_PRESSED||(view.hide(),serviceSend(states.IDLE))};export var changePasswordViewPageErrorHandlerFactory=view=>(ctx,activity)=>{let event=activity.type;event===states.GENERAL_CHANGE_PASSWORD_PAGE_ERROR?view.pageError():event!==events.NEXT_PRESSED&&event!==events.CLOSE_PRESSED||(view.hide(),serviceSend(states.IDLE))};