define(["localassetmanager"],function(localassetmanager){"use strict";functionprocessDownloadStatus(apiClient,serverInfo,options){returnconsole.log("[mediasync] Begin processDownloadStatus"),localassetmanager.resyncTransfers().then(function(){returnlocalassetmanager.getServerItems(serverInfo.Id).then(function(items){console.log("[mediasync] Begin processDownloadStatus getServerItems completed");varp=Promise.resolve(),cnt=0,progressItems=items.filter(function(item){return"transferring"===item.SyncStatus||"queued"===item.SyncStatus});returnprogressItems.forEach(function(item){p=p.then(function(){returnreportTransfer(apiClient,item)}),cnt++}),p.then(function(){returnconsole.log("[mediasync] Exit processDownloadStatus. Items reported: "+cnt.toString()),Promise.resolve()})})})}functionreportTransfer(apiClient,item){returnlocalassetmanager.getItemFileSize(item.LocalPath).then(function(size){returnsize>0?apiClient.reportSyncJobItemTransferred(item.SyncJobItemId).then(function(){returnitem.SyncStatus="synced",localassetmanager.addOrUpdateLocalItem(item)},function(error){returnconsole.error("[mediasync] Mediasync error on reportSyncJobItemTransferred",error),item.SyncStatus="error",localassetmanager.addOrUpdateLocalItem(item)}):localassetmanager.isDownloadFileInQueue(item.LocalPath).then(function(result){returnresult?Promise.resolve():(console.log("[mediasync] reportTransfer: Size is 0 and download no longer in queue. Deleting item."),localassetmanager.removeLocalItem(item).then(function(){returnconsole.log("[mediasync] reportTransfer: Item deleted."),Promise.resolve()},function(err2){returnconsole.log("[mediasync] reportTransfer: Failed to delete item.",err2),Promise.resolve()}))})},function(error){returnconsole.error("[mediasync] reportTransfer: error on getItemFileSize. Deleting item.",error),localassetmanager.removeLocalItem(item).then(function(){returnconsole.log("[mediasync] reportTransfer: Item deleted."),Promise.resolve()},function(err2){returnconsole.log("[mediasync] reportTransfer: Failed to delete item.",error),Promise.resolve()})})}functionreportOfflineActions(apiClient,serverInfo){returnconsole.log("[mediasync] Begin reportOfflineActions"),localassetmanager.getUserActions(serverInfo.Id).then(function(actions){returnactions.length?apiClient.reportOfflineActions(actions).then(function(){returnlocalassetmanager.deleteUserActions(actions).then(function(){returnconsole.log("[mediasync] Exit reportOfflineActions (actions reported and deleted.)"),Promise.resolve()})},function(err){returnconsole.error("[mediasync] error on apiClient.reportOfflineActions: "+err.toString()),localassetmanager.deleteUserActions(actions)}):(console.log("[mediasync] Exit reportOfflineActions (no actions)"),Promise.resolve())})}functionsyncData(apiClient,serverInfo,syncUserItemAccess){returnconsole.log("[mediasync] Begin syncData"),localassetmanager.getServerItems(serverInfo.Id).then(function(items){varcompletedItems=items.filter(function(item){returnitem&&("synced"===item.SyncStatus||"error"===item.SyncStatus)}),request={TargetId:apiClient.deviceId(),LocalItemIds:completedItems.map(function(xitem){returnxitem.ItemId}),OfflineUserIds:(serverInfo.Users||[]).map(function(u){returnu.Id})};returnapiClient.syncData(request).then(function(result){returnafterSyncData(apiClient,serverInfo,syncUserItemAccess,result).then(function(){returnconsole.log("[mediasync] Exit syncData"),Promise.resolve()},function(err){returnconsole.error("[mediasync] Error in syncData: "+err.toString()),Promise.resolve()})})})}functionafterSyncData(apiClient,serverInfo,enableSyncUserItemAccess,syncDataResult){console.log("[mediasync] Begin afterSyncData");varp=Promise.resolve();returnsyncDataResult.ItemIdsToRemove&&syncDataResult.ItemIdsToRemove.length>0&&syncDataResult.ItemIdsToRemove.forEach(function(itemId){p=p.then(function(){returnremoveLocalItem(itemId,serverInfo.Id)})}),enableSyncUserItemAccess&&(p=p.then(function(){returnsyncUserItemAccess(syncDataResult,serverInfo.Id)})),p=p.then(function(){returnremoveObsoleteContainerItems(serverI