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

"use strict";function getGroupsForSwitch(sw){var sensorIds=Object.keys(sw.endpoints).map(function(ep){return sw.endpoints[ep].id});return app.groups.reduce(function(acc,group){var dm=group.get("devicemembership");return dm&&_.intersection(dm,sensorIds).length>0&&acc.push(group.id),acc},[])}async function checkFlsSpecialGroups(group){if("LightGroup"!==group.get("type")&&"Room"!==group.get("type"))return;if(console.log("check FLS-PP lp special groups for "+group.id),gatewayVersion()<20519)return void console.log("check FLS-PP lp special groups, cancel, version too old");let members=group.get("lights")||[],flspp=(members=members.map(x=>app.lights.get(x))).filter(x=>x&&"FLS-PP3"===x.get("modelid"));console.log("fls-pp count "+flspp.length),0===flspp.length&&console.log("TODO MPI delete orphan groups");let hexgid=parseInt(group.id).toString(16).padStart(4,"0"),uniqueid1="10:00:"+(hexgid=hexgid.substr(0,2)+":"+hexgid.substr(2,2)),uniqueid2="20:00:"+hexgid;await restGetResource(app.groups);let g1=app.groups.find(x=>x.get("uniqueid")===uniqueid1),g2=app.groups.find(x=>x.get("uniqueid")===uniqueid2),gid1=g1?g1.id:void 0,gid2=g2?g2.id:void 0;if(gid1||(console.log("create helper1",gid1),gid1=await restCreateResource(app.groups,{uniqueid:uniqueid1,name:"FLS-PP3 helper1 for group "+group.id,type:"Luminaire",lights:[]})),gid2||(console.log("create helper2",gid1),gid2=await restCreateResource(app.groups,{uniqueid:uniqueid2,name:"FLS-PP3 helper2 for group "+group.id,type:"Luminaire",lights:[]})),await restGetResource(app.groups),g1=g1||app.groups.find(x=>x.get("uniqueid")===uniqueid1),g2=g2||app.groups.find(x=>x.get("uniqueid")===uniqueid2),!g1||!g2)return void console.log("failed to create FLS-PP helper groups");let memb1=members.filter(x=>x&&!isFlsPpWhite(x)).map(x=>x.id),memb2=members.filter(x=>x&&isFlsPpWhite(x)).map(x=>x.id);_.isEqual(memb1,g1.get("lights")||[])?console.log("FLS-PP lp special RGB group "+g1.id+" is fresh for "+group.get("name")):(console.log("update FLS-PP lp special RGB group "+g1.id+" for "+group.get("name")),await restUpdateResource(g1,{lights:memb1})),_.isEqual(memb2,g2.get("lights")||[])?console.log("FLS-PP lp special white channel group "+g2.id+" is fresh for "+group.get("name")):(console.log("update FLS-PP lp special white channel group "+g2.id+" for "+group.get("name")),await restUpdateResource(g2,{lights:memb2}))}function countRoomElements(roomId){var groups=0,devices=0,scenes=0,schedules=0,automations=0,sensorControls=0,switches=0,groupId=roomId||app.session.roomId,room=app.rooms.get(groupId);if(room){app.resourcelinks.forEach(rl=>{if(rl.get("name")!=="room "+groupId)return;let groupLinks=rl.get("links").filter(link=>link.includes("/groups"));groups=groupLinks.length});let countLights=0;(room.get("lights")||[]).forEach(function(lid){var light=app.lights.get(lid);light&&light.get("modelid")!==FLS_PP_WHITE&&countLights++});let countSensors=room.get("sensors")?room.get("sensors").length:0,countBlinds=room.get("blinds")?room.get("blinds").length:0,countThermostats=room.get("thermostats")?room.get("thermostats").length:0;devices=countLights+countBlinds+countSensors+countThermostats,room.has("scenes")&&(scenes=room.get("scenes").length),app.schedules.forEach(function(schedule){var addr=schedule.attributes.command.address;-1===addr.indexOf(`/groups/${groupId}/action`)&&-1===addr.indexOf(`/groups/${groupId}/scenes`)||schedules++}),app.resourcelinks.forEach(rl=>{if(rl.get("name").startsWith("PIR-")){-1!==(rl.get("links")||[]).indexOf("/groups/"+groupId)&&automations++}}),app.resourcelinks.forEach(rl=>{if(2020===rl.get("classid")){-1!==(rl.get("links")||[]).indexOf("/groups/"+groupId)&&sensorControls++}});let switchesCounted=[];const SWITCH_EDIT_CLASS_MIN=2027,SWITCH_EDIT_CLASS_MAX=3028;app.resourcelinks.forEach(rl=>{if(-1===(rl.get("links")||[]).indexOf("/groups/"+groupId))return!1;if(rl.get("classid")>=SWITCH_EDIT_CLASS_MIN&&rl.get("classid")<=SWITCH_EDIT_CLASS_MAX){let sensor,swLink=rl.get("links").find(link=>-1!==link.indexOf("/sensors/")),sid=swLink?swLink.split("/sensors/")[1]:void 0;sid&&(sensor=app.sensors.where({uniqueid:sid})[0]),sensor&&(sid&&-1!==sid.indexOf("-")&&(sid=sid.split("-")[0]),switchesCounted.includes(sid)||(switchesCounted.push(sid),switches++))}})}return{groups:groups,devices:devices,scenes:scenes,schedules:schedules,automations:automations,sensorControls:sensorControls,switches:switches}}$(document).on("show.bs.modal","#deleteGroupModal",function(){if(app.session.gid){var group=app.groups.get(app.session.gid);group&&$(".modal-body span b",this).html(group.get("name"))}});