import{g as J,p as H,a0 as Q,L as A,n as X,E as V,ah as j,ae as oe,C as G,a8 as re,K as T,a2 as K,J as W,i as E,a7 as Y,d7 as ue,d8 as ce,a6 as D,R as ie,O as de,d9 as ve,ca as fe,t as me,a1 as he,o as ge,s as _,q as pe,r as ye,cc as Se,a3 as we,Q as Ve}from"./index-D-FY8Yx2.js";const Te=H({renderless:Boolean,...G()},"VVirtualScrollItem"),Ie=J()({name:"VVirtualScrollItem",inheritAttrs:!1,props:Te(),emits:{"update:height":l=>!0},setup(l,a){let{attrs:d,emit:s,slots:n}=a;const{resizeRef:o,contentRect:c}=Q(void 0,"border");A(()=>c.value?.height,g=>{g!=null&&s("update:height",g)}),X(()=>l.renderless?V(j,null,[n.default?.({itemRef:o})]):V("div",oe({ref:o,class:["v-virtual-scroll__item",l.class],style:l.style},d),[n.default?.()]))}}),Re=-1,Oe=1,C=100,Pe=H({itemHeight:{type:[Number,String],default:null},itemKey:{type:[String,Array,Function],default:null},height:[Number,String]},"virtual");function be(l,a){const d=re(),s=T(0);K(()=>{s.value=parseFloat(l.itemHeight||0)});const n=T(0),o=T(Math.ceil((parseInt(l.height)||d.height.value)/(s.value||16))||1),c=T(0),g=T(0),u=W(),m=W();let I=0;const{resizeRef:F,contentRect:P}=Q();K(()=>{F.value=u.value});const y=E(()=>u.value===document.documentElement?d.height.value:P.value?.height||parseInt(l.height)||0),M=E(()=>!!(u.value&&m.value&&y.value&&s.value));let h=Array.from({length:a.value.length}),i=Array.from({length:a.value.length});const v=T(0);let b=-1;function Z(e){return h[e]||s.value}const k=ue(()=>{const e=performance.now();i[0]=0;const t=a.value.length;for(let r=1;r<=t;r++)i[r]=(i[r-1]||0)+Z(r-1);v.value=Math.max(v.value,performance.now()-e)},v),$=A(M,e=>{e&&($(),I=m.value.offsetTop,k.immediate(),S(),~b&&ie(()=>{de&&window.requestAnimationFrame(()=>{U(b),b=-1})}))});Y(()=>{k.clear()});function ee(e,t){const r=h[e],f=s.value;s.value=f?Math.min(s.value,t):t,(r!==t||f!==s.value)&&(h[e]=t,k())}function p(e){e=D(e,0,a.value.length);const t=Math.floor(e),r=e%1,f=t+1,O=i[t]||0,w=i[f]||O;return O+(w-O)*r}function N(e){return ke(i,e)}let x=0,R=0,z=0;A(y,(e,t)=>{t&&(S(),e<t&&requestAnimationFrame(()=>{R=0,S()}))});let B=-1;function te(){if(!u.value||!m.value)return;const e=u.value.scrollTop,t=performance.now();t-z>500?(R=Math.sign(e-x),I=m.value.offsetTop):R=e-x,x=e,z=t,window.clearTimeout(B),B=window.setTimeout(q,500),S()}function q(){!u.value||!m.value||(R=0,z=0,window.clearTimeout(B),S())}let L=-1;function S(){cancelAnimationFrame(L),L=requestAnimationFrame(le)}function le(){if(!u.value||!y.value||!s.value)return;const e=x-I,t=Math.sign(R),r=Math.max(0,e-C),f=D(N(r),0,a.value.length),O=e+y.value+C,w=D(N(O)+1,f+1,a.value.length);if((t!==Re||f<n.value)&&(t!==Oe||w>o.value)){const ne=p(n.value)-p(f),se=p(w)-p(o.value);Math.max(ne,se)>C?(n.value=f,o.value=w):(f<=0&&(n.value=f),w>=a.value.length&&(o.value=w))}c.value=p(n.value),g.value=p(a.value.length)-p(o.value)}function U(e){const t=p(e);!u.value||e&&!t?b=e:u.value.scrollTop=t}const ae=E(()=>a.value.slice(n.value,o.value).map((e,t)=>{const r=t+n.value;return{raw:e,index:r,key:ce(e,l.itemKey,r)}}));return A(a,()=>{h=Array.from({length:a.value.length}),i=Array.from({length:a.value.length}),k.immediate(),S()},{deep:1}),{calculateVisibleItems:S,containerRef:u,markerRef:m,computedItems:ae,paddingTop:c,paddingBottom:g,scrollToIndex:U,handleScroll:te,handleScrollend:q,handleItemResize:ee}}function ke(l,a){let d=l.length-1,s=0,n=0,o=null,c=-1;if(l[d]<a)return d;for(;s<=d;)if(n=s+d>>1,o=l[n],o>a)d=n-1;else if(o<a)c=n,s=n+1;else return o===a?n:s;return c}const xe=H({items:{type:Array,default:()=>[]},renderless:Boolean,...Pe(),...G(),...Se()},"VVirtualScroll"),Fe=J()({name:"VVirtualScroll",props:xe(),setup(l,a){let{slots:d}=a;const s=ve("VVirtualScroll"),{dimensionStyles:n}=fe(l),{calculateVisibleItems:o,containerRef:c,markerRef:g,handleScroll:u,handleScrollend:m,handleItemResize:I,scrollToIndex:F,paddingTop:P,paddingBottom:y,computedItems:M}=be(l,me(()=>l.items));return he(()=>l.renderless,()=>{function h(){const v=(arguments.length>0&&arguments[0]!==void 0?arguments[0]:!1)?"addEventListener":"removeEventListener";c.value===document.documentElement?(document[v]("scroll",u,{passive:!0}),document[v]("scrollend",m)):(c.value?.[v]("scroll",u,{passive:!0}),c.value?.[v]("scrollend",m))}we(()=>{c.value=Ve(s.vnode.el,!0),h(!0)}),Y(h)}),X(()=>{const h=M.value.map(i=>ge(Ie,{key:i.key,renderless:l.renderless,"onUpdate:height":v=>I(i.index,v)},{default:v=>d.default?.({item:i.raw,index:i.index,...v})}));return l.renderless?V(j,null,[V("div",{ref:g,class:"v-virtual-scroll__spacer",style:{paddingTop:_(P.value)}},null),h,V("div",{class:"v-virtual-scroll__spacer",style:{paddingBottom:_(y.value)}},null)]):V("div",{ref:c,class:ye(["v-virtual-scroll",l.class]),onScrollPassive:u,onScrollend:m,style:pe([n.value,l.style])},[V("div",{ref:g,class:"v-virtual-scroll__container",style:{paddingTop:_(P.value),paddingBottom:_(y.value)}},[h])])}),{calculateVisibleItems:o,scrollToIndex:F}}});export{Fe as V};
