'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _rollupPluginBabelHelpers = require('../../../../_virtual/_rollupPluginBabelHelpers.js');
var React = require('react');
var Draggable = require('react-draggable');
var index = require('../../../../utils/index.js');
var index$1 = require('../../utils/index.js');
var Thumb = require('../Thumb/Thumb.js');
var Slider_tokens = require('../../Slider.tokens.js');
var Handler_styles = require('./Handler.styles.js');
var computeKeyPressData = require('./computeKeyPressData.js');
var useForkRef = require('../../../../hooks/useForkRef.js');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var React__default = /*#__PURE__*/_interopDefault(React);
var Draggable__default = /*#__PURE__*/_interopDefault(Draggable);

var _excluded = ["size", "visibility", "isHovered", "orientation", "stepSize", "onChangeCommitted", "onChange", "position", "min", "max", "bounds", "zIndex", "disabled", "side", "showCurrentValue", "startOffset", "endOffset", "value", "valuePlacement"];

// TODO: PLASMA-1707

var Handler = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
  var size = _ref.size,
    visibility = _ref.visibility,
    isHovered = _ref.isHovered,
    orientation = _ref.orientation,
    stepSize = _ref.stepSize,
    onChangeCommitted = _ref.onChangeCommitted,
    onChange = _ref.onChange,
    _ref$position = _ref.position,
    position = _ref$position === void 0 ? 0 : _ref$position,
    min = _ref.min,
    max = _ref.max,
    _ref$bounds = _ref.bounds,
    bounds = _ref$bounds === void 0 ? [] : _ref$bounds,
    zIndex = _ref.zIndex,
    disabled = _ref.disabled,
    side = _ref.side,
    _ref$showCurrentValue = _ref.showCurrentValue,
    showCurrentValue = _ref$showCurrentValue === void 0 ? false : _ref$showCurrentValue,
    _ref$startOffset = _ref.startOffset,
    startOffset = _ref$startOffset === void 0 ? 0 : _ref$startOffset,
    _ref$endOffset = _ref.endOffset,
    endOffset = _ref$endOffset === void 0 ? 0 : _ref$endOffset,
    value = _ref.value,
    valuePlacement = _ref.valuePlacement,
    rest = _rollupPluginBabelHelpers.objectWithoutProperties(_ref, _excluded);
  var nodeRef = React.useRef(null);
  var combinedRef = useForkRef.useForkRef(nodeRef, ref);
  var isVertical = orientation === 'vertical';
  var _useState = React.useState(false),
    _useState2 = _rollupPluginBabelHelpers.slicedToArray(_useState, 2),
    isDrag = _useState2[0],
    setIsDrag = _useState2[1];
  var lastOnChangeValue = React.useRef();
  var _getOffsets = index$1.getOffsets(nodeRef, side, isVertical),
    _getOffsets2 = _rollupPluginBabelHelpers.slicedToArray(_getOffsets, 2),
    startClientOffset = _getOffsets2[0],
    endClientOffset = _getOffsets2[1];
  var _bounds = _rollupPluginBabelHelpers.slicedToArray(bounds, 2),
    startValueBound = _bounds[0],
    endValueBound = _bounds[1];
  var startPositionBound = startValueBound ? (startValueBound - min) * stepSize : null;
  var endPositionBound = endValueBound ? (endValueBound - min) * stepSize : null;
  var dragPosition = typeof position === 'number' ? {
    x: isVertical ? 0 : position,
    y: isVertical ? position : 0
  } : undefined;
  var tabIndex = disabled ? -1 : 0;
  var computedBounds = _rollupPluginBabelHelpers.defineProperty(_rollupPluginBabelHelpers.defineProperty({}, isVertical ? 'top' : 'left', (startPositionBound !== null && startPositionBound !== void 0 ? startPositionBound : 0) - (startClientOffset ? stepSize : 0)), isVertical ? 'bottom' : 'right', (endPositionBound !== null && endPositionBound !== void 0 ? endPositionBound : stepSize * (max - min)) - (endClientOffset ? stepSize : 0));
  var showCurrentValueCondition = showCurrentValue && (position >= startOffset && position <= max * stepSize - endOffset || position === 0 && value !== 0);
  var onDrag = function onDrag(_, data) {
    var newValue = index$1.getSliderThumbValue(isVertical ? data.y : data.x, stepSize, min, max);
    if (lastOnChangeValue.current !== newValue) {
      onChange === null || onChange === void 0 || onChange(newValue, data);
      lastOnChangeValue.current = newValue;
    }
  };
  var onStart = function onStart() {
    setIsDrag(true);
  };
  var onStop = function onStop(_, data) {
    setIsDrag(false);
    var newValue = index$1.getSliderThumbValue(isVertical ? data.y : data.x, stepSize, min, max);
    onChangeCommitted && onChangeCommitted(newValue, data);
  };
  var onKeyPress = function onKeyPress(event) {
    event.persist();
    var computedMultipleSteps = stepSize * (rest.multipleStepSize / 100 * max);
    var data = computeKeyPressData.computeKeyPressData(event, {
      isVertical: isVertical,
      stepSize: stepSize,
      position: position,
      max: max,
      computedMultipleSteps: computedMultipleSteps
    });
    if (!data) {
      return;
    }
    var left = computedBounds.left,
      right = computedBounds.right,
      top = computedBounds.top,
      bottom = computedBounds.bottom;

    /*
     * INFO: Находим значение в диапазоне между указанными левой и правой границами.
     * Или между верхней и нижней
     * Необходимо для правильного расчета положения SliderThumb.
     * см. функция clamp
     */
    var boundedValue = isVertical ? Math.max(Math.min(bottom, data.y), top) : Math.max(Math.min(right, data.x), left);
    var computedValue = index$1.getSliderThumbValue(boundedValue, stepSize, min, max);
    lastOnChangeValue.current = computedValue;
    onChangeCommitted && onChangeCommitted(computedValue, data);
  };
  var isPointerHidden = visibility === 'hover' && !isHovered || size === 'none';
  if (isDrag) {
    isPointerHidden = false;
  }
  return /*#__PURE__*/React__default.default.createElement(Draggable__default.default, {
    nodeRef: nodeRef,
    axis: isVertical ? 'y' : 'x',
    bounds: computedBounds,
    grid: isVertical ? [1, stepSize] : [stepSize, 1],
    onStart: onStart,
    onStop: onStop,
    onDrag: onDrag,
    position: dragPosition,
    disabled: disabled
  }, /*#__PURE__*/React__default.default.createElement(Handler_styles.HandlerStyled, {
    ref: combinedRef,
    style: {
      zIndex: zIndex
    },
    className: index.cx(isVertical && Slider_tokens.classes.verticalOrientation, valuePlacement === 'left' && Slider_tokens.classes.valuePlacementLeft),
    isLarge: size === 'large',
    isPointerHidden: isPointerHidden,
    onKeyDown: onKeyPress
  }, !isPointerHidden && /*#__PURE__*/React__default.default.createElement(Thumb.Thumb, _rollupPluginBabelHelpers.extends({
    tabIndex: tabIndex,
    min: min,
    max: max,
    value: value,
    disabled: disabled,
    orientation: orientation
  }, rest)), showCurrentValueCondition && /*#__PURE__*/React__default.default.createElement(Handler_styles.StyledValue, {
    isPointerHidden: isPointerHidden
  }, value)));
});

exports.Handler = Handler;
//# sourceMappingURL=Handler.js.map
