/*!
 * KoliBri - The accessible HTML-Standard
 */
import{r as t,h as e,H as i,g as s}from"./index-a5e15b1e.js";import{v as a}from"./index-c3ae175d.js";const r="kol-tree-item",o=class{constructor(e){t(this,e),this.state={_label:""},this._label=void 0}validateLabel(e){a(this,e)}render(){return e(i,{onSlotchange:this.handleSlotchange.bind(this)},e("nav",{class:"tree","aria-label":this.state._label},e("ul",{class:"treeview-navigation",role:"tree","aria-label":this.state._label},e("slot",null))))}static isTreeItem(e){return(null==e?void 0:e.tagName)===r.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(o.isTreeItem)}handleTreeChange(){this.treeItemElements=this.getTreeItemElements(),this.ensureActiveItemVisibility()}getTreeItemElements(){return this.getTopLevelTreeItems().reduce(((e,t)=>{const s=t.querySelectorAll(r);return[...e,t,...s]}),[])}async getOpenTreeItemElements(){if(!this.treeItemElements)return;const e=async t=>!o.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,s,a,i;const l=await this.getOpenTreeItemElements(),n=null===(t=document.activeElement)||void 0===t?void 0:t.closest(r);if(!l||!n)return;const o=null==l?void 0:l.findIndex((e=>e===n));switch(e.key){case"ArrowDown":await(null===(s=l[o+1])||void 0===s?void 0:s.focusLink()),e.preventDefault();break;case"ArrowUp":await(null===(a=l[o-1])||void 0===a?void 0:a.focusLink()),e.preventDefault();break;case"ArrowRight":await n.expand(),e.preventDefault();break;case"ArrowLeft":await n.collapse(),e.preventDefault();break;case"*":{const e=null===(i=n.parentElement)||void 0===i?void 0:i.querySelectorAll(r);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=>{o.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(o.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 s(this)}static get watchers(){return{_label:["validateLabel"]}}};export{o as kol_tree_wc};