/*! phoscon-app 2024-01-19 */

"use strict";function bbInitPlugs(){function createPlugFromLight(l){if(!l.modelid)return;var sensorAttributes=function(l){let mac=l.uniqueid.split("-")[0]||-1,state=app.sensors.filter(sensor=>sensor.get("uniqueid")&&-1!==sensor.get("uniqueid").indexOf(mac)).map(s=>s.get("state"));return Object.assign({},...state)}(l);Object.assign(l.state,sensorAttributes);let mac=l.uniqueid.split("-")[0],light=_.clone(l);return app.plugs.add({...light,mac:mac,product:translate("text_plug")})}function addLight(light,collection){if(light.isNew())setTimeout(()=>addLight(light),100);else if(-1!=light.get("type").indexOf("plug")){var pl=createPlugFromLight(_.clone(light.attributes));app.api.trigger("plug-added",pl)}}app.Plug=Backbone.Model.extend({rename:function(name){this.save({name:name},{patch:!0})},sync:function(method,model,options){}}),app.PlugCollection=Backbone.Collection.extend({name:"plugs",model:app.Plug}),app.plugs=new app.PlugCollection,app.lights.on("add",addLight),app.lights.on("remove",function(light,options){let pl=app.plugs.find(pl=>pl.get("uniqueid")===light.get("uniqueid"))||void 0;pl&&pl.destroy()}),app.lights.on("change",function(light,options){var pl=app.plugs.get(light.id),a=light.changedAttributes();pl&&(a&&a.state&&(pl.set({reachable:a.state.reachable}),pl.set({alert:a.state.alert}),pl.set({on:a.state.on})),a.name&&pl.set({name:a.name}),a.manufacturer&&pl.set({manufacturer:a.manufacturer}),a.manufacturername&&pl.set({manufacturername:a.manufacturername}),a.modelid&&pl.set({modelid:a.modelid}))}),app.sensors.on("change",function(sensor,options){var pl=app.plugs.find(p=>-1!==sensor.get("uniqueid").indexOf(p.get("mac")));if(pl){var stateChanges=sensor.changedAttributes();stateChanges.state&&pl.set(stateChanges.state)}}),app.lights.forEach(addLight)}