define(["exports","./../modules/emby-apiclient/connectionmanager.js","./../modules/common/globalize.js","./../modules/emby-apiclient/events.js","./../modules/common/input/api.js","./../modules/common/methodtimer.js","./../modules/emby-elements/emby-button/emby-button.js","./../modules/emby-elements/emby-progressbar/emby-progressbar.js"],function(_exports,_connectionmanager,_globalize,_events,_api,_methodtimer,_embyButton,_embyProgressbar){Object.defineProperty(_exports,"__esModule",{value:!0}),_exports.default=function(options){var pollInterval,button=options.button,serverId=ApiClient.serverId();function pollTasks(){_connectionmanager.default.getApiClient(serverId).getScheduledTasks({IsEnabled:!0}).then(updateTasks)}function updateTasks(tasks){var wasRunning,tasks=tasks.filter(function(t){return t.Key===options.taskKey})[0];options.panel&&(tasks?options.panel.classList.remove("hide"):options.panel.classList.add("hide")),tasks&&("Idle"===tasks.State?(wasRunning=button.hasAttribute("disabled"),button.removeAttribute("disabled"),wasRunning&&options.onStatusChange&&options.onStatusChange()):button.setAttribute("disabled","disabled"),button.setAttribute("data-taskid",tasks.Id),wasRunning=(tasks.CurrentProgressPercentage||0).toFixed(1),options.progressElem&&(options.progressElem.innerHTML='<div class="itemProgressBarForeground itemProgressBarForegroundRound" style="width:'+wasRunning+'%;"></div>',"Running"===tasks.State?options.progressElem.classList.remove("hide"):options.progressElem.classList.add("hide")),options.lastResultElem)&&("Failed"===(wasRunning=tasks.LastExecutionResult?tasks.LastExecutionResult.Status:"")?options.lastResultElem.html('<span style="color:#FF0000;">('+_globalize.default.translate("Failed")+")</span>"):"Cancelled"===wasRunning?options.lastResultElem.html('<span style="color:#0026FF;">('+_globalize.default.translate("Cancelled")+")</span>"):"Aborted"===wasRunning?options.lastResultElem.html('<span style="color:#FF0000;">'+_globalize.default.translate("LabelAbortedByServerShutdown")+"</span>"):options.lastResultElem.html(wasRunning))}function onButtonClick(){var taskId=this.getAttribute("data-taskid");taskId=taskId,_connectionmanager.default.getApiClient(serverId).startScheduledTask(taskId).then(pollTasks)}function onScheduledTasksUpdate(e,apiClient,info){apiClient.serverId()===serverId&&updateTasks(info)}function onPollIntervalFired(){_connectionmanager.default.getApiClient(serverId).isMessageChannelOpen()||pollTasks()}function stopInterval(){pollInterval&&pollInterval.destroy(),pollInterval=null}options.panel&&options.panel.classList.add("hide");"off"===options.mode?(button.removeEventListener("click",onButtonClick),_events.default.off(_api.default,"ScheduledTasksInfo",onScheduledTasksUpdate),_connectionmanager.default.getApiClient(serverId).stopMessageListener("ScheduledTasksInfo"),stopInterval()):(button.addEventListener("click",onButtonClick),pollTasks(),function(){var apiClient=_connectionmanager.default.getApiClient(serverId);stopInterval(),apiClient.startMessageListener("ScheduledTasksInfo","1000,1000"),pollInterval=new _methodtimer.default({onInterval:onPollIntervalFired,timeoutMs:1e4,type:"interval"})}(),_events.default.on(_api.default,"ScheduledTasksInfo",onScheduledTasksUpdate))}});