import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _inherits from "@babel/runtime/helpers/inherits";
import _createSuper from "@babel/runtime/helpers/createSuper";
import _extends from "@babel/runtime/helpers/extends";
var _excluded = ["allowThousandth", "className", "size", "prefixCls"];
import React, { Component } from 'react';
import classNames from 'classnames';
import omit from 'lodash/omit';
import RcInputNumber from '../rc-components/input-number';
import Input from '../input/Input';
import { Size } from '../_util/enum';
import ConfigContext from '../config-provider/ConfigContext';
export function formatNumber(value) {
  var vs = String(value).split('.');
  var v = vs[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',');
  return vs.length > 1 ? "".concat(v, ".").concat(vs[1]) : v;
}
export function parseNumber(value) {
  if (value.indexOf(',') !== -1) {
    return value.replace(/,/g, '');
  }

  return value;
}

function renderInput(props) {
  return /*#__PURE__*/React.createElement(Input, _extends({}, props));
}

var InputNumber = /*#__PURE__*/function (_Component) {
  _inherits(InputNumber, _Component);

  var _super = _createSuper(InputNumber);

  function InputNumber() {
    var _this;

    _classCallCheck(this, InputNumber);

    _this = _super.apply(this, arguments);

    _this.numberFormatter = function (value) {
      var formatter = _this.props.formatter;
      var v = formatNumber(value);
      return formatter ? formatter(v) : v;
    };

    _this.numberParser = function (value) {
      var parser = _this.props.parser;
      var v = parseNumber(value);
      return parser ? parser(v) : v;
    };

    return _this;
  }

  _createClass(InputNumber, [{
    key: "render",
    value: function render() {
      var _classNames,
          _this2 = this;

      var _this$props = this.props,
          _this$props$allowThou = _this$props.allowThousandth,
          allowThousandth = _this$props$allowThou === void 0 ? false : _this$props$allowThou,
          className = _this$props.className,
          size = _this$props.size,
          customizePrefixCls = _this$props.prefixCls,
          others = _objectWithoutProperties(_this$props, _excluded);

      var getPrefixCls = this.context.getPrefixCls;
      var prefixCls = getPrefixCls('input-number', customizePrefixCls);
      var inputNumberClass = classNames((_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-lg"), size === Size.large), _defineProperty(_classNames, "".concat(prefixCls, "-sm"), size === Size.small), _classNames), className);
      var otherProp = others;

      if (allowThousandth) {
        otherProp = _objectSpread(_objectSpread({}, otherProp), {}, {
          formatter: this.numberFormatter,
          parser: this.numberParser
        });
      }

      return /*#__PURE__*/React.createElement(RcInputNumber, _extends({
        ref: function ref(c) {
          return _this2.inputNumberRef = c;
        },
        className: inputNumberClass,
        prefixCls: prefixCls,
        renderInput: renderInput
      }, omit(otherProp, ['form'])));
    }
  }, {
    key: "focus",
    value: function focus() {
      this.inputNumberRef.focus();
    }
  }, {
    key: "blur",
    value: function blur() {
      this.inputNumberRef.blur();
    }
  }], [{
    key: "contextType",
    get: function get() {
      return ConfigContext;
    }
  }]);

  return InputNumber;
}(Component);

export { InputNumber as default };
InputNumber.displayName = 'InputNumber';
InputNumber.defaultProps = {
  step: 1
};
//# sourceMappingURL=index.js.map
