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"],function(_exports,_connectionmanager,_globalize,_events,_api,_methodtimer,_embyButton){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 progress,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?button.removeAttribute("disabled"):button.setAttribute("disabled","disabled"),button.setAttribute("data-taskid",tasks.Id),progress=(tasks.CurrentProgressPercentage||0).toFixed(1),options.progressElem&&(options.progressElem.innerHTML='<div class="itemProgressBarForeground itemProgressBarForegroundRound" style="width:'+progress+'%;"></div>',"Running"===tasks.State?options.progressElem.classList.remove("hide"):options.progressElem.classList.add("hide")),options.lastResultElem)&&("Failed"===(progress=tasks.LastExecutionResult?tasks.LastExecutionResult.Status:"")?options.lastResultElem.html('<span style="color:#FF0000;">('+_globalize.default.translate("LabelFailed")+")</span>"):"Cancelled"===progress?options.lastResultElem.html('<span style="color:#0026FF;">('+_globalize.default.translate("LabelCancelled")+")</span>"):"Aborted"===progress?options.lastResultElem.html('<span style="color:#FF0000;">'+_globalize.default.translate("LabelAbortedByServerShutdown")+"</span>"):options.lastResultElem.html(progress))}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))}});