import{__decorate as t}from"tslib";import{css as i,html as s}from"lit";import{property as e,state as n,customElement as h}from"lit/decorators.js";import{N as o}from"../internal/nile-element.esm.js";import"../nile-floating-panel/nile-floating-panel.esm.js";import"../internal/accessibility/a11y.state.enum.esm.js";import"../internal/accessibility/a11y.property.enum.esm.js";import"../internal/accessibility/role.enum.esm.js";import"../nile-floating-panel/nile-floating-panel.css.esm.js";import"../tippy.esm-57628c2b.esm.js";import"../nile-lite-tooltip/utils.esm.js";import"../utilities/visibility-manager.esm.js";var r;const l="nile-context-menu-item",c="nile-context-submenu";let u=0,a=r=class extends o{constructor(){super(...arguments),this.zIndex=9999,this._open=!1,this._parentItem=null,this._proxyId="nile-context-submenu-anchor-"+ ++u,this._menuContainerRef=null,this._setupDone=!1,this._onParentEnter=()=>{this._parentItem?.disabled||(this._clearTimers(),this._open||(this._openTimer=window.setTimeout((()=>this.openSubmenu()),100)))},this._onParentLeave=()=>{null!=this._openTimer&&(clearTimeout(this._openTimer),this._openTimer=void 0),this._open&&(this._closeTimer=window.setTimeout((()=>this.closeSubmenu()),300))},this._onParentClick=t=>{t.stopPropagation(),this._parentItem?.disabled||(this._clearTimers(),this._open?this.closeSubmenu():this.openSubmenu())},this._onPanelEnter=()=>{this._clearTimers()},this._onPanelLeave=()=>{this._open&&(this._closeTimer=window.setTimeout((()=>{r.openStack.some((t=>t!==this&&this._menuContainerRef?.contains(t)))||this.closeSubmenu()}),300))},this._onMenuMouseOver=t=>{const i=t.composedPath().find((t=>t instanceof HTMLElement&&t.tagName.toLowerCase()===l));i&&!i.disabled&&i.focus()},this._onNestedSelect=t=>{t.target!==this&&"click"===t.detail?.type&&this.emit("nile-change",t.detail)},this._onMenuClick=t=>{const i=t.composedPath().find((t=>t instanceof HTMLElement&&t.tagName.toLowerCase()===l));if(!i||i.disabled)return;if(i.querySelector(`:scope > ${c}`))return;const s={id:i.id,value:i.value,name:(i.textContent??"").trim(),target:null,originalEvent:t};try{i.onSelect?.(s)}catch(t){console.error("[nile-context-submenu] onSelect callback threw:",t)}this.emit("nile-change",{type:"click",...s})}}static get styles(){return[i`
        :host { display: contents; }
      `]}connectedCallback(){super.connectedCallback();let t=this.parentElement;for(;t&&t.tagName.toLowerCase()!==l;)t=t.parentElement;this._parentItem=t,this._ensureProxy(),this._parentItem&&(this._parentItem.setSubmenuExpanded?.(!1),this._parentItem.addEventListener("mouseenter",this._onParentEnter),this._parentItem.addEventListener("mouseleave",this._onParentLeave),this._parentItem.addEventListener("click",this._onParentClick,!0)),this._ensureBodyPanel()}_ensureProxy(){if(this._proxyEl)return;const t=document.createElement("div");t.id=this._proxyId,t.setAttribute("aria-hidden","true"),t.style.cssText="position: fixed; top: 0; left: 0; width: 0; height: 0; pointer-events: none;",document.body.appendChild(t),this._proxyEl=t}_syncProxyToParent(){if(!this._proxyEl||!this._parentItem)return;const t=this._parentItem.getBoundingClientRect();this._proxyEl.style.left=`${t.left}px`,this._proxyEl.style.top=`${t.top}px`,this._proxyEl.style.width=`${t.width}px`,this._proxyEl.style.height=`${t.height}px`}disconnectedCallback(){super.disconnectedCallback(),this._parentItem&&(this._parentItem.removeEventListener("mouseenter",this._onParentEnter),this._parentItem.removeEventListener("mouseleave",this._onParentLeave),this._parentItem.removeEventListener("click",this._onParentClick,!0)),this._clearTimers(),this._open&&this.closeSubmenu(),this._teardownBodyArtifacts(),r.openStack=r.openStack.filter((t=>t!==this))}_teardownBodyArtifacts(){const t=this._menuContainerRef;if(t)for(t.removeEventListener("click",this._onMenuClick),t.removeEventListener("mouseover",this._onMenuMouseOver),t.removeEventListener("mouseenter",this._onPanelEnter),t.removeEventListener("mouseleave",this._onPanelLeave),t.removeEventListener("nile-change",this._onNestedSelect);t.firstChild;)this.appendChild(t.firstChild);this._floatingPanelEl?.remove(),this._floatingPanelEl=void 0,this._menuContainerRef=null,this._setupDone=!1,this._proxyEl?.remove(),this._proxyEl=void 0}_ensureBodyPanel(){if(this._setupDone)return;const t=document.createElement("nile-floating-panel");t.setAttribute("for",this._proxyId),t.setAttribute("trigger","manual"),t.setAttribute("placement","right-start"),t.panelClass="nile-context-menu-panel",t.zIndex=this.zIndex,t.interactive=!0,t.hideOnClick=!1,t.closeOnEscape=!1,t.arrow="none",t.distance=0;const i=document.createElement("div");i.className="nile-context-menu__menu",i.setAttribute("role","menu"),t.appendChild(i),document.body.appendChild(t),this._floatingPanelEl=t,this._menuContainerRef=i,i.addEventListener("click",this._onMenuClick),i.addEventListener("mouseover",this._onMenuMouseOver),i.addEventListener("mouseenter",this._onPanelEnter),i.addEventListener("mouseleave",this._onPanelLeave),i.addEventListener("nile-change",this._onNestedSelect),this._relocateLightChildren(),this._setupDone=!0}firstUpdated(){this._relocateLightChildren()}_relocateLightChildren(){if(this._menuContainerRef)for(const t of Array.from(this.children))this._menuContainerRef.appendChild(t)}_clearTimers(){null!=this._openTimer&&(clearTimeout(this._openTimer),this._openTimer=void 0),null!=this._closeTimer&&(clearTimeout(this._closeTimer),this._closeTimer=void 0)}openSubmenu(){this._open||this._parentItem?.disabled||(this._closeSiblingSubmenus(),this._ensureProxy(),this._ensureBodyPanel(),this._syncProxyToParent(),this._open=!0,this._floatingPanelEl&&(this._floatingPanelEl.open=!0),r.openStack.push(this),this._parentItem?.setSubmenuExpanded?.(!0))}closeSubmenu(){if(this._open){for(const t of[...r.openStack])t!==this&&this._menuContainerRef?.contains(t)&&t.closeSubmenu();this._open=!1,this._floatingPanelEl&&(this._floatingPanelEl.open=!1),r.openStack=r.openStack.filter((t=>t!==this)),this._clearTimers(),this._parentItem?.setSubmenuExpanded?.(!1)}}get isOpen(){return this._open}static findByContainer(t){for(const i of r.openStack)if(i._menuContainerRef===t)return i;return null}get parentItem(){return this._parentItem}focusFirstItem(){if(!this._menuContainerRef)return;const t=Array.from(this._menuContainerRef.querySelectorAll(l));for(const i of t){let t=i.parentElement,s=!1;for(;t&&t!==this._menuContainerRef;){if(t.tagName.toLowerCase()===c){s=!0;break}t=t.parentElement}if(!s&&!i.disabled)return void i.focus()}}_closeSiblingSubmenus(){const t=this._parentLevel();for(const i of[...r.openStack])i!==this&&i._parentLevel()===t&&i.closeSubmenu()}_parentLevel(){const t=this._parentItem;return t?t.parentElement?.closest(".nile-context-menu__menu")??null:null}render(){return s``}};a.openStack=[],t([e({attribute:!0,type:Number,reflect:!0})],a.prototype,"zIndex",void 0),t([n()],a.prototype,"_open",void 0),a=r=t([h("nile-context-submenu")],a);export{a as N};
