define(["layoutManager","emby-tabs","emby-button","emby-linkbutton"],function(layoutManager){"use strict";var tabOwnerView,headerTabsContainer,tabsElem,headerTop;function ensureElements(){headerTabsContainer=headerTabsContainer||document.querySelector(".headerMiddle")}function onViewTabsReady(){this.selectedIndex(this.readySelectedIndex),this.readySelectedIndex=null}function defaultGetTabContainersFn(){return[]}return{setTabs:function(view,selectedIndex,getTabsFn,getTabContainersFn,onTabChange,setSelectedIndex){if(!view)return tabOwnerView&&(headerTabsContainer=headerTabsContainer||document.querySelector(".headerMiddle"),ensureElements(),headerTop&&headerTop.classList.remove("headerTop-withSectionTabs","headerTop-tv-withSectionTabs"),headerTabsContainer.innerHTML="",headerTabsContainer.classList.add("hide"),tabOwnerView=null),{tabsContainer:headerTabsContainer,replaced:!1};ensureElements();var tabsContainerElem=headerTabsContainer;if(tabOwnerView||tabsContainerElem.classList.remove("hide"),tabOwnerView===view)return(tabsElem=tabsElem||tabsContainerElem.querySelector('[is="emby-tabs"]')).selectedIndex(selectedIndex),tabOwnerView=view,{tabsContainer:tabsContainerElem,tabs:tabsElem,replaced:!1};var index=0,tabsHtml='<div is="emby-tabs"'+(null==selectedIndex?"":' data-index="'+selectedIndex+'"')+' class="tabs-viewmenubar"><div class="emby-tabs-slider">'+getTabsFn().map(function(t){var tabHtml,tabClass="emby-tab-button";return!1===t.enabled&&(tabClass+=" hide"),t.cssClass&&(tabClass+=" "+t.cssClass),tabHtml=t.href?'<a href="'+t.href+'" is="emby-linkbutton" class="'+tabClass+'" data-index="'+index+'">'+t.name+"</a>":'<button type="button" is="emby-button" class="'+tabClass+'" data-index="'+index+'">'+t.name+"</button>",index++,tabHtml}).join("")+"</div></div>";return tabsContainerElem.innerHTML=tabsHtml,headerTop=headerTop||document.querySelector(".headerTop"),layoutManager.tv?headerTop.classList.add("headerTop-withSectionTabs","headerTop-tv-withSectionTabs"):headerTop.classList.add("headerTop-withSectionTabs"),tabOwnerView=view,tabsElem=tabsContainerElem.querySelector('[is="emby-tabs"]'),getTabContainersFn=getTabContainersFn||defaultGetTabContainersFn,tabsElem.addEventListener("beforetabchange",function(e){var previousPanel,tabContainers=getTabContainersFn();null==e.detail.previousIndex||(previousPanel=tabContainers[e.detail.previousIndex])&&previousPanel.classList.remove("is-active");var newPanel=tabContainers[e.detail.selectedTabIndex];newPanel&&newPanel.classList.add("is-active")}),onTabChange&&tabsElem.addEventListener("tabchange",onTabChange),!1!==setSelectedIndex&&(tabsElem.selectedIndex?tabsElem.selectedIndex(selectedIndex):(tabsElem.readySelectedIndex=selectedIndex,tabsElem.addEventListener("ready",onViewTabsReady))),{tabsContainer:tabsContainerElem,tabs:tabsContainerElem.querySelector('[is="emby-tabs"]'),replaced:!0}},getTabsElement:function(){return document.querySelector(".tabs-viewmenubar")},selectedTabIndex:function(index){tabsElem=tabsElem||headerTabsContainer.querySelector('[is="emby-tabs"]'),null!=index?tabsElem.selectedIndex(index):tabsElem.triggerTabChange()}}});