import { Parser } from 'expr-eval';
var NumberCellEditor = (function () {
    function NumberCellEditor() {
        this.fractionDigits = 2;
    }
    NumberCellEditor.prototype.getValue = function () {
        if (this.eGui.value == null || this.eGui.value.length == 0)
            return null;
        try {
            var expr = this.eGui.value
                .replace(new RegExp('\\,', 'g'), function (match) {
                return match === ',' ? '.' : ',';
            });
            var value = Parser.evaluate('roundTo(' + expr + ', ' + this.fractionDigits + ')');
            return value;
        }
        catch (error) {
            console.error('Error when evaluating expression : ' + error);
            return null;
        }
    };
    NumberCellEditor.prototype.getGui = function () {
        return this.eGui;
    };
    NumberCellEditor.prototype.afterGuiAttached = function () {
        this.eGui.focus();
    };
    NumberCellEditor.prototype.isCancelBeforeStart = function () {
        return this.cancelBeforeStart;
    };
    NumberCellEditor.prototype.isCancelAfterEnd = function () {
        return false;
    };
    NumberCellEditor.prototype.destroy = function () {
    };
    NumberCellEditor.prototype.isPopup = function () {
        return false;
    };
    NumberCellEditor.prototype.init = function (params) {
        var _this = this;
        this.eGui = document.createElement('input');
        if (this.isCharNumeric(params.charPress)) {
            this.eGui.value = params.charPress;
        }
        else {
            if (params.value !== undefined && params.value !== null) {
                this.eGui.value = params.value;
            }
        }
        this.eGui.addEventListener('keypress', function (event) {
            if (!_this.isKeyPressedNumeric(event)) {
                _this.eGui.focus();
                if (event.preventDefault)
                    event.preventDefault();
            }
            else if (_this.isKeyPressedNavigation(event)) {
                event.stopPropagation();
            }
        });
        var charPressIsNotANumber = params.charPress && ('1234567890+-()/*,.'.indexOf(params.charPress) < 0);
        this.cancelBeforeStart = charPressIsNotANumber;
        if (params.fractionDigits != null) {
            this.fractionDigits = params.fractionDigits;
        }
    };
    NumberCellEditor.prototype.isCharNumeric = function (charStr) {
        return ('1234567890+-()/*,.'.indexOf(charStr) >= 0);
    };
    NumberCellEditor.prototype.isKeyPressedNumeric = function (event) {
        var charCode = this.getCharCodeFromEvent(event);
        var charStr = String.fromCharCode(charCode);
        return this.isCharNumeric(charStr);
    };
    NumberCellEditor.prototype.isKeyPressedNavigation = function (event) {
        return event.keyCode === 39
            || event.keyCode === 37;
    };
    NumberCellEditor.prototype.getCharCodeFromEvent = function (event) {
        event = event || window.event;
        return (typeof event.which == "undefined") ? event.keyCode : event.which;
    };
    return NumberCellEditor;
}());
export default NumberCellEditor;

//# sourceMappingURL=number-cell-editor.js.map
