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

import * as React from 'react';
import { View, Input } from 'remax/one';
import classNames from 'classnames';
import Icon from '../icon';
import { getPrefixCls } from './../common';
import { addUnit } from './../_utils';
var prefixCls = 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 = 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(View, {
    className: classNames(prefixCls, _defineProperty({}, "".concat(prefixCls, "-").concat(size), size))
  }, /*#__PURE__*/React.createElement(View, {
    className: classNames("".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, {
    type: "move",
    size: iconSize,
    color: "#999"
  })), /*#__PURE__*/React.createElement(View, {
    className: "".concat(prefixCls, "-input-wrapper")
  }, /*#__PURE__*/React.createElement(Input, {
    className: classNames("".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(View, {
    className: classNames("".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, {
    type: "add",
    size: iconSize,
    color: color || '#333'
  })));
};

export default Stepper;