define(["globalize","loading","connectionManager","registrationServices"],function(globalize,loading,connectionManager,registrationServices){"use strict";function changeRecordingToSeries(apiClient,timerId,programId,confirmTimerCancellation){return loading.show(),apiClient.getItem(apiClient.getCurrentUserId(),programId).then(function(item){return item.IsSeries?apiClient.getNewLiveTvTimerDefaults({programId:programId}).then(function(timerDefaults){return apiClient.createLiveTvSeriesTimer(timerDefaults).then(function(){loading.hide(),sendToast(globalize.translate("SeriesRecordingScheduled"))})}):confirmTimerCancellation?cancelTimerWithConfirmation(timerId,apiClient.serverId()):cancelTimer(apiClient.serverId(),timerId,!0)})}function cancelTimerWithConfirmation(timerId,serverId){return require(["confirm"]).then(function(responses){return(0,responses[0])({text:globalize.translate("MessageConfirmRecordingCancellation"),primary:"cancel",confirmText:globalize.translate("HeaderCancelRecording"),cancelText:globalize.translate("HeaderKeepRecording")}).then(function(){return loading.show(),cancelTimer(connectionManager.getApiClient(serverId),timerId,!0)})})}function cancelTimer(apiClient,timerId,hideLoading){return loading.show(),apiClient.cancelLiveTvTimer(timerId).then(function(){!1!==hideLoading&&(loading.hide(),sendToast(globalize.translate("RecordingCancelled")))})}function createRecording(apiClient,programId,isSeries){return loading.show(),apiClient.getNewLiveTvTimerDefaults({programId:programId}).then(function(item){return(isSeries?apiClient.createLiveTvSeriesTimer(item):apiClient.createLiveTvTimer(item)).then(function(){loading.hide(),sendToast(globalize.translate("RecordingScheduled"))})})}function sendToast(msg){require(["toast"],function(toast){toast(msg)})}return{cancelTimer:cancelTimer,createRecording:createRecording,changeRecordingToSeries:changeRecordingToSeries,toggleRecording:function(serverId,programId,timerId,timerStatus,seriesTimerId){return registrationServices.validateFeature("dvr").then(function(){var apiClient=connectionManager.getApiClient(serverId),hasTimer=timerId&&"Cancelled"!==timerStatus;return seriesTimerId&&hasTimer?function(serverId,timerId,timerStatus,seriesTimerId){return require(["dialog"]).then(function(responses){var dialog=responses[0],items=[];return items.push({name:globalize.translate("HeaderKeepRecording"),id:"cancel",type:"submit"}),"InProgress"===timerStatus?items.push({name:globalize.translate("HeaderStopRecording"),id:"canceltimer",type:"cancel"}):items.push({name:globalize.translate("HeaderCancelRecording"),id:"canceltimer",type:"cancel"}),items.push({name:globalize.translate("HeaderCancelSeries"),id:"cancelseriestimer",type:"cancel"}),dialog({text:globalize.translate("MessageConfirmRecordingCancellation"),buttons:items}).then(function(result){var apiClient=connectionManager.getApiClient(serverId);return"canceltimer"===result?(loading.show(),cancelTimer(apiClient,timerId,!0)):"cancelseriestimer"===result?(loading.show(),apiClient.cancelLiveTvSeriesTimer(seriesTimerId).then(function(){require(["toast"],function(toast){toast(globalize.translate("SeriesCancelled"))}),loading.hide()})):Promise.resolve()})})}(serverId,timerId,timerStatus,seriesTimerId):hasTimer&&programId?changeRecordingToSeries(apiClient,timerId,programId,!0):programId?createRecording(apiClient,programId):Promise.reject()})},cancelTimerWithConfirmation:cancelTimerWithConfirmation,cancelSeriesTimerWithConfirmation:function(timerId,serverId){return require(["confirm"]).then(function(responses){return(0,responses[0])({text:globalize.translate("MessageConfirmRecordingCancellation"),primary:"cancel",confirmText:globalize.translate("HeaderCancelSeries"),cancelText:globalize.translate("HeaderKeepSeries")}).then(function(){return loading.show(),connectionManager.getApiClient(serverId).cancelLiveTvSeriesTimer(timerId).then(function(){require(["toast"],function(toast){toast(globalize.translate("SeriesCancelled"))}),loading.hide()})})})}}});