import{r as y,h as m,H as k,ar as f,aq as L,g as E}from"./index-DscFWUNz.js";import{v as A}from"./label-CdtNxOKu-DuSxxbJz.js";/*!
 * KoliBri - The accessible HTML-Standard
 */const b=class{constructor(e){y(this,e),this.state={_label:""}}validateLabel(e){A(this,e)}render(){return m(k,{key:"774db0e9ae59f0de739b7df7777f74fa8e07be03",onSlotchange:this.handleSlotchange.bind(this)},m("nav",{key:"a7f95daedbd9e25f5b1ee82a16aa495c8ac1b7c1",class:"kol-tree","aria-label":this.state._label},m("ul",{key:"9a3686682fd2a7e625dba024eb0ace8caff1aee7",class:"kol-tree__treeview-navigation",role:"tree","aria-label":this.state._label},m("slot",{key:"a6f63218223e1daf2f96fd2dbcf49e87742a419d"}))))}static isTreeItem(e){return e?.tagName===f.toUpperCase()}componentWillLoad(){this.validateLabel(this._label),this.handleTreeChange(),this.observeChildListMutations()}disconnectedCallback(){var e;(e=this.observer)===null||e===void 0||e.disconnect()}observeChildListMutations(){this.observer=new MutationObserver(this.handleTreeChange.bind(this)),this.observeTopLevelItems()}handleSlotchange(){this.observeTopLevelItems(),this.handleTreeChange()}observeTopLevelItems(){var e;(e=this.getTopLevelTreeItems())===null||e===void 0||e.forEach(i=>{var t;(t=this.observer)===null||t===void 0||t.observe(i,{childList:!0,subtree:!0})})}getTopLevelTreeItems(){var e,i,t;return(t=(i=(e=this.host.querySelector("slot"))===null||e===void 0?void 0:e.assignedNodes)===null||i===void 0?void 0:i.call(e))===null||t===void 0?void 0:t.filter(b.isTreeItem)}handleTreeChange(){this.treeItemElements=this.getTreeItemElements(),this.ensureActiveItemVisibility()}getTreeItemElements(){var e;return(e=this.getTopLevelTreeItems())===null||e===void 0?void 0:e.reduce((i,t)=>{const s=t.querySelectorAll(f);return[...i,t,...s]},[])}async getOpenTreeItemElements(){if(!this.treeItemElements)return;const e=async t=>b.isTreeItem(t.parentElement)?await t.parentElement.isOpen()&&await e(t.parentElement):!0;return(await Promise.all(this.treeItemElements.map(async t=>({value:t,include:await e(t)})))).filter(t=>t.include).map(t=>t.value)}async handleKeyDown(e){var i,t,s,n,o,d,h,c,v;const a=await this.getOpenTreeItemElements(),l=(i=document.activeElement)===null||i===void 0?void 0:i.closest(f),w=e.metaKey||e.altKey||e.ctrlKey||e.shiftKey;if(!a||!l)return;const p=a?.findIndex(r=>r===l);switch(e.key){case"ArrowDown":{await((t=a[p+1])===null||t===void 0?void 0:t.focusLink()),e.preventDefault();break}case"ArrowUp":{await((s=a[p-1])===null||s===void 0?void 0:s.focusLink()),e.preventDefault();break}case"Right":case"ArrowRight":{e.preventDefault(),await l.isOpen()?await((n=a[p+1])===null||n===void 0?void 0:n.focusLink()):await l.expand();break}case"Left":case"ArrowLeft":{if(e.preventDefault(),await l.isOpen())await l.collapse();else{const r=a.findIndex(u=>u===l.parentElement);r!==-1&&await((o=a[r])===null||o===void 0?void 0:o.focusLink())}break}case"Home":{await((d=a[0])===null||d===void 0?void 0:d.focusLink()),e.preventDefault();break}case"End":{await((h=a[a.length-1])===null||h===void 0?void 0:h.focusLink()),e.preventDefault();break}case((c=e.key.match(/^[a-zA-Z0-9]$/))===null||c===void 0?void 0:c.input):{if(!w){const r=e.key.toLowerCase(),u=a.indexOf(l)+1,g=a.concat(a),_=g.slice(u,u+a.length).findIndex(T=>{var I;return(I=T.getAttribute("_label"))===null||I===void 0?void 0:I.trim().toLowerCase().startsWith(r)});_!==-1&&(await g[u+_].focusLink(),e.preventDefault())}break}case"*":{const r=(v=l.parentElement)===null||v===void 0?void 0:v.querySelectorAll(f);r?.forEach(u=>{u.expand()});break}}}async handleFocusOut(e){e.relatedTarget&&!e.relatedTarget.closest(L)&&await this.ensureActiveItemVisibility()}async ensureActiveItemVisibility(){const e=()=>{var s,n,o,d;const h=(d=(o=(n=(s=this.host.querySelector("slot"))===null||s===void 0?void 0:s.assignedNodes)===null||n===void 0?void 0:n.call(s))===null||o===void 0?void 0:o.filter(b.isTreeItem))!==null&&d!==void 0?d:[];for(const c of h){if(c._active)return c;const v=c.querySelector(`${f}[_active="true"]`);if(v&&v._active)return v}},i=s=>{b.isTreeItem(s.parentElement)&&(s.parentElement.expand(),i(s.parentElement))},t=e();t&&i(t)}get host(){return E(this)}static get watchers(){return{_label:["validateLabel"]}}};export{b as kol_tree_wc};
//# sourceMappingURL=kol-tree-wc.entry-CDVBvDDx.js.map
