/*
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, __values } from "tslib";
import { BuiltInFunctionBase } from "./BuiltInFunctionBase";
import { ExcelCalcValue } from "./ExcelCalcValue";
import { fromEnum, markType } from "igniteui-angular-core";
import { ExcelCalcErrorValue } from "./ExcelCalcErrorValue";
/**
 * @hidden
 */
var UltraCalcFunctionIRR = /** @class */ /*@__PURE__*/ (function (_super) {
    __extends(UltraCalcFunctionIRR, _super);
    function UltraCalcFunctionIRR() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    UltraCalcFunctionIRR.prototype.evaluate = function (a, b) {
        var e_1, _a;
        var c = 1E-07;
        var d = 0.1;
        if (b == 2) {
            var e_2 = a.pop();
            if (!((function () { var f = e_2._toDouble1(d); d = f.p0; return f.ret; })())) {
                return new ExcelCalcValue(new ExcelCalcErrorValue(4));
            }
            if (Math.abs(d) < c) {
                d = 0.1;
            }
        }
        d += 1;
        var f = a.pop();
        if (f._w == false) {
            return new ExcelCalcValue(new ExcelCalcErrorValue(1));
        }
        var g = 0;
        var h = 0;
        var i = 0;
        for (var j = 0; j < 20; ++j) {
            var k = true;
            var l = 0;
            var m = false;
            var n = false;
            var o = f.toArrayProxy();
            var _loop_1 = function (p) {
                if (!((function () { var q = p._toDouble1(i); i = q.p0; return q.ret; })())) {
                    return "continue";
                }
                if (i < 0) {
                    n = true;
                }
                else if (0 < i) {
                    m = true;
                }
                l++;
                if (k) {
                    g = i;
                    h = 0;
                    k = false;
                }
                else {
                    h = g + (h * d);
                    g = i + (g * d);
                }
            };
            try {
                for (var _b = (e_1 = void 0, __values(fromEnum(o))), _c = _b.next(); !_c.done; _c = _b.next()) {
                    var p = _c.value;
                    _loop_1(p);
                }
            }
            catch (e_1_1) {
                e_1 = { error: e_1_1 };
            }
            finally {
                try {
                    if (_c && !_c.done && (_a = _b.return))
                        _a.call(_b);
                }
                finally {
                    if (e_1)
                        throw e_1.error;
                }
            }
            if (l < 2 || n == false || m == false) {
                return new ExcelCalcValue(new ExcelCalcErrorValue(4));
            }
            var q = g / h;
            d -= q;
            if (Math.abs(q) < c) {
                return new ExcelCalcValue(d - 1);
            }
            if (Math.abs(d) < c) {
                return new ExcelCalcValue(new ExcelCalcErrorValue(4));
            }
        }
        return new ExcelCalcValue(new ExcelCalcErrorValue(4));
    };
    UltraCalcFunctionIRR.prototype.canParameterBeEnumerable = function (a) {
        return a == 0;
    };
    UltraCalcFunctionIRR.prototype.get_name = function () {
        return "irr";
    };
    Object.defineProperty(UltraCalcFunctionIRR.prototype, "name", {
        get: function () {
            return this.get_name();
        },
        enumerable: false,
        configurable: true
    });
    UltraCalcFunctionIRR.prototype.get_minArgs = function () {
        return 1;
    };
    Object.defineProperty(UltraCalcFunctionIRR.prototype, "minArgs", {
        get: function () {
            return this.get_minArgs();
        },
        enumerable: false,
        configurable: true
    });
    UltraCalcFunctionIRR.prototype.get_maxArgs = function () {
        return 2;
    };
    Object.defineProperty(UltraCalcFunctionIRR.prototype, "maxArgs", {
        get: function () {
            return this.get_maxArgs();
        },
        enumerable: false,
        configurable: true
    });
    UltraCalcFunctionIRR.$t = markType(UltraCalcFunctionIRR, 'UltraCalcFunctionIRR', BuiltInFunctionBase.$);
    return UltraCalcFunctionIRR;
}(BuiltInFunctionBase));
export { UltraCalcFunctionIRR };
