"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));

var _react = _interopRequireWildcard(require("react"));

var _bem = require("@zarm-design/bem");

var _react2 = require("@use-gesture/react");

var _isPlainObject = _interopRequireDefault(require("lodash/isPlainObject"));

var _events = _interopRequireDefault(require("../utils/events"));

var _tooltip = _interopRequireDefault(require("../tooltip"));

var _ensureValuePrecision = _interopRequireDefault(require("./utils/ensureValuePrecision"));

var _getValue = _interopRequireDefault(require("./utils/getValue"));

var _configProvider = require("../config-provider");

var Slider = /*#__PURE__*/_react.default.forwardRef(function (props, ref) {
  var _React$useContext = _react.default.useContext(_configProvider.ConfigContext),
      prefixCls = _React$useContext.prefixCls;

  var bem = (0, _bem.createBEM)('slider', {
    prefixCls: prefixCls
  });
  var className = props.className,
      disabled = props.disabled,
      min = props.min,
      max = props.max,
      vertical = props.vertical,
      showMark = props.showMark,
      value = props.value,
      marks = props.marks,
      onChange = props.onChange,
      onSlideChange = props.onSlideChange,
      defaultValue = props.defaultValue,
      style = props.style;

  var _useState = (0, _react.useState)(false),
      _useState2 = (0, _slicedToArray2.default)(_useState, 2),
      tooltip = _useState2[0],
      setTooltip = _useState2[1];

  var _React$useState = _react.default.useState((0, _getValue.default)(props, 0)),
      _React$useState2 = (0, _slicedToArray2.default)(_React$useState, 2),
      currentValue = _React$useState2[0],
      setCurrentValue = _React$useState2[1];

  var lineRef = _react.default.useRef(null);

  var getMaxOffset = function getMaxOffset() {
    var _lineRef$current;

    var divRect = lineRef === null || lineRef === void 0 ? void 0 : (_lineRef$current = lineRef.current) === null || _lineRef$current === void 0 ? void 0 : _lineRef$current.getBoundingClientRect();
    return (vertical ? divRect === null || divRect === void 0 ? void 0 : divRect.height : divRect === null || divRect === void 0 ? void 0 : divRect.width) || 0;
  };

  var getOffsetByValue = function getOffsetByValue(val) {
    var maxOffset = getMaxOffset();
    var range = max - min;
    return vertical ? maxOffset * ((max - val) / range) : maxOffset * ((val - min) / range);
  };

  var getValueByOffset = function getValueByOffset(offset) {
    var maxOffset = getMaxOffset();
    var percent = offset / maxOffset;
    var val = vertical ? (1 - percent) * (max - min) + min : Math.round(min + (max - min) * percent);
    return (0, _ensureValuePrecision.default)(val, props);
  };

  var offsetStart = (0, _react.useRef)(0);
  (0, _react.useEffect)(function () {
    var val = (0, _getValue.default)({
      defaultValue: defaultValue,
      value: value
    }, 0);
    setCurrentValue(val);
  }, [defaultValue, value]);
  var trackClick = (0, _react.useCallback)(function (event) {
    event.stopPropagation();
    if (disabled) return;
    var line = lineRef.current;
    if (!line) return;

    var _line$getBoundingClie = line.getBoundingClientRect(),
        left = _line$getBoundingClie.left,
        top = _line$getBoundingClie.top;

    var offset = vertical ? event.clientY - top : event.clientX - left;
    var current = getValueByOffset(offset);
    current = Math.min(max, Math.max(min, current));
    setCurrentValue(current);

    if (typeof onChange === 'function') {
      onChange(current);
    }
  }, [onChange, max, min]);
  var bind = (0, _react2.useDrag)(function (state) {
    var offsetX = state.xy[0] - state.initial[0],
        offsetY = state.xy[1] - state.initial[1];
    state.event.stopPropagation();

    if (state.first) {
      offsetStart.current = getOffsetByValue(currentValue);
      setTooltip(true);
    }

    var offset = vertical ? offsetY : offsetX;
    offset += offsetStart.current;
    var maxOffset = getMaxOffset();
    offset = Math.min(maxOffset, Math.max(offset, 0));
    var current = getValueByOffset(offset);
    setCurrentValue(current);

    if (state.dragging && !state.first) {
      onSlideChange === null || onSlideChange === void 0 ? void 0 : onSlideChange(current);
    }

    if (state.last) {
      setTooltip(false);
      onChange === null || onChange === void 0 ? void 0 : onChange(currentValue);
    }
  }, {
    enabled: !props.disabled,
    axis: vertical ? 'y' : 'x',
    pointer: {
      touch: true
    },
    preventDefault: !_events.default.supportsPassiveEvents
  });

  var renderMark = function renderMark() {
    var isEmptyMarks = !(0, _isPlainObject.default)(marks) || JSON.stringify(marks) === '{}';

    if (showMark && isEmptyMarks) {
      console.error('请输入有效的 marks');
      return null;
    } // 判断是否为空对象


    if (isEmptyMarks) {
      return null;
    }

    var markKeys = Object.keys(marks || {});
    var lineDot = markKeys.map(function (item) {
      var dotStyle = bem('dot', [{
        active: currentValue >= +item
      }]);
      var markStyle = (0, _defineProperty2.default)({}, "".concat(vertical ? 'bottom' : 'left'), "".concat(item, "%"));
      return /*#__PURE__*/_react.default.createElement("span", {
        key: item,
        className: dotStyle,
        style: markStyle
      });
    });

    if (!showMark) {
      return lineDot;
    }

    var marksElement = markKeys.map(function (item) {
      var markStyle = (0, _defineProperty2.default)({}, "".concat(vertical ? 'bottom' : 'left'), "".concat(item, "%"));
      return /*#__PURE__*/_react.default.createElement("span", {
        key: item,
        className: bem('mark'),
        style: markStyle
      }, marks === null || marks === void 0 ? void 0 : marks[+item]);
    });
    return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, lineDot, /*#__PURE__*/_react.default.createElement("div", {
      className: bem('marks')
    }, marksElement));
  };

  var cls = bem([{
    disabled: disabled,
    vertical: vertical,
    marked: showMark
  }, className]);
  var ratio = (currentValue - min) / (max - min);
  var offset = "".concat(ratio * 100, "%");
  var knobStyle = (0, _defineProperty2.default)({}, "".concat(vertical ? 'bottom' : 'left'), offset || 0);
  var lineBg = (0, _defineProperty2.default)({}, "".concat(vertical ? 'height' : 'width'), offset || 0);
  return /*#__PURE__*/_react.default.createElement("div", {
    className: cls,
    ref: ref,
    style: style
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: bem('content')
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: bem('line'),
    ref: lineRef,
    onClick: trackClick
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: bem('line__bg'),
    style: lineBg
  }), renderMark()), /*#__PURE__*/_react.default.createElement("div", (0, _extends2.default)({
    className: bem('knob'),
    role: "slider",
    "aria-valuemin": min,
    "aria-valuemax": max,
    "aria-valuenow": value,
    "aria-orientation": vertical ? 'vertical' : 'horizontal',
    style: knobStyle
  }, bind()), /*#__PURE__*/_react.default.createElement(_tooltip.default, {
    trigger: "manual",
    arrowPointAtCenter: true,
    visible: tooltip,
    content: currentValue
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: bem('knob__shadow')
  })))));
});

Slider.displayName = 'Slider';
Slider.defaultProps = {
  disabled: false,
  showMark: false,
  vertical: false,
  step: 1,
  min: 0,
  max: 100,
  marks: {}
};
var _default = Slider;
exports.default = _default;