/*!
 * 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$1=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,{key:"e890882e0c30f2dc33e17e0a7aff0a782b9cbaca",onSlotchange:this.handleSlotchange.bind(this),class:"kol-tree-wc"},h("nav",{key:"5c051db3cb3b7fd89aca1a7d2f19a4677c4345b0",class:"tree","aria-label":this.state._label},h("ul",{key:"5b5b42abb8b55c468342890ed40e92d5e2bfb82d",class:"treeview-navigation",role:"tree","aria-label":this.state._label},h("slot",{key:"4224f966cb6385a7322cceab67d6a9eea3000eb2"}))))}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,a,s;const r=await this.getOpenTreeItemElements(),i=null===(t=document.activeElement)||void 0===t?void 0:t.closest("kol-tree-item");if(!r||!i)return;const n=null==r?void 0:r.findIndex((e=>e===i));switch(e.key){case"ArrowDown":await(null===(l=r[n+1])||void 0===l?void 0:l.focusLink()),e.preventDefault();break;case"ArrowUp":await(null===(a=r[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 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$1(){if("undefined"==typeof customElements)return;["kol-tree-wc"].forEach((e=>{if("kol-tree-wc"===e)customElements.get(e)||customElements.define(e,KolTreeWc$1)}))}const KolTreeWc=KolTreeWc$1,defineCustomElement=defineCustomElement$1;export{KolTreeWc,defineCustomElement};