define(["playbackManager","userSettings","connectionManager","events"],function(playbackManager,userSettings,connectionManager,events){"use strict";var currentOwnerId,currentPlayer;function playThemeMedia(items,ownerId){var currentThemeItems=items.filter(function(i){return"Video"!==i.MediaType?userSettings.enableThemeSongs():userSettings.enableThemeVideos()});if(currentThemeItems.length){if(!currentOwnerId&&playbackManager.isPlaying())return;currentThemeItems.map(function(i){return i.Id}),playbackManager.play({items:currentThemeItems,fullscreen:!1,enableRemotePlayers:!1}).then(function(){currentOwnerId=ownerId})}else stopIfPlaying()}function stopIfPlaying(){currentOwnerId&&playbackManager.stop(),currentOwnerId=null}var excludeTypes=["CollectionFolder","UserView","Program","SeriesTimer","Person","TvChannel","Channel"];document.addEventListener("viewshow",function(e){if(!currentPlayer||currentPlayer.isLocalPlayer){var item=(e.detail.state||{}).item;if(item&&item.ServerId){var itemType=item.Type;if("User"!==itemType&&"Plugin"!==itemType&&"Device"!==itemType)return void function(item){var apiClient;!item.CollectionType&&!excludeTypes.includes(item.Type)&&item.ServerId&&item.Id?(apiClient=connectionManager.getApiClient(item)).getThemeMedia(item.Id,{UserId:apiClient.getCurrentUserId(),InheritFromParent:!0,EnableThemeSongs:userSettings.enableThemeSongs(),EnableThemeVideos:userSettings.enableThemeVideos()}).then(function(themeMediaResult){var ownerId=themeMediaResult.ThemeVideosResult.Items.length?themeMediaResult.ThemeVideosResult.OwnerId:themeMediaResult.ThemeSongsResult.OwnerId;ownerId!==currentOwnerId&&playThemeMedia(themeMediaResult.ThemeVideosResult.Items.length?themeMediaResult.ThemeVideosResult.Items:themeMediaResult.ThemeSongsResult.Items,ownerId)}):stopIfPlaying()}(item)}e.detail.supportsThemeMedia||playThemeMedia([],null)}},!0),events.on(playbackManager,"playerchange",function(e,player){currentPlayer=player})});