/*!
 * KoliBri - The accessible HTML-Standard
 */
import{r as e,h as i,H as t,g as a}from"./index-b2c61df0.js";import{z as s,y as o}from"./component-names-068dc2ae.js";import{v as n}from"./label-20475610.js";import"./prop.validators-db70e3c4.js";import"./index-db4aa6e4.js";import"./dev.utils-1ca991a2.js";const l=class{constructor(t){e(this,t),this.state={_label:""},this._label=void 0}validateLabel(e){n(this,e)}render(){return i(t,{key:"d8edad29edf6c40702763ed84d2eff012c17c511",onSlotchange:this.handleSlotchange.bind(this),class:"kol-tree-wc"},i("nav",{key:"f35ee92ece4bdb6375c016cbabba84d8cfa6be25",class:"tree","aria-label":this.state._label},i("ul",{key:"0c2d424ec9f9dcb9d2a115f445cc2aff5dff5864",class:"treeview-navigation",role:"tree","aria-label":this.state._label},i("slot",{key:"5caf2660158c816c84caacc9301a8ce1b22910e3"}))))}static isTreeItem(e){return(null==e?void 0:e.tagName)===s.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(){var e;null===(e=this.getTopLevelTreeItems())||void 0===e||e.forEach((e=>{var t;null===(t=this.observer)||void 0===t||t.observe(e,{childList:!0,subtree:!0})}))}getTopLevelTreeItems(){var e,t,a;return null===(a=null===(t=null===(e=this.host.querySelector("slot"))||void 0===e?void 0:e.assignedNodes)||void 0===t?void 0:t.call(e))||void 0===a?void 0:a.filter(l.isTreeItem)}handleTreeChange(){this.treeItemElements=this.getTreeItemElements(),this.ensureActiveItemVisibility()}getTreeItemElements(){var e;return null===(e=this.getTopLevelTreeItems())||void 0===e?void 0:e.reduce(((e,t)=>{const a=t.querySelectorAll(s);return[...e,t,...a]}),[])}async getOpenTreeItemElements(){if(!this.treeItemElements)return;const e=async t=>!l.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,i,l,n,r,o,c,d;const v=await this.getOpenTreeItemElements(),u=null===(t=document.activeElement)||void 0===t?void 0:t.closest(s);if(!v||!u)return;const h=null==v?void 0:v.findIndex((e=>e===u));switch(e.key){case"ArrowDown":await(null===(a=v[h+1])||void 0===a?void 0:a.focusLink()),e.preventDefault();break;case"ArrowUp":await(null===(i=v[h-1])||void 0===i?void 0:i.focusLink()),e.preventDefault();break;case"Right":case"ArrowRight":e.preventDefault(),await u.isOpen()?await(null===(l=v[h+1])||void 0===l?void 0:l.focusLink()):await u.expand();break;case"Left":case"ArrowLeft":if(e.preventDefault(),await u.isOpen())await u.collapse();else{const e=v.findIndex((e=>e===u.parentElement));-1!==e&&await(null===(n=v[e])||void 0===n?void 0:n.focusLink())}break;case"Home":await(null===(r=v[0])||void 0===r?void 0:r.focusLink()),e.preventDefault();break;case"End":await(null===(o=v[v.length-1])||void 0===o?void 0:o.focusLink()),e.preventDefault();break;case null===(c=e.key.match(/[a-zA-Z0-9]/))||void 0===c?void 0:c.input:{const t=e.key.toLowerCase(),a=v.indexOf(u)+1,i=v.concat(v),s=i.slice(a,a+v.length).findIndex((e=>{var a;return null===(a=e.getAttribute("_label"))||void 0===a?void 0:a.trim().toLowerCase().startsWith(t)}));-1!==s&&(await i[a+s].focusLink(),e.preventDefault());break}case"*":{const e=null===(d=u.parentElement)||void 0===d?void 0:d.querySelectorAll(s);null==e||e.forEach((e=>{e.expand()}));break}}}async handleFocusOut(e){e.relatedTarget&&!e.relatedTarget.closest(o)&&await this.ensureActiveItemVisibility()}async ensureActiveItemVisibility(){const e=t=>{l.isTreeItem(t.parentElement)&&(t.parentElement.expand(),e(t.parentElement))},t=(()=>{var e,t,a,i;const n=null!==(i=null===(a=null===(t=null===(e=this.host.querySelector("slot"))||void 0===e?void 0:e.assignedNodes)||void 0===t?void 0:t.call(e))||void 0===a?void 0:a.filter(l.isTreeItem))&&void 0!==i?i:[];for(const e of n){if(e._active)return e;const t=e.querySelector(`${s}[_active="true"]`);if(t&&t._active)return t}})();t&&e(t)}get host(){return a(this)}static get watchers(){return{_label:["validateLabel"]}}};export{l as kol_tree_wc};