/*!
 * KoliBri - The accessible HTML-Standard
 */
import{proxyCustomElement,HTMLElement,h,Host}from"@stencil/core/internal/client";import{q as KolTreeItemTag,p as KolTreeTag}from"./component-names.js";import{v as validateLabel}from"./label.js";const 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:"7ed304596f8d9f4ab353c881aa3e7a058d23b1cc",onSlotchange:this.handleSlotchange.bind(this)},h("nav",{key:"53d283e8c52792eb96d540a364cd13f6e504a433",class:"kol-tree","aria-label":this.state._label},h("ul",{key:"c2ce2459919900ee430507a5599ff09d54638376",class:"kol-tree__treeview-navigation",role:"tree","aria-label":this.state._label},h("slot",{key:"cba291c46b0bbfe699b154b0333932a1829ff03e"}))))}static isTreeItem(e){return(null==e?void 0:e.tagName)===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 t,l,a;return null===(a=null===(l=null===(t=this.host.querySelector("slot"))||void 0===t?void 0:t.assignedNodes)||void 0===l?void 0:l.call(t))||void 0===a?void 0:a.filter(e.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 l=t.querySelectorAll(KolTreeItemTag);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,i,n,s,o,r,c;const d=await this.getOpenTreeItemElements(),u=null===(t=document.activeElement)||void 0===t?void 0:t.closest(KolTreeItemTag);if(!d||!u)return;const v=null==d?void 0:d.findIndex((e=>e===u));switch(e.key){case"ArrowDown":await(null===(l=d[v+1])||void 0===l?void 0:l.focusLink()),e.preventDefault();break;case"ArrowUp":await(null===(a=d[v-1])||void 0===a?void 0:a.focusLink()),e.preventDefault();break;case"Right":case"ArrowRight":e.preventDefault(),await u.isOpen()?await(null===(i=d[v+1])||void 0===i?void 0:i.focusLink()):await u.expand();break;case"Left":case"ArrowLeft":if(e.preventDefault(),await u.isOpen())await u.collapse();else{const e=d.findIndex((e=>e===u.parentElement));-1!==e&&await(null===(n=d[e])||void 0===n?void 0:n.focusLink())}break;case"Home":await(null===(s=d[0])||void 0===s?void 0:s.focusLink()),e.preventDefault();break;case"End":await(null===(o=d[d.length-1])||void 0===o?void 0:o.focusLink()),e.preventDefault();break;case null===(r=e.key.match(/[a-zA-Z0-9]/))||void 0===r?void 0:r.input:{const t=e.key.toLowerCase(),l=d.indexOf(u)+1,a=d.concat(d),i=a.slice(l,l+d.length).findIndex((e=>{var l;return null===(l=e.getAttribute("_label"))||void 0===l?void 0:l.trim().toLowerCase().startsWith(t)}));-1!==i&&(await a[l+i].focusLink(),e.preventDefault());break}case"*":{const e=null===(c=u.parentElement)||void 0===c?void 0:c.querySelectorAll(KolTreeItemTag);null==e||e.forEach((e=>{e.expand()}));break}}}async handleFocusOut(e){e.relatedTarget&&!e.relatedTarget.closest(KolTreeTag)&&await this.ensureActiveItemVisibility()}async ensureActiveItemVisibility(){const t=l=>{e.isTreeItem(l.parentElement)&&(l.parentElement.expand(),t(l.parentElement))},l=(()=>{var t,l,a,i;const n=null!==(i=null===(a=null===(l=null===(t=this.host.querySelector("slot"))||void 0===t?void 0:t.assignedNodes)||void 0===l?void 0:l.call(t))||void 0===a?void 0:a.filter(e.isTreeItem))&&void 0!==i?i:[];for(const e of n){if(e._active)return e;const t=e.querySelector(`${KolTreeItemTag}[_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};