import{r as E,h as b,H as k,ar as m,aq as L,g as A}from"./index-DCi8lcS0.js";import{v as x}from"./label-pIlPsS_A-DDeB9XCl.js";const f=class{constructor(e){E(this,e),this.state={_label:""}}validateLabel(e){x(this,e)}render(){return b(k,{key:"c491bc228b1db3a9707ee892a6c307ce675bde4e",onSlotchange:this.handleSlotchange.bind(this)},b("nav",{key:"50bbc945f1302a0ddd8376076e61823a0b123aa3",class:"kol-tree","aria-label":this.state._label},b("ul",{key:"3d915f2ef9fe71b126b2778cc093e91a7907f010",class:"kol-tree__treeview-navigation",role:"tree","aria-label":this.state._label},b("slot",{key:"a97a01e4eb25a47c667531d99db716d47e404779"}))))}static isTreeItem(e){return e?.tagName===m.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(f.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(m);return[...i,t,...s]},[])}async getOpenTreeItemElements(){if(!this.treeItemElements)return;const e=async t=>f.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,o,n,d,h,c,v,p;const a=await this.getOpenTreeItemElements(),l=(i=document.activeElement)===null||i===void 0?void 0:i.closest(m),T=e.metaKey||e.altKey||e.ctrlKey||e.shiftKey;if(!a||!l)return;const I=a?.findIndex(r=>r===l);switch(e.key){case"ArrowDown":{await((t=a[I+1])===null||t===void 0?void 0:t.focus()),e.preventDefault();break}case"ArrowUp":{await((s=a[I-1])===null||s===void 0?void 0:s.focus()),e.preventDefault();break}case"Right":case"ArrowRight":{e.preventDefault(),await l.isOpen()?await((o=a[I+1])===null||o===void 0?void 0:o.focus()):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((n=a[r])===null||n===void 0?void 0:n.focus())}break}case"Home":{await((d=a[0])===null||d===void 0?void 0:d.focus()),e.preventDefault();break}case"End":{await((h=a[a.length-1])===null||h===void 0?void 0:h.focus()),e.preventDefault();break}case((c=e.key.match(/^[a-zA-Z0-9]$/))===null||c===void 0?void 0:c.input):{if(!T){const r=e.key.toLowerCase(),u=a.indexOf(l)+1,_=a.concat(a),w=_.slice(u,u+a.length).findIndex(y=>{var g;return(g=y.getAttribute("_label"))===null||g===void 0?void 0:g.trim().toLowerCase().startsWith(r)});w!==-1&&(await((v=_[u+w])===null||v===void 0?void 0:v.focus()),e.preventDefault())}break}case"*":{const r=(p=l.parentElement)===null||p===void 0?void 0:p.querySelectorAll(m);r?.forEach(u=>{u.expand()});break}}}async handleFocusOut(e){e.relatedTarget&&!e.relatedTarget.closest(L)&&await this.ensureActiveItemVisibility()}async ensureActiveItemVisibility(){const e=()=>{var s,o,n,d;const h=(d=(n=(o=(s=this.host.querySelector("slot"))===null||s===void 0?void 0:s.assignedNodes)===null||o===void 0?void 0:o.call(s))===null||n===void 0?void 0:n.filter(f.isTreeItem))!==null&&d!==void 0?d:[];for(const c of h){if(c._active)return c;const v=c.querySelector(`${m}[_active="true"]`);if(v&&v._active)return v}},i=s=>{f.isTreeItem(s.parentElement)&&(s.parentElement.expand(),i(s.parentElement))},t=e();t&&i(t)}get host(){return A(this)}static get watchers(){return{_label:["validateLabel"]}}};export{f as kol_tree_wc};
//# sourceMappingURL=kol-tree-wc.entry-BcNVd93B.js.map
