import _toConsumableArray from"@babel/runtime/helpers/toConsumableArray";import _slicedToArray from"@babel/runtime/helpers/slicedToArray";import{useCallback,useEffect,useMemo,useRef,useState}from"react";import{useClickOutside}from"../../../hooks/useClickOutside";import{useComponentSize}from"../../../hooks/useComponentSize/useComponentSize";import{useMutableRef}from"../../../hooks/useMutableRef/useMutableRef";import{getActiveValue,getNewValue,getValidValue,getValueByPosition,isNotRangeParams,isRangeParams,trackPosition,valueToSring}from"./helper";export var COUNT_STEPS=250;export function useSlider(a){var b=a.disabled,c=a.range,d=a.value,f=a.min,g=a.max,h=a.step,i=void 0===h?1:h,j=a.onChange,k=a.onAfterChange,l=a.sliderRef,m=a.containerRef,n=a.buttonRefs,o=g>f?f:0,p=g>f?g:100,q=useMemo(function(){if(!Array.isArray(i)&&1<=i){var a=Math.abs((p-o)/COUNT_STEPS);return a>i?a-a%i:Math.max(a,i)}return i},[g,f,i]),r=useState(d),s=_slicedToArray(r,2),t=s[0],u=s[1],v=useState(null),w=_slicedToArray(v,2),x=w[0],y=w[1],z=useState(null),A=_slicedToArray(z,2),B=A[0],C=A[1],D=useState(null),E=_slicedToArray(D,2),F=E[0],G=E[1],H=useRef(null),I=useComponentSize(l),J=useMutableRef(t),K=useMutableRef(j),L=useMutableRef(k);useClickOutside({isActive:!0,ignoreClicksInsideRefs:[m],handler:function handler(){G(null),H.current=null}}),useEffect(function(){b&&M("remove")},[b]);var M=function(a){var b="add"===a?"addEventListener":"removeEventListener";document[b]("mouseup",R),document[b]("touchend",R),document[b]("mousemove",Q),document[b]("touchmove",Q)};useEffect(function(){Array.isArray(t)?(O(t[0],0),O(t[1],1)):O(t,0)},[t]),useEffect(function(){valueToSring(d)!==valueToSring(t)&&(u(d),O(getActiveValue(d,H.current),0),G(null),H.current=null)},[d]),useEffect(function(){var a=Array.isArray(t)?[getNewValue(t[0],t[0],i,f,g,0),getNewValue(t[1],t[1],i,f,g,1)]:getNewValue(t,t,i,f,g,0);if(valueToSring(a)!==valueToSring(t)){var b;null===(b=K.current)||void 0===b?void 0:b.call(K,a,{})}},[i]),useEffect(function(){if("undefined"!=typeof d){var b=isRangeParams(a)?[getValidValue(a.value[0],f,g,i),getValidValue(a.value[1],f,g,i)]:getValidValue(a.value,f,g,i);u(b)}},[c,typeof d]),useEffect(function(){if(isRangeParams(a)&&a.value[0]>a.value[1]){var b,c=[a.value[1],a.value[1]];null===(b=K.current)||void 0===b?void 0:b.call(K,c,{})}},[]);var N=useCallback(function(a,c){if(!b&&"number"==typeof c&&"undefined"!=typeof t){a.preventDefault(),a.stopPropagation();var d=Array.isArray(i)?1:i||1,e=!1,h=getActiveValue(t,c);switch(a.key){case"ArrowUp":case"ArrowRight":case"+":e=!0;break;case"ArrowLeft":case"ArrowDown":case"-":e=!0,d*=-1;break;default:}if(e){var j;Array.isArray(i)&&i.forEach(function(a,b){"number"==typeof c&&h===a&&(0<=d?0===b?d=i[1]-o:d=("undefined"==typeof i[b+1]?p:i[b+1])-a:0===b?d=o-i[1]:d=("undefined"==typeof i[b-1]?o:i[b-1])-a)});var k=getNewValue(h+d,t,i,f,g,c);u(k),O(getActiveValue(k,c),c),null===(j=K.current)||void 0===j?void 0:j.call(K,k,{e:a})}}},[t,i,f,g]),O=function(a,b){if(l.current&&"number"==typeof b){var c=n[b].current||l.current,d=l.current.getBoundingClientRect(),e=d.x,f=d.width,g={y:c.offsetTop+c.offsetHeight+50,x:e+Math.abs((a-o)/(p-o))*f};0===b?y(g):C(g)}},P=function(a){if("number"!=typeof H.current)return d;var b=trackPosition(a),c=getValueByPosition(b,l,o,p,i);return getNewValue(c,t,q,f,g,H.current)},Q=function(a,b){var c=b||H.current;if("number"==typeof c){var d=P(a),e=getActiveValue(t,c),f=getActiveValue(d,c);if(u(d),valueToSring(e)!==valueToSring(f)){var g;null===(g=L.current)||void 0===g?void 0:g.call(L,d,{e:a})}}};useEffect(function(){if(isRangeParams(a)){var b;null===(b=a.value)||void 0===b?void 0:b.forEach(function(a,b){O(getActiveValue(a,H.current),0===b?0:1)})}isNotRangeParams(a)&&O(getActiveValue(d,H.current),0),H.current=null},[I,typeof d]);var R=useCallback(function(b){if(M("remove"),isRangeParams(a)&&Array.isArray(J.current)){var c,d=_toConsumableArray(J.current).sort(function(c,a){return+c-+a});null===(c=K.current)||void 0===c?void 0:c.call(K,d,{e:b})}if(isNotRangeParams(a)&&"number"==typeof J.current){var e;null===(e=K.current)||void 0===e?void 0:e.call(K,J.current,{e:b})}G(null),H.current=null},[d,Q]),S=useCallback(function(a){b||(G(a),H.current=a,M("add"))},[t,d,Q,b]);return{onKeyPress:N,onFocus:function onFocus(a,b){G(b),H.current=b},handlePress:S,onSliderClick:function onSliderClick(b){if(isNotRangeParams(a)){var c=getValueByPosition({x:b.pageX,y:b.pageY},l,o,p,i),e=getNewValue(c,t,q,f,g,H.current);if(e!==d){var h;u(e),O(getActiveValue(e,0),0),null===(h=K.current)||void 0===h?void 0:h.call(K,e,{e:b})}}},activeButton:F,popoverPosition:[x,B],currentValue:Array.isArray(t)?t:[t]}}
//# sourceMappingURL=useSlider.js.map