import{m as U,x as K,c as W,a as re,u as oe}from"./VListItem-0e8aff4c.js";import{l as D,m as G,L as F,f as S,F as X,E as se,a5 as ue,K as R,at as q,r as L,z,as as Z,aO as B,aZ as ce,T as ie,aK as ve,aG as fe,y as de,aq as me,C as A,o as he}from"./index-2cc203e3.js";import{i as ge}from"./VMenu-d17aeffc.js";const pe=D({renderless:Boolean,...U()},"VVirtualScrollItem"),Se=G()({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}=K(void 0,"border");F(()=>{var u;return(u=v.value)==null?void 0:u.height},u=>{u!=null&&o("update:height",u)}),W(()=>{var u,s;return l.renderless?S(X,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,E=100,Te=D({itemHeight:{type:[Number,String],default:null},height:[Number,String]},"virtual");function Ie(l,a){const i=ue(),o=R(0);q(()=>{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=L(),g=L();let w=0;const{resizeRef:b,contentRect:O}=K();q(()=>{b.value=s.value});const T=z(()=>{var e;return s.value===document.documentElement?i.height.value:((e=O.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 $(e){return f[e]||o.value}const _=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)+$(d-1);m.value=Math.max(m.value,performance.now()-e)},m),j=F(V,e=>{e&&(j(),w=g.value.offsetTop,_.immediate(),I(),~y&&ie(()=>{ve&&window.requestAnimationFrame(()=>{N(y),y=-1})}))});Z(()=>{_.clear()});function J(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,_())}function p(e){return e=B(e,0,a.value.length-1),c[e]||0}function H(e){return Re(c,e)}let k=0,x=0,M=0;F(T,(e,r)=>{r&&(I(),e<r&&requestAnimationFrame(()=>{x=0,I()}))});function Q(){if(!s.value||!g.value)return;const e=s.value.scrollTop,r=performance.now();r-M>500?(x=Math.sign(e-k),w=g.value.offsetTop):x=e-k,k=e,M=r,I()}function Y(){!s.value||!g.value||(x=0,M=0,I())}let C=-1;function I(){cancelAnimationFrame(C),C=requestAnimationFrame(ee)}function ee(){if(!s.value||!T.value)return;const e=k-w,r=Math.sign(x),d=Math.max(0,e-E),h=B(H(d),0,a.value.length),le=e+T.value+E,P=B(H(le)+1,h+1,a.value.length);if((r!==Ve||h<t.value)&&(r!==ye||P>n.value)){const ae=p(t.value)-p(h),ne=p(P)-p(n.value);Math.max(ae,ne)>E?(t.value=h,n.value=P):(h<=0&&(t.value=h),P>=a.value.length&&(n.value=P))}v.value=p(t.value),u.value=p(a.value.length)-p(n.value)}function N(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 F(a,()=>{f=Array.from({length:a.value.length}),c=Array.from({length:a.value.length}),_.immediate(),I()},{deep:!0}),{containerRef:s,markerRef:g,computedItems:te,paddingTop:v,paddingBottom:u,scrollToIndex:N,handleScroll:Q,handleScrollend:Y,handleItemResize:J}}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=D({items:{type:Array,default:()=>[]},renderless:Boolean,...Te(),...U(),...re()},"VVirtualScroll"),_e=G()({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:b,paddingBottom:O,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)}),Z(V)}),W(()=>{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(X,null,[S("div",{ref:v,class:"v-virtual-scroll__spacer",style:{paddingTop:A(b.value)}},null),V,S("div",{class:"v-virtual-scroll__spacer",style:{paddingBottom:A(O.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:A(b.value),paddingBottom:A(O.value)}},[V])])}),{scrollToIndex:w}}});export{_e as V};
