!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).FloatingUICore={})}(this,(function(t){"use strict";const e=["top","right","bottom","left"],n=["start","end"],i=e.reduce(((t,e)=>t.concat(e,e+"-"+n[0],e+"-"+n[1])),[]),r=Math.min,o=Math.max,l={left:"right",right:"left",bottom:"top",top:"bottom"};function a(t,e,n){return o(t,r(e,n))}function s(t,e){return"function"==typeof t?t(e):t}function f(t){return t.split("-")[0]}function c(t){return t.split("-")[1]}function u(t){return"x"===t?"y":"x"}function m(t){return"y"===t?"height":"width"}function d(t){const e=t[0];return"t"===e||"b"===e?"y":"x"}function g(t){return u(d(t))}function p(t,e,n){void 0===n&&(n=!1);const i=c(t),r=g(t),o=m(r);let l="x"===r?i===(n?"end":"start")?"right":"left":"start"===i?"bottom":"top";return e.reference[o]>e.floating[o]&&(l=A(l)),[l,A(l)]}function h(t){return t.includes("start")?t.replace("start","end"):t.replace("end","start")}const y=["left","right"],w=["right","left"],x=["top","bottom"],v=["bottom","top"];function b(t,e,n,i){const r=c(t);let o=function(t,e,n){switch(t){case"top":case"bottom":return n?e?w:y:e?y:w;case"left":case"right":return e?x:v;default:return[]}}(f(t),"start"===n,i);return r&&(o=o.map((t=>t+"-"+r)),e&&(o=o.concat(o.map(h)))),o}function A(t){const e=f(t);return l[e]+t.slice(e.length)}function R(t){return"number"!=typeof t?function(t){var e,n,i,r;return{top:null!=(e=t.top)?e:0,right:null!=(n=t.right)?n:0,bottom:null!=(i=t.bottom)?i:0,left:null!=(r=t.left)?r:0}}(t):{top:t,right:t,bottom:t,left:t}}function O(t){const{x:e,y:n,width:i,height:r}=t;return{width:i,height:r,top:n,left:e,right:e+i,bottom:n+r,x:e,y:n}}function P(t,e,n){let{reference:i,floating:r}=t;const o=d(e),l=g(e),a=m(l),s=f(e),u="y"===o,p=i.x+i.width/2-r.width/2,h=i.y+i.height/2-r.height/2,y=i[a]/2-r[a]/2;let w;switch(s){case"top":w={x:p,y:i.y-r.height};break;case"bottom":w={x:p,y:i.y+i.height};break;case"right":w={x:i.x+i.width,y:h};break;case"left":w={x:i.x-r.width,y:h};break;default:w={x:i.x,y:i.y}}const x=c(e);return x&&(w[l]+=y*("end"===x?1:-1)*(n&&u?-1:1)),w}async function T(t,e){var n;void 0===e&&(e={});const{x:i,y:r,platform:o,rects:l,elements:a,strategy:f}=t,{boundary:c="clippingAncestors",rootBoundary:u="viewport",elementContext:m="floating",altBoundary:d=!1,padding:g=0}=s(e,t),p=R(g),h=a[d?"floating"===m?"reference":"floating":m],y=O(await o.getClippingRect({element:null==(n=await(null==o.isElement?void 0:o.isElement(h)))||n?h:h.contextElement||await(null==o.getDocumentElement?void 0:o.getDocumentElement(a.floating)),boundary:c,rootBoundary:u,strategy:f})),w="floating"===m?{x:i,y:r,width:l.floating.width,height:l.floating.height}:l.reference,x=await(null==o.getOffsetParent?void 0:o.getOffsetParent(a.floating)),v=await(null==o.isElement?void 0:o.isElement(x))&&await(null==o.getScale?void 0:o.getScale(x))||{x:1,y:1},b=O(o.convertOffsetParentRelativeRectToViewportRelativeRect?await o.convertOffsetParentRelativeRectToViewportRelativeRect({elements:a,rect:w,offsetParent:x,strategy:f}):w);return{top:(y.top-b.top+p.top)/v.y,bottom:(b.bottom-y.bottom+p.bottom)/v.y,left:(y.left-b.left+p.left)/v.x,right:(b.right-y.right+p.right)/v.x}}function D(t,e){return{top:t.top-e.height,right:t.right-e.width,bottom:t.bottom-e.height,left:t.left-e.width}}function E(t){return e.some((e=>t[e]>=0))}function L(t){const e=r(...t.map((t=>t.left))),n=r(...t.map((t=>t.top)));return{x:e,y:n,width:o(...t.map((t=>t.right)))-e,height:o(...t.map((t=>t.bottom)))-n}}const k=new Set(["left","top"]);t.arrow=t=>({name:"arrow",options:t,async fn(e){const{x:n,y:i,placement:o,rects:l,platform:f,elements:u,middlewareData:d}=e,{element:p,padding:h=0}=s(t,e)||{};if(null==p)return{};const y=R(h),w={x:n,y:i},x=g(o),v=m(x),b=await f.getDimensions(p),A="y"===x,O=A?"top":"left",P=A?"bottom":"right",T=A?"clientHeight":"clientWidth",D=l.reference[v]+l.reference[x]-w[x]-l.floating[v],E=w[x]-l.reference[x],L=await(null==f.getOffsetParent?void 0:f.getOffsetParent(p));let k=L?L[T]:0;k&&await(null==f.isElement?void 0:f.isElement(L))||(k=u.floating[T]||l.floating[v]);const C=D/2-E/2,H=k/2-b[v]/2-1,S=r(y[O],H),B=r(y[P],H),F=k-b[v]-B,j=k/2-b[v]/2+C,z=a(S,j,F),M=!d.arrow&&null!=c(o)&&j!==z&&l.reference[v]/2-(j<S?S:B)-b[v]/2<0,V=M?j<S?j-S:j-F:0;return{[x]:w[x]+V,data:{[x]:z,centerOffset:j-z-V,...M&&{alignmentOffset:V}},reset:M}}}),t.autoPlacement=function(t){return void 0===t&&(t={}),{name:"autoPlacement",options:t,async fn(e){var n,r,o;const{rects:l,middlewareData:a,placement:u,platform:m,elements:d}=e,{crossAxis:g=!1,alignment:y,allowedPlacements:w=i,autoAlignment:x=!0,...v}=s(t,e),b=void 0!==y||w===i?function(t,e,n){return(t?[...n.filter((e=>c(e)===t)),...n.filter((e=>c(e)!==t))]:n.filter((t=>f(t)===t))).filter((n=>!t||c(n)===t||!!e&&h(n)!==n))}(y||null,x,w):w,A=(null==(n=a.autoPlacement)?void 0:n.index)||0,R=b[A];if(null==R)return{};if(u!==R)return{reset:{placement:b[0]}};const O=await m.detectOverflow(e,v),P=p(R,l,await(null==m.isRTL?void 0:m.isRTL(d.floating))),T=[O[f(R)],O[P[0]],O[P[1]]],D=[...(null==(r=a.autoPlacement)?void 0:r.overflows)||[],{placement:R,overflows:T}],E=b[A+1];if(E)return{data:{index:A+1,overflows:D},reset:{placement:E}};const L=D.map((t=>{const e=c(t.placement);return[t.placement,e&&g?t.overflows.slice(0,2).reduce(((t,e)=>t+e),0):t.overflows[0],t.overflows]})).sort(((t,e)=>t[1]-e[1])),k=(null==(o=L.filter((t=>t[2].slice(0,c(t[0])?2:3).every((t=>t<=0))))[0])?void 0:o[0])||L[0][0];return k!==u?{data:{index:A+1,overflows:D},reset:{placement:k}}:{}}}},t.computePosition=async(t,e,n)=>{const{placement:i="bottom",strategy:r="absolute",middleware:o=[],platform:l}=n,a=l.detectOverflow?l:{...l,detectOverflow:T},s=await(null==l.isRTL?void 0:l.isRTL(e));let f=await l.getElementRects({reference:t,floating:e,strategy:r}),{x:c,y:u}=P(f,i,s),m=i,d=0;const g={};for(let n=0;n<o.length;n++){const p=o[n];if(!p)continue;const{name:h,fn:y}=p,{x:w,y:x,data:v,reset:b}=await y({x:c,y:u,initialPlacement:i,placement:m,strategy:r,middlewareData:g,rects:f,platform:a,elements:{reference:t,floating:e}});c=null!=w?w:c,u=null!=x?x:u,g[h]={...g[h],...v},b&&d<50&&(d++,"object"==typeof b&&(b.placement&&(m=b.placement),b.rects&&(f=!0===b.rects?await l.getElementRects({reference:t,floating:e,strategy:r}):b.rects),({x:c,y:u}=P(f,m,s))),n=-1)}return{x:c,y:u,placement:m,strategy:r,middlewareData:g}},t.detectOverflow=T,t.flip=function(t){return void 0===t&&(t={}),{name:"flip",options:t,async fn(e){var n,i;const{placement:r,middlewareData:o,rects:l,initialPlacement:a,platform:c,elements:u}=e,{mainAxis:m=!0,crossAxis:g=!0,fallbackPlacements:y,fallbackStrategy:w="bestFit",fallbackAxisSideDirection:x="none",flipAlignment:v=!0,...R}=s(t,e);if(null!=(n=o.arrow)&&n.alignmentOffset)return{};const O=f(r),P=d(a),T=f(a)===a,D=await(null==c.isRTL?void 0:c.isRTL(u.floating)),E=y||(T||!v?[A(a)]:function(t){const e=A(t);return[h(t),e,h(e)]}(a)),L="none"!==x;!y&&L&&E.push(...b(a,v,x,D));const k=[a,...E],C=await c.detectOverflow(e,R),H=[];let S=(null==(i=o.flip)?void 0:i.overflows)||[];if(m&&H.push(C[O]),g){const t=p(r,l,D);H.push(C[t[0]],C[t[1]])}if(S=[...S,{placement:r,overflows:H}],!H.every((t=>t<=0))){var B,F;const t=((null==(B=o.flip)?void 0:B.index)||0)+1,e=k[t];if(e){if(!("alignment"===g&&P!==d(e))||S.every((t=>d(t.placement)!==P||t.overflows[0]>0)))return{data:{index:t,overflows:S},reset:{placement:e}}}let n=null==(F=S.filter((t=>t.overflows[0]<=0)).sort(((t,e)=>t.overflows[1]-e.overflows[1]))[0])?void 0:F.placement;if(!n)switch(w){case"bestFit":{var j;const t=null==(j=S.filter((t=>{if(L){const e=d(t.placement);return e===P||"y"===e}return!0})).map((t=>[t.placement,t.overflows.filter((t=>t>0)).reduce(((t,e)=>t+e),0)])).sort(((t,e)=>t[1]-e[1]))[0])?void 0:j[0];t&&(n=t);break}case"initialPlacement":n=a}if(r!==n)return{reset:{placement:n}}}return{}}}},t.hide=function(t){return void 0===t&&(t={}),{name:"hide",options:t,async fn(e){const{rects:n,platform:i}=e,{strategy:r="referenceHidden",...o}=s(t,e);switch(r){case"referenceHidden":{const t=D(await i.detectOverflow(e,{...o,elementContext:"reference"}),n.reference);return{data:{referenceHiddenOffsets:t,referenceHidden:E(t)}}}case"escaped":{const t=D(await i.detectOverflow(e,{...o,altBoundary:!0}),n.floating);return{data:{escapedOffsets:t,escaped:E(t)}}}default:return{}}}}},t.inline=function(t){return void 0===t&&(t={}),{name:"inline",options:t,async fn(e){const{placement:n,elements:i,rects:l,platform:a,strategy:c}=e,{padding:u=2,x:m,y:g}=s(t,e),p=Array.from(await(null==a.getClientRects?void 0:a.getClientRects(i.reference))||[]);if(!p.length)return{};const h=function(t){const e=t.slice().sort(((t,e)=>t.y-e.y)),n=[];let i=null;for(let t=0;t<e.length;t++){const r=e[t];!i||r.y-i.y>i.height/2?n.push([r]):n[n.length-1].push(r),i=r}return n.map((t=>O(L(t))))}(p),y=O(L(p)),w=R(u);const x=await a.getElementRects({reference:{getBoundingClientRect:function(){if(2===h.length&&(h[0].left>h[1].right||h[1].left>h[0].right)&&null!=m&&null!=g)return h.find((t=>m>t.left-w.left&&m<t.right+w.right&&g>t.top-w.top&&g<t.bottom+w.bottom))||y;if(h.length>=2){if("y"===d(n)){const t=h[0],e=h[h.length-1],i="top"===f(n),r=t.top,o=e.bottom,l=i?t.left:e.left;return O({x:l,y:r,width:(i?t.right:e.right)-l,height:o-r})}const t="left"===f(n),e=o(...h.map((t=>t.right))),i=r(...h.map((t=>t.left))),l=h.filter((n=>t?n.left===i:n.right===e)),a=l[0].top,s=l[l.length-1].bottom;return O({x:i,y:a,width:e-i,height:s-a})}return y}},floating:i.floating,strategy:c});return l.reference.x!==x.reference.x||l.reference.y!==x.reference.y||l.reference.width!==x.reference.width||l.reference.height!==x.reference.height?{reset:{rects:x}}:{}}}},t.limitShift=function(t){return void 0===t&&(t={}),{options:t,fn(e){var n,i;const{x:r,y:o,placement:l,rects:a,middlewareData:c}=e,{offset:m=0,mainAxis:g=!0,crossAxis:p=!0}=s(t,e),h={x:r,y:o},y=d(l),w=u(y);let x=h[w],v=h[y];const b=s(m,e),A="number"==typeof b?{mainAxis:b,crossAxis:0}:{mainAxis:null!=(n=b.mainAxis)?n:0,crossAxis:null!=(i=b.crossAxis)?i:0};if(g){const t="y"===w?"height":"width",e=a.reference[w]-a.floating[t]+A.mainAxis,n=a.reference[w]+a.reference[t]-A.mainAxis;x<e?x=e:x>n&&(x=n)}if(p){var R,O;const t="y"===w?"width":"height",e=k.has(f(l)),n=a.reference[y]-a.floating[t]+(e&&(null==(R=c.offset)?void 0:R[y])||0)+(e?0:A.crossAxis),i=a.reference[y]+a.reference[t]+(e?0:(null==(O=c.offset)?void 0:O[y])||0)-(e?A.crossAxis:0);v<n?v=n:v>i&&(v=i)}return{[w]:x,[y]:v}}}},t.offset=function(t){return void 0===t&&(t=0),{name:"offset",options:t,async fn(e){var n,i;const{x:r,y:o,placement:l,middlewareData:a}=e,u=await async function(t,e){const{placement:n,platform:i,elements:r}=t,o=await(null==i.isRTL?void 0:i.isRTL(r.floating)),l=f(n),a=c(n),u="y"===d(n),m=k.has(l)?-1:1,g=o&&u?-1:1,p=s(e,t);let{mainAxis:h,crossAxis:y,alignmentAxis:w}="number"==typeof p?{mainAxis:p,crossAxis:0,alignmentAxis:null}:{mainAxis:p.mainAxis||0,crossAxis:p.crossAxis||0,alignmentAxis:p.alignmentAxis};return a&&"number"==typeof w&&(y="end"===a?-1*w:w),u?{x:y*g,y:h*m}:{x:h*m,y:y*g}}(e,t);return l===(null==(n=a.offset)?void 0:n.placement)&&null!=(i=a.arrow)&&i.alignmentOffset?{}:{x:r+u.x,y:o+u.y,data:{...u,placement:l}}}}},t.rectToClientRect=O,t.shift=function(t){return void 0===t&&(t={}),{name:"shift",options:t,async fn(e){const{x:n,y:i,placement:r,platform:o}=e,{mainAxis:l=!0,crossAxis:f=!1,limiter:c={fn:t=>{let{x:e,y:n}=t;return{x:e,y:n}}},...m}=s(t,e),g={x:n,y:i},p=await o.detectOverflow(e,m),h=d(r),y=u(h);let w=g[y],x=g[h];const v=(t,e)=>a(e+p["y"===t?"top":"left"],e,e-p["y"===t?"bottom":"right"]);l&&(w=v(y,w)),f&&(x=v(h,x));const b=c.fn({...e,[y]:w,[h]:x});return{...b,data:{x:b.x-n,y:b.y-i,enabled:{[y]:l,[h]:f}}}}}},t.size=function(t){return void 0===t&&(t={}),{name:"size",options:t,async fn(e){const{placement:n,rects:i,platform:l,elements:a}=e,{apply:u=()=>{},...m}=s(t,e),g=await l.detectOverflow(e,m),p=f(n),h=c(n),y="y"===d(n),{width:w,height:x}=i.floating;let v,b;"top"===p||"bottom"===p?(v=p,b=h===(await(null==l.isRTL?void 0:l.isRTL(a.floating))?"start":"end")?"left":"right"):(b=p,v="end"===h?"top":"bottom");const A=x-g.top-g.bottom,R=w-g.left-g.right,O=r(x-g[v],A),P=r(w-g[b],R),T=e.middlewareData.shift,D=!T;let E=O,L=P;null!=T&&T.enabled.x&&(L=R),null!=T&&T.enabled.y&&(E=A),D&&!h&&(y?L=w-2*o(g.left,g.right):E=x-2*o(g.top,g.bottom)),await u({...e,availableWidth:L,availableHeight:E});const k=await l.getDimensions(a.floating);return w!==k.width||x!==k.height?{reset:{rects:!0}}:{}}}}}));
