import{n as e,t}from"./objectSpread2.C2EWlzLj.js";import{a as n,r}from"./dist.EDwYBRuO.js";import{computed as i,createElementBlock as a,createElementVNode as o,guardReactiveProps as s,mergeProps as c,nextTick as l,normalizeProps as u,normalizeStyle as d,onMounted as f,openBlock as p,reactive as m,ref as h,renderSlot as g,unref as _}from"/vue.js";e();var v=Object.assign({name:`NcVirtualScroll`},{__name:`nc-virtual-scroll`,props:{data:{type:Array,default:()=>[]},dataKey:{type:String,default:`id`},buffer:{type:Number,default:25},height:{type:String,default:`10em`},preCalc:{type:Number,default:0}},emits:[`scroll-change`],setup(e,{emit:v}){let y=v,b=e,x,S=h([]),C=h(),w=h(),T=m({}),E=h(30),D=h(0),O=h(b.buffer),k=i(()=>(console.log(`🚀 ~ file: nc-virtual-scroll.vue ~ props.data:`,b.data.length),S.value=[],b.data)),A=i(()=>{let e=0;return k.value.map((t,n)=>{let r=e,i=T[t[b.dataKey]]||E.value;return e+=i,Object.assign(t,{_topOffset:r,_index:n,_height:i})})}),j=i(()=>{let e=A.value.slice(D.value,O.value+1);return y(`scroll-change`,D.value,O.value,e.length,S.value.length),S.value.length>0&&(e=e.concat(S.value)),e}),M=i(()=>{var e;return(e=A.value[D.value])==null?void 0:e._topOffset}),N=i(()=>{var e,t;return((e=A.value[A.value.length-1])==null?void 0:e._topOffset)+((t=A.value[A.value.length-1])==null?void 0:t._height)}),P=i(()=>{var e;return N.value-((e=A.value[O.value])==null?void 0:e._topOffset)}),F={};function I(e=!1){let t=Array.from(w.value.children);!t||t.length===0||(t.forEach(e=>{L(e)}),e&&R())}function L(e){e.hasAttribute(`attachedResize`)||(e.setAttribute(`attachedResize`,``),x==null||x.observe(e)),T[e.id]!==e.offsetHeight&&e.id!==`skip`&&(F[e.id]=e.offsetHeight)}function R(){Object.keys(F).length>0&&(Object.assign(T,F),F={})}let{idle:z}=r(1*1e3);n(w,e=>{e.forEach(e=>{e.type===`childList`&&e.target===w.value?e.addedNodes.forEach(e=>L(e)):e.type===`attributes`&&e.target.parentNode===w.value&&L(e.target)})},{childList:!0,subtree:!0,attributeFilter:[`id`]});function B(e){for(let t of e){let e=t.contentRect.height;F[t.target.id]||e===0||T[t.target.id]!==e&&t.target.id!==`skip`&&(F[t.target.id]=e)}R()}let V=!1,H=()=>{C.value&&(V||(V=!0,requestAnimationFrame(()=>{if(V=!1,!C.value)return;let e=C.value.scrollTop,t=C.value.scrollTop+C.value.clientHeight*2,n=A.value.findIndex(t=>t._topOffset+t._height>e),r=n===-1?A.value.length-1-b.buffer:Math.max(n-b.buffer,0),i=A.value.findIndex(e=>e._topOffset+e._height>t),a=i===-1?A.value.length-1:Math.min(i+b.buffer,A.value.length-1);(r===0||a!==A.value.length-1||D.value!==r&&O.value!==a)&&(D.value=r,O.value=a,I())})))};f(()=>{x=new window.ResizeObserver(B),C.value&&(D.value=0,l(()=>{I(!0);let e=Object.values(T);e.length!==0&&(E.value=Math.floor(e.reduce((e,t)=>e+t,0)/e.length),G())}))});let U=e=>new Promise(t=>setTimeout(t,e)),W=()=>new Promise(e=>requestAnimationFrame(e));async function G(){if(b.preCalc<1)return;let e=A.value.filter(e=>!T[e[b.dataKey]]);if(e.length!==0){for(let n=0;n<e.length;n++){for(;!z.value;)await U(500);S.value.length<b.preCalc?S.value.push(t(t({},e[n]),{},{_test:`test${n%b.preCalc}`})):S.value[n%b.preCalc]=t(t({},e[n]),{},{_test:`test${n%b.preCalc}`}),n%b.preCalc===0&&(console.log(`virtual-scroll processing: `,n),await W(),S.value=[])}await W()}}let K={ref:C,onScrollPassive:()=>{H()},style:{overflow:`auto`}},q=i(()=>({ref:w,style:{width:`100%`,height:`${N.value}px`,paddingTop:`${M.value}px`,paddingBottom:`${P.value}px`}}));return(t,n)=>(p(),a(`div`,c(K,{style:{height:e.height}}),[o(`div`,u(s(_(q))),[g(t.$slots,`default`,{class:`nc-virtual-scroll`,list:_(j),wrapperProps:_(q),containerProps:K,style:d(t.$attrs.style)})],16)],16))}});export{v as default};