react-anchorlist 0.3.4 → 0.3.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"useChatVirtualizer.d.ts","sourceRoot":"","sources":["../../src/hooks/useChatVirtualizer.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EACV,kBAAkB,EAClB,kBAAkB,EAElB,wBAAwB,EACzB,MAAM,UAAU,CAAA;AAEjB;;GAEG;AACH,wBAAgB,kBAAkB,CAAC,CAAC,EAAE,OAAO,EAAE;IAC7C,KAAK,EAAE,CAAC,EAAE,CAAA;IACV,MAAM,EAAE,CAAC,IAAI,EAAE,CAAC,EAAE,KAAK,EAAE,MAAM,KAAK,MAAM,GAAG,MAAM,CAAA;IACnD,iBAAiB,CAAC,EAAE,MAAM,CAAA;IAC1B,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,iBAAiB,CAAC,EAAE,MAAM,CAAA;IAC1B,kBAAkB,CAAC,EAAE,kBAAkB,CAAA;IACvC,YAAY,CAAC,EAAE,MAAM,GAAG,QAAQ,GAAG,KAAK,CAAA;IACxC,gBAAgB,CAAC,EAAE,KAAK,GAAG,QAAQ,CAAA;IACnC,cAAc,CAAC,EAAE,kBAAkB,GAAG,IAAI,CAAA;IAC1C,cAAc,CAAC,EAAE,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IAC3C,YAAY,CAAC,EAAE,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IACzC,qBAAqB,CAAC,EAAE,MAAM,CAAA;IAC9B,mBAAmB,CAAC,EAAE,MAAM,CAAA;IAC5B,qEAAqE;IACrE,kBAAkB,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAAA;IAC3C,iEAAiE;IACjE,yBAAyB,CAAC,EAAE,MAAM,IAAI,CAAA;CACvC,GAAG,wBAAwB,CAAC,CAAC,CAAC,CAoL9B"}
1
+ {"version":3,"file":"useChatVirtualizer.d.ts","sourceRoot":"","sources":["../../src/hooks/useChatVirtualizer.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EACV,kBAAkB,EAClB,kBAAkB,EAElB,wBAAwB,EACzB,MAAM,UAAU,CAAA;AAEjB;;GAEG;AACH,wBAAgB,kBAAkB,CAAC,CAAC,EAAE,OAAO,EAAE;IAC7C,KAAK,EAAE,CAAC,EAAE,CAAA;IACV,MAAM,EAAE,CAAC,IAAI,EAAE,CAAC,EAAE,KAAK,EAAE,MAAM,KAAK,MAAM,GAAG,MAAM,CAAA;IACnD,iBAAiB,CAAC,EAAE,MAAM,CAAA;IAC1B,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,iBAAiB,CAAC,EAAE,MAAM,CAAA;IAC1B,kBAAkB,CAAC,EAAE,kBAAkB,CAAA;IACvC,YAAY,CAAC,EAAE,MAAM,GAAG,QAAQ,GAAG,KAAK,CAAA;IACxC,gBAAgB,CAAC,EAAE,KAAK,GAAG,QAAQ,CAAA;IACnC,cAAc,CAAC,EAAE,kBAAkB,GAAG,IAAI,CAAA;IAC1C,cAAc,CAAC,EAAE,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IAC3C,YAAY,CAAC,EAAE,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IACzC,qBAAqB,CAAC,EAAE,MAAM,CAAA;IAC9B,mBAAmB,CAAC,EAAE,MAAM,CAAA;IAC5B,qEAAqE;IACrE,kBAAkB,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAAA;IAC3C,iEAAiE;IACjE,yBAAyB,CAAC,EAAE,MAAM,IAAI,CAAA;CACvC,GAAG,wBAAwB,CAAC,CAAC,CAAC,CAqM9B"}
@@ -1 +1 @@
1
- {"version":3,"file":"useVirtualEngine.d.ts","sourceRoot":"","sources":["../../src/hooks/useVirtualEngine.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAkB,sBAAsB,EAAe,MAAM,UAAU,CAAA;AAEnF;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,OAAO,EAAE;IAC3C,KAAK,EAAE,CAAC,EAAE,CAAA;IACV,MAAM,EAAE,CAAC,IAAI,EAAE,CAAC,EAAE,KAAK,EAAE,MAAM,KAAK,MAAM,GAAG,MAAM,CAAA;IACnD,iBAAiB,EAAE,MAAM,CAAA;IACzB,QAAQ,EAAE,MAAM,CAAA;IAChB,gBAAgB,EAAE,KAAK,GAAG,QAAQ,CAAA;CACnC,GAAG,sBAAsB,CAAC,CAAC,CAAC,CAgX5B"}
1
+ {"version":3,"file":"useVirtualEngine.d.ts","sourceRoot":"","sources":["../../src/hooks/useVirtualEngine.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAkB,sBAAsB,EAAe,MAAM,UAAU,CAAA;AAEnF;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,OAAO,EAAE;IAC3C,KAAK,EAAE,CAAC,EAAE,CAAA;IACV,MAAM,EAAE,CAAC,IAAI,EAAE,CAAC,EAAE,KAAK,EAAE,MAAM,KAAK,MAAM,GAAG,MAAM,CAAA;IACnD,iBAAiB,EAAE,MAAM,CAAA;IACzB,QAAQ,EAAE,MAAM,CAAA;IAChB,gBAAgB,EAAE,KAAK,GAAG,QAAQ,CAAA;CACnC,GAAG,sBAAsB,CAAC,CAAC,CAAC,CAoX5B"}
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const N=require("react/jsx-runtime"),r=require("react");function fe(i){const e=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(i){for(const t in i)if(t!=="default"){const s=Object.getOwnPropertyDescriptor(i,t);Object.defineProperty(e,t,s.get?s:{enumerable:!0,get:()=>i[t]})}}return e.default=i,Object.freeze(e)}const ee=fe(r);class he{constructor(e,t){this.defaultSize=t,this.sizes=e>0?Array(e).fill(t):[],this.offsets=e>0?Array(e).fill(0):[],e>0&&this._recalcFrom(0)}_recalcFrom(e){for(let t=e;t<this.sizes.length;t++)this.offsets[t]=t===0?0:(this.offsets[t-1]??0)+(this.sizes[t-1]??this.defaultSize)}getOffset(e){return this.offsets[e]??0}getSize(e){return this.sizes[e]??this.defaultSize}setSize(e,t){return this.sizes[e]===t?!1:(this.sizes[e]=t,this._recalcFrom(e+1),!0)}prepend(e){const t=Array(e).fill(this.defaultSize);this.sizes=[...t,...this.sizes],this.offsets=Array(this.sizes.length).fill(0),this._recalcFrom(0)}append(e){const t=this.sizes.length;for(let s=0;s<e;s++)this.sizes.push(this.defaultSize),this.offsets.push(0);this._recalcFrom(t)}resize(e){const t=this.sizes.length;e>t?this.append(e-t):e<t&&(this.sizes=this.sizes.slice(0,e),this.offsets=this.offsets.slice(0,e))}totalSize(){if(this.sizes.length===0)return 0;const e=this.sizes.length-1;return(this.offsets[e]??0)+(this.sizes[e]??this.defaultSize)}get count(){return this.sizes.length}getOffsets(){return this.offsets}getSizes(){return this.sizes}}class de{constructor(){this.cache=new Map}get(e){return this.cache.get(e)}set(e,t){this.cache.set(e,t)}has(e){return this.cache.has(e)}delete(e){this.cache.delete(e)}clear(){this.cache.clear()}applyToOffsetMap(e,t){for(const[s,l]of this.cache){const o=t.get(s);o!==void 0&&e.setSize(o,l)}}}function se(i,e){if(i.length===0)return 0;let t=0,s=i.length-1;for(;t<s;){const l=t+s>>1;(i[l]??0)<e?t=l+1:s=l}return Math.max(0,t>0&&(i[t]??0)>e?t-1:t)}function me(i,e,t){if(i.length===0)return 0;let s=0,l=i.length-1,o=0;for(;s<=l;){const n=s+l>>1;(i[n]??0)<t?(o=n,s=n+1):l=n-1}return o}function ge(i){const{firstVisible:e,lastVisible:t,itemCount:s,overscan:l}=i;return s===0?{start:0,end:-1}:{start:Math.max(0,e-l),end:Math.min(s-1,t+l)}}function pe(i,e,t,s){const l=s==null?void 0:s.reconcile,o=r.useRef(null);return r.useCallback((n,S)=>{const u=i.current,h=e.current;if(!u||!h)return;o.current!==null&&(cancelAnimationFrame(o.current),o.current=null);let y,f;if(typeof n=="object"&&n!==null?(y=n.index,f={align:n.align,behavior:n.behavior,offset:n.offset}):(y=n,f=S),!Number.isFinite(y))return;const v=Math.max(0,Math.min(Math.floor(y),h.count-1));h.getOffset(v),h.getSize(v);const I=(f==null?void 0:f.align)??"start",F=(f==null?void 0:f.behavior)??"auto",E=(f==null?void 0:f.offset)??0,d=()=>{var p;const H=e.current,a=i.current;if(!H||!a)return 0;const m=((p=t==null?void 0:t.current)==null?void 0:p.offsetTop)??0,z=H.getOffset(v),M=H.getSize(v);return I==="start"?m+z+E:I==="center"?m+z-a.clientHeight/2+M/2+E:m+z-a.clientHeight+M+E},w=d();((H,a)=>{typeof u.scrollTo=="function"?u.scrollTo({top:H,behavior:a}):u.scrollTop=H})(Math.max(0,w),F);const k=performance.now();let T=0;const j=12,_=300,O=1,K=()=>{o.current=null;const H=i.current;if(!H)return;const a=Math.max(0,d()),m=Math.abs(H.scrollTop-a),z=performance.now()-k>=_,M=T>=j;m<=O||z||M||(typeof H.scrollTo=="function"?H.scrollTo({top:a,behavior:"auto"}):H.scrollTop=a,T+=1,o.current=requestAnimationFrame(K))};o.current=requestAnimationFrame(K)},[i,e,t,l])}function oe(i){var ne;const{items:e,getKey:t,estimatedItemSize:s,overscan:l,initialAlignment:o}=i,n=r.useRef(null),S=r.useRef(null),u=r.useRef(null),h=r.useRef(new de),y=r.useRef([]),f=r.useRef(new Map),v=r.useRef(e.length),I=r.useRef(null),F=r.useRef(null),E=r.useRef(!1),d=r.useRef(0),w=r.useRef(0),R=r.useRef(null),k=r.useRef(!1),T=r.useRef(null),j=r.useRef(0),_=r.useRef(0),O=r.useRef(0),K=r.useRef(null),H=r.useRef(!1),[,a]=r.useState(0),m=r.useCallback(()=>a(c=>c+1),[]);u.current||(u.current=new he(e.length,s));const z=e.length>0?t(e[0],0):null,M=e.length>0?t(e[e.length-1],e.length-1):null;if(e.length!==v.current||z!==I.current||M!==F.current){const c=u.current,g=e.map((L,q)=>t(L,q)),A=y.current,x=A.length,C=g.length;if(C===0)c.resize(0);else if(x===0)c.resize(C);else if(C>x){const L=C-x;A.length>0&&L>=0&&g[L]===A[0]?c.prepend(L):c.resize(C)}else C<x,c.resize(C);const b=new Map;g.forEach((L,q)=>b.set(L,q)),f.current=b,h.current.applyToOffsetMap(c,b),y.current=g,v.current=e.length,I.current=z,F.current=M}const P=r.useCallback(()=>{K.current===null&&(K.current=requestAnimationFrame(()=>{K.current=null;const c=n.current;if(!c)return;const g=j.current;Math.abs(g)<.01||(j.current=0,_.current+=g,O.current+=g,c.scrollTop+=g,d.current=c.scrollTop,H.current=!0,m())}))},[m]);r.useEffect(()=>{const c=n.current;if(!c)return;const g=()=>{R.current===null&&(R.current=requestAnimationFrame(()=>{R.current=null,d.current=c.scrollTop,w.current=c.clientHeight,H.current&&(H.current=!1,_.current=0,O.current=0),m()}))};return c.addEventListener("scroll",g,{passive:!0}),()=>{c.removeEventListener("scroll",g),R.current!==null&&(cancelAnimationFrame(R.current),R.current=null),K.current!==null&&(cancelAnimationFrame(K.current),K.current=null)}},[m]),r.useEffect(()=>{const c=n.current;if(!c)return;w.current=c.clientHeight,d.current=c.scrollTop;const g=new ResizeObserver(([A])=>{A&&(w.current=A.contentRect.height,m())});return g.observe(c),()=>g.disconnect()},[m]),r.useLayoutEffect(()=>{if(E.current||e.length===0)return;const c=n.current;if(c){if(o==="bottom"){c.scrollTop=c.scrollHeight,d.current=c.scrollTop,w.current=c.clientHeight,k.current=!0,T.current!==null&&cancelAnimationFrame(T.current);const g=performance.now();let A=0,x=c.scrollHeight;const C=()=>{if(!k.current){T.current=null;return}const b=c.scrollHeight,L=Math.abs(b-x);x=b,L<1?A+=1:A=0,c.scrollTop=c.scrollHeight,d.current=c.scrollTop;const q=performance.now()-g;if(A>=3||q>=500){k.current=!1,T.current=null;return}T.current=requestAnimationFrame(C)};T.current=requestAnimationFrame(C)}E.current=!0}},[o,e.length]),r.useEffect(()=>{e.length===0&&(E.current=!1,k.current=!1,T.current!==null&&(cancelAnimationFrame(T.current),T.current=null))},[e.length]);const V=r.useCallback((c,g)=>{var Y;const A=u.current;if(!A||h.current.get(c)===g)return;h.current.set(c,g);const C=f.current.get(c);if(C===void 0)return;const b=A.getSize(C),L=g-b,q=n.current;q&&!k.current&&L!==0&&A.getOffset(C)+b+(((Y=S.current)==null?void 0:Y.offsetTop)??0)<q.scrollTop&&(j.current+=L,P()),A.setSize(C,g)&&m()},[P,m]),J=r.useCallback((c,g="auto")=>{var A;(A=n.current)==null||A.scrollTo({top:c,behavior:g})},[]),U=pe(n,u,S,{reconcile:!0}),X=r.useCallback(()=>{var Q;const c=n.current,g=u.current;if(!c||!g||g.count===0)return null;const A=((Q=S.current)==null?void 0:Q.offsetTop)??0,x=Math.max(0,c.scrollTop-A),C=g.getOffsets(),b=se(C,x),L=y.current[b]??null,q=[];for(let Y=b;Y<Math.min(g.count,b+6);Y++){const Z=y.current[Y]??null;Z!==null&&q.push({key:Z,offsetWithinItem:x-g.getOffset(Y)})}return{key:L,offsetWithinItem:x-g.getOffset(b),candidates:q,scrollTop:c.scrollTop,scrollHeight:c.scrollHeight}},[]),W=r.useCallback((c,g)=>{var b;const A=u.current;if(!A)return null;const x=f.current.get(c);return x===void 0?null:(((b=S.current)==null?void 0:b.offsetTop)??0)+A.getOffset(x)+g},[]),B=u.current,ie=B?B.totalSize():0,D=n.current,$=(D==null?void 0:D.scrollTop)??d.current,te=(D==null?void 0:D.clientHeight)??w.current,ue=((ne=S.current)==null?void 0:ne.offsetTop)??0,re=Math.max(0,$-ue),G=[];if(B&&B.count>0&&te>0){const c=B.getOffsets(),g=B.getSizes(),A=se(c,re),x=me(c,g,re+te),C=ge({firstVisible:A,lastVisible:x,itemCount:B.count,overscan:l});for(let b=C.start;b<=C.end&&b<e.length;b++)G.push({key:y.current[b]??t(e[b],b),index:b,start:B.getOffset(b),size:B.getSize(b),data:e[b]})}else if(B&&B.count>0){const c=Math.min(e.length,l*2+1),g=o==="bottom"?Math.max(0,e.length-c):0,A=g+c-1;for(let x=g;x<=A;x++)G.push({key:y.current[x]??t(e[x],x),index:x,start:B.getOffset(x),size:B.getSize(x),data:e[x]})}const ae=D?D.scrollHeight-D.scrollTop-D.clientHeight:1/0;return{scrollerRef:n,innerRef:S,virtualItems:G,totalSize:ie,measureItem:V,scrollToIndex:U,scrollToOffset:J,captureAnchorSnapshot:X,resolveAnchorTop:W,isAtTop:$<=1,isAtBottom:ae<=1,scrollTop:$}}function ve(i){var l;const{snapshot:e,currentScrollHeight:t,resolveAnchorTop:s}=i;if(e.key!==null){const o=s(e.key,e.offsetWithinItem);if(o!==null)return o}if((l=e.candidates)!=null&&l.length)for(const o of e.candidates){const n=s(o.key,o.offsetWithinItem);if(n!==null)return n}return e.scrollTop+(t-e.scrollHeight)}function Re(i){const{scrollerRef:e,itemCount:t,captureAnchorSnapshot:s,resolveAnchorTop:l,onRestored:o}=i,n=r.useRef(null),S=r.useRef(!1),u=r.useRef({first:null,second:null,timeout:null}),h=r.useCallback(()=>{const{first:f,second:v,timeout:I}=u.current;f&&cancelAnimationFrame(f),v&&cancelAnimationFrame(v),I&&clearTimeout(I),u.current={first:null,second:null,timeout:null}},[]),y=r.useCallback(()=>{const f=e.current;if(!f)return;const v=s();n.current=v??{key:null,offsetWithinItem:0,scrollTop:f.scrollTop,scrollHeight:f.scrollHeight},S.current=!0},[e,s]);return r.useLayoutEffect(()=>{if(!S.current)return;const f=e.current,v=n.current;if(!f||!v)return;S.current=!1;const I=()=>{const F=ve({snapshot:v,currentScrollHeight:f.scrollHeight,resolveAnchorTop:l});Number.isFinite(F)&&Math.abs(f.scrollTop-F)>1&&(f.scrollTop=F)};return h(),I(),o==null||o(),u.current.first=requestAnimationFrame(()=>{u.current.first=null,I(),u.current.second=requestAnimationFrame(()=>{u.current.second=null,I()})}),u.current.timeout=setTimeout(()=>{u.current.timeout=null,I()},90),()=>h()},[t,e,l,h,o]),{prepareAnchor:y}}function ye(i){const{previous:e,distanceFromBottom:t,threshold:s,hysteresis:l}=i;if(!l)return t<=s;const o=Math.max(0,l.enter),n=Math.max(o,l.leave);return e?t<=n:t<=o}function Te(i,e){const t=typeof e=="number"?e:e.threshold??200,s=typeof e=="number"?void 0:e.hysteresis,[l,o]=r.useState(!0),n=r.useRef(null),S=r.useRef(!0);return r.useEffect(()=>{const u=i.current;if(!u)return;const h=()=>{const f=u.scrollHeight-u.scrollTop-u.clientHeight,v=ye({previous:S.current,distanceFromBottom:f,threshold:t,hysteresis:s});S.current=v,o(v)},y=()=>{n.current!==null&&cancelAnimationFrame(n.current),n.current=requestAnimationFrame(h)};return u.addEventListener("scroll",y,{passive:!0}),h(),()=>{u.removeEventListener("scroll",y),n.current!==null&&cancelAnimationFrame(n.current)}},[i,t,s==null?void 0:s.enter,s==null?void 0:s.leave]),l}function Se(i){const{itemCount:e,firstKey:t,lastKey:s,isAtBottom:l,scrollToIndex:o,mode:n}=i,S=r.useRef(e),u=r.useRef(t),h=r.useRef(s);r.useLayoutEffect(()=>{if(!n){S.current=e,u.current=t,h.current=s;return}const y=S.current,f=u.current,v=h.current;e>y&&t===f&&s!==v&&l&&e>0&&o(e-1,{align:"end",behavior:n==="smooth"?"smooth":"auto"}),S.current=e,u.current=t,h.current=s},[e,t,s,l,o,n])}function ce(i){const{items:e,getKey:t,estimatedItemSize:s=80,overscan:l=20,atBottomThreshold:o=200,atBottomHysteresis:n,followOutput:S="auto",initialAlignment:u="bottom",scrollModifier:h=null,onStartReached:y,onEndReached:f,startReachedThreshold:v=300,endReachedThreshold:I=300,scrollToMessageKey:F,onScrollToMessageComplete:E}=i,d=oe({items:e,getKey:t,estimatedItemSize:s,overscan:l,initialAlignment:u}),w=Te(d.scrollerRef,{threshold:o,hysteresis:n??{enter:80,leave:160}}),[,R]=r.useState(0),k=r.useCallback(()=>R(p=>p+1),[]),{prepareAnchor:T}=Re({scrollerRef:d.scrollerRef,itemCount:e.length,captureAnchorSnapshot:d.captureAnchorSnapshot,resolveAnchorTop:d.resolveAnchorTop,onRestored:k}),j=e.length>0?t(e[0],0):null,_=e.length>0?t(e[e.length-1],e.length-1):null;Se({itemCount:e.length,firstKey:j,lastKey:_,isAtBottom:w,scrollToIndex:d.scrollToIndex,mode:S??!1});const O=r.useCallback((p="auto")=>{e.length!==0&&d.scrollToIndex(e.length-1,{align:"end",behavior:p})},[e.length,d]),K=r.useCallback((p,P)=>{const V=e.findIndex((J,U)=>t(J,U)===p);V!==-1&&d.scrollToIndex(V,P)},[e,t,d]),H=r.useRef(null);r.useLayoutEffect(()=>{if(h&&H.current!==h.id){if(H.current=h.id,h.type==="prepend"){T();return}if(h.type==="append"){if(h.ifAtBottomOnly&&!w)return;O(h.behavior??"auto");return}if(h.type==="items-change"){w&&O("auto");return}K(h.key,{align:h.align??"center",behavior:h.behavior??"auto"})}},[h,w,T,O,K]);const a=r.useRef(!1),m=r.useRef(u==="top");r.useEffect(()=>{const p=d.scrollerRef.current;if(!p||!y)return;const P=()=>{const V=p.scrollTop;m.current||(p.scrollHeight<=p.clientHeight+v||V>v)&&(m.current=!0),m.current&&V<=v&&!a.current&&(a.current=!0,Promise.resolve(y()).finally(()=>{a.current=!1}))};return p.addEventListener("scroll",P,{passive:!0}),P(),()=>p.removeEventListener("scroll",P)},[d.scrollerRef,y,v,u]);const z=r.useRef(!1);r.useEffect(()=>{const p=d.scrollerRef.current;if(!p||!f)return;const P=()=>{p.scrollHeight-p.scrollTop-p.clientHeight<=I&&!z.current&&(z.current=!0,Promise.resolve(f()).finally(()=>{z.current=!1}))};return p.addEventListener("scroll",P,{passive:!0}),()=>p.removeEventListener("scroll",P)},[d.scrollerRef,f,I]);const M=r.useRef(null);return r.useEffect(()=>{if(!F||M.current===F)return;const p=e.findIndex((P,V)=>t(P,V)===F);p!==-1&&(M.current=F,d.scrollToIndex(p,{align:"center",behavior:"auto"}),E==null||E())},[F,e,t,d,E]),{scrollerRef:d.scrollerRef,innerRef:d.innerRef,virtualItems:d.virtualItems,totalSize:d.totalSize,measureItem:d.measureItem,scrollToIndex:d.scrollToIndex,scrollToBottom:O,scrollToKey:K,isAtBottom:w,prepareAnchor:T}}function le({virtualItem:i,measureItem:e,children:t}){const s=r.useRef(null),l=r.useRef(!1);return r.useEffect(()=>{const o=s.current;if(!o)return;l.current=!1;const n=new ResizeObserver(([S])=>{S&&(l.current=!0,e(i.key,S.contentRect.height))});return n.observe(o),()=>n.disconnect()},[i.key,e]),N.jsx("div",{ref:s,style:{position:"absolute",top:0,transform:`translateY(${i.start}px)`,width:"100%",minHeight:l.current?void 0:i.size},children:t})}function be(i,e){const{data:t,itemContent:s,computeItemKey:l,estimatedItemSize:o=80,overscan:n=20,followOutput:S="auto",atBottomThreshold:u=200,atBottomHysteresis:h,initialAlignment:y="bottom",scrollModifier:f,onStartReached:v,onEndReached:I,startReachedThreshold:F=300,endReachedThreshold:E=300,scrollToMessageKey:d,onScrollToMessageComplete:w,onAtBottomChange:R,components:k={},className:T,style:j}=i,{scrollerRef:_,innerRef:O,virtualItems:K,totalSize:H,measureItem:a,scrollToIndex:m,scrollToBottom:z,scrollToKey:M,isAtBottom:p,prepareAnchor:P}=ce({items:t,getKey:(W,B)=>l(B,W),estimatedItemSize:o,overscan:n,atBottomThreshold:u,atBottomHysteresis:h,followOutput:S,initialAlignment:y,scrollModifier:f,onStartReached:v,onEndReached:I,startReachedThreshold:F,endReachedThreshold:E,scrollToMessageKey:d,onScrollToMessageComplete:w}),V=ee.useRef(p);ee.useEffect(()=>{V.current!==p&&(V.current=p,R==null||R(p))},[p,R]),r.useImperativeHandle(e,()=>({scrollToBottom:z,scrollToIndex:m,scrollToKey:M,getScrollTop:()=>{var W;return((W=_.current)==null?void 0:W.scrollTop)??0},isAtBottom:()=>p,prepareAnchor:P}),[z,m,M,_,p,P]);const{Header:J,Footer:U,EmptyPlaceholder:X}=k;return t.length===0&&X?N.jsx(X,{}):N.jsxs("div",{ref:_,className:T,style:{overflow:"auto",height:"100%",position:"relative",overflowAnchor:"none",overscrollBehaviorY:"contain",...j},children:[J&&N.jsx(J,{}),N.jsx("div",{ref:O,style:{height:H,position:"relative",width:"100%"},children:K.map(W=>N.jsx(le,{virtualItem:W,measureItem:a,children:s(W.index,W.data)},W.key))}),U&&N.jsx(U,{})]})}const ze=r.forwardRef(be);function Ae({data:i,itemContent:e,computeItemKey:t,estimatedItemSize:s=60,overscan:l=20,onEndReached:o,endReachedThreshold:n=300,components:S={},className:u,style:h}){const{scrollerRef:y,innerRef:f,virtualItems:v,totalSize:I,measureItem:F}=oe({items:i,getKey:(R,k)=>t(k,R),estimatedItemSize:s,overscan:l,initialAlignment:"top"});ee.useEffect(()=>{const R=y.current;if(!R||!o)return;let k=!1;const T=()=>{R.scrollHeight-R.scrollTop-R.clientHeight<=n&&!k&&(k=!0,Promise.resolve(o()).finally(()=>{k=!1}))};return R.addEventListener("scroll",T,{passive:!0}),()=>R.removeEventListener("scroll",T)},[y,o,n]);const{Header:E,Footer:d,EmptyPlaceholder:w}=S;return i.length===0&&w?N.jsx(w,{}):N.jsxs("div",{ref:y,className:u,style:{overflow:"auto",height:"100%",position:"relative",overflowAnchor:"none",overscrollBehaviorY:"contain",...h},children:[E&&N.jsx(E,{}),N.jsx("div",{ref:f,style:{height:I,position:"relative",width:"100%"},children:v.map(R=>N.jsx(le,{virtualItem:R,measureItem:F,children:e(R.index,R.data)},R.key))}),d&&N.jsx(d,{})]})}function xe(i){const{fetcher:e,initialPage:t=1,direction:s="append",getKey:l,onPageLoaded:o,onError:n}=i,[S,u]=r.useState([]),[h,y]=r.useState(t),[f,v]=r.useState(!0),[I,F]=r.useState(!1),[E,d]=r.useState(!1),[w,R]=r.useState(!1),k=r.useRef(new Set),T=r.useRef(!1),j=r.useCallback(a=>l?a.filter(m=>{const z=l(m);return k.current.has(z)?!1:(k.current.add(z),!0)}):a,[l]),_=r.useCallback(async()=>{if(!(T.current||!f)){T.current=!0,R(!0);try{const a=h+1,m=await e(a),z=j(m.data);u(M=>s==="prepend"?[...z,...M]:[...M,...z]),y(a),v(m.hasNextPage),F(m.hasPrevPage),o==null||o(a,z)}catch(a){n==null||n(a instanceof Error?a:new Error(String(a)))}finally{R(!1),T.current=!1}}},[h,f,e,j,s,o,n]),O=r.useCallback(async()=>{if(!(T.current||!I)){T.current=!0,R(!0);try{const a=h-1,m=await e(a),z=j(m.data);u(M=>[...z,...M]),y(a),F(m.hasPrevPage),v(m.hasNextPage),o==null||o(a,z)}catch(a){n==null||n(a instanceof Error?a:new Error(String(a)))}finally{R(!1),T.current=!1}}},[h,I,e,j,o,n]),K=r.useCallback(async()=>{if(!T.current){T.current=!0,d(!0);try{const a=await e(t),m=a.data;if(l){const z=new Set(m.map(l));m.forEach(M=>k.current.add(l(M))),u(M=>{const p=M.filter(P=>!z.has(l(P)));return s==="prepend"?[...m,...p]:[...p,...m]})}else u(m);y(t),v(a.hasNextPage),F(a.hasPrevPage),o==null||o(t,m)}catch(a){n==null||n(a instanceof Error?a:new Error(String(a)))}finally{d(!1),T.current=!1}}},[e,t,l,s,o,n]),H=r.useCallback(()=>{u([]),y(t),v(!0),F(!1),d(!1),R(!1),k.current.clear(),T.current=!1},[t]);return{items:S,loadNextPage:_,loadPrevPage:O,hasNextPage:f,hasPrevPage:I,loading:E,loadingMore:w,refresh:K,reset:H,currentPage:h}}exports.ChatVirtualList=ze;exports.VirtualList=Ae;exports.useChatVirtualizer=ce;exports.usePagination=xe;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const q=require("react/jsx-runtime"),r=require("react");function he(i){const e=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(i){for(const t in i)if(t!=="default"){const s=Object.getOwnPropertyDescriptor(i,t);Object.defineProperty(e,t,s.get?s:{enumerable:!0,get:()=>i[t]})}}return e.default=i,Object.freeze(e)}const te=he(r);class de{constructor(e,t){this.defaultSize=t,this.sizes=e>0?Array(e).fill(t):[],this.offsets=e>0?Array(e).fill(0):[],e>0&&this._recalcFrom(0)}_recalcFrom(e){for(let t=e;t<this.sizes.length;t++)this.offsets[t]=t===0?0:(this.offsets[t-1]??0)+(this.sizes[t-1]??this.defaultSize)}getOffset(e){return this.offsets[e]??0}getSize(e){return this.sizes[e]??this.defaultSize}setSize(e,t){return this.sizes[e]===t?!1:(this.sizes[e]=t,this._recalcFrom(e+1),!0)}prepend(e){const t=Array(e).fill(this.defaultSize);this.sizes=[...t,...this.sizes],this.offsets=Array(this.sizes.length).fill(0),this._recalcFrom(0)}append(e){const t=this.sizes.length;for(let s=0;s<e;s++)this.sizes.push(this.defaultSize),this.offsets.push(0);this._recalcFrom(t)}resize(e){const t=this.sizes.length;e>t?this.append(e-t):e<t&&(this.sizes=this.sizes.slice(0,e),this.offsets=this.offsets.slice(0,e))}totalSize(){if(this.sizes.length===0)return 0;const e=this.sizes.length-1;return(this.offsets[e]??0)+(this.sizes[e]??this.defaultSize)}get count(){return this.sizes.length}getOffsets(){return this.offsets}getSizes(){return this.sizes}}class me{constructor(){this.cache=new Map}get(e){return this.cache.get(e)}set(e,t){this.cache.set(e,t)}has(e){return this.cache.has(e)}delete(e){this.cache.delete(e)}clear(){this.cache.clear()}applyToOffsetMap(e,t){for(const[s,l]of this.cache){const o=t.get(s);o!==void 0&&e.setSize(o,l)}}}function oe(i,e){if(i.length===0)return 0;let t=0,s=i.length-1;for(;t<s;){const l=t+s>>1;(i[l]??0)<e?t=l+1:s=l}return Math.max(0,t>0&&(i[t]??0)>e?t-1:t)}function pe(i,e,t){if(i.length===0)return 0;let s=0,l=i.length-1,o=0;for(;s<=l;){const n=s+l>>1;(i[n]??0)<t?(o=n,s=n+1):l=n-1}return o}function ge(i){const{firstVisible:e,lastVisible:t,itemCount:s,overscan:l}=i;return s===0?{start:0,end:-1}:{start:Math.max(0,e-l),end:Math.min(s-1,t+l)}}function Re(i,e,t,s){const l=s==null?void 0:s.reconcile,o=r.useRef(null);return r.useCallback((n,y)=>{const u=i.current,d=e.current;if(!u||!d)return;o.current!==null&&(cancelAnimationFrame(o.current),o.current=null);let R,h;if(typeof n=="object"&&n!==null?(R=n.index,h={align:n.align,behavior:n.behavior,offset:n.offset}):(R=n,h=y),!Number.isFinite(R))return;const p=Math.max(0,Math.min(Math.floor(R),d.count-1));d.getOffset(p),d.getSize(p);const I=(h==null?void 0:h.align)??"start",k=(h==null?void 0:h.behavior)??"auto",E=(h==null?void 0:h.offset)??0,a=()=>{var P;const H=e.current,f=i.current;if(!H||!f)return 0;const S=((P=t==null?void 0:t.current)==null?void 0:P.offsetTop)??0,v=H.getOffset(p),C=H.getSize(p);return I==="start"?S+v+E:I==="center"?S+v-f.clientHeight/2+C/2+E:S+v-f.clientHeight+C+E},w=a();((H,f)=>{typeof u.scrollTo=="function"?u.scrollTo({top:H,behavior:f}):u.scrollTop=H})(Math.max(0,w),k);const F=performance.now();let T=0;const j=12,L=300,D=1,K=()=>{o.current=null;const H=i.current;if(!H)return;const f=Math.max(0,a()),S=Math.abs(H.scrollTop-f),v=performance.now()-F>=L,C=T>=j;S<=D||v||C||(typeof H.scrollTo=="function"?H.scrollTo({top:f,behavior:"auto"}):H.scrollTop=f,T+=1,o.current=requestAnimationFrame(K))};o.current=requestAnimationFrame(K)},[i,e,t,l])}function ce(i){var se;const{items:e,getKey:t,estimatedItemSize:s,overscan:l,initialAlignment:o}=i,n=r.useRef(null),y=r.useRef(null),u=r.useRef(null),d=r.useRef(new me),R=r.useRef([]),h=r.useRef(new Map),p=r.useRef(e.length),I=r.useRef(null),k=r.useRef(null),E=r.useRef(!1),a=r.useRef(0),w=r.useRef(0),g=r.useRef(null),F=r.useRef(!1),T=r.useRef(null),j=r.useRef(0),L=r.useRef(0),D=r.useRef(0),K=r.useRef(null),H=r.useRef(!1),f=r.useRef(!1),[,S]=r.useState(0),v=r.useCallback(()=>S(c=>c+1),[]);u.current||(u.current=new de(e.length,s));const C=e.length>0?t(e[0],0):null,P=e.length>0?t(e[e.length-1],e.length-1):null;if(e.length!==p.current||C!==I.current||P!==k.current){const c=u.current,m=e.map((O,W)=>t(O,W)),z=R.current,x=z.length,M=m.length;if(M===0)c.resize(0);else if(x===0)c.resize(M);else if(M>x){const O=M-x;z.length>0&&O>=0&&m[O]===z[0]?c.prepend(O):c.resize(M)}else M<x,c.resize(M);const A=new Map;m.forEach((O,W)=>A.set(O,W)),h.current=A,d.current.applyToOffsetMap(c,A),R.current=m,p.current=e.length,I.current=C,k.current=P}const b=r.useCallback(()=>{K.current===null&&(K.current=requestAnimationFrame(()=>{K.current=null;const c=n.current;if(!c)return;const m=j.current;Math.abs(m)<.01||(j.current=0,L.current+=m,D.current+=m,c.scrollTop+=m,a.current=c.scrollTop,H.current=!0,v())}))},[v]);r.useEffect(()=>{const c=n.current;if(!c)return;const m=()=>{g.current===null&&(g.current=requestAnimationFrame(()=>{g.current=null,a.current=c.scrollTop,w.current=c.clientHeight,H.current&&(H.current=!1,L.current=0,D.current=0),v()}))};return c.addEventListener("scroll",m,{passive:!0}),()=>{c.removeEventListener("scroll",m),g.current!==null&&(cancelAnimationFrame(g.current),g.current=null),K.current!==null&&(cancelAnimationFrame(K.current),K.current=null)}},[v]),r.useEffect(()=>{const c=n.current;if(!c)return;w.current=c.clientHeight,a.current=c.scrollTop;const m=new ResizeObserver(([z])=>{z&&(w.current=z.contentRect.height,v())});return m.observe(c),()=>m.disconnect()},[v]),r.useLayoutEffect(()=>{if(E.current||e.length===0)return;const c=n.current;if(c){if(o==="bottom"){c.scrollTop=c.scrollHeight,a.current=c.scrollTop,w.current=c.clientHeight,F.current=!0,T.current!==null&&cancelAnimationFrame(T.current);const m=performance.now();let z=0,x=c.scrollHeight;const M=()=>{if(!F.current){T.current=null;return}const A=c.scrollHeight,O=Math.abs(A-x);x=A,O<1?z+=1:z=0,c.scrollTop=c.scrollHeight,a.current=c.scrollTop;const W=performance.now()-m;if(z>=3||W>=500){F.current=!1,T.current=null;return}T.current=requestAnimationFrame(M)};T.current=requestAnimationFrame(M)}E.current=!0}},[o,e.length]),r.useEffect(()=>{e.length===0&&(E.current=!1,F.current=!1,T.current!==null&&(cancelAnimationFrame(T.current),T.current=null))},[e.length]);const B=r.useCallback((c,m)=>{var X;const z=u.current;if(!z||d.current.get(c)===m)return;d.current.set(c,m);const M=h.current.get(c);if(M===void 0)return;const A=z.getSize(M),O=m-A,W=n.current;W&&!F.current&&!f.current&&O!==0&&z.getOffset(M)+A+(((X=y.current)==null?void 0:X.offsetTop)??0)<W.scrollTop&&(j.current+=O,b()),z.setSize(M,m)&&v()},[b,v]),V=r.useCallback((c,m="auto")=>{var z;(z=n.current)==null||z.scrollTo({top:c,behavior:m})},[]),Y=Re(n,u,y,{reconcile:!0}),N=r.useCallback(()=>{var Z;const c=n.current,m=u.current;if(!c||!m||m.count===0)return null;const z=((Z=y.current)==null?void 0:Z.offsetTop)??0,x=Math.max(0,c.scrollTop-z),M=m.getOffsets(),A=oe(M,x),O=R.current[A]??null,W=[];for(let X=A;X<Math.min(m.count,A+6);X++){const ee=R.current[X]??null;ee!==null&&W.push({key:ee,offsetWithinItem:x-m.getOffset(X)})}return{key:O,offsetWithinItem:x-m.getOffset(A),candidates:W,scrollTop:c.scrollTop,scrollHeight:c.scrollHeight}},[]),$=r.useCallback((c,m)=>{var A;const z=u.current;if(!z)return null;const x=h.current.get(c);return x===void 0?null:(((A=y.current)==null?void 0:A.offsetTop)??0)+z.getOffset(x)+m},[]),_=u.current,ue=_?_.totalSize():0,J=n.current,G=(J==null?void 0:J.scrollTop)??a.current,re=(J==null?void 0:J.clientHeight)??w.current,ae=((se=y.current)==null?void 0:se.offsetTop)??0,ne=Math.max(0,G-ae),Q=[];if(_&&_.count>0&&re>0){const c=_.getOffsets(),m=_.getSizes(),z=oe(c,ne),x=pe(c,m,ne+re),M=ge({firstVisible:z,lastVisible:x,itemCount:_.count,overscan:l});for(let A=M.start;A<=M.end&&A<e.length;A++)Q.push({key:R.current[A]??t(e[A],A),index:A,start:_.getOffset(A),size:_.getSize(A),data:e[A]})}else if(_&&_.count>0){const c=Math.min(e.length,l*2+1),m=o==="bottom"?Math.max(0,e.length-c):0,z=m+c-1;for(let x=m;x<=z;x++)Q.push({key:R.current[x]??t(e[x],x),index:x,start:_.getOffset(x),size:_.getSize(x),data:e[x]})}const fe=J?J.scrollHeight-J.scrollTop-J.clientHeight:1/0;return{scrollerRef:n,innerRef:y,virtualItems:Q,totalSize:ue,measureItem:B,scrollToIndex:Y,scrollToOffset:V,captureAnchorSnapshot:N,resolveAnchorTop:$,isAtTop:G<=1,isAtBottom:fe<=1,scrollTop:G,jumpSuppressedRef:f}}function ve(i){var l;const{snapshot:e,currentScrollHeight:t,resolveAnchorTop:s}=i;if(e.key!==null){const o=s(e.key,e.offsetWithinItem);if(o!==null)return o}if((l=e.candidates)!=null&&l.length)for(const o of e.candidates){const n=s(o.key,o.offsetWithinItem);if(n!==null)return n}return e.scrollTop+(t-e.scrollHeight)}function ye(i){const{scrollerRef:e,itemCount:t,captureAnchorSnapshot:s,resolveAnchorTop:l,onRestored:o}=i,n=r.useRef(null),y=r.useRef(!1),u=r.useRef({first:null,second:null,timeout:null}),d=r.useCallback(()=>{const{first:h,second:p,timeout:I}=u.current;h&&cancelAnimationFrame(h),p&&cancelAnimationFrame(p),I&&clearTimeout(I),u.current={first:null,second:null,timeout:null}},[]),R=r.useCallback(()=>{const h=e.current;if(!h)return;const p=s();n.current=p??{key:null,offsetWithinItem:0,scrollTop:h.scrollTop,scrollHeight:h.scrollHeight},y.current=!0},[e,s]);return r.useLayoutEffect(()=>{if(!y.current)return;const h=e.current,p=n.current;if(!h||!p)return;y.current=!1;const I=()=>{const k=ve({snapshot:p,currentScrollHeight:h.scrollHeight,resolveAnchorTop:l});Number.isFinite(k)&&Math.abs(h.scrollTop-k)>1&&(h.scrollTop=k)};return d(),I(),o==null||o(),u.current.first=requestAnimationFrame(()=>{u.current.first=null,I(),u.current.second=requestAnimationFrame(()=>{u.current.second=null,I()})}),u.current.timeout=setTimeout(()=>{u.current.timeout=null,I()},90),()=>d()},[t,e,l,d,o]),{prepareAnchor:R}}function Te(i){const{previous:e,distanceFromBottom:t,threshold:s,hysteresis:l}=i;if(!l)return t<=s;const o=Math.max(0,l.enter),n=Math.max(o,l.leave);return e?t<=n:t<=o}function Se(i,e){const t=typeof e=="number"?e:e.threshold??200,s=typeof e=="number"?void 0:e.hysteresis,[l,o]=r.useState(!0),n=r.useRef(null),y=r.useRef(!0);return r.useEffect(()=>{const u=i.current;if(!u)return;const d=()=>{const h=u.scrollHeight-u.scrollTop-u.clientHeight,p=Te({previous:y.current,distanceFromBottom:h,threshold:t,hysteresis:s});y.current=p,o(p)},R=()=>{n.current!==null&&cancelAnimationFrame(n.current),n.current=requestAnimationFrame(d)};return u.addEventListener("scroll",R,{passive:!0}),d(),()=>{u.removeEventListener("scroll",R),n.current!==null&&cancelAnimationFrame(n.current)}},[i,t,s==null?void 0:s.enter,s==null?void 0:s.leave]),l}function be(i){const{itemCount:e,firstKey:t,lastKey:s,isAtBottom:l,scrollToIndex:o,mode:n}=i,y=r.useRef(e),u=r.useRef(t),d=r.useRef(s);r.useLayoutEffect(()=>{if(!n){y.current=e,u.current=t,d.current=s;return}const R=y.current,h=u.current,p=d.current;e>R&&t===h&&s!==p&&l&&e>0&&o(e-1,{align:"end",behavior:n==="smooth"?"smooth":"auto"}),y.current=e,u.current=t,d.current=s},[e,t,s,l,o,n])}function le(i){const{items:e,getKey:t,estimatedItemSize:s=80,overscan:l=20,atBottomThreshold:o=200,atBottomHysteresis:n,followOutput:y="auto",initialAlignment:u="bottom",scrollModifier:d=null,onStartReached:R,onEndReached:h,startReachedThreshold:p=300,endReachedThreshold:I=300,scrollToMessageKey:k,onScrollToMessageComplete:E}=i,a=ce({items:e,getKey:t,estimatedItemSize:s,overscan:l,initialAlignment:u}),w=Se(a.scrollerRef,{threshold:o,hysteresis:n??{enter:80,leave:160}}),[,g]=r.useState(0),F=r.useRef(null),T=r.useCallback(()=>{g(b=>b+1),F.current&&clearTimeout(F.current),F.current=setTimeout(()=>{a.jumpSuppressedRef.current=!1,F.current=null},200)},[a.jumpSuppressedRef]),{prepareAnchor:j}=ye({scrollerRef:a.scrollerRef,itemCount:e.length,captureAnchorSnapshot:a.captureAnchorSnapshot,resolveAnchorTop:a.resolveAnchorTop,onRestored:T}),L=r.useCallback(()=>{a.jumpSuppressedRef.current=!0,j()},[j,a.jumpSuppressedRef]),D=e.length>0?t(e[0],0):null,K=e.length>0?t(e[e.length-1],e.length-1):null;be({itemCount:e.length,firstKey:D,lastKey:K,isAtBottom:w,scrollToIndex:a.scrollToIndex,mode:y??!1});const H=r.useCallback((b="auto")=>{e.length!==0&&a.scrollToIndex(e.length-1,{align:"end",behavior:b})},[e.length,a]),f=r.useCallback((b,B)=>{const V=e.findIndex((Y,N)=>t(Y,N)===b);V!==-1&&a.scrollToIndex(V,B)},[e,t,a]),S=r.useRef(null);r.useLayoutEffect(()=>{if(d&&S.current!==d.id){if(S.current=d.id,d.type==="prepend"){L();return}if(d.type==="append"){if(d.ifAtBottomOnly&&!w)return;H(d.behavior??"auto");return}if(d.type==="items-change"){w&&H("auto");return}f(d.key,{align:d.align??"center",behavior:d.behavior??"auto"})}},[d,w,L,H,f]);const v=r.useRef(!1),C=r.useRef(u==="top");r.useEffect(()=>{const b=a.scrollerRef.current;if(!b||!R)return;const B=()=>{const V=b.scrollTop;C.current||(b.scrollHeight<=b.clientHeight+p||V>p)&&(C.current=!0),C.current&&V<=p&&!v.current&&(v.current=!0,Promise.resolve(R()).finally(()=>{v.current=!1}))};return b.addEventListener("scroll",B,{passive:!0}),B(),()=>b.removeEventListener("scroll",B)},[a.scrollerRef,R,p,u]);const P=r.useRef(!1);r.useEffect(()=>{const b=a.scrollerRef.current;if(!b||!h)return;const B=()=>{b.scrollHeight-b.scrollTop-b.clientHeight<=I&&!P.current&&(P.current=!0,Promise.resolve(h()).finally(()=>{P.current=!1}))};return b.addEventListener("scroll",B,{passive:!0}),()=>b.removeEventListener("scroll",B)},[a.scrollerRef,h,I]);const U=r.useRef(null);return r.useEffect(()=>{if(!k||U.current===k)return;const b=e.findIndex((B,V)=>t(B,V)===k);b!==-1&&(U.current=k,a.scrollToIndex(b,{align:"center",behavior:"auto"}),E==null||E())},[k,e,t,a,E]),{scrollerRef:a.scrollerRef,innerRef:a.innerRef,virtualItems:a.virtualItems,totalSize:a.totalSize,measureItem:a.measureItem,scrollToIndex:a.scrollToIndex,scrollToBottom:H,scrollToKey:f,isAtBottom:w,prepareAnchor:L}}function ie({virtualItem:i,measureItem:e,children:t}){const s=r.useRef(null),l=r.useRef(!1);return r.useEffect(()=>{const o=s.current;if(!o)return;l.current=!1;const n=new ResizeObserver(([y])=>{y&&(l.current=!0,e(i.key,y.contentRect.height))});return n.observe(o),()=>n.disconnect()},[i.key,e]),q.jsx("div",{ref:s,style:{position:"absolute",top:0,transform:`translateY(${i.start}px)`,width:"100%",minHeight:l.current?void 0:i.size},children:t})}function Ae(i,e){const{data:t,itemContent:s,computeItemKey:l,estimatedItemSize:o=80,overscan:n=20,followOutput:y="auto",atBottomThreshold:u=200,atBottomHysteresis:d,initialAlignment:R="bottom",scrollModifier:h,onStartReached:p,onEndReached:I,startReachedThreshold:k=300,endReachedThreshold:E=300,scrollToMessageKey:a,onScrollToMessageComplete:w,onAtBottomChange:g,components:F={},className:T,style:j}=i,{scrollerRef:L,innerRef:D,virtualItems:K,totalSize:H,measureItem:f,scrollToIndex:S,scrollToBottom:v,scrollToKey:C,isAtBottom:P,prepareAnchor:U}=le({items:t,getKey:(N,$)=>l($,N),estimatedItemSize:o,overscan:n,atBottomThreshold:u,atBottomHysteresis:d,followOutput:y,initialAlignment:R,scrollModifier:h,onStartReached:p,onEndReached:I,startReachedThreshold:k,endReachedThreshold:E,scrollToMessageKey:a,onScrollToMessageComplete:w}),b=te.useRef(P);te.useEffect(()=>{b.current!==P&&(b.current=P,g==null||g(P))},[P,g]),r.useImperativeHandle(e,()=>({scrollToBottom:v,scrollToIndex:S,scrollToKey:C,getScrollTop:()=>{var N;return((N=L.current)==null?void 0:N.scrollTop)??0},isAtBottom:()=>P,prepareAnchor:U}),[v,S,C,L,P,U]);const{Header:B,Footer:V,EmptyPlaceholder:Y}=F;return t.length===0&&Y?q.jsx(Y,{}):q.jsxs("div",{ref:L,className:T,style:{overflow:"auto",height:"100%",position:"relative",overflowAnchor:"none",overscrollBehaviorY:"contain",...j},children:[B&&q.jsx(B,{}),q.jsx("div",{ref:D,style:{height:H,position:"relative",width:"100%"},children:K.map(N=>q.jsx(ie,{virtualItem:N,measureItem:f,children:s(N.index,N.data)},N.key))}),V&&q.jsx(V,{})]})}const ze=r.forwardRef(Ae);function xe({data:i,itemContent:e,computeItemKey:t,estimatedItemSize:s=60,overscan:l=20,onEndReached:o,endReachedThreshold:n=300,components:y={},className:u,style:d}){const{scrollerRef:R,innerRef:h,virtualItems:p,totalSize:I,measureItem:k}=ce({items:i,getKey:(g,F)=>t(F,g),estimatedItemSize:s,overscan:l,initialAlignment:"top"});te.useEffect(()=>{const g=R.current;if(!g||!o)return;let F=!1;const T=()=>{g.scrollHeight-g.scrollTop-g.clientHeight<=n&&!F&&(F=!0,Promise.resolve(o()).finally(()=>{F=!1}))};return g.addEventListener("scroll",T,{passive:!0}),()=>g.removeEventListener("scroll",T)},[R,o,n]);const{Header:E,Footer:a,EmptyPlaceholder:w}=y;return i.length===0&&w?q.jsx(w,{}):q.jsxs("div",{ref:R,className:u,style:{overflow:"auto",height:"100%",position:"relative",overflowAnchor:"none",overscrollBehaviorY:"contain",...d},children:[E&&q.jsx(E,{}),q.jsx("div",{ref:h,style:{height:I,position:"relative",width:"100%"},children:p.map(g=>q.jsx(ie,{virtualItem:g,measureItem:k,children:e(g.index,g.data)},g.key))}),a&&q.jsx(a,{})]})}function Ie(i){const{fetcher:e,initialPage:t=1,direction:s="append",getKey:l,onPageLoaded:o,onError:n}=i,[y,u]=r.useState([]),[d,R]=r.useState(t),[h,p]=r.useState(!0),[I,k]=r.useState(!1),[E,a]=r.useState(!1),[w,g]=r.useState(!1),F=r.useRef(new Set),T=r.useRef(!1),j=r.useCallback(f=>l?f.filter(S=>{const v=l(S);return F.current.has(v)?!1:(F.current.add(v),!0)}):f,[l]),L=r.useCallback(async()=>{if(!(T.current||!h)){T.current=!0,g(!0);try{const f=d+1,S=await e(f),v=j(S.data);u(C=>s==="prepend"?[...v,...C]:[...C,...v]),R(f),p(S.hasNextPage),k(S.hasPrevPage),o==null||o(f,v)}catch(f){n==null||n(f instanceof Error?f:new Error(String(f)))}finally{g(!1),T.current=!1}}},[d,h,e,j,s,o,n]),D=r.useCallback(async()=>{if(!(T.current||!I)){T.current=!0,g(!0);try{const f=d-1,S=await e(f),v=j(S.data);u(C=>[...v,...C]),R(f),k(S.hasPrevPage),p(S.hasNextPage),o==null||o(f,v)}catch(f){n==null||n(f instanceof Error?f:new Error(String(f)))}finally{g(!1),T.current=!1}}},[d,I,e,j,o,n]),K=r.useCallback(async()=>{if(!T.current){T.current=!0,a(!0);try{const f=await e(t),S=f.data;if(l){const v=new Set(S.map(l));S.forEach(C=>F.current.add(l(C))),u(C=>{const P=C.filter(U=>!v.has(l(U)));return s==="prepend"?[...S,...P]:[...P,...S]})}else u(S);R(t),p(f.hasNextPage),k(f.hasPrevPage),o==null||o(t,S)}catch(f){n==null||n(f instanceof Error?f:new Error(String(f)))}finally{a(!1),T.current=!1}}},[e,t,l,s,o,n]),H=r.useCallback(()=>{u([]),R(t),p(!0),k(!1),a(!1),g(!1),F.current.clear(),T.current=!1},[t]);return{items:y,loadNextPage:L,loadPrevPage:D,hasNextPage:h,hasPrevPage:I,loading:E,loadingMore:w,refresh:K,reset:H,currentPage:d}}exports.ChatVirtualList=ze;exports.VirtualList=xe;exports.useChatVirtualizer=le;exports.usePagination=Ie;