/*
THIS INFRAGISTICS ULTIMATE SOFTWARE LICENSE  AGREEMENT ("AGREEMENT") LOCATED HERE:
https://www.infragistics.com/legal/license/igultimate-la
https://www.infragistics.com/legal/license/igultimate-eula
GOVERNS THE LICENSING, INSTALLATION AND USE OF INFRAGISTICS SOFTWARE. BY DOWNLOADING AND/OR INSTALLING AND USING INFRAGISTICS SOFTWARE:  you are indicating that you have read and understand this Agreement, and agree to be legally bound by it on behalf of the yourself and your company.
*/
import { __extends } from "tslib";
import { BuiltInFunctionBase } from "./BuiltInFunctionBase";
import { ExcelCalcValue } from "./ExcelCalcValue";
import { markType } from "igniteui-angular-core";
import { ExcelCalcErrorValue } from "./ExcelCalcErrorValue";
import { MathUtilities } from "./MathUtilities";
import { stringIsNullOrEmpty, stringIsNullOrWhiteSpace } from "igniteui-angular-core";
import { isInfinity } from "igniteui-angular-core";
/**
 * @hidden
 */
var UltraCalcFunctionNumberValue = /** @class */ /*@__PURE__*/ (function (_super) {
    __extends(UltraCalcFunctionNumberValue, _super);
    function UltraCalcFunctionNumberValue() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    UltraCalcFunctionNumberValue.prototype.evaluate = function (a, b) {
        var c = null;
        var d = null;
        if (b == 3) {
            c = a.pop();
        }
        if (b >= 2) {
            d = a.pop();
        }
        var e = a.pop();
        if (e.isError) {
            return new ExcelCalcValue(e.toErrorValue());
        }
        if (d != null && d.isError) {
            return new ExcelCalcValue(d.toErrorValue());
        }
        if (c != null && c.isError) {
            return new ExcelCalcValue(c.toErrorValue());
        }
        var f = e.toString();
        var g = d == null ? a._w.numberFormat.numberDecimalSeparator : d.toString();
        var h = c == null ? a._w.numberFormat.numberGroupSeparator : c.toString();
        if (stringIsNullOrEmpty(h) || stringIsNullOrEmpty(g)) {
            return new ExcelCalcValue(new ExcelCalcErrorValue(1));
        }
        var i = h.charAt(0);
        var j = g.charAt(0);
        if (j == i) {
            return new ExcelCalcValue(new ExcelCalcErrorValue(1));
        }
        if (stringIsNullOrWhiteSpace(f)) {
            return new ExcelCalcValue(0);
        }
        var k = false;
        var l = "";
        var m = "";
        var n = 0;
        var o = false;
        var p = 0;
        var _loop_1 = function (q) {
            var r = f.charAt(q);
            if (/\s/i.test(r)) {
            }
            else if (r == j) {
                if (k || n > 0) {
                    o = true;
                }
                k = true;
            }
            else if (r == i) {
                if (k) {
                    o = true;
                }
            }
            else if (r == '%') {
                n++;
            }
            else if ('0'.charCodeAt(0) <= r.charCodeAt(0) && r.charCodeAt(0) <= '9'.charCodeAt(0)) {
                if (n > 0) {
                    o = true;
                }
                else {
                    var s = r;
                    if (p == 15) {
                        s = '0';
                    }
                    else if (p > 0 || s != '0') {
                        p++;
                    }
                    if (k) {
                        m += s;
                    }
                    else {
                        l += s;
                    }
                }
            }
            else {
                if (!e.isBoolean) {
                    var t_1;
                    if (((function () { var u = e._toDouble1(t_1); t_1 = u.p0; return u.ret; })())) {
                        if (isInfinity(t_1)) {
                            o = true;
                        }
                        else {
                            return { value: new ExcelCalcValue(t_1) };
                        }
                    }
                }
                o = true;
            }
            if (o) {
                return "break";
            }
        };
        for (var q = 0; q < f.length; q++) {
            var state_1 = _loop_1(q);
            if (typeof state_1 === "object")
                return state_1.value;
            if (state_1 === "break")
                break;
        }
        if (o) {
            return new ExcelCalcValue(new ExcelCalcErrorValue(1));
        }
        var u = l.length > 0 ? parseFloat(l) : 0;
        if (m.length > 0) {
            u += parseFloat(m) / Math.pow(10, m.length);
        }
        if (n > 0) {
            u /= Math.pow(10, n * 2);
        }
        return new ExcelCalcValue(MathUtilities.i(u));
    };
    UltraCalcFunctionNumberValue.prototype.get_name = function () {
        return "numbervalue";
    };
    Object.defineProperty(UltraCalcFunctionNumberValue.prototype, "name", {
        get: function () {
            return this.get_name();
        },
        enumerable: false,
        configurable: true
    });
    UltraCalcFunctionNumberValue.prototype.get_minArgs = function () {
        return 1;
    };
    Object.defineProperty(UltraCalcFunctionNumberValue.prototype, "minArgs", {
        get: function () {
            return this.get_minArgs();
        },
        enumerable: false,
        configurable: true
    });
    UltraCalcFunctionNumberValue.prototype.get_maxArgs = function () {
        return 3;
    };
    Object.defineProperty(UltraCalcFunctionNumberValue.prototype, "maxArgs", {
        get: function () {
            return this.get_maxArgs();
        },
        enumerable: false,
        configurable: true
    });
    UltraCalcFunctionNumberValue.$t = markType(UltraCalcFunctionNumberValue, 'UltraCalcFunctionNumberValue', BuiltInFunctionBase.$);
    return UltraCalcFunctionNumberValue;
}(BuiltInFunctionBase));
export { UltraCalcFunctionNumberValue };
