"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
exports.__esModule = true;
exports["default"] = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _react = _interopRequireWildcard(require("react"));
var _rcSlider = require("rc-slider");
var _Focusable = require("../common/Focusable");
var _constants = require("./constants");
var _SliderSt = require("./Slider.st.css.js");
var _Tooltip = _interopRequireDefault(require("../Tooltip"));
var _utils = require("./utils");
var _jsxFileName = "/home/builduser/work/57e038ea7326c1ec/packages/wix-design-system/dist/cjs/Slider/SliderHandle.jsx",
  _this = void 0;
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
var SliderHandle = function SliderHandle(_ref) {
  var tooltipValue = _ref.tooltipValue,
    className = _ref.className,
    _ref$onFocusCustom = _ref.onFocusCustom,
    onFocusCustom = _ref$onFocusCustom === void 0 ? function () {} : _ref$onFocusCustom,
    _ref$onBlurCustom = _ref.onBlurCustom,
    onBlurCustom = _ref$onBlurCustom === void 0 ? function () {} : _ref$onBlurCustom,
    focusableOnFocus = _ref.focusableOnFocus,
    focusableOnBlur = _ref.focusableOnBlur,
    disabled = _ref.disabled,
    handleProps = _ref.handleProps,
    _ref$displayTooltip = _ref.displayTooltip,
    displayTooltip = _ref$displayTooltip === void 0 ? true : _ref$displayTooltip,
    index = _ref.index,
    dataHook = _ref.dataHook,
    direction = _ref.direction,
    gradientColor = _ref.gradientColor;
  var min = handleProps.min,
    max = handleProps.max,
    value = handleProps.value;
  var gradientValue = (value - min) / (max - min);
  var _useState = (0, _react.useState)(false),
    _useState2 = (0, _slicedToArray2["default"])(_useState, 2),
    hovered = _useState2[0],
    setHovered = _useState2[1];
  var tooltipRef = _react["default"].useRef(null);
  var handleMouseEnter = function handleMouseEnter() {
    setHovered(true);
  };
  var handleMouseLeave = function handleMouseLeave() {
    setHovered(false);
  };
  var handleOnFocus = function handleOnFocus(event) {
    focusableOnFocus();
    tooltipRef.current.open();
    onFocusCustom(Number(tooltipValue), event);
  };
  var handleOnBlur = function handleOnBlur(event) {
    focusableOnBlur();
    tooltipRef.current.close();
    onBlurCustom(Number(tooltipValue), event);
  };
  var tooltipDataHook = dataHook ? "".concat(dataHook, "-").concat(_constants.dataHooks.sliderTooltip, "-").concat(index) : "".concat(_constants.dataHooks.sliderTooltip, "-").concat(index);
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _SliderSt.st)(_SliderSt.classes.sliderHandleWrapper, {}, className),
    onBlur: handleOnBlur,
    __self: _this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 55,
      columnNumber: 5
    }
  }, /*#__PURE__*/_react["default"].createElement(_rcSlider.Handle, (0, _extends2["default"])({}, handleProps, {
    vertical: direction === 'vertical',
    onFocus: handleOnFocus,
    className: (0, _SliderSt.st)(_SliderSt.classes.sliderHandle, {
      disabled: disabled
    }),
    onMouseEnter: handleMouseEnter,
    onMouseLeave: handleMouseLeave,
    "data-hook": _constants.dataHooks.sliderHandle,
    __self: _this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 59,
      columnNumber: 7
    }
  }), /*#__PURE__*/_react["default"].createElement(_Tooltip["default"], {
    ref: tooltipRef,
    className: _SliderSt.classes.sliderHandleTooltip,
    disabled: !displayTooltip || disabled || !tooltipValue,
    content: tooltipValue,
    moveBy: !!gradientColor ? {
      y: 3.5
    } : {
      y: 3
    },
    dataHook: tooltipDataHook,
    __self: _this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 68,
      columnNumber: 9
    }
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _SliderSt.st)(_SliderSt.classes.sliderThumb, {
      hovered: hovered,
      hasGradient: !!gradientColor
    }),
    style: (0, _utils.getThumbStyle)({
      gradientColor: gradientColor,
      alpha: gradientValue,
      disabled: disabled
    }),
    "data-hook": _constants.dataHooks.sliderThumb,
    __self: _this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 76,
      columnNumber: 11
    }
  }))));
};
var _default = exports["default"] = (0, _Focusable.withFocusable)(SliderHandle);