import{slicedToArray as r}from"../../../_virtual/_rollupPluginBabelHelpers.js";import{useState as n,useRef as e,useCallback as t}from"react";import u from"../ssr/useIsomorphicLayoutEffect.js";function i(i){var c=i.containerRef,o=i.gap,l=i.deductedSpaceRef,f=i.itemRefs,a=i.minVisibleCount,s=void 0===a?0:a,d=n(0),h=r(d,2),v=h[0],g=h[1],m=n(!1),p=r(m,2),b=p[0],R=p[1],y=e([]),C=e(0),M=e(!1),w=t((function(){var r=null==c?void 0:c.current;if(r&&f.length){for(var n=r.offsetWidth-C.current,e=0,t=0,u=Math.min(f.length,y.current.length),i=0;u>i;i++){var l=y.current[i],a=i>0?l+o:l;if(e+a>n)break;e+=a,t++}g(Math.max(t,Math.min(s,u)))}else g(f.length)}),[c,f,o,s]),B=t((function(){C.current=(null==l?void 0:l.current)?l.current.getBoundingClientRect().width:0}),[l]),j=t((function(){if(!c.current||!f.length)return g(f.length),void R(!0);B();var r=f.map((function(r){return r.current})).filter((function(r){return null!==r}));if(0===r.length)return g(0),y.current=[],void R(!0);y.current=r.map((function(r){return r.getBoundingClientRect().width})),w(),R(!0)}),[c,f,w,B]),x=t((function(){M.current||(M.current=!0,requestAnimationFrame((function(){try{j()}finally{M.current=!1}})))}),[j]);return u((function(){if(c.current){var r=new ResizeObserver((function(){f.length>0?y.current.length?(B(),w()):x():(g(0),y.current=[])}));return r.observe(c.current),(null==l?void 0:l.current)&&r.observe(l.current),function(){return r.disconnect()}}}),[c,l,B,w,x,f]),u((function(){f.length>0?(R(!1),j()):(g(0),y.current=[],R(!0))}),[f,j]),{visibleCount:v,hasMeasured:b}}export{i as default};
//# sourceMappingURL=useItemsOverflow.js.map
