/*! phoscon-app 2024-08-14 */

import{initStateHandlers}from"./login/state-handler-map.js";import{initRouter}from"./login/router.js";import{initMachine}from"./login/machine.js";import{injectLoginSvg}from"./login/load-svg.js";import{states}from"./login/constants.js";import{types}from"./login/constants.js";import{initOverviewView}from"./login/overview-view.js";import{initGatewayView}from"./login/gateway-view.js";import*as overviewFuncs from"./login/overview-states.js";import*as gatewayFuncs from"./login/gateway-states.js";import{pageMachine}from"./login/page-machine.js";import{scanMachine}from"./login/scan-machine.js";import{pollGateways,checkAppType,iOS}from"./login/helper-functions.js";location.href&&-1!==location.href.indexOf("https://phoscon.de/pwa")&&(location.href=location.href.replace("https://","http://"));const getViews=()=>{let overviewViews=initOverviewView(injectLoginSvg);return{...initGatewayView(injectLoginSvg),...overviewViews}},getFuncs=()=>({...overviewFuncs,...gatewayFuncs});function checkHistory(){let fragment=Backbone.history.getFragment();fragment&&""!==fragment||app.router.navigate(states.OVERVIEW,{trigger:!0})}function initLogin(){const unsup=["Worker"].find(x=>!(x in window));checkAppType()===types.APP_LOCAL?(app.appType="APP_LOCAL",console.log("app type: LOCAL")):checkAppType()===types.APP_SMARTPHONE?(app.appType="APP_SMARTPHONE",console.log("app type: SMARTPHONE")):(app.appType="APP_ONLINE",console.log("app type: ONLINE")),loadLanguage();const Webapp={sendMessage:function(tag,msg){console.log("WebApp Send Message: "+tag+" - "+msg)}},IOS={sendMessage:function(tag,msg){console.log("iOS Send Message: "+tag+" - "+msg),window.webkit.messageHandlers.IOs.postMessage({tag:tag,message:msg})}};if(app.Devicetype="undefined"!=typeof Android?Android:window.webkit&&window.webkit.messageHandlers&&window.webkit.messageHandlers.IOs?IOS:Webapp,unsup)return void document.body.classList.add("unsupported-browser");const views=getViews(),funcs=getFuncs();Object.keys(views).forEach(key=>views[key]=new views[key]);const stateHandlerMap=initStateHandlers({states:states,views:views,funcs:funcs}),page=pageMachine(states),scan=scanMachine(states),{machine:machine,service:service}=initMachine({states:states,stateHandlerMap:stateHandlerMap,page:page,scan:scan});Object.keys(views).forEach(key=>views[key].service=service),machine.context.service=service;let Router=initRouter({service:service,states:states});if(setTimeout(()=>{app.router=new Router,Backbone.history.start(),checkHistory()},200),app.pageDate=document.querySelector("meta[data-generated]"),app.pageDate){app.pageDate=app.pageDate.getAttribute("data-generated");const d=new Date(app.pageDate);app.pageDate=d.getTime()}if(-1!==document.location.hash.indexOf("nocache"))return;let cache=localStorage.getItem("app.gateways");if(cache){let ips=[],unique=[];const maxAge=6048e5;try{cache=JSON.parse(cache)||[]}catch(err){cache=[]}cache.forEach(gw=>{gw.reachable=!1,gw.id&&0!==parseInt(gw.id,16)&&gw.lastseen&&(gw.lastseen&&Date.now()-gw.lastseen>maxAge||(gw.ip&&ips.push(gw.ip),unique.find(x=>x.id===gw.id)||(unique.push(gw),addGateway(gw))))})}0===app.gateways.length&&console.log("no GW found"),app.Devicetype.sendMessage("PAGE_READY","LOGIN"),checkAppType()===types.APP_SMARTPHONE?(app.Devicetype.sendMessage("GET_GATEWAYS_DATA",""),setTimeout(function(){setInterval(pollGateways,5e3)},2e3)):setInterval(pollGateways,5e3),translateAll()}initLogin();