/*
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 { ExcelCalcErrorValue } from "./ExcelCalcErrorValue";
import { UltraCalcFunctionBinomDist } from "./UltraCalcFunctionBinomDist";
import { markType } from "igniteui-angular-core";
import { truncate } from "igniteui-angular-core";
/**
 * @hidden
 */
var UltraCalcFunctionCritBinom = /** @class */ (function (_super) {
    tslib_1.__extends(UltraCalcFunctionCritBinom, _super);
    function UltraCalcFunctionCritBinom() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    UltraCalcFunctionCritBinom.prototype.evaluate = function (a, b) {
        var c = a.pop();
        if (c.isError) {
            return new ExcelCalcValue(c.toErrorValue());
        }
        var d = c._toDouble3();
        c = a.pop();
        if (c.isError) {
            return new ExcelCalcValue(c.toErrorValue());
        }
        var e = c._toDouble3();
        c = a.pop();
        if (c.isError) {
            return new ExcelCalcValue(c.toErrorValue());
        }
        var f = truncate(c._toDecimal2());
        if (f < 0 || e <= 0 || e >= 1 || d <= 0 || d >= 1) {
            return new ExcelCalcValue(new ExcelCalcErrorValue(4));
        }
        var g = 0;
        if (f >= 1030) {
            var h = Math.pow(10, -12);
            var i = f;
            var j = e;
            var k = void 0, l = void 0;
            if (d <= 0.5) {
                k = Math.sqrt(Math.log(1 / (d * d)));
                l = -k + (2.515517 + 0.802853 * k + 0.010328 * k * k) / (1 + 1.432788 * k + 0.189269 * k * k + 0.001308 * k * k * k);
            }
            else {
                k = Math.sqrt(Math.log(1 / Math.pow(1 - d, 2)));
                l = k - (2.515517 + 0.802853 * k + 0.010328 * k * k) / (1 + 1.432788 * k + 0.189269 * k * k + 0.001308 * k * k * k);
            }
            var m = truncate(Math.floor(i * j + l * Math.sqrt(i * j * (1 - j))));
            if (m < 0) {
                m = 0;
            }
            else if (m > i) {
                m = i;
            }
            var n = truncate((i * j));
            var o = 0;
            var p = 0;
            var q = 0;
            o = o + 1;
            if (n == m) {
                p = p + 1;
            }
            if (n <= m) {
                q = q + 1;
            }
            var r = 1;
            var s = false;
            var t = n + 1;
            while (!s && t <= i) {
                var u = r * (i - t + 1) * j / (t * (1 - j));
                o += u;
                if (t == m) {
                    p += u;
                }
                if (t <= m) {
                    q += u;
                }
                if (u <= h) {
                    s = true;
                }
                r = u;
                t = t + 1;
            }
            r = 1;
            s = false;
            t = n - 1;
            while (!s && t >= 0) {
                var v = r * (t + 1) * (1 - j) / ((i - t) * j);
                o = o + v;
                if (t == m) {
                    p += v;
                }
                if (t <= m) {
                    q += v;
                }
                if (v <= h) {
                    s = true;
                }
                r = v;
                t = t - 1;
            }
            var w = p / o;
            var x = q / o;
            var y = x - w;
            while (true) {
                if (y < d && x >= d) {
                    g = m;
                    break;
                }
                if (y < d && x < d) {
                    var z = w * (i - m) * j / m / (1 - j);
                    y = x;
                    x = x + z;
                    w = z;
                    m++;
                }
                else if (y > d && x > d) {
                    var aa = w * m * (1 - j) / (i - m + 1) / j;
                    x = y;
                    y = y - w;
                    w = aa;
                    m--;
                }
                else {
                    break;
                }
            }
        }
        else {
            var ab = 0;
            for (g = 0; g <= f; g++) {
                ab += UltraCalcFunctionBinomDist._al(g, f, e);
                if (ab >= d) {
                    break;
                }
            }
        }
        return new ExcelCalcValue(g);
    };
    UltraCalcFunctionCritBinom.prototype.get_name = function () {
        return "critbinom";
    };
    Object.defineProperty(UltraCalcFunctionCritBinom.prototype, "name", {
        get: function () {
            return this.get_name();
        },
        enumerable: true,
        configurable: true
    });
    UltraCalcFunctionCritBinom.prototype.get_minArgs = function () {
        return 3;
    };
    Object.defineProperty(UltraCalcFunctionCritBinom.prototype, "minArgs", {
        get: function () {
            return this.get_minArgs();
        },
        enumerable: true,
        configurable: true
    });
    UltraCalcFunctionCritBinom.prototype.get_maxArgs = function () {
        return 3;
    };
    Object.defineProperty(UltraCalcFunctionCritBinom.prototype, "maxArgs", {
        get: function () {
            return this.get_maxArgs();
        },
        enumerable: true,
        configurable: true
    });
    UltraCalcFunctionCritBinom.$t = markType(UltraCalcFunctionCritBinom, 'UltraCalcFunctionCritBinom', BuiltInFunctionBase.$);
    return UltraCalcFunctionCritBinom;
}(BuiltInFunctionBase));
export { UltraCalcFunctionCritBinom };
//# sourceMappingURL=UltraCalcFunctionCritBinom.js.map