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