define(["connectionManager","confirm","appRouter","globalize"],function(connectionManager,confirm,appRouter,globalize){"use strict";function onItemDeleted(options,serverId,parentId){"back"===options.navigate?appRouter.back():options.navigate&&(parentId?appRouter.showItem(parentId,serverId):appRouter.goHome())}return{deleteItem:function(options){var item=options.item,apiClient=connectionManager.getApiClient(item);if("Device"===item.Type)return function(item,apiClient,options){return require(["confirm"]).then(function(responses){return(0,responses[0])({text:globalize.translate("DeleteDeviceConfirmation"),title:globalize.translate("HeaderDeleteDevice"),confirmText:globalize.translate("Delete"),primary:"cancel"}).then(function(){return apiClient.ajax({type:"DELETE",url:apiClient.getUrl("Devices",{Id:item.Id})}).then(function(){return onItemDeleted(options)})})})}(item,apiClient,options);if("Server"===item.Type)return function(item,apiClient,options){return require(["confirm"]).then(function(responses){return(0,responses[0])({text:globalize.translate("DeleteServerConfirmation",item.Name),title:globalize.translate("HeaderDeleteServer"),confirmText:globalize.translate("Delete"),primary:"cancel"}).then(function(){return connectionManager.deleteServer(item.Id).then(function(){return onItemDeleted(options)})})})}(item,0,options);if("User"===item.Type)return function(item,apiClient,options){return require(["confirm"]).then(function(responses){return(0,responses[0])({text:globalize.translate("DeleteUserConfirmation",item.Name),title:globalize.translate("HeaderDeleteUser"),confirmText:globalize.translate("Delete"),primary:"cancel"}).then(function(){return ApiClient.deleteUser(item.Id).then(function(){return onItemDeleted(options)})})})}(item,0,options);if("Plugin"===item.Type)return function(item,apiClient,options){return require(["confirm"]).then(function(responses){return(0,responses[0])({text:globalize.translate("UninstallPluginConfirmation"),title:globalize.translate("HeaderUninstallPlugin"),confirmText:globalize.translate("Uninstall"),primary:"cancel"}).then(function(){return ApiClient.uninstallPlugin(item.Id).then(function(){return onItemDeleted(options)})})})}(item,0,options);var itemId=item.Id,parentId=item.SeasonId||item.SeriesId||item.ParentId,serverId=item.ServerId,msg=globalize.translate("ConfirmDeleteItem"),title=globalize.translate("HeaderDeleteItem");return apiClient.getDeleteInfo(itemId).then(function(deleteInfo){return deleteInfo.Paths.length&&(msg+="\n\n"+globalize.translate("FollowingFilesWillBeDeleted")+"\n"+deleteInfo.Paths.join("\n")),msg+="\n\n"+globalize.translate("AreYouSureToContinue"),confirm({title:title,text:msg,confirmText:globalize.translate("Delete"),primary:"cancel"}).then(function(){return apiClient.deleteItem(itemId).then(function(){return onItemDeleted(options,serverId,parentId)},function(err){function result(){return Promise.reject(err)}return function(options){return new Promise(function(resolve,reject){require(["alert"],function(alert){alert(options).then(resolve,resolve)})})}(globalize.translate("ErrorDeletingItem")).then(result,result)})})})}}});