'use strict';

import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/objectWithoutPropertiesLoose";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import React from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import validator from 'validator';
import ButtonGroup from 'react-bootstrap/lib/ButtonGroup';
import { FormControl } from './FormControl';
import { TextInputControl } from './TextInputControl';
import { KeyCodes } from '../../../utils/constants';
import { Button } from '../Button';
import { Icon } from '../Icon';
var Natural_Number_Regex = /^[0-9]+$/;
var MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER || 9007199254740991;
export var InputCounterControl =
/*#__PURE__*/
function (_FormControl) {
  _inheritsLoose(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;

    _defineProperty(_assertThisInitialized(_this), "onTextInputKeyDown", function (e) {
      if (e.keyCode === KeyCodes.ARROW_UP || e.keyCode === KeyCodes.ARROW_DOWN) {
        if (e.keyCode === 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.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 = _objectWithoutPropertiesLoose(_this$getPropsFromCon3, ["testAutomationId", "decreaseLabel", "increaseLabel"]);

    var inputCounterClassNames = classnames('input-counter');
    return React.createElement(ButtonGroup, {
      className: inputCounterClassNames,
      "data-test-automation-id": this.props.hasFormGroupContainer ? undefined : testAutomationId
    }, React.createElement(Button, {
      testAutomationId: "decrementButton",
      className: "decrement-button",
      onClick: this.decrement.bind(this),
      "aria-label": decreaseLabel,
      tabIndex: "-1",
      disabled: props.value <= this.props.minValue
    }, React.createElement(Icon, {
      name: "checkbox-mix-readonly"
    })), React.createElement(TextInputControl, _extends({}, 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.createElement(Button, {
      testAutomationId: "incrementButton",
      className: "increment-button",
      onClick: this.increment.bind(this),
      "aria-label": increaseLabel,
      tabIndex: "-1",
      disabled: props.value >= this.props.maxValue
    }, React.createElement(Icon, {
      name: "add-big"
    })));
  };

  return InputCounterControl;
}(FormControl);

_defineProperty(InputCounterControl, "propTypes", Object.assign({}, FormControl.propTypes, {
  maxLength: PropTypes.number,
  minValue: PropTypes.number,
  maxValue: PropTypes.number,
  decreaseLabel: PropTypes.string,
  increaseLabel: PropTypes.string
}));

_defineProperty(InputCounterControl, "defaultProps", Object.assign({}, FormControl.defaultProps, {
  testAutomationId: 'inputCounter',
  maxLength: String(MAX_SAFE_INTEGER).length,
  minValue: 0,
  maxValue: MAX_SAFE_INTEGER,
  decreaseLabel: 'TRANSLATE:DECREASE',
  increaseLabel: 'TRANSLATE:INCREASE'
}));