/*! phoscon-app 2024-05-27 */

export var emptyHandlerFactory=(view,bottomview,constants)=>(ctx,event)=>{let c=constants,type=event.type;type===c.states.EMPTY?view.show():type===c.events.SCAN?startScan(view,bottomview,event.time):view.hide()};export var overviewHandlerFactory=(view,bottomview,constants)=>(ctx,event)=>{let c=constants,type=event.type;type===c.states.OVERVIEW||type===c.states.INIT?view.show():type===c.events.ROW_CLICKED?app.router.navigate("plug/"+event.mId,{trigger:!0}):type===c.events.SCAN?startScan(view,bottomview,event.time):view.hide()};export var detailsHandlerFactory=(view,bottomview,constants)=>(ctx,event)=>{let c=constants,type=event.type;type===c.states.DETAILS?(view.render(event.model),view.show()):type===c.events.SELECT_GROUP_CHANGED?addPlugToGroup(event.gId,view):type===c.events.DELETE_CLICKED?deletePlug(event.mId,view):type===c.events.SCAN?startScan(view,bottomview,event.time):view.hide()};function addPlugToGroup(gId,view){if(!gId)return;let group=app.groups.find(group=>group.id===gId);if(!group)return;let members=group.get("lights")||[],plug=app.plugs.get(view.model.id);plug&&restUpdateResource(group,{lights:_.union(members,[plug.id])}).then(console.log).catch(console.log)}function deletePlug(id,view){var plug=app.plugs.get(id)||void 0;if(!plug)return void console.log("plug"+plugId+" not found");let jobs=[],uid=plug.get("uniqueid");uid=uid.substring(0,uid.indexOf("-")),app.lights.forEach(x=>{x.has("uniqueid")&&0===x.get("uniqueid").indexOf(uid)&&jobs.push(restDeleteResource(x))}),app.sensors.forEach(y=>{y.has("uniqueid")&&0===y.get("uniqueid").indexOf(uid)&&jobs.push(restDeleteResource(y))}),Promise.all(jobs).then(()=>restGetResource(app.lights)).then(()=>{app.router.navigate("table",{trigger:!0})})}