/*! phoscon-app 2024-02-26 */

"use strict";function bbInitGroups(){let pollBusy=!1,pollQueue=[],pollTimer=null;function processPoll(){if(pollTimer=null,pollBusy||0===pollQueue.length)return;pollBusy=!0;let r=pollQueue.shift();pollQueue.length>3&&(console.log("poll /groups instead",pollQueue.length,"single requests"),pollQueue=[],r=app.groups),restGetResource(r).catch(console.log).then(()=>{pollBusy=!1,pollQueue.length>0&&(pollTimer=setTimeout(processPoll,50))})}app.api.on("event",function(e){if("groups"===e.r)if("added"!==e.e){if("changed"===e.e){var g=app.groups.get(e.id);if(g){e.name&&g.set({name:e.name}),pollQueue.push(g),pollTimer||(pollTimer=setTimeout(processPoll,5e3));var args={};_.map(e,function(val,key){if(g.has(key)){if("state"===key){var mod=!1,state=g.attributes.state;return _.map(val,function(val2,key2){state[key2]!==val2&&(state[key2]=val2,mod=!0)}),void(mod&&(g.unset("state",{silent:!0}),g.set({state:state})))}g.get(key)!==val&&(args[key]=val)}}),g.set(args)}}}else app.groups.fetch()});var Group=Backbone.Model.extend({sync:function(method,model,options){return console.log(method,model.get("name"),options),"patch"===method&&(method="update"),Backbone.sync(method,model,options)},initialize:function(){}});app.GroupCollection=Backbone.Collection.extend({name:"groups",model:Group,url:"//"+app.session.gw+"/api/"+app.apikey+"/groups",parse:function(sensors){return _.map(sensors,function(group,gid){return group.id=gid,group.devicemembership=group.devicemembership||[],group.scenes=group.scenes||[],group})}}),app.groups=new app.GroupCollection;var cache=sessionStorage.getItem("app.groups");cache&&app.groups.reset(JSON.parse(cache)),app.groups.on("add",function(group,collection){}),app.groups.on("change",function(group,options){})}