/*!
 * KoliBri - The accessible HTML-Standard
 */
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});const index=require("./index-f0fde70b.js"),index$1=require("./index-cb2ce614.js"),TREE_ITEM_TAG_NAME="kol-tree-item",KolTreeWc=class{constructor(e){index.registerInstance(this,e),this.state={_label:""},this._label=void 0}validateLabel(e){index$1.validateLabel(this,e)}render(){return index.h(index.Host,{key:"e890882e0c30f2dc33e17e0a7aff0a782b9cbaca",onSlotchange:this.handleSlotchange.bind(this),class:"kol-tree-wc"},index.h("nav",{key:"5c051db3cb3b7fd89aca1a7d2f19a4677c4345b0",class:"tree","aria-label":this.state._label},index.h("ul",{key:"5b5b42abb8b55c468342890ed40e92d5e2bfb82d",class:"treeview-navigation",role:"tree","aria-label":this.state._label},index.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 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,i;const s=await this.getOpenTreeItemElements(),l=null===(t=document.activeElement)||void 0===t?void 0:t.closest("kol-tree-item");if(!s||!l)return;const n=null==s?void 0:s.findIndex((e=>e===l));switch(e.key){case"ArrowDown":await(null===(r=s[n+1])||void 0===r?void 0:r.focusLink()),e.preventDefault();break;case"ArrowUp":await(null===(a=s[n-1])||void 0===a?void 0:a.focusLink()),e.preventDefault();break;case"ArrowRight":await l.expand(),e.preventDefault();break;case"ArrowLeft":await l.collapse(),e.preventDefault();break;case"*":{const e=null===(i=l.parentElement)||void 0===i?void 0:i.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 index.getElement(this)}static get watchers(){return{_label:["validateLabel"]}}};exports.kol_tree_wc=KolTreeWc;