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

import{submitPassword,setPassword,resetPassword,sendInstallCode}from"./helper-functions.js";export var gatewayHandlerFactory=(view,states)=>(ctx,event)=>{let evType=event.type,service=ctx.service;if(evType!=states.OVERVIEW)if(evType==states.GATEWAY){view.show();let gwid=event.gwid||void 0,ls=localStorage.getItem("app.gw");ls=JSON.parse(ls);let gw=app.gateways.get(gwid)||new Backbone.Model(ls);if(!gw)return;view.render(gw)}else"BTN_BACK_PRESSED"==evType&&service.send({type:"CALL_ROUTER",route:"OVERVIEW"});else view.hide()};export var gwInputHandlerFactory=(view,states)=>(ctx,event)=>{let evType=event.type,service=ctx.service,gwid=sessionStorage.getItem("app.gwid"),gw=app.gateways.get(gwid);if(evType==states.GATEWAY_INPUT)view.show(),gw&&view.render(gw);else if("BTN_LOGIN_PRESSED"==evType){let pw=view.getInput();submitPassword(pw).catch(err=>{view.showLoginErr(err)})}else"BTN_PWFORGOT_PRESSED"==evType?service.send({type:"CALL_ROUTER",route:`GATEWAY/${gwid}/reset`}):(view.setInput(""),view.hide())};export var gwResetHandlerFactory=(view,states)=>(ctx,event)=>{let evType=event.type,gwid=sessionStorage.getItem("app.gwid"),gw=app.gateways.get(gwid);evType==states.GATEWAY_RESET?(view.show(),gw&&view.render(gw)):"BTN_PWRESET_PRESSED"==evType?resetPassword(gw).then(()=>{view.showResetSuccess()}).catch(err=>{view.showResetError()}):view.hide()};export var gwNewPwHandlerFactory=(view,states)=>(ctx,event)=>{let evType=event.type,gwid=sessionStorage.getItem("app.gwid"),gw=gwid?app.gateways.get(gwid):void 0;if(evType==states.GATEWAY_NEWPW)view.show(),gw&&view.render(gw);else if("INPUT_CHANGE"==evType){let res=isInputsEqualAndValid(view.getInputs());"success"!==res&&view.showLoginErr(res)}else if("BTN_LOGIN_PRESSED"==evType){let{password2:password2,name:name}=view.getInputs();setPassword(password2,name,gw)}else view.setInput(""),view.hide()};export var gwInstallHandlerFactory=(view,states)=>(ctx,event)=>{let evType=event.type,gwid=sessionStorage.getItem("app.gwid"),gw=gwid?app.gateways.get(gwid):void 0;if(evType==states.GATEWAY_INSTALL)view.show(),gw&&view.render(gw);else if("BTN_LOGIN_PRESSED"==evType){let installCode=view.getInstallCode();sendInstallCode(gw,installCode).catch(err=>view.showLoginErr(err))}else view.setInput(""),view.hide()};const PASSWORD_REGEXP=new RegExp(/([A-Za-z0-9!.*?/]){8,56}$/);function isInputsEqualAndValid({name:name,password1:password1,password2:password2}){if("string"!=typeof password1||"string"!=typeof password2)throw new TypeError;return 0===name.length?"Namen vergeben.":0===password1.length?"":password1.length<8?"Passwort zu kurz.":null===password1.match(PASSWORD_REGEXP)?"Erlaubte Sonderzeichen sind !.*?/":0===password2.length?"":password1!==password2?"Passwörter stimmen nicht überein.":"success"}