/*! phoscon-app 2024-08-14 */

export const getSwitchPageView=c=>Backbone.View.extend({el:"#pageSwitches",template:_.template(document.getElementById("pageSwitch-template").innerHTML),initialize:function(c){this.c=c,this.hf=this.c.helperFuncs,this.el.innerHTML=this.template(),this.service=null,this.listView=new SwitchListModalView(this.c),this.activeSwid=null},show:function(){this.el.classList.add("active-view")},hide:function(){this.el.classList.remove("active-view")},setSwActive:function(el){let swid=el?el.getAttribute("data-swid"):-1;app.session.m_swid=swid,this.listView.views.forEach(v=>{let vid=v.el.getAttribute("data-swid");swid==vid?v.el.classList.add("active-swBtn"):v.el.classList.remove("active-swBtn")})}});const SwitchWizardItemView=Backbone.View.extend({tagName:"li",className:"switch list-group-item",template:_.template(document.getElementById("switch-modal-list-item").innerHTML),initialize:function(){this.el.innerHTML=this.template(this.model.attributes),this.el.setAttribute("data-swid",this.model.get("id")),this.el.setAttribute("data-btn-ev","SW_MODAL_PRESSED"),this.icon=this.el.querySelector(".item-icon"),this.name=this.el.querySelector(".item-name"),this.listenTo(this.model,"change",this.swChanged)},render:function(){this.hf=this.c.helperFuncs;let iconPath=this.c.getSvgIconPath(this.model,"img/switches/","svgPathsSwitches");var btnMap=this.hf.getBtnMap(this.model);return-1!==iconPath.indexOf("generic")&&btnMap?getGenericSwIcon(btnMap).then(svg=>{svg&&this.icon.replaceWith(svg)}).catch(console.log):(this.icon.setAttribute("src",iconPath),this.c.injectSvg([this.icon])),this},swChanged:function(model){let change=model.changed;if(!change)return;let changeSize=_.size(change);if(change.buttonevent||1==changeSize&&change.lastupdated){model.get("buttonevent");app.trigger("switchButtonClicked",model)}}}),SwitchListModalView=Backbone.View.extend({el:"#switchModalList",initialize:function(c){this.c=c,this.hf=this.c.helperFuncs,this.service=null,this.views=[],app.switches.forEach(sw=>{c.getSwitchesWithRules().includes(sw.get("uniqueid"))||this.addSw(sw)})},addSw:function(model){let view=this.views.find(v=>v.model===model);view||((view=new SwitchWizardItemView({model:model})).c=this.c,this.el.appendChild(view.render().el),this.views.push(view))},showFreeSwitches:function(ids){ids.forEach(id=>{let sw=app.switches.where({uniqueid:id})[0];if(!sw)return;let view=this.views.find(v=>v.el.getAttribute("data-swid")==sw.get("id"));view&&view.el.classList.add("hidden")})}});