/*! phoscon-app 2025-05-20 */

var activeModel,savelock=!1,state="SCENE_IDLE",spinTimer=null,saveTimer=null;export function saveScene(view,settingsView,constants){if(state!==constants.states.SCENE_CHANGED)throw"unexpected app state";if(activeModel){if(activeModel.has("tt")){let tt=activeModel.get("tt");activeModel.set({transitiontime:tt})}state=constants.states.SCENE_SAVING,setTimeout(()=>{view.showSceneSaving()},700),spinTimer&&(clearTimeout(spinTimer),spinTimer=null),saveTimer&&(clearTimeout(saveTimer),saveTimer=null),spinTimer=setTimeout(()=>{savelock=!1,view.showSavingSuccess(),saveTimer=setTimeout(()=>{view.showSavingDone()},2e3),setSceneStateIdle(view,settingsView,constants)},4e3),restStoreScene(activeModel).then(()=>restGetResource(app.scenes))}};export function setSceneStateIdle(view,settingsView,constants){if(state===constants.states.SCENE_SAVING||state===constants.states.SCENE_CHANGED){let dismiss=state===constants.states.SCENE_CHANGED;if(state=constants.states.SCENE_IDLE,savelock=!1,view.setSceneStateIdle(),activeModel&&(activeModel.unset("tt"),dismiss)){recallScene();let tt=app.scenes.get(activeModel).attributes.transitiontime||void 0;if(tt){let val=tt/10;settingsView.setFadingTime(val)}}}};export function setActiveScene(listView,settingsView,constants){if(savelock)return;if(0===app.scenes.length)return;void 0===app.session.scid&&(app.session.scid=app.scenes.models[0].attributes.id);let itemView=listView.views.find(v=>v.model.id===app.session.scid),model=itemView.model;itemView.setActive(listView),activeModel!==model&&(activeModel=model,app.trigger("active-item-changed",{model:model}));let tt=app.scenes.get(activeModel).attributes.transitiontime||void 0;if(tt){let val=tt/10;settingsView.setFadingTime(val)}if(app.router){let hash=document.location.hash.substring(1,document.location.hash.indexOf("/"));app.router.navigate("/"+hash+"/"+app.session.gid+"/"+app.session.scid,{trigger:!0})}recallScene()};export function toggleLight(model,constants){sendControlState(model,{on:!model.attributes.state.on}).catch(console.log)};export function sceneChanged(view,constants){state===constants.states.SCENE_IDLE&&(state=constants.states.SCENE_CHANGED,savelock=!0,view.sceneChanged())};export function fadingTimeChanged(settingsView,navView,ev,constants){let val=ev.currentTarget.value;var tt=10*app.timeSliderValues[val];activeModel.set({tt:tt}),settingsView.setFadingTimeText(val),sceneChanged(navView,constants)};export function addScene(){savelock||$("#createSceneModal").modal("show")};export function renameScene(){let model=app.scenes.get(app.session.scid);if(!model)return;let name=document.querySelector("#inputRenameScene").value.trim();name.length<1||restUpdateResource(model,{name:name}).then(()=>restGetResource(model)).then(()=>{$("#renameSceneModal").modal("hide")})};export function deleteScene(listView){if(app.session.scid){var scene=app.scenes.get(app.session.scid);scene&&restDeleteResource(scene).then(()=>restGetResource(app.scenes)).then(()=>{if(app.scenes.length>0)app.session.scid=app.scenes.models[0].attributes.id,screenMax(SCREEN_MAX_WIDTH_PHONE_LANDSCAPE)&&isThisPortrait()?listView.backToSceneList():listView.setActiveScene();else{app.session.scid=void 0,app.router.navigate("/",{trigger:!0});const sliderDiv=document.getElementById("sliderDiv");sliderDiv&&sliderDiv.classList.add("hide"),screenMax(SCREEN_MAX_WIDTH_PHONE_LANDSCAPE)&&isThisPortrait()&&listView.backToSceneList()}})}};