"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 _classnames = _interopRequireDefault(require("classnames"));

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

var _common = require("./../common");

var _utils = require("./../_utils");

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

var prefixCls = (0, _common.getPrefixCls)('stepper');

var Stepper = function Stepper(props) {
  var _classNames2, _classNames3, _classNames4;

  var _props$min = props.min,
      min = _props$min === void 0 ? 1 : _props$min,
      max = props.max,
      _props$step = props.step,
      step = _props$step === void 0 ? 1 : _props$step,
      _props$value = props.value,
      value = _props$value === void 0 ? min : _props$value,
      disabled = props.disabled,
      plain = props.plain,
      shape = props.shape,
      size = props.size,
      _props$fontSize = props.fontSize,
      fontSize = _props$fontSize === void 0 ? 20 : _props$fontSize,
      color = props.color,
      bgColor = props.bgColor,
      onChange = props.onChange,
      onInput = props.onInput,
      onTap = props.onTap,
      onFocus = props.onFocus,
      onBlur = props.onBlur;
  var val = value;
  var minusDisabled = val <= min || disabled;
  var plusDisabled = (max || max === 0) && val >= max || disabled;
  var iconSize = (0, _utils.addUnit)(fontSize);

  var handlePlus = function handlePlus() {
    if (!val && val !== 0) {
      return;
    }

    if (plusDisabled) {
      return;
    }

    var newValue = max && val + step > max ? max : val + step;
    onTap === null || onTap === void 0 ? void 0 : onTap(newValue);
    onChange === null || onChange === void 0 ? void 0 : onChange(newValue);
  };

  var handleMinus = function handleMinus() {
    if (!val && val !== 0) {
      return;
    }

    if (minusDisabled) {
      return;
    }

    var newValue = val - step < min ? min : val - step;
    onTap === null || onTap === void 0 ? void 0 : onTap(newValue);
    onChange === null || onChange === void 0 ? void 0 : onChange(newValue);
  };

  var handleInput = function handleInput(e) {
    onInput === null || onInput === void 0 ? void 0 : onInput(Number(e.target.value), e);
    onChange === null || onChange === void 0 ? void 0 : onChange(Number(e.target.value), e);
  };

  var handleFocus = function handleFocus(e) {
    onFocus === null || onFocus === void 0 ? void 0 : onFocus(e);
  };

  var handleBlur = function handleBlur(e) {
    onBlur === null || onBlur === void 0 ? void 0 : onBlur(e);

    if (minusDisabled) {
      onInput === null || onInput === void 0 ? void 0 : onInput(min);
      onChange === null || onChange === void 0 ? void 0 : onChange(min);
    }

    if (plusDisabled) {
      onInput === null || onInput === void 0 ? void 0 : onInput(max);
      onChange === null || onChange === void 0 ? void 0 : onChange(max);
    }
  };

  return /*#__PURE__*/React.createElement(_one.View, {
    className: (0, _classnames.default)(prefixCls, _defineProperty({}, "".concat(prefixCls, "-").concat(size), size))
  }, /*#__PURE__*/React.createElement(_one.View, {
    className: (0, _classnames.default)("".concat(prefixCls, "-btn"), "".concat(prefixCls, "-minus"), (_classNames2 = {}, _defineProperty(_classNames2, "".concat(prefixCls, "-btn-disabled"), minusDisabled), _defineProperty(_classNames2, "".concat(prefixCls, "-btn-plain"), plain), _defineProperty(_classNames2, "".concat(prefixCls, "-btn-plain-disabled"), plain && minusDisabled), _defineProperty(_classNames2, "".concat(prefixCls, "-btn-circle"), shape === 'circle'), _classNames2)),
    onTap: handleMinus
  }, /*#__PURE__*/React.createElement(_icon.default, {
    type: "move",
    size: iconSize,
    color: "#999"
  })), /*#__PURE__*/React.createElement(_one.View, {
    className: "".concat(prefixCls, "-input-wrapper")
  }, /*#__PURE__*/React.createElement(_one.Input, {
    className: (0, _classnames.default)("".concat(prefixCls, "-input"), (_classNames3 = {}, _defineProperty(_classNames3, "".concat(prefixCls, "-input-disabled"), disabled), _defineProperty(_classNames3, "".concat(prefixCls, "-input-circle"), shape === 'circle'), _defineProperty(_classNames3, "".concat(prefixCls, "-input-plain"), plain), _defineProperty(_classNames3, "".concat(prefixCls, "-binputtn-plain-disabled"), plain && disabled), _classNames3)),
    type: "number",
    value: String(val),
    onInput: handleInput,
    onFocus: handleFocus,
    onBlur: handleBlur,
    disabled: disabled
  })), /*#__PURE__*/React.createElement(_one.View, {
    className: (0, _classnames.default)("".concat(prefixCls, "-btn"), "".concat(prefixCls, "-plus"), (_classNames4 = {}, _defineProperty(_classNames4, "".concat(prefixCls, "-btn-disabled"), plusDisabled), _defineProperty(_classNames4, "".concat(prefixCls, "-btn-plain"), plain), _defineProperty(_classNames4, "".concat(prefixCls, "-btn-plain-disabled"), plain && plusDisabled), _defineProperty(_classNames4, "".concat(prefixCls, "-btn-circle"), shape === 'circle'), _classNames4)),
    style: {
      borderColor: bgColor || color,
      backgroundColor: bgColor,
      opacity: plusDisabled && bgColor ? 0.4 : undefined
    },
    onTap: handlePlus
  }, /*#__PURE__*/React.createElement(_icon.default, {
    type: "add",
    size: iconSize,
    color: color || '#333'
  })));
};

var _default = Stepper;
exports.default = _default;