import{slicedToArray as t}from"../../_virtual/_rollupPluginBabelHelpers.js";import{jsx as i}from"react/jsx-runtime";import{forwardRef as r,useState as e,useMemo as o,useRef as n,useCallback as l,useEffect as s}from"react";import{noop as a}from"lodash-es";import c from"classnames";import{VariableSizeList as u}from"react-window";import f from"react-virtualized-auto-sizer";import d from"../../hooks/usePrevious/index.js";import m from"../../hooks/useThrottledCallback.js";import v from"../../hooks/useMergeRefs.js";import{getNormalizedItems as h,getMaxOffset as p,easeInOutQuint as I,getOnItemsRenderedData as g,isLayoutDirectionScrollbarVisible as T}from"../../services/virtualized-service.js";import{getTestId as S}from"../../tests/testIds.js";import{ComponentDefaultTestId as O}from"../../tests/constants.js";import"./VirtualizedList.scss.js";var b=r((function(r,b){var z=r.className,j=r.id,y=r.items,C=void 0===y?[]:y,R=r.itemRenderer,w=void 0===R?function(t,i,r){return t}:R,x=r.getItemHeight,F=void 0===x?function(t,i){return t.height}:x,L=r.getItemSize,V=void 0===L?null:L,A=r.layout,D=void 0===A?"vertical":A,k=r.onScroll,M=r.overscanCount,N=void 0===M?0:M,U=r.getItemId,_=void 0===U?function(t,i){return t.id}:U,q=r.scrollToId,E=r.scrollDuration,H=void 0===E?200:E,P=r.onScrollToFinished,Z=void 0===P?a:P,B=r.onItemsRendered,W=r.onItemsRenderedThrottleMs,G=void 0===W?200:W,J=r.onSizeUpdate,K=void 0===J?a:J,Q=r.onVerticalScrollbarVisiblityChange,X=void 0===Q?null:Q,Y=r.onLayoutDirectionScrollbarVisibilityChange,$=void 0===Y?null:Y,tt=r.virtualListRef,it=r.scrollableClassName,rt=r.role,et=r.style,ot=e(0),nt=t(ot,2),lt=nt[0],st=nt[1],at=e(0),ct=t(at,2),ut=ct[0],ft=ct[1],dt="horizontal"!==D,mt=o((function(){return dt?lt:ut}),[dt,lt,ut]),vt=d(q),ht=n(null),pt=n(null),It=n(null),gt=n(0),Tt=n({initialized:!1,scrollOffsetInitial:0,scrollOffsetFinal:0,animationStartTime:0}),St=v({refs:[b,ht]}),Ot=v({refs:[tt,It]}),bt=Tt.current;bt.initialized||(bt.initialized=!0,bt.scrollOffsetInitial=0,bt.scrollOffsetFinal=0,bt.animationStartTime=0);var zt=l((function(t,i){var r=(V||F)(t,i);return void 0===r&&console.error("Couldn't get height for item: ",t),r}),[F,V]),jt=l((function(t,i){var r=_(t,i);return void 0===r&&console.error("Couldn't get id for item: ",t),r}),[_]),yt=o((function(){return h(C,jt,zt)}),[C,jt,zt]),Ct=o((function(){return p(mt,yt)}),[mt,yt]),Rt=l((function(t){var i=t.scrollDirection,r=t.scrollOffset,e=t.scrollUpdateWasRequested;gt.current=r,e||(bt.scrollOffsetInitial=r),k&&k(i,r,e)}),[k,gt,bt]),wt=l((function(){requestAnimationFrame((function(){var t=performance.now()-bt.animationStartTime,i=bt.scrollOffsetFinal-bt.scrollOffsetInitial,r=I(Math.min(1,t/H)),e=Math.min(Ct,bt.scrollOffsetInitial+i*r);gt.current=e,It.current.scrollTo(e),H>t?wt():(bt.animationStartTime=void 0,Z&&Z())}))}),[H,bt,It,Ct,Z]),xt=l((function(t){var i=t.offsetTop;bt.animationStartTime?bt.scrollOffsetFinal=i:bt.scrollOffsetInitial!==i?(bt.scrollOffsetFinal=i,bt.animationStartTime=performance.now(),wt()):Z&&Z()}),[bt,wt,Z]),Ft=l((function(t){var i=t.index;return w(C[i],i,t.style)}),[C,w]),Lt=l((function(t){return zt(C[t],t)}),[C,zt]),Vt=l((function(t,i){i===lt&&t===ut||setTimeout((function(){st(i),ft(t),K(t,i)}),0)}),[lt,ut,K]),At=m((function(t){if(B){var i=g(C,yt,jt,t.visibleStartIndex,t.visibleStopIndex,mt,gt.current);B(i)}}),{wait:G,trailing:!0},[B,C,yt,jt,mt]);return s((function(){if(q&&vt!==q){var t=T(C,yt,jt,mt),i=yt[q];t&&i&&xt(i)}}),[vt,q,xt,yt,C,jt,mt]),s((function(){It.current&&It.current.resetAfterIndex(0)}),[yt]),s((function(){var t=$||X;if(t){var i=T(C,yt,jt,mt);pt.current!==i&&(pt.current=i,t(i))}}),[$,X,C,yt,mt,jt]),i("div",Object.assign({ref:St,className:c("virtualized-list--wrapper",z),id:j,role:rt,"data-testid":S(O.VIRTUALIZED_LIST,j),style:et},{children:i(f,{children:function(t){var r=t.height,e=t.width;return Vt(e,r),i(u,Object.assign({ref:Ot,height:r,width:e,itemCount:C.length,itemSize:Lt,onScroll:Rt,layout:D,overscanCount:N,onItemsRendered:At,className:c("virtualized-list-scrollable-container",it)},{children:Ft}))}})}))}));Object.assign(b,{defaultTestId:O.VIRTUALIZED_LIST});var z=b;export{z as default};
