define(["backdrop","mainTabsManager","layoutManager","inputManager","connectionManager","emby-tabs"],function(backdrop,mainTabsManager,layoutManager,inputManager,connectionManager){"use strict";function onViewDestroy(e){this.inputHandler&&(inputManager.off(this.view,onInputCommand),this.inputHandler=null);var tabControllers=this.tabControllers;tabControllers&&(tabControllers.forEach(function(t){t.destroy&&t.destroy()}),this.tabControllers=null),this.view=null,this.params=null,this.currentTabController=null,this.initialTabIndex=null,this.item=null}function onBeforeTabChange(){}function TabbedView(view,params){function loadTab(index,previousIndex){self.getTabController(index).then(function(controller){var refresh=!controller.refreshed;controller.onResume({autoFocus:null==previousIndex&&layoutManager.tv,refresh:refresh}),controller.refreshed=!0,currentTabIndex=index,self.currentTabController=controller})}function getTabContainers(){return view.querySelectorAll(".tabContent")}function onTabChange(e){var newIndex=parseInt(e.detail.selectedTabIndex),previousIndex=e.detail.previousIndex,previousTabController=null==previousIndex?null:self.tabControllers[previousIndex];previousTabController&&previousTabController.onPause&&previousTabController.onPause(),loadTab(newIndex,previousIndex)}this.tabControllers=[],this.view=view,this.params=params;var self=this,currentTabIndex=parseInt(params.tab||this.getDefaultTabIndex(params.parentId));this.initialTabIndex=currentTabIndex,view.addEventListener("viewbeforehide",this.onPause.bind(this)),view.addEventListener("viewbeforeshow",function(e){mainTabsManager.setTabs(view,currentTabIndex,self.getTabs,getTabContainers,onBeforeTabChange,onTabChange,!1)}),view.addEventListener("viewshow",function(e){self.onResume(e.detail)}),view.addEventListener("viewdestroy",onViewDestroy.bind(this))}function onInputCommand(e){switch(e.detail.command){case"refresh":var currentTabController=this.currentTabController;currentTabController&&currentTabController.refresh&&currentTabController.refresh({refresh:!0})}}function refreshItem(instance){if(instance.item)return void onItemRefreshed.call(instance,instance.item);var params=instance.params,apiClient=connectionManager.getApiClient(params.serverId);apiClient.getItem(apiClient.getCurrentUserId(),params.parentId).then(onItemRefreshed.bind(instance))}function onItemRefreshed(item){this.item=item,this.setTitle()}return TabbedView.prototype.onResume=function(options){var params=this.params;params.serverId&&params.parentId?refreshItem(this):this.setTitle(),backdrop.clear();var currentTabController=this.currentTabController;currentTabController?currentTabController&&currentTabController.onResume&&currentTabController.onResume({}):mainTabsManager.selectedTabIndex(this.initialTabIndex);var inputHandler=onInputCommand.bind(this);inputManager.on(window,inputHandler),this.inputHandler=inputHandler},TabbedView.prototype.onPause=function(){this.inputHandler&&(inputManager.off(window,onInputCommand),this.inputHandler=null);var currentTabController=this.currentTabController;currentTabController&&currentTabController.onPause&&currentTabController.onPause()},TabbedView.prototype.setTitle=function(){Emby.Page.setTitle(this.getTitle())},TabbedView.prototype.getTitle=function(){var item=this.item;return item?item.Name:""},TabbedView});