/*! phoscon-app 2025-08-29 */

import*as constants from"./group-scenes/constants.js";import*as factorys from"./group-scenes/factory-functions.js";import{getView}from"./group-scenes/overview-view.js";import{getSettingsView}from"./group-scenes/settings-view.js";import{getEmptyView}from"./group-scenes/empty-view.js";import{getMachine}from"./group-scenes/machine.js";function initLogic(){app.state=constants.states.SCENE_IDLE;let views=getView(constants);views.settingsView=getSettingsView(constants),views.emptyView=getEmptyView(constants);let{service:service}=getMachine(views,constants,factorys);Object.values(views).forEach(v=>{v.service=service,(v.views||[]).forEach(s=>s.service=service)}),0===app.scenes.length?service.send({type:"EMPTY"}):views.sceneListView.setActiveScene(),app.scenes.on("add",function(){Object.values(views.sceneListView.views).forEach(v=>{null===v.service&&(v.service=service)})});let handles=document.querySelectorAll(".handle");Object.values(handles).forEach(handle=>{handle.addEventListener("touchend",function(){service.send({type:"SCENE_CHANGED"})}),handle.addEventListener("click",function(){service.send({type:"SCENE_CHANGED"})})}),loadLanguage();var Router=Backbone.Router.extend({routes:{"lights/:gid/:sid":"showSceneLights","newScene/:gid/:sid":"newScene","settings/:gid/:sid":"showSceneSettings","control/group/:gid/:lid":"groupControlPage","control/group/:gid":"groupControlPage"},showSceneLights:function(gid,sid){app.session.scid=sid;let s=app.scenes.get(sid);s&&(service.send({type:"OVERVIEW",model:s}),views.sceneListView.setActiveScene(views.settingsView))},newScene:function(gid,sid){app.session.scid=sid;let s=app.scenes.get(sid);if(s){service.send({type:"OVERVIEW",model:s});let view=void 0;views.sceneListView.views.forEach(v=>{v.model.attributes.id!==sid||(view=v)}),view&&(view.openScene(views.sceneListView),views.sceneListView.setActiveScene(views.settingsView))}},showSceneSettings:function(gid,sid){app.session.scid=sid;let s=app.scenes.get(sid);s&&(service.send({type:"SETTINGS",model:s}),views.sceneListView.setActiveScene(views.settingsView))},groupControlPage:function(gid,lid){app.session.lid=lid,app.lights.get(lid)&&views.overviewView.showSliders()}});let navigation=document.querySelector("#navigation"),overview=document.querySelector("#overview"),settings=document.querySelector("#settings"),menuBtnIcon=document.querySelector("#menu-btn svg use"),menuBtnLabel=document.querySelector("#menu-btn span"),headLeftBtn=document.querySelector(".header #menu-btn"),deleteBtn=document.querySelector("#deleteBtn"),allLightsBtn=document.querySelector("#allLightsBtn");deleteBtn.addEventListener("click",function(){service.send({type:constants.events.DELETE_CLICKED})}),app.router=new Router,Backbone.history.start(),app.$groupControlToggle=$("#grp-toggle:checkbox"),app.$groupControlToggle.change(function(){setGroupControl(this.checked)}),$(".hdr-toggle").prev().on("click",function(e){setGroupControl("btn-group-control"===e.currentTarget.id)}),$(".hdr-toggle").next().on("click",function(e){setGroupControl("btn-group-control"===e.currentTarget.id)}),app.$groupControlToggle.prop("checked",!0),allLightsBtn.addEventListener("click",function(e){console.log("clicked on allLights button")}),screenMax(SCREEN_MAX_WIDTH_PHONE_LANDSCAPE)&&isThisPortrait()&&(headLeftBtn.onclick=void 0,headLeftBtn.addEventListener("click",function(){"SCENE_LIGHTS"===app.currentSite||"SCENE_SETTINGS"===app.currentSite?(service.send({type:constants.states.OVERVIEW}),navigation.classList.remove("page-left"),overview.classList.add("hide"),settings.classList.add("hide"),menuBtnIcon.setAttribute("xlink:href","#icon-hamburger-sm"),menuBtnLabel.innerText="",app.currentSite="NAV_SCENES",app.router.navigate("/",{trigger:!0})):"SCENE_LIGHT_SLIDERS"===app.currentSite?(app.session.gid&&app.session.scid?app.router.navigate("/"+app.session.gid+"/"+app.session.scid,{trigger:!0}):app.router.navigate("/",{trigger:!0}),views.overviewView.hideSliders()):"SCENE_GROUP_SLIDERS"===app.currentSite?(app.session.gid&&app.session.scid?app.router.navigate("/"+app.session.gid+"/"+app.session.scid,{trigger:!0}):app.router.navigate("/",{trigger:!0}),views.overviewView.hideGroupSliders()):openNav()}))}initApp(),restGetResource(app.config).then(()=>restGetResource(app.lights)).then(()=>restGetResource(app.scenes)).then(()=>restGetResource(app.groups)).then(res=>{setTimeout(()=>app.poll.pollRepeated(app.config),5e3),setTimeout(()=>app.poll.pollRepeated(app.lights),5e3),setTimeout(()=>app.poll.pollRepeated(app.scenes),5e3)}).then(()=>initLogic());