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

const SEARCH_SENSORS_DISABLED=0,SEARCH_SENSORS_ENABLED=1,FAST_POLL_DELAY=500,SLOW_POLL_DELAY=5e3;var pollState=SEARCH_SENSORS_DISABLED,pollDelay=SLOW_POLL_DELAY,timerSeconds=0,searchRaf=null;function startScan(view,bottomview,min){if(pollState!==SEARCH_SENSORS_ENABLED&&(app.lightsBeforeSearch=[],app.lights.forEach(l=>{isFlsPpWhite(l)||app.lightsBeforeSearch.push(l.id)})),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(bottomview),1e3));let startTime=Date.now(),endTime=startTime+1e3*timerSeconds,lastWidth=0;searchRaf||(searchRaf=requestAnimationFrame(function(){!function frame(view){searchRaf=null;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,view)):(width!==lastWidth&&(view.updateProgressBar(width),enableSensorSearch(),lastWidth=width),searchRaf=requestAnimationFrame(function(){frame(view)}))}(bottomview)}))}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(){console.log("poll done"),pollState===SEARCH_SENSORS_DISABLED?(pollDelay=SLOW_POLL_DELAY,(app.lightScan.isActive()||app.config.get("permitjoin")>0)&&app.config.save({permitjoin:0},{patch:!0})):pollState===SEARCH_SENSORS_ENABLED&&(pollDelay=FAST_POLL_DELAY,app.lightScan.isActive()||app.lightScan.start()),app.lightScan.fetch(),setTimeout(pollDone,pollDelay)}function enableSensorSearch(){pollState!==SEARCH_SENSORS_ENABLED&&(pollState=SEARCH_SENSORS_ENABLED,console.log("enable sensor search"))}function disableSensorSearch(){pollState!==SEARCH_SENSORS_DISABLED&&(pollState=SEARCH_SENSORS_DISABLED,console.log("disable sensor search"))}