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