/*!
 * KoliBri - The accessible HTML-Standard
 */
import{r as e,h as t,H as i,g as a}from"./index-9ca46203.js";import{v as s}from"./index-0489af72.js";const r="kol-tree-item",o=class{constructor(t){e(this,t),this.state={_label:""},this._label=void 0}validateLabel(e){s(this,e)}render(){return t(i,{key:"e890882e0c30f2dc33e17e0a7aff0a782b9cbaca",onSlotchange:this.handleSlotchange.bind(this),class:"kol-tree-wc"},t("nav",{key:"5c051db3cb3b7fd89aca1a7d2f19a4677c4345b0",class:"tree","aria-label":this.state._label},t("ul",{key:"5b5b42abb8b55c468342890ed40e92d5e2bfb82d",class:"treeview-navigation",role:"tree","aria-label":this.state._label},t("slot",{key:"4224f966cb6385a7322cceab67d6a9eea3000eb2"}))))}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 a=t.querySelectorAll(r);return[...e,t,...a]}),[])}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,a,s,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===(a=l[o+1])||void 0===a?void 0:a.focusLink()),e.preventDefault();break;case"ArrowUp":await(null===(s=l[o-1])||void 0===s?void 0:s.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 a(this)}static get watchers(){return{_label:["validateLabel"]}}};export{o as kol_tree_wc};