import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
import React, { useState, useRef, useEffect, useCallback } from 'react';
import PropTypes from 'prop-types';
import { DOMMouseMoveTracker, addStyle, getWidth } from 'dom-lib';
import Tooltip from '../Tooltip';
import { useClassNames, mergeRefs } from '../utils';
var defaultProps = {
  as: 'div',
  classPrefix: 'slider'
};
var Handle = /*#__PURE__*/React.forwardRef(function (props, ref) {
  var _extends2;

  var Component = props.as,
      classPrefix = props.classPrefix,
      className = props.className,
      disabled = props.disabled,
      style = props.style,
      children = props.children,
      position = props.position,
      vertical = props.vertical,
      tooltip = props.tooltip,
      rtl = props.rtl,
      value = props.value,
      renderTooltip = props.renderTooltip,
      onDragStart = props.onDragStart,
      onDragMove = props.onDragMove,
      onDragEnd = props.onDragEnd,
      rest = _objectWithoutPropertiesLoose(props, ["as", "classPrefix", "className", "disabled", "style", "children", "position", "vertical", "tooltip", "rtl", "value", "renderTooltip", "onDragStart", "onDragMove", "onDragEnd"]);

  var _useState = useState(false),
      active = _useState[0],
      setActive = _useState[1];

  var rootRef = useRef();
  var horizontalKey = rtl ? 'right' : 'left';
  var direction = vertical ? 'top' : horizontalKey;

  var styles = _extends({}, style, (_extends2 = {}, _extends2[direction] = position + "%", _extends2));

  var _useClassNames = useClassNames(classPrefix),
      merge = _useClassNames.merge,
      prefix = _useClassNames.prefix;

  var handleClasses = merge(className, prefix('handle'), {
    active: active
  });
  var tooltipRef = useRef();
  var mouseMoveTracker = useRef();
  var releaseMouseMoves = useCallback(function () {
    var _mouseMoveTracker$cur;

    (_mouseMoveTracker$cur = mouseMoveTracker.current) === null || _mouseMoveTracker$cur === void 0 ? void 0 : _mouseMoveTracker$cur.releaseMouseMoves();
    mouseMoveTracker.current = null;
  }, []);
  var setTooltipPosition = useCallback(function () {
    var tooltipElement = tooltipRef.current;

    if (tooltip && tooltipElement) {
      var width = getWidth(tooltipElement);
      addStyle(tooltipElement, 'left', "-" + width / 2 + "px");
    }
  }, [tooltip]);
  var handleDragMove = useCallback(function (_deltaX, _deltaY, event) {
    var _mouseMoveTracker$cur2;

    if ((_mouseMoveTracker$cur2 = mouseMoveTracker.current) === null || _mouseMoveTracker$cur2 === void 0 ? void 0 : _mouseMoveTracker$cur2.isDragging()) {
      onDragMove === null || onDragMove === void 0 ? void 0 : onDragMove(event, rootRef.current.dataset);
      setTooltipPosition();
    }
  }, [onDragMove, setTooltipPosition]);
  var handleDragEnd = useCallback(function (event) {
    setActive(false);
    releaseMouseMoves();
    onDragEnd === null || onDragEnd === void 0 ? void 0 : onDragEnd(event, rootRef.current.dataset);
  }, [onDragEnd, releaseMouseMoves]);
  var getMouseMoveTracker = useCallback(function () {
    return mouseMoveTracker.current || new DOMMouseMoveTracker(handleDragMove, handleDragEnd, document.body);
  }, [handleDragEnd, handleDragMove]);
  var handleMouseDown = useCallback(function (event) {
    var _mouseMoveTracker$cur3, _rootRef$current;

    if (disabled) {
      return;
    }

    mouseMoveTracker.current = getMouseMoveTracker();
    (_mouseMoveTracker$cur3 = mouseMoveTracker.current) === null || _mouseMoveTracker$cur3 === void 0 ? void 0 : _mouseMoveTracker$cur3.captureMouseMoves(event);
    (_rootRef$current = rootRef.current) === null || _rootRef$current === void 0 ? void 0 : _rootRef$current.focus();
    setActive(true);
    onDragStart === null || onDragStart === void 0 ? void 0 : onDragStart(event);
  }, [disabled, getMouseMoveTracker, onDragStart]);
  var handleMouseEnter = useCallback(function () {
    setTooltipPosition();
  }, [setTooltipPosition]);
  useEffect(function () {
    return function () {
      releaseMouseMoves();
    };
  }, [releaseMouseMoves]);
  return /*#__PURE__*/React.createElement(Component, _extends({}, rest, {
    ref: mergeRefs(ref, rootRef),
    className: handleClasses,
    onMouseDown: handleMouseDown,
    onMouseEnter: handleMouseEnter,
    style: styles
  }), tooltip && /*#__PURE__*/React.createElement(Tooltip, {
    ref: tooltipRef,
    className: merge(prefix('tooltip'), 'placement-top')
  }, renderTooltip ? renderTooltip(value) : value), children);
});
Handle.displayName = 'Handle';
Handle.defaultProps = defaultProps;
Handle.propTypes = {
  as: PropTypes.elementType,
  className: PropTypes.string,
  classPrefix: PropTypes.string,
  children: PropTypes.node,
  disabled: PropTypes.bool,
  vertical: PropTypes.bool,
  tooltip: PropTypes.bool,
  rtl: PropTypes.bool,
  position: PropTypes.number,
  value: PropTypes.number,
  renderTooltip: PropTypes.func,
  style: PropTypes.object,
  onDragMove: PropTypes.func,
  onDragStart: PropTypes.func,
  onDragEnd: PropTypes.func
};
export default Handle;