import{g as X,p as H,a0 as J,L as A,n as Q,E as V,ah as Y,ae as oe,C as Z,a8 as re,K as T,a2 as K,J as W,i as E,a7 as $,cW as ue,cX as ce,a6 as D,R as ie,O as ve,cY as fe,bZ as de,t as me,a1 as he,o as ge,s as _,q as pe,r as ye,b$ as Se,a3 as we,Q as Ve}from"./index-BRUnowC8.js";const Te=H({renderless:Boolean,...Z()},"VVirtualScrollItem"),Ie=X()({name:"VVirtualScrollItem",inheritAttrs:!1,props:Te(),emits:{"update:height":l=>!0},setup(l,a){let{attrs:v,emit:s,slots:n}=a;const{resizeRef:o,contentRect:c}=J(void 0,"border");A(()=>c.value?.height,g=>{g!=null&&s("update:height",g)}),Q(()=>l.renderless?V(Y,null,[n.default?.({itemRef:o})]):V("div",oe({ref:o,class:["v-virtual-scroll__item",l.class],style:l.style},v),[n.default?.()]))}}),Re=-1,be=1,C=100,Oe=H({itemHeight:{type:[Number,String],default:null},itemKey:{type:[String,Array,Function],default:null},height:[Number,String]},"virtual");function Pe(l,a){const v=re(),s=T(0);K(()=>{s.value=parseFloat(l.itemHeight||0)});const n=T(0),o=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:F,contentRect:O}=J();K(()=>{F.value=u.value});const y=E(()=>u.value===document.documentElement?v.height.value:O.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 f=T(0);let P=-1;function j(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)+j(r-1);f.value=Math.max(f.value,performance.now()-e)},f),G=A(M,e=>{e&&(G(),I=m.value.offsetTop,k.immediate(),S(),~P&&ie(()=>{ve&&window.requestAnimationFrame(()=>{U(P),P=-1})}))});$(()=>{k.clear()});function ee(e,t){const r=h[e],d=s.value;s.value=d?Math.min(s.value,t):t,(r!==t||d!==s.value)&&(h[e]=t,k())}function p(e){e=D(e,0,a.value.length);const t=Math.floor(e),r=e%1,d=t+1,b=i[t]||0,w=i[d]||b;return b+(w-b)*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),d=D(N(r),0,a.value.length),b=e+y.value+C,w=D(N(b)+1,d+1,a.value.length);if((t!==Re||d<n.value)&&(t!==be||w>o.value)){const ne=p(n.value)-p(d),se=p(w)-p(o.value);Math.max(ne,se)>C?(n.value=d,o.value=w):(d<=0&&(n.value=d),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?P=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 v=l.length-1,s=0,n=0,o=null,c=-1;if(l[v]<a)return v;for(;s<=v;)if(n=s+v>>1,o=l[n],o>a)v=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,...Oe(),...Z(),...Se()},"VVirtualScroll"),Fe=X()({name:"VVirtualScroll",props:xe(),setup(l,a){let{slots:v}=a;const s=fe("VVirtualScroll"),{dimensionStyles:n}=de(l),{calculateVisibleItems:o,containerRef:c,markerRef:g,handleScroll:u,handleScrollend:m,handleItemResize:I,scrollToIndex:F,paddingTop:O,paddingBottom:y,computedItems:M}=Pe(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)}),$(h)}),Q(()=>{const h=M.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(Y,null,[V("div",{ref:g,class:"v-virtual-scroll__spacer",style:{paddingTop:_(O.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:_(O.value),paddingBottom:_(y.value)}},[h])])}),{calculateVisibleItems:o,scrollToIndex:F}}});export{Fe as V};
