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