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