const e=require("../_util/classNames.js"),t=require("../_util/raf.js"),n=require("../_util/supportsPassive.js"),r=require("../_util/createRef.js");let i=require("vue");var a=20;function o(e){return`touches`in e?e.touches[0].pageY:e.pageY}var s=(0,i.defineComponent)({compatConfig:{MODE:3},name:`ScrollBar`,inheritAttrs:!1,props:{prefixCls:String,scrollTop:Number,scrollHeight:Number,height:Number,count:Number,onScroll:{type:Function},onStartMove:{type:Function},onStopMove:{type:Function}},setup(){return{moveRaf:null,scrollbarRef:r.default(),thumbRef:r.default(),visibleTimeout:null,state:(0,i.reactive)({dragging:!1,pageY:null,startTop:null,visible:!1})}},watch:{scrollTop:{handler(){this.delayHidden()},flush:`post`}},mounted(){this.scrollbarRef.current?.addEventListener(`touchstart`,this.onScrollbarTouchStart,n.default?{passive:!1}:!1),this.thumbRef.current?.addEventListener(`touchstart`,this.onMouseDown,n.default?{passive:!1}:!1)},beforeUnmount(){this.removeEvents(),clearTimeout(this.visibleTimeout)},methods:{delayHidden(){clearTimeout(this.visibleTimeout),this.state.visible=!0,this.visibleTimeout=setTimeout(()=>{this.state.visible=!1},2e3)},onScrollbarTouchStart(e){e.preventDefault()},onContainerMouseDown(e){e.stopPropagation(),e.preventDefault()},patchEvents(){window.addEventListener(`mousemove`,this.onMouseMove),window.addEventListener(`mouseup`,this.onMouseUp),this.thumbRef.current.addEventListener(`touchmove`,this.onMouseMove,n.default?{passive:!1}:!1),this.thumbRef.current.addEventListener(`touchend`,this.onMouseUp)},removeEvents(){window.removeEventListener(`mousemove`,this.onMouseMove),window.removeEventListener(`mouseup`,this.onMouseUp),this.scrollbarRef.current.removeEventListener(`touchstart`,this.onScrollbarTouchStart,n.default?{passive:!1}:!1),this.thumbRef.current&&(this.thumbRef.current.removeEventListener(`touchstart`,this.onMouseDown,n.default?{passive:!1}:!1),this.thumbRef.current.removeEventListener(`touchmove`,this.onMouseMove,n.default?{passive:!1}:!1),this.thumbRef.current.removeEventListener(`touchend`,this.onMouseUp)),t.default.cancel(this.moveRaf)},onMouseDown(e){let{onStartMove:t}=this.$props;Object.assign(this.state,{dragging:!0,pageY:o(e),startTop:this.getTop()}),t(),this.patchEvents(),e.stopPropagation(),e.preventDefault()},onMouseMove(e){let{dragging:n,pageY:r,startTop:i}=this.state,{onScroll:a}=this.$props;if(t.default.cancel(this.moveRaf),n){let n=i+(o(e)-r),s=this.getEnableScrollRange(),c=this.getEnableHeightRange(),l=c?n/c:0,u=Math.ceil(l*s);this.moveRaf=t.default(()=>{a(u)})}},onMouseUp(){let{onStopMove:e}=this.$props;this.state.dragging=!1,e(),this.removeEvents()},getSpinHeight(){let{height:e,scrollHeight:t}=this.$props,n=e/t*100;return n=Math.max(n,a),n=Math.min(n,e/2),Math.floor(n)},getEnableScrollRange(){let{scrollHeight:e,height:t}=this.$props;return e-t||0},getEnableHeightRange(){let{height:e}=this.$props;return e-this.getSpinHeight()||0},getTop(){let{scrollTop:e}=this.$props,t=this.getEnableScrollRange(),n=this.getEnableHeightRange();return e===0||t===0?0:e/t*n},showScroll(){let{height:e,scrollHeight:t}=this.$props;return t>e}},render(){let{dragging:t,visible:n}=this.state,{prefixCls:r}=this.$props,a=`${this.getSpinHeight()}px`,o=`${this.getTop()}px`,s=this.showScroll(),c=s&&n;return(0,i.createVNode)(`div`,{ref:this.scrollbarRef,class:e.default(`${r}-scrollbar`,{[`${r}-scrollbar-show`]:s}),style:{width:`8px`,top:0,bottom:0,right:0,position:`absolute`,display:c?void 0:`none`},onMousedown:this.onContainerMouseDown,onMousemove:this.delayHidden},[(0,i.createVNode)(`div`,{ref:this.thumbRef,class:e.default(`${r}-scrollbar-thumb`,{[`${r}-scrollbar-thumb-moving`]:t}),style:{width:`100%`,height:a,top:o,left:0,position:`absolute`,background:`rgba(0, 0, 0, 0.5)`,borderRadius:`99px`,cursor:`pointer`,userSelect:`none`},onMousedown:this.onMouseDown},null)])}});exports.default=s;