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

import{StateEvents,UiStates,WifiEvents,ButtonEvents,Types}from"./state-constants.js";const subStateHandlerMap={};let stateConfiguredHandler=()=>{};function appSetConfiguredState(state){if(!state)throw new Error("state is missing");if(stateConfiguredHandler(StateEvents.LEAVE_STATE),!(state in subStateHandlerMap))throw new Error("state not found: "+state);(stateConfiguredHandler=subStateHandlerMap[state])(StateEvents.ENTER_STATE),app.router.navigate("//"+state,{trigger:!0})}export const clientConfiguredHandlerFactory=(view,setState,wifi,setSubState=appSetConfiguredState)=>(event,args)=>{if(event===StateEvents.ENTER_STATE)subStateHandlerMap[UiStates.CLIENT_CONFIGURED_CONNECTED]=clientConnectedHandlerFactory(view,setSubState,wifi),subStateHandlerMap[UiStates.CLIENT_CONFIGURED_NOT_CONNECTED_LIST]=clientNotConnectedListHandlerFactory(view,setSubState,wifi),subStateHandlerMap[UiStates.CLIENT_CONFIGURED_NOT_CONNECTED_EMPTY]=clientNotConnectedEmptyHandlerFactory(view,setSubState,wifi),wifi.get("workingtype")===Types.CLIENT?setSubState(UiStates.CLIENT_CONFIGURED_CONNECTED):wifi.get("wifiavailable").length>=1?setSubState(UiStates.CLIENT_CONFIGURED_NOT_CONNECTED_LIST):setSubState(UiStates.CLIENT_CONFIGURED_NOT_CONNECTED_EMPTY);else if(event===StateEvents.LEAVE_STATE)view.hide();else if(event===WifiEvents.USER_CONFIG_CHANGED||event===WifiEvents.WORKING_CONFIG_CHANGED)stateConfiguredHandler(StateEvents.LEAVE_STATE),setState(UiStates.CLIENT);else if(event===ButtonEvents.DEACTIVATE_CLIENT_PRESSED)setState(UiStates.CASE_2_PAGE_1);else if(stateConfiguredHandler(event)===StateEvents.NOT_HANDLED)if(event===ButtonEvents.TAB_AP_PRESSED)stateConfiguredHandler(StateEvents.LEAVE_STATE),setState(UiStates.AP);else{if(event!==ButtonEvents.CONNECT_TO_CLIENT_PRESSED||!wifi.get("eth0"))return StateEvents.NOT_HANDLED;setState(UiStates.CASE_1_PAGE_1,args)}};const clientConnectedHandlerFactory=(view,setState,wifi)=>event=>{if(event===StateEvents.ENTER_STATE)view.show(),view.showList();else{if(event!==StateEvents.LEAVE_STATE)return StateEvents.NOT_HANDLED;view.hide()}},clientNotConnectedListHandlerFactory=(view,setState,wifi)=>event=>{if(event===StateEvents.ENTER_STATE)view.show(),view.showList();else{if(event!==StateEvents.LEAVE_STATE)return StateEvents.NOT_HANDLED;view.hide()}},clientNotConnectedEmptyHandlerFactory=(view,setState,wifi)=>event=>{if(event===StateEvents.ENTER_STATE)view.show(),view.showEmpty();else{if(event!==StateEvents.LEAVE_STATE)return StateEvents.NOT_HANDLED;view.hide()}};