import{autoUpdate as Z,flip as ee,inner as te,offset as ne,shift as le,size as re,useFloating as oe,useInnerOffset as ie,useInteractions as se}from"@floating-ui/react";import*as j from"react";import{createContext as _,useCallback as ae,useContext as R,useMemo as v,useRef as ue,useState as E}from"react";import{useDisposables as fe}from'../hooks/use-disposables.js';import{useEvent as z}from'../hooks/use-event.js';import{useIsoMorphicEffect as A}from'../hooks/use-iso-morphic-effect.js';let y=_({styles:void 0,setReference:()=>{},setFloating:()=>{},getReferenceProps:()=>({}),getFloatingProps:()=>({}),slot:{}});y.displayName="FloatingContext";let S=_(null);S.displayName="PlacementContext";function xe(e){return v(()=>e?typeof e=="string"?{to:e}:e:null,[e])}function ye(){return R(y).setReference}function Fe(){return R(y).getReferenceProps}function be(){let{getFloatingProps:e,slot:t}=R(y);return ae((...n)=>Object.assign({},e(...n),{"data-anchor":t.anchor}),[e,t])}function Re(e=null){e===!1&&(e=null),typeof e=="string"&&(e={to:e});let t=R(S),n=v(()=>e,[JSON.stringify(e,typeof HTMLElement!="undefined"?(r,o)=>o instanceof HTMLElement?o.outerHTML:o:void 0)]);A(()=>{t==null||t(n!=null?n:null)},[t,n]);let l=R(y);return v(()=>[l.setFloating,e?l.styles:{}],[l.setFloating,e,l.styles])}let q=4;function ve({children:e,enabled:t=!0}){let[n,l]=E(null),[r,o]=E(0),c=ue(null),[u,s]=E(null);pe(u);let i=t&&n!==null&&u!==null,{to:F="bottom",gap:C=0,offset:M=0,padding:p=0,inner:P}=ce(n,u),[a,f="center"]=F.split(" ");A(()=>{i&&o(0)},[i]);let{refs:b,floatingStyles:w,context:g}=oe({open:i,placement:a==="selection"?f==="center"?"bottom":`bottom-${f}`:f==="center"?`${a}`:`${a}-${f}`,strategy:"absolute",transform:!1,middleware:[ne({mainAxis:a==="selection"?0:C,crossAxis:M}),le({padding:p}),a!=="selection"&&ee({padding:p}),a==="selection"&&P?te({...P,padding:p,overflowRef:c,offset:r,minItemsVisible:q,referenceOverflowThreshold:p,onFallbackChange(h){var O,W;if(!h)return;let d=g.elements.floating;if(!d)return;let T=parseFloat(getComputedStyle(d).scrollPaddingBottom)||0,$=Math.min(q,d.childElementCount),B=0,N=0;for(let m of(W=(O=g.elements.floating)==null?void 0:O.childNodes)!=null?W:[])if(m instanceof HTMLElement){let x=m.offsetTop,k=x+m.clientHeight+T,H=d.scrollTop,U=H+d.clientHeight;if(x>=H&&k<=U)$--;else{N=Math.max(0,Math.min(k,U)-Math.max(x,H)),B=m.clientHeight;break}}$>=1&&o(m=>{let x=B*$-N+T;return m>=x?m:x})}}):null,re({padding:p,apply({availableWidth:h,availableHeight:d,elements:T}){Object.assign(T.floating.style,{overflow:"auto",maxWidth:`${h}px`,maxHeight:`min(var(--anchor-max-height, 100vh), ${d}px)`})}})].filter(Boolean),whileElementsMounted:Z}),[I=a,V=f]=g.placement.split("-");a==="selection"&&(I="selection");let G=v(()=>({anchor:[I,V].filter(Boolean).join(" ")}),[I,V]),K=ie(g,{overflowRef:c,onChange:o}),{getReferenceProps:Q,getFloatingProps:X}=se([K]),Y=z(h=>{s(h),b.setFloating(h)});return j.createElement(S.Provider,{value:l},j.createElement(y.Provider,{value:{setFloating:Y,setReference:b.setReference,styles:w,getReferenceProps:Q,getFloatingProps:X,slot:G}},e))}function pe(e){A(()=>{if(!e)return;let t=new MutationObserver(()=>{let n=window.getComputedStyle(e).maxHeight,l=parseFloat(n);if(isNaN(l))return;let r=parseInt(n);isNaN(r)||l!==r&&(e.style.maxHeight=`${Math.ceil(l)}px`)});return t.observe(e,{attributes:!0,attributeFilter:["style"]}),()=>{t.disconnect()}},[e])}function ce(e,t){var o,c,u;let n=L((o=e==null?void 0:e.gap)!=null?o:"var(--anchor-gap, 0)",t),l=L((c=e==null?void 0:e.offset)!=null?c:"var(--anchor-offset, 0)",t),r=L((u=e==null?void 0:e.padding)!=null?u:"var(--anchor-padding, 0)",t);return{...e,gap:n,offset:l,padding:r}}function L(e,t,n=void 0){let l=fe(),r=z((s,i)=>{if(s==null)return[n,null];if(typeof s=="number")return[s,null];if(typeof s=="string"){if(!i)return[n,null];let F=J(s,i);return[F,C=>{let M=D(s);{let p=M.map(P=>window.getComputedStyle(i).getPropertyValue(P));l.requestAnimationFrame(function P(){l.nextFrame(P);let a=!1;for(let[b,w]of M.entries()){let g=window.getComputedStyle(i).getPropertyValue(w);if(p[b]!==g){p[b]=g,a=!0;break}}if(!a)return;let f=J(s,i);F!==f&&(C(f),F=f)})}return l.dispose}]}return[n,null]}),o=v(()=>r(e,t)[0],[e,t]),[c=o,u]=E();return A(()=>{let[s,i]=r(e,t);if(u(s),!!i)return i(u)},[e,t]),c}function D(e){let t=/var\((.*)\)/.exec(e);if(t){let n=t[1].indexOf(",");if(n===-1)return[t[1]];let l=t[1].slice(0,n).trim(),r=t[1].slice(n+1).trim();return r?[l,...D(r)]:[l]}return[]}function J(e,t){let n=document.createElement("div");t.appendChild(n),n.style.setProperty("margin-top","0px","important"),n.style.setProperty("margin-top",e,"important");let l=parseFloat(window.getComputedStyle(n).marginTop)||0;return t.removeChild(n),l}export{ve as FloatingProvider,Re as useFloatingPanel,be as useFloatingPanelProps,ye as useFloatingReference,Fe as useFloatingReferenceProps,xe as useResolvedAnchor};
