import{defineComponent as C,shallowRef as u,watchEffect as T,ref as I,onMounted as b,onActivated as O,nextTick as A,watch as B,onBeforeUnmount as E,createVNode as w}from"vue";import N from"../_util/classNames.mjs";import x from"../_util/getScrollBarSize.mjs";import m from"../vc-util/Dom/addEventListener.mjs";import{getOffset as H}from"../vc-util/Dom/css.mjs";import{useInjectTable as U}from"./context/TableContext.mjs";import{useLayoutState as X}from"./hooks/useFrame.mjs";const P=C({name:"StickyScrollBar",inheritAttrs:!1,props:["offsetScroll","container","scrollBodyRef","scrollBodySizeInfo"],emits:["scroll"],setup(l,{emit:g,expose:p}){const M=U(),a=u(0),o=u(0),c=u(0);T(()=>{a.value=l.scrollBodySizeInfo.scrollWidth||0,o.value=l.scrollBodySizeInfo.clientWidth||0,c.value=a.value&&o.value*(o.value/a.value)},{flush:"post"});const R=u(),[f,d]=X({scrollLeft:0,isHiddenScrollBar:!0}),i=I({delta:0,x:0}),s=u(!1),W=()=>{s.value=!1},$=e=>{i.value={delta:e.pageX-f.value.scrollLeft,x:0},s.value=!0,e.preventDefault()},z=e=>{const{buttons:n}=e||window?.event;if(!s.value||n===0){s.value&&(s.value=!1);return}let t=i.value.x+e.pageX-i.value.x-i.value.delta;t<=0&&(t=0),t+c.value>=o.value&&(t=o.value-c.value),g("scroll",{scrollLeft:t/o.value*(a.value+2)}),i.value.x=e.pageX},v=()=>{if(!l.scrollBodyRef.value)return;const e=H(l.scrollBodyRef.value).top,n=e+l.scrollBodyRef.value.offsetHeight,t=l.container===window?document.documentElement.scrollTop+window.innerHeight:H(l.container).top+l.container.clientHeight;n-x()<=t||e>=t-l.offsetScroll?d(r=>({...r,isHiddenScrollBar:!0})):d(r=>({...r,isHiddenScrollBar:!1}))};p({setScrollLeft:e=>{d(n=>({...n,scrollLeft:e/a.value*o.value||0}))}});let h=null,y=null,L=null,S=null;b(()=>{h=m(document.body,"mouseup",W,!1),y=m(document.body,"mousemove",z,!1),L=m(window,"resize",v,!1)}),O(()=>{A(()=>{v()})}),b(()=>{setTimeout(()=>{B([c,s],()=>{v()},{immediate:!0,flush:"post"})})}),B(()=>l.container,()=>{S?.remove(),S=m(l.container,"scroll",v,!1)},{immediate:!0,flush:"post"}),E(()=>{h?.remove(),y?.remove(),S?.remove(),L?.remove()}),B(()=>({...f.value}),(e,n)=>{e.isHiddenScrollBar!==n?.isHiddenScrollBar&&!e.isHiddenScrollBar&&d(t=>{const r=l.scrollBodyRef.value;return r?{...t,scrollLeft:r.scrollLeft/r.scrollWidth*r.clientWidth}:t})},{immediate:!0});const k=x();return()=>{if(a.value<=o.value||!c.value||f.value.isHiddenScrollBar)return null;const{prefixCls:e}=M;return w("div",{style:{height:`${k}px`,width:`${o.value}px`,bottom:`${l.offsetScroll}px`},class:`${e}-sticky-scroll`},[w("div",{onMousedown:$,ref:R,class:N(`${e}-sticky-scroll-bar`,{[`${e}-sticky-scroll-bar-active`]:s.value}),style:{width:`${c.value}px`,transform:`translate3d(${f.value.scrollLeft}px, 0, 0)`}},null)])}}});export{P as default};
