"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

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

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

var _one = require("remax/one");

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

var _classnames = _interopRequireDefault(require("classnames"));

var _icon = _interopRequireDefault(require("../icon"));

var _indexModule = _interopRequireDefault(require("./index.module.less"));

var _excluded = ["min", "max", "value", "disabledColor", "defaultValue", "step", "iconColor", "borderColor", "className", "iconCls", "numberCls", "numberDisabledCls", "onChange", "formatValue", "unit", "disabled"];

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

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; }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

var _default = function _default(_ref) {
  var _ref$min = _ref.min,
      min = _ref$min === void 0 ? Number.MIN_SAFE_INTEGER : _ref$min,
      _ref$max = _ref.max,
      max = _ref$max === void 0 ? Number.MAX_SAFE_INTEGER : _ref$max,
      value = _ref.value,
      _ref$disabledColor = _ref.disabledColor,
      disabledColor = _ref$disabledColor === void 0 ? '#ddd' : _ref$disabledColor,
      _ref$defaultValue = _ref.defaultValue,
      defaultValue = _ref$defaultValue === void 0 ? 0 : _ref$defaultValue,
      _ref$step = _ref.step,
      step = _ref$step === void 0 ? 1 : _ref$step,
      iconColor = _ref.iconColor,
      borderColor = _ref.borderColor,
      className = _ref.className,
      iconCls = _ref.iconCls,
      numberCls = _ref.numberCls,
      numberDisabledCls = _ref.numberDisabledCls,
      onChange = _ref.onChange,
      _ref$formatValue = _ref.formatValue,
      formatValue = _ref$formatValue === void 0 ? function (a) {
    return a;
  } : _ref$formatValue,
      unit = _ref.unit,
      disabled = _ref.disabled,
      restProps = _objectWithoutProperties(_ref, _excluded);

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

  var _useState = (0, _react.useState)(defaultValue),
      _useState2 = _slicedToArray(_useState, 2),
      showVal = _useState2[0],
      setShowVal = _useState2[1];
  /** 显示的值 */


  var realVal = (0, _react.useMemo)(function () {
    return value !== undefined ? value : showVal;
  }, [value, showVal]);
  /** 是否禁用最大图标点击 */

  var maxDisabled = (0, _react.useMemo)(function () {
    return disabled || realVal >= max;
  }, [max, disabled, realVal]);
  /** 是否禁用最小图标点击 */

  var minDisabled = (0, _react.useMemo)(function () {
    return disabled || realVal <= min;
  }, [min, disabled, realVal]);
  /** 选中图标颜色 */

  var iconSelectColor = (0, _react.useMemo)(function () {
    return iconColor || brandPrimary;
  }, [iconColor, brandPrimary]);
  /** 选中边框颜色 */

  var borderSelectColor = (0, _react.useMemo)(function () {
    return borderColor || brandPrimary;
  }, [borderColor, brandPrimary]);
  /** 精度 */

  var precision = (0, _react.useMemo)(function () {
    var _step$toString$split$;

    return (_step$toString$split$ = step.toString().split('.')[1]) === null || _step$toString$split$ === void 0 ? void 0 : _step$toString$split$.length;
  }, [step]);
  /** 解决精度问题 如 0.0000000001 */

  var formatResult = function formatResult(val) {
    return Number(val.toFixed(precision || 0));
  };

  var handleAdd = function handleAdd() {
    if (maxDisabled) {
      return;
    }

    var value = formatResult(realVal + step);
    onChange === null || onChange === void 0 ? void 0 : onChange(value);
    setShowVal(value);
  };

  var handleSubtract = function handleSubtract() {
    if (minDisabled) {
      return;
    }

    var value = formatResult(realVal - step);
    onChange === null || onChange === void 0 ? void 0 : onChange(value);
    setShowVal(value);
  };

  var initRef = (0, _react.useRef)(true);
  (0, _react.useEffect)(function () {
    if (initRef.current && realVal !== undefined && onChange) {
      initRef.current = false;
      onChange === null || onChange === void 0 ? void 0 : onChange(realVal);
    }
  }, [onChange, realVal]);
  return /*#__PURE__*/_react.default.createElement(_one.View, _objectSpread(_objectSpread({
    className: (0, _classnames.default)(_indexModule.default.wrap, className)
  }, restProps), {}, {
    style: {
      border: "1PX solid ".concat(minDisabled ? disabledColor : borderSelectColor),
      borderRadius: '4px'
    }
  }), /*#__PURE__*/_react.default.createElement(_icon.default, {
    name: "move",
    onTap: handleSubtract,
    className: (0, _classnames.default)(_indexModule.default.icon, iconCls),
    color: minDisabled ? disabledColor : iconSelectColor
  }), /*#__PURE__*/_react.default.createElement(_one.Text, {
    style: {
      borderLeft: "1PX solid ".concat(minDisabled ? disabledColor : borderSelectColor),
      borderRight: "1PX solid ".concat(minDisabled ? disabledColor : borderSelectColor)
    },
    className: (0, _classnames.default)(_indexModule.default.number, numberCls, _defineProperty({}, numberDisabledCls || _indexModule.default.disabled, minDisabled && maxDisabled))
  }, formatValue(realVal.toFixed(precision)), /*#__PURE__*/_react.default.createElement(_one.Text, {
    className: _indexModule.default.unit
  }, unit)), /*#__PURE__*/_react.default.createElement(_icon.default, {
    name: "add",
    onTap: handleAdd,
    className: (0, _classnames.default)(_indexModule.default.icon, iconCls),
    color: maxDisabled ? disabledColor : iconSelectColor
  }));
};

exports.default = _default;