'use strict';
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0)
            t[p[i]] = s[p[i]];
    return t;
};
import React from 'react';
import validator from 'validator';
import { ButtonGroup } from 'react-bootstrap';
import classnames from 'classnames';
import { FormControl } from './FormControl';
import { TextInputControl } from './TextInputControl';
import { KeyCodes } from '../../../utils/constants';
import { Button } from '../Button';
import { Icon } from '../Icon';
import lang from '../../../lang/common/index-en_US';
var Natural_Number_Regex = /^[0-9]+$/;
var MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER || 9007199254740991;
var InputCounterControl = /** @class */ (function (_super) {
    __extends(InputCounterControl, _super);
    function InputCounterControl() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.inputRef = React.createRef();
        _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;
    }
    InputCounterControl.prototype.getValue = function () {
        var input = this.inputRef.current;
        if (input) {
            return input.getValue();
        }
    };
    InputCounterControl.prototype.setValue = function (value) {
        var props = __rest(this.getPropsFromContext(), []);
        props.onChange(this.getValidatedValue(value));
    };
    InputCounterControl.prototype.getInputDOMNode = function () {
        var input = this.inputRef.current;
        if (input) {
            return input.getInputDOMNode();
        }
    };
    InputCounterControl.prototype.getValidatedValue = function (value) {
        var minValue = this.props.minValue;
        var maxValue = this.props.maxValue;
        var isValid = validator.isInt(value, {
            min: minValue,
            max: maxValue
        });
        if (isValid) {
            return parseInt(value);
        }
        if (value < minValue || value === '') {
            return minValue;
        }
        if (value > maxValue) {
            return maxValue;
        }
    };
    InputCounterControl.prototype.onTextInputChange = function (e) {
        var value = e.target.value;
        var isNaturalNumber = Natural_Number_Regex.test(value);
        if (isNaturalNumber || value === '') {
            if (isNaturalNumber) {
                value = parseInt(value, 10);
            }
            this.setValue(value);
        }
    };
    InputCounterControl.prototype.onTextInputBlur = function () {
        this.setValue(this.getValue());
    };
    InputCounterControl.prototype.increment = function () {
        this.setValue(Number(this.getValue()) + 1);
    };
    InputCounterControl.prototype.decrement = function () {
        this.setValue(Number(this.getValue()) - 1);
    };
    InputCounterControl.prototype.render = function () {
        var _a = this.getPropsFromContext(), testAutomationId = _a.testAutomationId, decreaseLabel = _a.decreaseLabel, increaseLabel = _a.increaseLabel, isInvalid = _a.isInvalid, props = __rest(_a, ["testAutomationId", "decreaseLabel", "increaseLabel", "isInvalid"]);
        var className = classnames('input-counter', { 'not-valid': isInvalid });
        return (React.createElement(ButtonGroup, { className: className, "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.disabled || props.value <= this.props.minValue },
                React.createElement(Icon, { name: "checkbox-mix-readonly" })),
            React.createElement(TextInputControl, __assign({}, props, { className: "value", ref: this.inputRef, 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.disabled || props.value >= this.props.maxValue },
                React.createElement(Icon, { name: "add-big" }))));
    };
    InputCounterControl.defaultProps = __assign({}, FormControl.defaultProps, { testAutomationId: 'inputCounter', maxLength: String(MAX_SAFE_INTEGER).length, minValue: 0, maxValue: MAX_SAFE_INTEGER, isInvalid: false, get decreaseLabel() {
            return lang.DECREASE;
        },
        get increaseLabel() {
            return lang.INCREASE;
        } });
    return InputCounterControl;
}(FormControl));
export { InputCounterControl };
//# sourceMappingURL=InputCounterControl.js.map