/*!
 * KoliBri - The accessible HTML-Standard
 */
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});const index=require("./index-0c932844.js"),componentNames=require("./component-names-72b76dc7.js"),label=require("./label-b8654499.js");require("./prop.validators-96dfedce.js"),require("./index-38f5f0b2.js"),require("./dev.utils-58fde089.js");const KolTreeWc=class{constructor(e){index.registerInstance(this,e),this.state={_label:""},this._label=void 0}validateLabel(e){label.validateLabel(this,e)}render(){return index.h(index.Host,{key:"fc79fee1595fed55d37dc2fb4f3509ff6c16686b",onSlotchange:this.handleSlotchange.bind(this),class:"kol-tree-wc"},index.h("nav",{key:"d8d4800627e61c9766150d1d9a440889cf122440",class:"tree","aria-label":this.state._label},index.h("ul",{key:"40b1ee8c5d89febb2975168f5630fdd01b6e4e97",class:"treeview-navigation",role:"tree","aria-label":this.state._label},index.h("slot",{key:"86f66e7236df8aa03d942450c06a2f2f5930b987"}))))}static isTreeItem(e){return(null==e?void 0:e.tagName)===componentNames.KolTreeItemTag.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(KolTreeWc.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(componentNames.KolTreeItemTag);return[...e,t,...a]}),[])}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,a,i,l,n,s,r,o,d;const c=await this.getOpenTreeItemElements(),v=null===(t=document.activeElement)||void 0===t?void 0:t.closest(componentNames.KolTreeItemTag);if(!c||!v)return;const u=null==c?void 0:c.findIndex((e=>e===v));switch(e.key){case"ArrowDown":await(null===(a=c[u+1])||void 0===a?void 0:a.focusLink()),e.preventDefault();break;case"ArrowUp":await(null===(i=c[u-1])||void 0===i?void 0:i.focusLink()),e.preventDefault();break;case"Right":case"ArrowRight":e.preventDefault(),await v.isOpen()?await(null===(l=c[u+1])||void 0===l?void 0:l.focusLink()):await v.expand();break;case"Left":case"ArrowLeft":if(e.preventDefault(),await v.isOpen())await v.collapse();else{const e=c.findIndex((e=>e===v.parentElement));-1!==e&&await(null===(n=c[e])||void 0===n?void 0:n.focusLink())}break;case"Home":await(null===(s=c[0])||void 0===s?void 0:s.focusLink()),e.preventDefault();break;case"End":await(null===(r=c[c.length-1])||void 0===r?void 0:r.focusLink()),e.preventDefault();break;case null===(o=e.key.match(/[a-zA-Z0-9]/))||void 0===o?void 0:o.input:{const t=e.key.toLowerCase(),a=c.indexOf(v)+1,i=c.concat(c),l=i.slice(a,a+c.length).findIndex((e=>{var a;return null===(a=e.getAttribute("_label"))||void 0===a?void 0:a.trim().toLowerCase().startsWith(t)}));-1!==l&&(await i[a+l].focusLink(),e.preventDefault());break}case"*":{const e=null===(d=v.parentElement)||void 0===d?void 0:d.querySelectorAll(componentNames.KolTreeItemTag);null==e||e.forEach((e=>{e.expand()}));break}}}async handleFocusOut(e){e.relatedTarget&&!e.relatedTarget.closest(componentNames.KolTreeTag)&&await this.ensureActiveItemVisibility()}async ensureActiveItemVisibility(){const e=t=>{KolTreeWc.isTreeItem(t.parentElement)&&(t.parentElement.expand(),e(t.parentElement))},t=(()=>{var e,t,a,i;const l=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(KolTreeWc.isTreeItem))&&void 0!==i?i:[];for(const e of l){if(e._active)return e;const t=e.querySelector(`${componentNames.KolTreeItemTag}[_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;