'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;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var react_1 = __importDefault(require("react"));
var prop_types_1 = __importDefault(require("prop-types"));
var classnames_1 = __importDefault(require("classnames"));
var validator_1 = __importDefault(require("validator"));
var ButtonGroup_1 = __importDefault(require("react-bootstrap/lib/ButtonGroup"));
var FormControl_1 = require("./FormControl");
var TextInputControl_1 = require("./TextInputControl");
var constants_1 = require("../../../utils/constants");
var Button_1 = require("../Button");
var Icon_1 = require("../Icon");
var index_en_US_1 = __importDefault(require("../../../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.onTextInputKeyDown = function (e) {
            if (e.keyCode === constants_1.KeyCodes.ARROW_UP || e.keyCode === constants_1.KeyCodes.ARROW_DOWN) {
                if (e.keyCode === constants_1.KeyCodes.ARROW_UP) {
                    _this.increment();
                }
                else {
                    _this.decrement();
                }
                e.preventDefault();
            }
            e.stopPropagation();
        };
        return _this;
    }
    InputCounterControl.prototype.getValue = function () {
        return this.refs.input.getValue();
    };
    InputCounterControl.prototype.setValue = function (value) {
        var props = __rest(this.getPropsFromContext(), []);
        props.onChange(this.getValidatedValue(value));
    };
    InputCounterControl.prototype.getInputDOMNode = function () {
        return this.refs.input.getInputDOMNode();
    };
    InputCounterControl.prototype.getValidatedValue = function (value) {
        var minValue = this.props.minValue;
        var maxValue = this.props.maxValue;
        var isValid = validator_1.default.isInt(value, {
            min: minValue,
            max: maxValue
        });
        if (isValid) {
            return value;
        }
        if (value < minValue || value === '') {
            return minValue;
        }
        if (value > maxValue) {
            return maxValue;
        }
    };
    InputCounterControl.prototype.onTextInputChange = function (e) {
        var props = __rest(this.getPropsFromContext(), []);
        var value = e.target.value;
        var isNaturalNumber = Natural_Number_Regex.test(value);
        if (isNaturalNumber || value === '') {
            if (isNaturalNumber) {
                value = parseInt(value, 10);
            }
            props.onChange(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, props = __rest(_a, ["testAutomationId", "decreaseLabel", "increaseLabel"]);
        var inputCounterClassNames = classnames_1.default('input-counter');
        return (react_1.default.createElement(ButtonGroup_1.default, { className: inputCounterClassNames, "data-test-automation-id": this.props.hasFormGroupContainer ? undefined : testAutomationId },
            react_1.default.createElement(Button_1.Button, { testAutomationId: "decrementButton", className: "decrement-button", onClick: this.decrement.bind(this), "aria-label": decreaseLabel, tabIndex: -1, disabled: props.value <= this.props.minValue },
                react_1.default.createElement(Icon_1.Icon, { name: "checkbox-mix-readonly" })),
            react_1.default.createElement(TextInputControl_1.TextInputControl, __assign({}, 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_1.default.createElement(Button_1.Button, { testAutomationId: "incrementButton", className: "increment-button", onClick: this.increment.bind(this), "aria-label": increaseLabel, tabIndex: -1, disabled: props.value >= this.props.maxValue },
                react_1.default.createElement(Icon_1.Icon, { name: "add-big" }))));
    };
    InputCounterControl.propTypes = __assign({}, FormControl_1.FormControl.propTypes, { maxLength: prop_types_1.default.number, minValue: prop_types_1.default.number, maxValue: prop_types_1.default.number, decreaseLabel: prop_types_1.default.string, increaseLabel: prop_types_1.default.string });
    InputCounterControl.defaultProps = __assign({}, FormControl_1.FormControl.defaultProps, { testAutomationId: 'inputCounter', maxLength: String(MAX_SAFE_INTEGER).length, minValue: 0, maxValue: MAX_SAFE_INTEGER, get decreaseLabel() {
            return index_en_US_1.default.DECREASE;
        },
        get increaseLabel() {
            return index_en_US_1.default.INCREASE;
        } });
    return InputCounterControl;
}(FormControl_1.FormControl));
exports.InputCounterControl = InputCounterControl;
//# sourceMappingURL=InputCounterControl.js.map