/*! phoscon-app 2024-10-07 */

import{serviceSend}from"./settings-gateway-machine.js";import{states,events}from"./settings-gateway-constants.js";import{changeOtau,changeDiscovery,changeNetworkDuration,changeZigbeeChannel,connectAppAllowed,unloadListener}from"./settings-gateway-functions.js";const CHANNEL_CHANGE_TIME=15e3,OPEN_APP_DURATION=6e4;export var lightDurViewHandlerFactory=view=>(ctx,activity)=>{if(activity.type===events.LIGHTDUR_PRESSED){let activeDur=activity.activeDur;changeNetworkDuration(activeDur)}};export var changeOtauViewHandlerFactory=view=>(ctx,activity)=>{if(activity.type===events.CHANGE_OTAU_PRESSED){let otauState=activity.otau;changeOtau(otauState)}};export var changeDiscoveryViewHandlerFactory=view=>(ctx,activity)=>{if(activity.type===events.CHANGE_DISCOVERY_PRESSED){let discoveryState=activity.discovery;changeDiscovery(discoveryState)}};export var channelViewHandlerFactory=view=>(ctx,activity)=>{let event=activity.type;event===states.ADVANCED_CHANNEL?(view.show(),serviceSend(states.ADVANCED_CHANNEL_PAGE_1)):event!==events.CLOSE_PRESSED&&event!==events.CANCEL_PRESSED||(view.hide(),serviceSend(states.IDLE))};export var channelViewPage1HandlerFactory=view=>(ctx,activity)=>{let event=activity.type;event===states.ADVANCED_CHANNEL_PAGE_1?view.page1():event===events.NEXT_PRESSED&&serviceSend(states.ADVANCED_CHANNEL_PAGE_2)};export var channelViewPage2HandlerFactory=view=>(ctx,activity)=>{let event=activity.type;if(event===states.ADVANCED_CHANNEL_PAGE_2)view.page2();else if(event===states.ADVANCED_CHANNEL_PAGE_2_VALID)view.page2Valid();else if(event===states.ADVANCED_CHANNEL_PAGE_2_INVALID)view.page2Invalid();else if(event===events.CHANNEL_PRESSED){let newChannel=activity.activeChannel;if(!newChannel)return;view.isChannelBtnChanged(newChannel)?serviceSend(states.ADVANCED_CHANNEL_PAGE_2_VALID):serviceSend(states.ADVANCED_CHANNEL_PAGE_2_INVALID)}else event===events.NEXT_PRESSED&&serviceSend(states.ADVANCED_CHANNEL_PAGE_3)};export var channelViewPage3HandlerFactory=view=>(ctx,activity)=>{let event=activity.type;if(event===states.ADVANCED_CHANNEL_PAGE_3){view.page3();let c=view.getWishChannel();changeZigbeeChannel(c),window.addEventListener("beforeunload",unloadListener),setTimeout(()=>{let newChannel=app.config.get("zigbeechannel")||void 0;newChannel&&(view.isChannelChanged(newChannel)?serviceSend(states.ADVANCED_CHANNEL_PAGE_3_SUCCESS):serviceSend(states.ADVANCED_CHANNEL_PAGE_3_ERROR),window.removeEventListener("beforeunload",unloadListener))},15e3)}else event===states.ADVANCED_CHANNEL_PAGE_3_SUCCESS?view.page3Success():event===states.ADVANCED_CHANNEL_PAGE_3_ERROR?view.page3Error():event===events.NEXT_PRESSED&&(view.hide(),serviceSend(states.IDLE))};export var authAppViewHandlerFactory=view=>(ctx,activity)=>{if(activity.type===states.ADVANCED_CONNECT){window.addEventListener("beforeunload",unloadListener),view.showAuthAllowed(),connectAppAllowed(),countdown(view,60)}};function countdown(view,seconds){console.log(seconds),view.countdown(seconds),--seconds<0?(console.log("hide authinfo"),window.removeEventListener("beforeunload",unloadListener),view.hideAuthAllowed()):setTimeout(()=>countdown(view,seconds),1e3)}