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