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

const SEARCH_SENSORS_DISABLED=0,SEARCH_SENSORS_ENABLED=1,FAST_POLL_DELAY=500,SLOW_POLL_DELAY=5e3;let pollState=SEARCH_SENSORS_DISABLED,pollDelay=SLOW_POLL_DELAY,timerSeconds=0,searchRaf=null;export function startScan(bottomview,min){if(void 0===min&&(min=app.config.get("networkopenduration")/60),min>0&&timerSeconds>0)return;if(!(min>0))return bottomview.setSearchInactive(),void(timerSeconds>0&&(timerSeconds=0,console.log("stop scan")));bottomview.setSearchActive(),timerSeconds=60*min,min<=1?bottomview.setTimelabelOneMinute():(bottomview.setTimelabelMinutesRemaining(min),setTimeout(setTimeLabel,1e3,bottomview));let startTime=Date.now(),endTime=startTime+1e3*timerSeconds,lastWidth=0;searchRaf||(searchRaf=requestAnimationFrame(()=>(function frame(view){let pos=(Date.now()-startTime)/(endTime-startTime),width=Math.floor(100*pos);width>=100||0===timerSeconds?(timerSeconds=0,disableSensorSearch(),document.body.classList.contains("search-active")&&setTimeout(()=>{view.setSearchInactive()},5e3)):(width!==lastWidth&&(view.updateProgressBar(width),enableSensorSearch(),lastWidth=width),searchRaf=requestAnimationFrame(()=>frame(view)))})(bottomview)))};export function stopScan(view){view.setSearchInactive(),disableSensorSearch()};export function setTimeLabel(view){if(0!==timerSeconds){timerSeconds--;var min=Math.round(timerSeconds/60);timerSeconds>60?view.setTimeLabelMinutes(min):view.setTimelabelOneMinute(),timerSeconds>60&&setTimeout(setTimeLabel,1e3,view)}};function pollDone(){pollState===SEARCH_SENSORS_DISABLED?(pollDelay=SLOW_POLL_DELAY,(app.sensorScan&&app.sensorScan.isActive()||app.config.get("permitjoin")>0)&&app.config.save({permitjoin:0},{patch:!0})):pollState===SEARCH_SENSORS_ENABLED&&(pollDelay=FAST_POLL_DELAY,app.sensorScan&&!app.sensorScan.isActive()&&app.sensorScan.start()),app.sensorScan&&restGetResource(app.sensorScan).then(res=>{"lastscan"in res&&"active"===res.lastscan&&(console.log(res),Object.keys(res).forEach(x=>{if(isNaN(x));else{app.sensors.get(x)||restGetResource(app.sensors)}}))}),setTimeout(pollDone,pollDelay)}setTimeout(pollDone,1e3);export function enableSensorSearch(){-1===location.hash.indexOf("scan")&&(location.hash+="?scan"),pollState!==SEARCH_SENSORS_ENABLED&&(pollState=SEARCH_SENSORS_ENABLED,console.log("Sensor search enabled"))};export function disableSensorSearch(){pollState!==SEARCH_SENSORS_DISABLED&&(pollState=SEARCH_SENSORS_DISABLED,console.log("Sensor search disabled"))};disableSensorSearch();export function blinkAllLights(){const xhr=new XMLHttpRequest,url="//"+app.session.gw+"/api/"+app.apikey+"/groups/0/action";xhr.open("PUT",url,!0),xhr.setRequestHeader("Content-type","application/json; charset=utf-8"),xhr.setRequestHeader("Accept",app.apiversion),xhr.send(JSON.stringify({alert:"select"})),xhr.onreadystatechange=(e=>{if((4!==xhr.readyState||200!==xhr.status)&&4===xhr.readyState&&200!==xhr.status)throw new Error("blink lights request: "+xhr.status)})};export function renameSwitch(sw,doneCallback){for(var name=sw.get("product")||translate("text_switch"),num=1;app.switches.find(sw2=>sw!==sw2&&(1===num&&sw2.get("name")===name||sw2.get("name")===name+" ("+String(num)+")"));)num++;num>1&&(name+=" ("+String(num)+")"),sw.set({name:name}),function updateSensors(sensorIds){var s=app.sensors.get(sensorIds.pop());s?s.save({name:name},{patch:!0,success:()=>{updateSensors(sensorIds)},error:doneCallback}):doneCallback()}(_.reduce(sw.get("endpoints"),(acc,ep)=>(acc.push(ep.id),acc),[]))};export function renameDevice(sn,doneCallback){for(var name=sn.get("product")||translate("text_sensor"),num=1;app.sensors.find(sn2=>sn!==sn2&&(1===num&&sn2.get("name")===name||sn2.get("name")===name+" ("+String(num)+")"));)num++;num>1&&(name+=" ("+String(num)+")"),sn.save({name:name},{patch:!0,success:()=>{console.log("Rename success")},error:err=>{console.debug(err)}})};