"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;
var _react = _interopRequireDefault(require("react"));
var _styles = require("@material-ui/core/styles");
var _TextField = _interopRequireDefault(require("@material-ui/core/TextField"));
var _debounce = _interopRequireDefault(require("lodash/debounce"));
var _propTypes = _interopRequireDefault(require("prop-types"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
function _createSuper(t) { var r = _isNativeReflectConstruct(); return function () { var e, o = _getPrototypeOf(t); if (r) { var s = _getPrototypeOf(this).constructor; e = Reflect.construct(o, arguments, s); } else e = o.apply(this, arguments); return _possibleConstructorReturn(this, e); }; }
function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
var styles = function styles(theme) {
  var _MuiTextFieldRoot;
  return {
    MuiTextFieldRoot: (_MuiTextFieldRoot = {
      width: 95,
      '& input[type=number]::-webkit-inner-spin-button': {
        '-webkit-appearance': 'none',
        margin: 0
      },
      '& input[type=number]::-webkit-outer-spin-button': {
        '-webkit-appearance': 'none',
        margin: 0
      },
      '& > label': _defineProperty({
        margin: '-8px 0',
        '&.MuiInputLabel-shrink': {
          margin: 0
        }
      }, theme.breakpoints.down('xs'), {
        margin: 0
      })
    }, _defineProperty(_MuiTextFieldRoot, theme.breakpoints.down('xs'), {
      width: 75
    }), _defineProperty(_MuiTextFieldRoot, '& input', _defineProperty({
      height: 20
    }, theme.breakpoints.down('xs'), {
      height: 18
    })), _defineProperty(_MuiTextFieldRoot, '&:nth-child(even) ', {
      marginRight: 10
    }), _defineProperty(_MuiTextFieldRoot, theme.breakpoints.down('xs'), {
      marginRight: 10,
      '&:not(first-child) ': {
        marginRight: 0
      }
    }), _MuiTextFieldRoot),
    amountInput: _defineProperty({
      textAlign: 'center',
      maxWidth: 100,
      width: '100%',
      padding: '10px 0 10px',
      fontSize: '15px !important',
      color: theme.palette.primary.darkText
    }, theme.breakpoints.down('xs'), {
      padding: '19px 0 !important'
    })
  };
};
var AmountInput = /*#__PURE__*/function (_React$Component) {
  _inherits(AmountInput, _React$Component);
  var _super = _createSuper(AmountInput);
  function AmountInput() {
    var _this;
    _classCallCheck(this, AmountInput);
    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }
    _this = _super.call.apply(_super, [this].concat(args));
    _defineProperty(_assertThisInitialized(_this), "state", {
      value: '',
      changeInprogress: false
    });
    _defineProperty(_assertThisInitialized(_this), "updateDebounced", (0, _debounce["default"])(function () {
      var _assertThisInitialize = _assertThisInitialized(_this),
        value = _assertThisInitialize.props.value;
      if (value !== _this.state.value) {
        _this.setState({
          value: value
        });
      }
    }, 500));
    _defineProperty(_assertThisInitialized(_this), "validateKey", function (event) {
      var regEx = /^[0-9\b]+$/;
      var keyCode = event.keyCode || event.which;
      var keyValue = String.fromCharCode(keyCode);
      if (!regEx.test(keyValue)) {
        event.preventDefault();
      }
    });
    _defineProperty(_assertThisInitialized(_this), "onChangeDebounced", (0, _debounce["default"])(function () {
      _this.props.onChange(_this.state.value === '' ? 0 : _this.state.value);
    }, _this.props.onChangeDebounceWait));
    _defineProperty(_assertThisInitialized(_this), "onChange", function (_ref) {
      var inputValue = _ref.target.value;
      var _this$props = _this.props,
        min = _this$props.min,
        max = _this$props.max;
      var value = !inputValue ? inputValue : Math.min(max, Math.max(min, parseInt(inputValue)));
      _this.setState({
        value: isNaN(value) ? '' : value
      }, _this.onChangeDebounced);
    });
    _defineProperty(_assertThisInitialized(_this), "onBlur", function (event) {
      var value = event.target.value;
      var _this$props2 = _this.props,
        onChange = _this$props2.onChange,
        onBlur = _this$props2.onBlur,
        min = _this$props2.min,
        hasValueLabel = _this$props2.hasValueLabel;
      _this.setState({
        changeInprogress: false
      });
      if (value === '') {
        var initialValue = min;
        _this.setState({
          value: initialValue
        });
        onChange(initialValue);
      }
      if (hasValueLabel) {
        _this.updateDebounced();
      }
      onBlur && onBlur(event);
    });
    _defineProperty(_assertThisInitialized(_this), "onFocus", function (event) {
      var onFocus = _this.props.onFocus;
      _this.setState({
        changeInprogress: true
      });
      onFocus && onFocus(event);
    });
    return _this;
  }
  _createClass(AmountInput, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var value = this.props.value;
      this.setState({
        value: value
      });
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate(prevProps) {
      var value = this.props.value;
      if (prevProps.value !== value && !this.state.changeInprogress) {
        this.updateDebounced();
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props3 = this.props,
        classes = _this$props3.classes,
        amountLabel = _this$props3.amountLabel,
        min = _this$props3.min,
        max = _this$props3.max,
        autoFocus = _this$props3.autoFocus,
        error = _this$props3.error,
        disabled = _this$props3.disabled,
        onClick = _this$props3.onClick,
        readOnly = _this$props3.readOnly,
        rootProps = _this$props3.rootProps,
        inputProps = _this$props3.inputProps,
        type = _this$props3.type;
      return /*#__PURE__*/_react["default"].createElement(_TextField["default"], {
        value: this.state.value,
        onChange: this.onChange,
        onBlur: this.onBlur,
        autoFocus: autoFocus,
        label: amountLabel,
        margin: "normal",
        variant: "outlined",
        onKeyPress: this.validateKey,
        onPaste: function onPaste(event) {
          event.preventDefault();
        },
        onClick: onClick,
        inputProps: _objectSpread({
          className: classes.amountInput,
          min: min,
          max: max,
          readOnly: readOnly
        }, inputProps),
        classes: {
          root: classes.MuiTextFieldRoot + ' ' + rootProps
        },
        error: error,
        disabled: disabled,
        onFocus: this.onFocus,
        type: type
      });
    }
  }]);
  return AmountInput;
}(_react["default"].Component);
_defineProperty(AmountInput, "defaultProps", {
  min: 0,
  max: 999999,
  onChangeDebounceWait: 700,
  hasValueLabel: false,
  type: 'text'
});
var _default = exports["default"] = (0, _styles.withStyles)(styles, {
  name: 'AmountInput'
})(AmountInput);