/*!
 * KoliBri - The accessible HTML-Standard
 */
import{r as registerInstance,h,H as Host,g as getElement}from"./index-a5e15b1e.js";import{v as validateLabel}from"./index-c3ae175d.js";const TREE_ITEM_TAG_NAME="kol-tree-item",KolTreeWc=class{constructor(e){registerInstance(this,e),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 e;return(null===(e=this.host.querySelector("slot"))||void 0===e?void 0:e.assignedNodes()).filter(KolTreeWc.isTreeItem)}handleTreeChange(){this.treeItemElements=this.getTreeItemElements(),this.ensureActiveItemVisibility()}getTreeItemElements(){return this.getTopLevelTreeItems().reduce(((e,t)=>{const r=t.querySelectorAll("kol-tree-item");return[...e,t,...r]}),[])}async getOpenTreeItemElements(){if(!this.treeItemElements)return;const e=async t=>!KolTreeWc.isTreeItem(t.parentElement)||await t.parentElement.isOpen()&&await e(t.parentElement);return(await Promise.all(this.treeItemElements.map((async t=>({value:t,include:await e(t)}))))).filter((e=>e.include)).map((e=>e.value))}async handleKeyDown(e){var t,r,a,s;const l=await this.getOpenTreeItemElements(),i=null===(t=document.activeElement)||void 0===t?void 0:t.closest("kol-tree-item");if(!l||!i)return;const n=null==l?void 0:l.findIndex((e=>e===i));switch(e.key){case"ArrowDown":await(null===(r=l[n+1])||void 0===r?void 0:r.focusLink()),e.preventDefault();break;case"ArrowUp":await(null===(a=l[n-1])||void 0===a?void 0:a.focusLink()),e.preventDefault();break;case"ArrowRight":await i.expand(),e.preventDefault();break;case"ArrowLeft":await i.collapse(),e.preventDefault();break;case"*":{const e=null===(s=i.parentElement)||void 0===s?void 0:s.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 e=t=>{KolTreeWc.isTreeItem(t.parentElement)&&(t.parentElement.expand(),e(t.parentElement))},t=(()=>{var e;const t=(null===(e=this.host.querySelector("slot"))||void 0===e?void 0:e.assignedNodes()).filter(KolTreeWc.isTreeItem);for(const e of t){if(e._active)return e;const t=e.querySelector('kol-tree-item[_active="true"]');if(t&&t._active)return t}})();t&&e(t)}get host(){return getElement(this)}static get watchers(){return{_label:["validateLabel"]}}};export{KolTreeWc as kol_tree_wc};