import { slicedToArray as _slicedToArray } from '../../../_virtual/_rollupPluginBabelHelpers.js';
import { useState, useCallback, useEffect } from 'react';

var SCROLL_SPEED = 2;
var useDragScroll = function useDragScroll(scrollRef, isDragScrollDisabled) {
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    isDragging = _useState2[0],
    setDragging = _useState2[1];
  var _useState3 = useState(0),
    _useState4 = _slicedToArray(_useState3, 2),
    startX = _useState4[0],
    setStartX = _useState4[1];
  var _useState5 = useState(0),
    _useState6 = _slicedToArray(_useState5, 2),
    scrollLeft = _useState6[0],
    setScrollLeft = _useState6[1];
  var handleMouseMove = useCallback(function (e) {
    if (isDragging && scrollRef && scrollRef.current) {
      var dx = (e.clientX - startX) * SCROLL_SPEED;
      scrollRef.current.scrollLeft = scrollLeft - dx;
    }
  }, [scrollRef, scrollLeft, isDragging, startX]);
  var handleMouseUp = useCallback(function () {
    setDragging(false);
    if (scrollRef && scrollRef.current) {
      scrollRef.current.style.cursor = 'grab';
    }
  }, [scrollRef, setDragging]);
  var handleMouseDown = useCallback(function (e) {
    setDragging(true);
    if (scrollRef && scrollRef.current) {
      setStartX(e.clientX);
      setScrollLeft(scrollRef.current.scrollLeft);
      scrollRef.current.style.cursor = 'grabbing';
    }
  }, [scrollRef, setDragging, setStartX, setScrollLeft, handleMouseUp, handleMouseMove]);
  useEffect(function () {
    if (scrollRef && scrollRef.current && !isDragScrollDisabled) {
      scrollRef.current.style.userSelect = 'none';
      scrollRef.current.addEventListener('mousedown', handleMouseDown);
      scrollRef.current.addEventListener('mouseup', handleMouseUp);
      scrollRef.current.addEventListener('mouseleave', handleMouseUp);
      scrollRef.current.addEventListener('mousemove', handleMouseMove);
    }
    return function () {
      if (scrollRef && scrollRef.current && !isDragScrollDisabled) {
        scrollRef.current.removeEventListener('mousedown', handleMouseDown);
        scrollRef.current.removeEventListener('mouseup', handleMouseUp);
        scrollRef.current.removeEventListener('mouseleave', handleMouseUp);
        scrollRef.current.removeEventListener('mousemove', handleMouseMove);
      }
    };
  }, [scrollRef, handleMouseDown, handleMouseUp, handleMouseMove, isDragScrollDisabled]);
};

export { useDragScroll };
