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){function updateTasks(instance,tasks){var wasRunning,options=instance.options,instance=options.button,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=instance.hasAttribute("disabled"),instance.removeAttribute("disabled"),wasRunning&&options.onStatusChange&&options.onStatusChange()):instance.setAttribute("disabled","disabled"),instance.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"===(instance=tasks.LastExecutionResult?tasks.LastExecutionResult.Status:"")?options.lastResultElem.html('<span style="color:#FF0000;">('+_globalize.default.translate("Failed")+")</span>"):"Cancelled"===instance?options.lastResultElem.html('<span style="color:#0026FF;">('+_globalize.default.translate("Cancelled")+")</span>"):"Aborted"===instance?options.lastResultElem.html('<span style="color:#FF0000;">'+_globalize.default.translate("LabelAbortedByServerShutdown")+"</span>"):options.lastResultElem.html(instance))}function pollTasks(instance){_connectionmanager.default.getApiClient(instance.options.serverId).getScheduledTasks({IsEnabled:!0}).then(function(result){updateTasks(instance,result)})}function onButtonClick(e){!function(instance,taskId){_connectionmanager.default.getApiClient(instance.options.serverId).startScheduledTask(taskId).then(function(){return pollTasks(instance)})}(this,e.target.closest("button").getAttribute("data-taskid"))}function stopInterval(instance){var interval=instance.pollInterval;interval&&interval.destroy(),instance.pollInterval=null}function TaskButton(options){var instance=this||new TaskButton(options);(instance.options=options).serverId||(options.serverId=_connectionmanager.default.currentApiClient().serverId()),options.panel&&options.panel.classList.add("hide"),instance.paused=!0,instance.boundOnButtonClick=onButtonClick.bind(instance),instance.boundonScheduledTasksUpdate=function(e,apiClient,info){apiClient.serverId()===this.options.serverId&&updateTasks(this,info)}.bind(instance),instance.boundonPollIntervalFired=function(){_connectionmanager.default.getApiClient(this.options.serverId).isMessageChannelOpen()||pollTasks(this)}.bind(instance),"on"===options.mode?instance.resume({}):"off"===options.mode&&instance.pause()}Object.defineProperty(_exports,"__esModule",{value:!0}),_exports.default=void 0,TaskButton.prototype.resume=function(options){var instance,apiClient;this.paused&&(this.paused=!1,this.options.button.addEventListener("click",this.boundOnButtonClick),pollTasks(this),instance=this,apiClient=_connectionmanager.default.getApiClient(instance.options.serverId),stopInterval(instance),apiClient.startMessageListener("ScheduledTasksInfo","1000,1000"),instance.pollInterval=new _methodtimer.default({onInterval:instance.boundonPollIntervalFired,timeoutMs:1e4,type:"interval"}),_events.default.on(_api.default,"ScheduledTasksInfo",this.boundonScheduledTasksUpdate))},TaskButton.prototype.pause=function(){var instance;this.paused=!0,this.boundOnButtonClick&&this.options.button.removeEventListener("click",this.boundOnButtonClick),this.boundonScheduledTasksUpdate&&_events.default.off(_api.default,"ScheduledTasksInfo",this.boundonScheduledTasksUpdate),instance=this,_connectionmanager.default.getApiClient(instance.options.serverId).stopMessageListener("ScheduledTasksInfo"),stopInterval(instance)},TaskButton.prototype.destroy=function(){this.pause(),this.options=null,this.boundOnButtonClick=null,this.boundonScheduledTasksUpdate=null,this.boundonPollIntervalFired=null};_exports.default=TaskButton});