/*!
 * KoliBri - The accessible HTML-Standard
 */
import{proxyCustomElement,HTMLElement,h,Host}from"@stencil/core/internal/client";import{x as KolTreeItemTag,w as KolTreeTag}from"./component-names.js";import"./common.js";import{v as validateLabel}from"./label.js";const KolTreeWc$1=proxyCustomElement(class e extends HTMLElement{constructor(e){super(),!1!==e&&this.__registerHost(),this.state={_label:""}}validateLabel(e){validateLabel(this,e)}render(){return h(Host,{key:"c491bc228b1db3a9707ee892a6c307ce675bde4e",onSlotchange:this.handleSlotchange.bind(this)},h("nav",{key:"50bbc945f1302a0ddd8376076e61823a0b123aa3",class:"kol-tree","aria-label":this.state._label},h("ul",{key:"3d915f2ef9fe71b126b2778cc093e91a7907f010",class:"kol-tree__treeview-navigation",role:"tree","aria-label":this.state._label},h("slot",{key:"a97a01e4eb25a47c667531d99db716d47e404779"}))))}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,o,s,r,c,d;const u=await this.getOpenTreeItemElements(),v=null===(t=document.activeElement)||void 0===t?void 0:t.closest(KolTreeItemTag),m=e.metaKey||e.altKey||e.ctrlKey||e.shiftKey;if(!u||!v)return;const h=null==u?void 0:u.findIndex(e=>e===v);switch(e.key){case"ArrowDown":await(null===(l=u[h+1])||void 0===l?void 0:l.focus()),e.preventDefault();break;case"ArrowUp":await(null===(a=u[h-1])||void 0===a?void 0:a.focus()),e.preventDefault();break;case"Right":case"ArrowRight":e.preventDefault(),await v.isOpen()?await(null===(i=u[h+1])||void 0===i?void 0:i.focus()):await v.expand();break;case"Left":case"ArrowLeft":if(e.preventDefault(),await v.isOpen())await v.collapse();else{const e=u.findIndex(e=>e===v.parentElement);-1!==e&&await(null===(n=u[e])||void 0===n?void 0:n.focus())}break;case"Home":await(null===(o=u[0])||void 0===o?void 0:o.focus()),e.preventDefault();break;case"End":await(null===(s=u[u.length-1])||void 0===s?void 0:s.focus()),e.preventDefault();break;case null===(r=e.key.match(/^[a-zA-Z0-9]$/))||void 0===r?void 0:r.input:if(!m){const t=e.key.toLowerCase(),l=u.indexOf(v)+1,a=u.concat(u),i=a.slice(l,l+u.length).findIndex(e=>{var l;return null===(l=e.getAttribute("_label"))||void 0===l?void 0:l.trim().toLowerCase().startsWith(t)});-1!==i&&(await(null===(c=a[l+i])||void 0===c?void 0:c.focus()),e.preventDefault())}break;case"*":{const e=null===(d=v.parentElement)||void 0===d?void 0:d.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"]}}},[260,"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};