import{c as t,o as i,s,f as o,a as e,p as a,b as n}from"../floating-ui.dom-cc9126da.esm.js";import{P as l}from"../nile-select/portal-utils.esm.js";class h{constructor(t){this.portalContainer=null,this.originalListboxParent=null,this.measuredPopupHeight=null,this.cleanupAutoUpdate=null,this.currentPlacement="bottom",this.component=t}get portalContainerElement(){return this.portalContainer}createPortalContainer(){const t=document.createElement("div");return t.style.position="absolute",t.style.zIndex="9999",t.style.pointerEvents="none",t.className="nile-combobox-portal",t}async computePosition(){if(!this.portalContainer)return;const n=this.component.combobox||this.component,h=this.portalContainer;try{const r=l.findBoundaryElements(n),c=l.getOptimalPlacement(n),{x:u,y:p,placement:b}=await t(n,h,{placement:c,strategy:"fixed",middleware:[i(4),s({apply:({availableWidth:t,availableHeight:i,elements:s,rects:o})=>{const e=l.calculateOptimalHeight(o.reference,window.innerHeight,this.currentPlacement);s.floating.style.maxWidth=`${t}px`,s.floating.style.maxHeight=`${e}px`,s.floating.style.setProperty("--auto-size-available-width",`${t}px`),s.floating.style.setProperty("--auto-size-available-height",`${e}px`)},padding:10,boundary:r}),o({fallbackPlacements:["bottom","top","bottom-start","top-start"],fallbackStrategy:"bestFit",padding:10,boundary:r}),e({padding:10,crossAxis:!0,boundary:r})],platform:a}),m=n.getBoundingClientRect();Object.assign(h.style,{left:`${u}px`,top:`${p}px`,position:"fixed",pointerEvents:"auto",width:`${m.width}px`}),this.currentPlacement=b;const d=b.split("-")[0];h.className=`nile-combobox-portal combobox__listbox--${d}`}catch{this.fallbackPositioning()}}fallbackPositioning(){if(!this.portalContainer)return;const t=(this.component.combobox||this.component).getBoundingClientRect(),i=window.innerHeight-t.bottom,s=t.top;let o,e,a;s>i?(e=Math.max(s-20,100),o=Math.max(t.top-e-4,10),a="top"):(e=Math.max(i-20,100),o=t.bottom+4,a="bottom"),Object.assign(this.portalContainer.style,{left:`${t.left}px`,top:`${o}px`,width:`${t.width}px`,maxHeight:`${e}px`,pointerEvents:"auto"}),this.portalContainer.className=`nile-combobox-portal combobox__listbox--${a}`,this.portalContainer.style.setProperty("--auto-size-available-height",`${e}px`),this.portalContainer.style.setProperty("--auto-size-available-width",`${t.width}px`)}extractStylesAsCSS(t){return"string"==typeof t?t:Array.isArray(t)?t.map((t=>this.extractStylesAsCSS(t))).join("\n"):t&&"object"==typeof t&&t.cssText?t.cssText:""}injectStyles(){if(!this.portalContainer)return;const t=`nile-combobox-styles-${Math.random().toString(36).substring(2,11)}`;if(document.getElementById(t))return;const i=this.component.constructor.styles;if(!i)return;const s=document.createElement("style");s.id=t,s.textContent=this.extractStylesAsCSS(i),document.head.appendChild(s),this.portalContainer.t=t}setupPortal(){this.component.portal&&this.component.updateComplete.then((()=>{const t=this.component.shadowRoot?.querySelector("#listbox");t&&(this.originalListboxParent=t.parentElement,this.portalContainer=this.createPortalContainer(),this.portalContainer.appendChild(t),document.body.appendChild(this.portalContainer),this.injectStyles(),t.style.display="",this.computePosition(),this.cleanupAutoUpdate=n(this.component,this.portalContainer,(()=>this.computePosition()),{ancestorScroll:!0,ancestorResize:!0,elementResize:!0,layoutShift:!0,animationFrame:!0}))}))}cleanupPortal(){if(this.cleanupAutoUpdate&&(this.cleanupAutoUpdate(),this.cleanupAutoUpdate=null),this.portalContainer&&this.portalContainer.parentNode){const t=this.portalContainer.querySelector("#listbox");t&&this.originalListboxParent&&(this.originalListboxParent.appendChild(t),t.style.display=this.component.portal?"none":"");const i=this.portalContainer.t;i&&document.getElementById(i)?.remove(),this.portalContainer.parentNode.removeChild(this.portalContainer)}this.portalContainer=null,this.originalListboxParent=null,this.measuredPopupHeight=null,this.currentPlacement="bottom"}updatePosition(){this.component.portal&&this.portalContainer&&this.computePosition()}resetMeasuredHeight(){this.measuredPopupHeight=null}async resetScrollPosition(){await this.component.updateComplete,requestAnimationFrame((()=>{let t=null;if(t=this.component.portal&&this.portalContainer?this.portalContainer.querySelector("#listbox"):this.component.shadowRoot?.querySelector("#listbox"),!t||!t.isConnected)return;t.scrollTop=0;const i=t.querySelector(".virtualized");if(i&&i.isConnected){this.component.filteredData?.length<5?(i.style.overflowY="hidden",i.style.maxHeight="none",t.style.overflowY="hidden",t.style.maxHeight="fit-content"):(i.style.overflowY="auto",i.style.maxHeight="",t.style.overflowY="auto",t.style.maxHeight=""),i.scrollTop=0}const s=t.querySelector("lit-virtualizer");s&&s.isConnected&&(s.scrollTop=0)}))}}export{h as C};
