/*!
 * KoliBri - The accessible HTML-Standard
 */
import{proxyCustomElement,HTMLElement,h,Host}from"@stencil/core/internal/client";import{v as validateLabel}from"./index2.js";const TREE_ITEM_TAG_NAME="kol-tree-item",KolTreeWc=proxyCustomElement(class e extends HTMLElement{constructor(){super(),this.__registerHost(),this.state={_label:""},this._label=void 0}validateLabel(e){validateLabel(this,e)}render(){return h(Host,{onSlotchange:this.handleSlotchange.bind(this)},h("nav",{class:"tree","aria-label":this.state._label},h("ul",{class:"treeview-navigation",role:"tree","aria-label":this.state._label},h("slot",null))))}static isTreeItem(e){return(null==e?void 0:e.tagName)==="kol-tree-item".toUpperCase()}componentWillLoad(){this.validateLabel(this._label),this.handleTreeChange(),this.observeChildListMutations()}disconnectedCallback(){var e;null===(e=this.observer)||void 0===e||e.disconnect()}observeChildListMutations(){this.observer=new MutationObserver(this.handleTreeChange.bind(this)),this.observeTopLevelItems()}handleSlotchange(){this.observeTopLevelItems(),this.handleTreeChange()}observeTopLevelItems(){this.getTopLevelTreeItems().forEach((e=>{var t;null===(t=this.observer)||void 0===t||t.observe(e,{childList:!0,subtree:!0})}))}getTopLevelTreeItems(){var t;return(null===(t=this.host.querySelector("slot"))||void 0===t?void 0:t.assignedNodes()).filter(e.isTreeItem)}handleTreeChange(){this.treeItemElements=this.getTreeItemElements(),this.ensureActiveItemVisibility()}getTreeItemElements(){return this.getTopLevelTreeItems().reduce(((e,t)=>{const l=t.querySelectorAll("kol-tree-item");return[...e,t,...l]}),[])}async getOpenTreeItemElements(){if(!this.treeItemElements)return;const t=async l=>!e.isTreeItem(l.parentElement)||await l.parentElement.isOpen()&&await t(l.parentElement);return(await Promise.all(this.treeItemElements.map((async e=>({value:e,include:await t(e)}))))).filter((e=>e.include)).map((e=>e.value))}async handleKeyDown(e){var t,l,s,r;const a=await this.getOpenTreeItemElements(),i=null===(t=document.activeElement)||void 0===t?void 0:t.closest("kol-tree-item");if(!a||!i)return;const n=null==a?void 0:a.findIndex((e=>e===i));switch(e.key){case"ArrowDown":await(null===(l=a[n+1])||void 0===l?void 0:l.focusLink()),e.preventDefault();break;case"ArrowUp":await(null===(s=a[n-1])||void 0===s?void 0:s.focusLink()),e.preventDefault();break;case"ArrowRight":await i.expand(),e.preventDefault();break;case"ArrowLeft":await i.collapse(),e.preventDefault();break;case"*":{const e=null===(r=i.parentElement)||void 0===r?void 0:r.querySelectorAll("kol-tree-item");null==e||e.forEach((e=>{e.expand()}));break}}}async handleFocusOut(e){e.relatedTarget&&!e.relatedTarget.closest("kol-tree")&&await this.ensureActiveItemVisibility()}async ensureActiveItemVisibility(){const t=l=>{e.isTreeItem(l.parentElement)&&(l.parentElement.expand(),t(l.parentElement))},l=(()=>{var t;const l=(null===(t=this.host.querySelector("slot"))||void 0===t?void 0:t.assignedNodes()).filter(e.isTreeItem);for(const e of l){if(e._active)return e;const t=e.querySelector('kol-tree-item[_active="true"]');if(t&&t._active)return t}})();l&&t(l)}get host(){return this}static get watchers(){return{_label:["validateLabel"]}}},[4,"kol-tree-wc",{_label:[1],state:[32]},[[0,"keydown","handleKeyDown"],[0,"focusout","handleFocusOut"]],{_label:["validateLabel"]}]);function defineCustomElement(){if("undefined"==typeof customElements)return;["kol-tree-wc"].forEach((e=>{if("kol-tree-wc"===e)customElements.get(e)||customElements.define(e,KolTreeWc)}))}export{KolTreeWc as K,defineCustomElement as d};