import{proxyCustomElement,HTMLElement,createEvent,h}from"@stencil/core/internal/client";const scTabGroupCss=":host{display:block;--sc-tabs-min-width:225px}.tab-group{display:flex;flex-wrap:wrap;position:relative;border:solid 1px transparent;border-radius:0;flex-direction:row}@media screen and (min-width: 750px){.tab-group{flex-wrap:nowrap}}.tab-group__tabs{display:flex;flex-wrap:wrap;flex:0 0 auto;flex-direction:column;margin-bottom:var(--sc-spacing-xx-large)}.tab-group__nav-container{order:1;flex:1 0 100%}@media screen and (min-width: 750px){.tab-group__nav-container{min-width:var(--sc-tabs-min-width);flex:0 1 auto}}.tab-group__body{flex:1 1 auto;order:2}@media screen and (min-width: 750px){.tab-group__body{padding:0 var(--sc-spacing-xx-large)}}::slotted(sc-tab){margin-bottom:var(--sc-spacing-xx-small)}",ScTabGroupStyle0=scTabGroupCss,ScTabGroup$1=proxyCustomElement(class extends HTMLElement{constructor(){super(),this.__registerHost(),this.__attachShadow(),this.scTabHide=createEvent(this,"scTabHide",7),this.scTabShow=createEvent(this,"scTabShow",7),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 a=this.tabs.indexOf(t);"Home"===e.key?a=0:"End"===e.key?a=this.tabs.length-1:"ArrowUp"===e.key?a=Math.max(0,a-1):"ArrowDown"===e.key&&(a=Math.min(this.tabs.length-1,a+1)),this.tabs[a].triggerFocus({preventScroll:!0}),e.preventDefault()}}}setActiveTab(e,t){if(t=Object.assign({emitEvents:!0,scrollBehavior:"auto"},t),e&&e!==this.activeTab&&!e.disabled){const a=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&&(a&&this.scTabHide.emit(a.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 a;return e.concat(t,[...(null===(a=null==t?void 0:t.querySelectorAll)||void 0===a?void 0:a.call(t,"*"))||[]])}),[]).filter((e=>{var a,s;return t.includes(null===(s=null===(a=null==e?void 0:e.tagName)||void 0===a?void 0:a.toLowerCase)||void 0===s?void 0:s.call(a))}))}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:"89ae3c3d802247ef553d79fe23541ee827a946f1",part:"base",class:{"tab-group":!0},onClick:e=>this.handleClick(e),onKeyDown:e=>this.handleKeyDown(e)},h("div",{key:"24d457d9eeb56b99cbcd478125edbbc1665992f2",class:"tab-group__nav-container",part:"nav"},h("div",{key:"cf5851850fe650bdb99a1594b8f8c5a1f751bb57",class:"tab-group__nav"},h("div",{key:"daaa67faeb45a2a5d9e42a65a6306e7efb710229",part:"tabs",class:"tab-group__tabs",role:"tablist"},h("slot",{key:"5e5813837616bf8fb622d48b6e88e71168e37535",onSlotchange:()=>this.syncTabsAndPanels(),name:"nav"})))),h("div",{key:"3bb8363340637eaf001e43ca506110618a285636",part:"body",class:"tab-group__body"},h("slot",{key:"7d5ce92abe81dab982207bd8971ac0d5bdf23daf",onSlotchange:()=>this.syncTabsAndPanels()})))}get el(){return this}static get style(){return ScTabGroupStyle0}},[1,"sc-tab-group",{activeTab:[32]}]);function defineCustomElement$1(){"undefined"!=typeof customElements&&["sc-tab-group"].forEach((e=>{"sc-tab-group"===e&&(customElements.get(e)||customElements.define(e,ScTabGroup$1))}))}const ScTabGroup=ScTabGroup$1,defineCustomElement=defineCustomElement$1;export{ScTabGroup,defineCustomElement};