'use strict';

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

exports.__esModule = true;
exports.InputCounterControl = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));

var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));

var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

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

var _propTypes = _interopRequireDefault(require("prop-types"));

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

var _validator = _interopRequireDefault(require("validator"));

var _ButtonGroup = _interopRequireDefault(require("react-bootstrap/lib/ButtonGroup"));

var _FormControl2 = require("./FormControl");

var _TextInputControl = require("./TextInputControl");

var _constants = require("../../../utils/constants");

var _Button = require("../Button");

var _Icon = require("../Icon");

var Natural_Number_Regex = /^[0-9]+$/;
var MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER || 9007199254740991;

var InputCounterControl =
/*#__PURE__*/
function (_FormControl) {
  (0, _inheritsLoose2.default)(InputCounterControl, _FormControl);

  function InputCounterControl() {
    var _this;

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _FormControl.call.apply(_FormControl, [this].concat(args)) || this;
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "onTextInputKeyDown", function (e) {
      if (e.keyCode === _constants.KeyCodes.ARROW_UP || e.keyCode === _constants.KeyCodes.ARROW_DOWN) {
        if (e.keyCode === _constants.KeyCodes.ARROW_UP) {
          _this.increment();
        } else {
          _this.decrement();
        }

        e.preventDefault();
      }

      e.stopPropagation();
    });
    return _this;
  }

  var _proto = InputCounterControl.prototype;

  _proto.getValue = function getValue() {
    return this.refs.input.getValue();
  };

  _proto.setValue = function setValue(value) {
    var _this$getPropsFromCon = this.getPropsFromContext(),
        props = Object.assign({}, _this$getPropsFromCon);

    props.onChange(this.getValidatedValue(value));
  };

  _proto.getInputDOMNode = function getInputDOMNode() {
    return this.refs.input.getInputDOMNode();
  };

  _proto.getValidatedValue = function getValidatedValue(value) {
    var minValue = this.props.minValue;
    var maxValue = this.props.maxValue;

    var isValid = _validator.default.isInt(value, {
      min: minValue,
      max: maxValue
    });

    if (isValid) {
      return value;
    } else {
      if (value < minValue || value === '') {
        return minValue;
      }

      if (value > maxValue) {
        return maxValue;
      }
    }
  };

  _proto.onTextInputChange = function onTextInputChange(e) {
    var _this$getPropsFromCon2 = this.getPropsFromContext(),
        props = Object.assign({}, _this$getPropsFromCon2);

    var value = e.target.value;
    var isNaturalNumber = Natural_Number_Regex.test(value);

    if (isNaturalNumber || value === '') {
      if (isNaturalNumber) {
        value = parseInt(value, 10);
      }

      props.onChange(value);
    }
  };

  _proto.onTextInputBlur = function onTextInputBlur() {
    this.setValue(this.getValue());
  };

  _proto.increment = function increment() {
    this.setValue(Number(this.getValue()) + 1);
  };

  _proto.decrement = function decrement() {
    this.setValue(Number(this.getValue()) - 1);
  };

  _proto.render = function render() {
    var _this$getPropsFromCon3 = this.getPropsFromContext(),
        testAutomationId = _this$getPropsFromCon3.testAutomationId,
        decreaseLabel = _this$getPropsFromCon3.decreaseLabel,
        increaseLabel = _this$getPropsFromCon3.increaseLabel,
        props = (0, _objectWithoutPropertiesLoose2.default)(_this$getPropsFromCon3, ["testAutomationId", "decreaseLabel", "increaseLabel"]);

    var inputCounterClassNames = (0, _classnames.default)('input-counter');
    return _react.default.createElement(_ButtonGroup.default, {
      className: inputCounterClassNames,
      "data-test-automation-id": this.props.hasFormGroupContainer ? undefined : testAutomationId
    }, _react.default.createElement(_Button.Button, {
      testAutomationId: "decrementButton",
      className: "decrement-button",
      onClick: this.decrement.bind(this),
      "aria-label": decreaseLabel,
      tabIndex: "-1",
      disabled: props.value <= this.props.minValue
    }, _react.default.createElement(_Icon.Icon, {
      name: "checkbox-mix-readonly"
    })), _react.default.createElement(_TextInputControl.TextInputControl, (0, _extends2.default)({}, props, {
      className: "value",
      ref: "input",
      role: "spinbutton",
      "aria-valuenow": props.value,
      "aria-valuemin": props.minValue,
      "aria-valuemax": props.maxValue,
      min: props.minValue,
      max: props.maxValue,
      onChange: this.onTextInputChange.bind(this),
      onKeyDown: this.onTextInputKeyDown,
      onBlur: this.onTextInputBlur.bind(this)
    })), _react.default.createElement(_Button.Button, {
      testAutomationId: "incrementButton",
      className: "increment-button",
      onClick: this.increment.bind(this),
      "aria-label": increaseLabel,
      tabIndex: "-1",
      disabled: props.value >= this.props.maxValue
    }, _react.default.createElement(_Icon.Icon, {
      name: "add-big"
    })));
  };

  return InputCounterControl;
}(_FormControl2.FormControl);

exports.InputCounterControl = InputCounterControl;
(0, _defineProperty2.default)(InputCounterControl, "propTypes", Object.assign({}, _FormControl2.FormControl.propTypes, {
  maxLength: _propTypes.default.number,
  minValue: _propTypes.default.number,
  maxValue: _propTypes.default.number,
  decreaseLabel: _propTypes.default.string,
  increaseLabel: _propTypes.default.string
}));
(0, _defineProperty2.default)(InputCounterControl, "defaultProps", Object.assign({}, _FormControl2.FormControl.defaultProps, {
  testAutomationId: 'inputCounter',
  maxLength: String(MAX_SAFE_INTEGER).length,
  minValue: 0,
  maxValue: MAX_SAFE_INTEGER,
  decreaseLabel: 'TRANSLATE:DECREASE',
  increaseLabel: 'TRANSLATE:INCREASE'
}));