import{top,left,right,bottom,start}from"../enums.js";import getBasePlacement from"../utils/getBasePlacement.js";import getMainAxisFromPlacement from"../utils/getMainAxisFromPlacement.js";import getAltAxis from"../utils/getAltAxis.js";import{within,withinMaxClamp}from"../utils/within.js";import getLayoutRect from"../dom-utils/getLayoutRect.js";import getOffsetParent from"../dom-utils/getOffsetParent.js";import detectOverflow from"../utils/detectOverflow.js";import getVariation from"../utils/getVariation.js";import getFreshSideObject from"../utils/getFreshSideObject.js";import{min as mathMin,max as mathMax}from"../utils/math.js";function preventOverflow(t){var e,i,a,o,r,s,n,m,l,f=t.state,p=t.options,t=t.name,d=p.mainAxis,d=void 0===d||d,g=p.altAxis,g=void 0!==g&&g,c=p.boundary,h=p.rootBoundary,u=p.altBoundary,x=p.padding,w=p.tether,w=void 0===w||w,p=p.tetherOffset,p=void 0===p?0:p,c=detectOverflow(f,{boundary:c,rootBoundary:h,padding:x,altBoundary:u}),h=getBasePlacement(f.placement),x=getVariation(f.placement),u=!x,A=getMainAxisFromPlacement(h),y=getAltAxis(A),O=f.modifiersData.popperOffsets,j=f.rects.reference,v=f.rects.popper,p="function"==typeof p?p(Object.assign({},f.rects,{placement:f.placement})):p,p="number"==typeof p?{mainAxis:p,altAxis:p}:Object.assign({mainAxis:0,altAxis:0},p),b=f.modifiersData.offset?f.modifiersData.offset[f.placement]:null,M={x:0,y:0};O&&(d&&(d="y"===A?"height":"width",m=(e=O[A])+c[o="y"===A?top:left],l=e-c[i="y"===A?bottom:right],a=w?-v[d]/2:0,s=(x===start?j:v)[d],x=x===start?-v[d]:-j[d],n=f.elements.arrow,n=w&&n?getLayoutRect(n):{width:0,height:0},o=(r=f.modifiersData["arrow#persistent"]?f.modifiersData["arrow#persistent"].padding:getFreshSideObject())[o],r=r[i],i=within(0,j[d],n[d]),n=u?j[d]/2-a-i-o-p.mainAxis:s-i-o-p.mainAxis,s=u?-j[d]/2+a+i+r+p.mainAxis:x+i+r+p.mainAxis,u=(o=f.elements.arrow&&getOffsetParent(f.elements.arrow))?"y"===A?o.clientTop||0:o.clientLeft||0:0,x=e+s-(a=null!=(d=null==b?void 0:b[A])?d:0),i=within(w?mathMin(m,e+n-a-u):m,e,w?mathMax(l,x):l),O[A]=i,M[A]=i-e),g&&(r="y"===y?"height":"width",s=(o=O[y])+c["x"===A?top:left],d=o-c["x"===A?bottom:right],n=-1!==[top,left].indexOf(h),u=null!=(a=null==b?void 0:b[y])?a:0,m=n?s:o-j[r]-v[r]-u+p.altAxis,x=n?o+j[r]+v[r]-u-p.altAxis:d,l=w&&n?withinMaxClamp(m,o,x):within(w?m:s,o,w?x:d),O[y]=l,M[y]=l-o),f.modifiersData[t]=M)}export default{name:"preventOverflow",enabled:!0,phase:"main",fn:preventOverflow,requiresIfExists:["offset"]};