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

export function initDevicesBlindsView(addClassToAll,removeClassToAll,injectSwitchWizzardSvg){return new Promise((response,reject)=>{return response(new(Backbone.View.extend({template:_.template(document.getElementById("blind-wizard-modal-template").innerHTML),el:"#blindWizardModal",initialize:function(){this.el.innerHTML=this.template(),this.modalBody=this.el.querySelector(".fade-in .modal-body"),this.modalTitle=this.el.querySelector(".modal-title"),this.modalFooter=this.el.querySelector(".modal-footer"),this.fadeIn=this.el.querySelector(".fade-in"),this.fadeOut=this.el.querySelector(".fade-out"),this.addClassToAll=addClassToAll,this.removeClassToAll=removeClassToAll,this.service=null,this.lastClick=null,this.touch=!1,this.checkIOS=iOS(),this.listenTo(app.switches,"change",this.switchClicked),this},events:{"click [data-btn-ev]:not(.disabled)":function(ev){const elem=ev.currentTarget,dataEv=elem.getAttribute("data-btn-ev");let aFunc=this.el.querySelector(".active-func");if(aFunc&&aFunc.classList.remove("active-func"),elem.hasAttribute("data-vendor")&&(app.session.vendor=elem.getAttribute("data-vendor")),elem.hasAttribute("data-product")&&(app.session.product=elem.getAttribute("data-product")),elem.hasAttribute("data-swid")&&(app.session.swid=elem.getAttribute("data-swid")),elem.hasAttribute("data-fid")&&(app.session.fid=elem.getAttribute("data-fid"),elem.classList.add("active-func")),""===dataEv)throw new Error("missing data-btn-ev attribute value");this.lastClick=dataEv,console.log(dataEv),this.service.send(dataEv)},"click .sw-target":"targetClicked","input .input-number":"inputNumberChanged"},render:function(){return this},showPage:function(pageContent){if(pageContent.trans){const newScript=document.createElement("script"),inlineScript=document.createTextNode(pageContent.trans.innerHTML);newScript.appendChild(inlineScript),document.body.appendChild(newScript)}this.fadeIn.classList.remove("animate-prev"),this.fadeIn.classList.remove("animate-next"),this.fadeOut.classList.remove("animate-prev"),this.fadeOut.classList.remove("animate-next"),pageContent.body&&(this.modalBody.innerHTML=pageContent.body.innerHTML),pageContent.title&&(this.modalTitle.innerHTML=pageContent.title.innerHTML),pageContent.footer&&(this.modalFooter.innerHTML=pageContent.footer.innerHTML),app.session.vendor&&this.el.querySelectorAll(".fade-in .vendor").forEach(el=>el.innerText=app.session.vendor),app.session.product&&this.el.querySelectorAll(".fade-in .product-name").forEach(el=>el.innerText=app.session.product);let images=this.el.querySelectorAll("img");injectSwitchWizzardSvg(images)},switchClicked:function(model){if(1==_.size(model.changed)&&model.changed.lastupdated||model.changed.buttonevent){setActiveTarget(model.get("buttonevent"),this.el),this.service.send("SWITCH_BTN_PRESSED")}},targetClicked:function(ev){setActiveTarget(ev.currentTarget.getAttribute("data-buttonEvent"),this.el),this.service.send("SWITCH_BTN_PRESSED")},addClass:function(data){let elemList=this.el.querySelectorAll(data.identifier);this.addClassToAll(elemList,data.c)},removeClass:function(data){let elemList=this.el.querySelectorAll(data.identifier);this.removeClassToAll(elemList,data.c)},setInputMinus:function(ev){let el=this.modalBody.querySelector(".input-number"),newVal=parseInt(el.value)-1;el.value=newVal<1?1:newVal,this.inputNumberChanged(ev)},setInputPlus:function(ev){let el=this.modalBody.querySelector(".input-number"),newVal=parseInt(el.value)+1;el.value=newVal>100?100:newVal,this.inputNumberChanged(ev)},inputNumberChanged:_.debounce(function(ev){let elem=this.modalBody.querySelector(".input-number"),newVal=parseInt(elem.value);newVal<1?elem.value=1:newVal>100&&(elem.value=100);let aFunc=this.el.querySelector(".active-func");aFunc&&aFunc.classList.remove("active-func");let node=elem.closest("[data-fid]");node&&(app.session.fid=node.getAttribute("data-fid"),node.classList.add("active-func")),this.service.send("INPUT_CHANGED")},400)})))})};function iOS(){return["iPad Simulator","iPhone Simulator","iPod Simulator","iPad","iPhone","iPod"].includes(navigator.platform)||navigator.userAgent.includes("Mac")&&"ontouchend"in document}function setActiveTarget(btnEvent,el){el.querySelectorAll(".interactive .sw-target").forEach(t=>{t.classList.remove("active-target");let data=t.getAttribute("data-buttonEvent");-1!==data.indexOf(btnEvent)&&(t.classList.add("active-target"),app.session.activeBtn=data)})}