import{store,getContext,getElement,withSyncEvent}from"@wordpress/interactivity";var{actions,state}=store("core/tabs",{state:{get tabsList(){const context=getContext();const tabsId=context?.tabsId;const tabsList=state[tabsId];return tabsList;},get tabIndex(){const{attributes}=getElement();const tabId=attributes?.id?.replace("tab__","")||null;if(!tabId){return null;}
const{tabsList}=state;const tabIndex=tabsList.findIndex((t)=>t===tabId);return tabIndex;},get isActiveTab(){const{activeTabIndex}=getContext();const{tabIndex}=state;return activeTabIndex===tabIndex;},get tabIndexAttribute(){return state.isActiveTab?0:-1;}},actions:{handleTabKeyDown:withSyncEvent((event)=>{const{tabIndex,tabsList}=state;if(tabIndex===null){return;}
if(event.key==="ArrowRight"){event.preventDefault();actions.moveFocus(tabIndex+1);}else if(event.key==="ArrowLeft"){event.preventDefault();actions.moveFocus(tabIndex-1);}else if(event.key==="Home"){event.preventDefault();actions.moveFocus(0);}else if(event.key==="End"){event.preventDefault();actions.moveFocus(tabsList.length-1);}}),handleTabClick:withSyncEvent((event)=>{event.preventDefault();const{tabIndex}=state;if(tabIndex!==null){actions.setActiveTab(tabIndex);}}),moveFocus:(tabIndex)=>{const{tabsList}=state;if(!tabsList||tabsList.length===0){return;}
let newIndex=tabIndex;if(newIndex<0){newIndex=tabsList.length-1;}else if(newIndex>=tabsList.length){newIndex=0;}
const tabId=tabsList[newIndex];const tabElement=document.getElementById("tab__"+tabId);if(tabElement){tabElement.focus();}},setActiveTab:(tabIndex,scrollToTab=false)=>{const{tabsList}=state;if(!tabsList||tabsList.length===0){return;}
let newIndex=tabIndex;if(newIndex<0){newIndex=0;}else if(newIndex>=tabsList.length){newIndex=tabsList.length-1;}
const context=getContext();context.activeTabIndex=newIndex;if(scrollToTab){const tabId=tabsList[newIndex];const tabElement=document.getElementById(tabId);if(tabElement){setTimeout(()=>{tabElement.scrollIntoView({behavior:"smooth"});},100);}}}},callbacks:{onTabsInit:()=>{const{tabsList}=state;if(tabsList.length===0){return;}
const{hash}=window.location;const tabId=hash.replace("#","");const tabIndex=tabsList.findIndex((t)=>t===tabId);if(tabIndex>=0){actions.setActiveTab(tabIndex,true);}}}},{lock:true});