function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

/*
 * @Author: codingfly
 * @Description:
 * @Date: 2022-02-21 08:08:15
 * @LastEditTime: 2022-06-14 14:21:57
 * @FilePath: \remaxui\src\slider\index.tsx
 */
import React, { useState, useEffect, useRef, useMemo } from 'react';
import { View } from 'remax/one';
import classnames from 'classnames';
import styles from './styles/index.module.less';
import createSelectorQuery from '../create-selector-query';
import { useConfig } from '../config-provider';
import { guid } from '../_utils';

var Slider = function Slider(props) {
  var _props$defaultValue = props.defaultValue,
      defaultValue = _props$defaultValue === void 0 ? 0 : _props$defaultValue,
      handleClassName = props.handleClassName,
      trackColor = props.trackColor,
      railClassName = props.railClassName,
      text = props.text,
      _props$disabled = props.disabled,
      disabled = _props$disabled === void 0 ? false : _props$disabled,
      _props$showPivot = props.showPivot,
      showPivot = _props$showPivot === void 0 ? true : _props$showPivot,
      _props$stroke = props.stroke,
      stroke = _props$stroke === void 0 ? '4px' : _props$stroke,
      _props$max = props.max,
      max = _props$max === void 0 ? 100 : _props$max,
      _props$min = props.min,
      min = _props$min === void 0 ? 0 : _props$min,
      _props$value = props.value,
      value = _props$value === void 0 ? 0 : _props$value,
      onChange = props.onChange;

  var _useConfig = useConfig(),
      brandPrimary = _useConfig.brandPrimary;

  var showColor = trackColor || brandPrimary;

  var _useState = useState(0),
      _useState2 = _slicedToArray(_useState, 2),
      rectW = _useState2[0],
      setRectW = _useState2[1];

  var _useState3 = useState(Math.round(defaultValue / (max - min) * 100)),
      _useState4 = _slicedToArray(_useState3, 2),
      step = _useState4[0],
      setStep = _useState4[1];

  var _useState5 = useState(0),
      _useState6 = _slicedToArray(_useState5, 2),
      startX = _useState6[0],
      setStartX = _useState6[1];

  var isFirstMount = useRef(true);
  var sliderId = useMemo(function () {
    return guid();
  }, []);
  useEffect(function () {
    !isFirstMount.current && setStep(value);
  }, [value]);
  useEffect(function () {
    var handleSliderWidth = function handleSliderWidth() {
      createSelectorQuery() // @ts-ignore
      .select(".slider_".concat(sliderId)).boundingClientRect().exec(function (rect) {
        var _rect$, _rect$2;

        var w = Array.isArray(rect) ? rect === null || rect === void 0 ? void 0 : (_rect$ = rect[0]) === null || _rect$ === void 0 ? void 0 : _rect$.width : rect === null || rect === void 0 ? void 0 : rect.width;
        var x = Array.isArray(rect) ? rect === null || rect === void 0 ? void 0 : (_rect$2 = rect[0]) === null || _rect$2 === void 0 ? void 0 : _rect$2.x : rect === null || rect === void 0 ? void 0 : rect.x;
        setRectW(w);
        setStartX(x);
      });
    };

    handleSliderWidth();
    isFirstMount.current = false;
  }, []);

  var getStepValues = function getStepValues(step) {
    if (step < 0) {
      return 0;
    } else if (step > 100) {
      return 100;
    }

    return step;
  };

  var setStepValues = function setStepValues(x) {
    if (!disabled) {
      var offsetX = x - startX;
      var percent = offsetX / rectW;
      var stepValue = Math.round(percent * 100);
      setStep(getStepValues(stepValue));
      onChange && onChange(Math.abs(getStepValues((max - min) * stepValue / 100)));
    }
  };

  return /*#__PURE__*/React.createElement(View, {
    className: classnames(styles.slider, _defineProperty({}, "slider_".concat(sliderId), true))
  }, /*#__PURE__*/React.createElement(View, {
    className: classnames(styles.rail, railClassName)
  }), /*#__PURE__*/React.createElement(View, {
    className: styles.track,
    style: Object.assign({
      width: "".concat(step, "%"),
      height: stroke,
      borderRadius: '4px',
      backgroundColor: showColor
    })
  }), showPivot && /*#__PURE__*/React.createElement(View, {
    className: classnames(styles.handle, handleClassName, disabled && styles.disabled),
    style: Object.assign({
      left: "".concat(step, "%"),
      border: "1Px solid ".concat(showColor),
      color: showColor
    }),
    onTouchMove: function onTouchMove(e) {
      return setStepValues(e.touches[0].clientX);
    }
  }, text ? text : "".concat(step, "%")));
};

export default Slider;