import{r as y,h as b,H as k,an as m,am as L,m as E}from"./index-BeCeuELg.js";import{v as A}from"./label-3757689e-D7ErDa5R.js";const f=class{constructor(e){y(this,e),this.state={_label:""},this._label=void 0}validateLabel(e){A(this,e)}render(){return b(k,{key:"1787e04f7c274d06626ba519287830ee0dfac428",onSlotchange:this.handleSlotchange.bind(this),class:"kol-tree-wc"},b("nav",{key:"531cecb03fb89162726d16d21b422b623c3a274f",class:"tree","aria-label":this.state._label},b("ul",{key:"8901ad4bbf1a08b301d1dba576f473c3189738e8",class:"treeview-navigation",role:"tree","aria-label":this.state._label},b("slot",{key:"2a1c6f38ad5a0ea45dbc28db982ccc354f3918d2"}))))}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,n,o,d,h,c,u;const a=await this.getOpenTreeItemElements(),l=(i=document.activeElement)===null||i===void 0?void 0:i.closest(m),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(v=>v===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(),v=a.indexOf(l)+1,g=a.concat(a),_=g.slice(v,v+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[v+_].focusLink(),e.preventDefault())}break}case"*":{const r=(u=l.parentElement)===null||u===void 0?void 0:u.querySelectorAll(m);r?.forEach(v=>{v.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(f.isTreeItem))!==null&&d!==void 0?d:[];for(const c of h){if(c._active)return c;const u=c.querySelector(`${m}[_active="true"]`);if(u&&u._active)return u}},i=s=>{f.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{f as kol_tree_wc};
//# sourceMappingURL=kol-tree-wc.entry-DqazPB3B.js.map
