/*
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 * as tslib_1 from "tslib";
import { BuiltInFunctionBase } from "./BuiltInFunctionBase";
import { ExcelCalcValue } from "./ExcelCalcValue";
import { markType } from "igniteui-angular-core";
import { ExcelCalcErrorValue } from "./ExcelCalcErrorValue";
/**
 * @hidden
 */
var UltraCalcFunctionRate = /** @class */ (function (_super) {
    tslib_1.__extends(UltraCalcFunctionRate, _super);
    function UltraCalcFunctionRate() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    UltraCalcFunctionRate.prototype.evaluate = function (a, b) {
        var c = 0;
        var d = 0;
        var e = 0;
        var f = 0;
        var g = 0;
        var h = 0.1;
        if (b < 3 || b > 6) {
            return new ExcelCalcValue(new ExcelCalcErrorValue(1));
        }
        if (b == 6 && !((function () { var i = a.pop()._toDouble1(h); h = i.p0; return i.ret; })())) {
            return new ExcelCalcValue(new ExcelCalcErrorValue(4));
        }
        if (b >= 5) {
            g = a.pop()._toInt();
            if (g < 0 || g > 1) {
                return new ExcelCalcValue(new ExcelCalcErrorValue(1));
            }
        }
        if (b >= 4 && !((function () { var i = a.pop()._toDouble1(c); c = i.p0; return i.ret; })())) {
            return new ExcelCalcValue(new ExcelCalcErrorValue(4));
        }
        if (b >= 3) {
            if (!((function () { var i = a.pop()._toDouble1(d); d = i.p0; return i.ret; })()) || !((function () { var i = a.pop()._toDouble1(e); e = i.p0; return i.ret; })()) || !((function () { var i = a.pop()._toDouble1(f); f = i.p0; return i.ret; })())) {
                return new ExcelCalcValue(new ExcelCalcErrorValue(4));
            }
            if (f <= 0) {
                return new ExcelCalcValue(new ExcelCalcErrorValue(1));
            }
        }
        var i = function (j) { return c + e * (1 + j * g) * (Math.pow(1 + j, f) - 1) / j + d * Math.pow(1 + j, f); };
        var j = function (k) { return (e * (f * k * Math.pow(1 + k, f - 1 + g) - Math.pow(1 + k, f) + 1) / (k * k)) + f * d * Math.pow(1 + k, f - 1); };
        var k = 1E-08;
        var l = h;
        for (var m = 0; m < 40; m++) {
            var n = i(l);
            var o = j(l);
            if (Math.abs(o) <= 4.94065645841247E-324) {
                break;
            }
            var p = l - n / o;
            if (Math.abs(p - l) <= k) {
                return new ExcelCalcValue(p);
            }
            l = p;
        }
        return new ExcelCalcValue(new ExcelCalcErrorValue(4));
    };
    UltraCalcFunctionRate.prototype.get_name = function () {
        return "rate";
    };
    Object.defineProperty(UltraCalcFunctionRate.prototype, "name", {
        get: function () {
            return this.get_name();
        },
        enumerable: true,
        configurable: true
    });
    UltraCalcFunctionRate.prototype.get_minArgs = function () {
        return 3;
    };
    Object.defineProperty(UltraCalcFunctionRate.prototype, "minArgs", {
        get: function () {
            return this.get_minArgs();
        },
        enumerable: true,
        configurable: true
    });
    UltraCalcFunctionRate.prototype.get_maxArgs = function () {
        return 6;
    };
    Object.defineProperty(UltraCalcFunctionRate.prototype, "maxArgs", {
        get: function () {
            return this.get_maxArgs();
        },
        enumerable: true,
        configurable: true
    });
    UltraCalcFunctionRate.$t = markType(UltraCalcFunctionRate, 'UltraCalcFunctionRate', BuiltInFunctionBase.$);
    return UltraCalcFunctionRate;
}(BuiltInFunctionBase));
export { UltraCalcFunctionRate };
//# sourceMappingURL=UltraCalcFunctionRate.js.map