import{h}from"@stencil/core";export class ScTabGroup{constructor(){this.tabs=[],this.panels=[],this.activeTab=void 0}componentDidLoad(){this.syncTabsAndPanels(),this.setAriaLabels(),this.setActiveTab(this.getActiveTab()||this.tabs[0],{emitEvents:!1}),this.mutationObserver=new MutationObserver((()=>{this.syncTabsAndPanels()})),this.mutationObserver.observe(this.el,{attributes:!0,childList:!0,subtree:!0})}disconnectedCallback(){this.mutationObserver.disconnect()}syncTabsAndPanels(){this.tabs=this.getAllTabs(),this.panels=this.getAllPanels()}setAriaLabels(){this.tabs.map((e=>{const t=this.panels.find((t=>t.name===e.panel));t&&(e.setAttribute("aria-controls",t.getAttribute("id")),t.setAttribute("aria-labelledby",e.getAttribute("id")))}))}handleClick(e){const t=e.target.closest("sc-tab");(null==t?void 0:t.closest("sc-tab-group"))===this.el&&t&&this.setActiveTab(t,{scrollBehavior:"smooth"})}handleKeyDown(e){const t=e.target.closest("sc-tab");if((null==t?void 0:t.closest("sc-tab-group"))!==this.el)return!0;if(["Enter"," "].includes(e.key)&&t&&this.setActiveTab(t,{scrollBehavior:"smooth"}),["ArrowUp","ArrowDown","Home","End"].includes(e.key)){const t=document.activeElement;if(t&&"sc-tab"===t.tagName.toLowerCase()){let s=this.tabs.indexOf(t);"Home"===e.key?s=0:"End"===e.key?s=this.tabs.length-1:"ArrowUp"===e.key?s=Math.max(0,s-1):"ArrowDown"===e.key&&(s=Math.min(this.tabs.length-1,s+1)),this.tabs[s].triggerFocus({preventScroll:!0}),e.preventDefault()}}}setActiveTab(e,t){if(t=Object.assign({emitEvents:!0,scrollBehavior:"auto"},t),e&&e!==this.activeTab&&!e.disabled){const s=this.activeTab;this.activeTab=e,this.tabs.map((e=>e.active=e===this.activeTab)),this.panels.map((e=>e.active=e.name===this.activeTab.panel)),t.emitEvents&&(s&&this.scTabHide.emit(s.panel),this.scTabShow.emit(this.activeTab.panel))}}getActiveTab(){return this.getAllTabs().find((e=>e.active))}getAllChildren(){const e=this.el.shadowRoot.querySelectorAll("slot"),t=["sc-tab","sc-tab-panel"];return Array.from(e).map((e=>{var t;return null===(t=null==e?void 0:e.assignedElements)||void 0===t?void 0:t.call(e,{flatten:!0})})).flat().reduce(((e,t)=>{var s;return e.concat(t,[...(null===(s=null==t?void 0:t.querySelectorAll)||void 0===s?void 0:s.call(t,"*"))||[]])}),[]).filter((e=>{var s,a;return t.includes(null===(a=null===(s=null==e?void 0:e.tagName)||void 0===s?void 0:s.toLowerCase)||void 0===a?void 0:a.call(s))}))}getAllTabs(e=!1){return this.getAllChildren().filter((t=>e?"sc-tab"===t.tagName.toLowerCase():"sc-tab"===t.tagName.toLowerCase()&&!t.disabled))}getAllPanels(){return this.getAllChildren().filter((e=>"sc-tab-panel"===e.tagName.toLowerCase()))}render(){return h("div",{key:"6db418090da7a490097c1957fbe62221f9c52f82",part:"base",class:{"tab-group":!0},onClick:e=>this.handleClick(e),onKeyDown:e=>this.handleKeyDown(e)},h("div",{key:"5b803ebe415624dc3c3fb80452bca1e29901fb08",class:"tab-group__nav-container",part:"nav"},h("div",{key:"88d46da46e9fe1c429d962f31bf63800ce42c566",class:"tab-group__nav"},h("div",{key:"ba6bc25bce35fb166e00d881eb00e242bff25dba",part:"tabs",class:"tab-group__tabs",role:"tablist"},h("slot",{key:"1b7e3c2faa6aa402ac03e76a0edb5419729a4e03",onSlotchange:()=>this.syncTabsAndPanels(),name:"nav"})))),h("div",{key:"2cc04c39158d4cee0f93f4f8c5597cb2317773cb",part:"body",class:"tab-group__body"},h("slot",{key:"9ac01bf13e0529ca5bf2971b31ce9c305059c694",onSlotchange:()=>this.syncTabsAndPanels()})))}static get is(){return"sc-tab-group"}static get encapsulation(){return"shadow"}static get originalStyleUrls(){return{$:["sc-tab-group.scss"]}}static get styleUrls(){return{$:["sc-tab-group.css"]}}static get states(){return{activeTab:{}}}static get events(){return[{method:"scTabHide",name:"scTabHide",bubbles:!0,cancelable:!0,composed:!0,docs:{tags:[],text:""},complexType:{original:"string",resolved:"string",references:{}}},{method:"scTabShow",name:"scTabShow",bubbles:!0,cancelable:!0,composed:!0,docs:{tags:[],text:""},complexType:{original:"string",resolved:"string",references:{}}}]}static get elementRef(){return"el"}}