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