/*
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 { UltraCalcFunctionFact } from "./UltraCalcFunctionFact";
import { Stack$1 } from "igniteui-angular-core";
import { Number_$type, markType } from "igniteui-angular-core";
import { truncate, intDivide, isNaN_ } from "igniteui-angular-core";
/**
 * @hidden
 */
var UltraCalcFunctionCombin = /** @class */ (function (_super) {
    tslib_1.__extends(UltraCalcFunctionCombin, _super);
    function UltraCalcFunctionCombin() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    UltraCalcFunctionCombin.prototype.evaluate = function (a, b) {
        var c = a.pop();
        if (c.isError) {
            return new ExcelCalcValue(c.toErrorValue());
        }
        var d = c._toDecimal2();
        c = a.pop();
        if (c.isError) {
            return new ExcelCalcValue(c.toErrorValue());
        }
        var e = c._toDecimal2();
        var f = truncate(d);
        var g = truncate(e);
        if (e < 0 || d < 0 || g < f) {
            return new ExcelCalcValue(new ExcelCalcErrorValue(4));
        }
        var h = UltraCalcFunctionCombin._am(g, f, false);
        if (isNaN_(h)) {
            return new ExcelCalcValue(new ExcelCalcErrorValue(4));
        }
        return new ExcelCalcValue(h);
    };
    UltraCalcFunctionCombin._am = function (a, b, c) {
        if (c) {
            a = a + b - 1;
        }
        if (a <= 170) {
            var d = UltraCalcFunctionFact._ak(a);
            var e = UltraCalcFunctionFact._ak(b);
            var f = a - b;
            var g = UltraCalcFunctionFact._ak(f);
            var h = d / (e * g);
            return h;
        }
        else {
            return UltraCalcFunctionCombin._an(a, b);
        }
    };
    UltraCalcFunctionCombin._an = function (a, b) {
        if (b > a) {
            return NaN;
        }
        if (UltraCalcFunctionCombin._ak == null) {
            UltraCalcFunctionCombin._ak = new Stack$1(Number_$type);
        }
        else {
            UltraCalcFunctionCombin._ak.g();
        }
        if (UltraCalcFunctionCombin._al == null) {
            UltraCalcFunctionCombin._al = new Stack$1(Number_$type);
        }
        else {
            UltraCalcFunctionCombin._al.g();
        }
        var c = 1;
        while (true) {
            if (b <= 0) {
                break;
            }
            if (b > intDivide(a, 2)) {
                b = a - b;
            }
            else {
                UltraCalcFunctionCombin._ak.h(b);
                UltraCalcFunctionCombin._al.h(a);
                a--;
                b--;
            }
        }
        while (UltraCalcFunctionCombin._al.f > 0) {
            var d = UltraCalcFunctionCombin._al.e();
            var e = UltraCalcFunctionCombin._ak.e();
            c = d * c / e;
        }
        return c;
    };
    UltraCalcFunctionCombin.prototype.get_name = function () {
        return "combin";
    };
    Object.defineProperty(UltraCalcFunctionCombin.prototype, "name", {
        get: function () {
            return this.get_name();
        },
        enumerable: true,
        configurable: true
    });
    UltraCalcFunctionCombin.prototype.get_minArgs = function () {
        return 2;
    };
    Object.defineProperty(UltraCalcFunctionCombin.prototype, "minArgs", {
        get: function () {
            return this.get_minArgs();
        },
        enumerable: true,
        configurable: true
    });
    UltraCalcFunctionCombin.prototype.get_maxArgs = function () {
        return 2;
    };
    Object.defineProperty(UltraCalcFunctionCombin.prototype, "maxArgs", {
        get: function () {
            return this.get_maxArgs();
        },
        enumerable: true,
        configurable: true
    });
    UltraCalcFunctionCombin.$t = markType(UltraCalcFunctionCombin, 'UltraCalcFunctionCombin', BuiltInFunctionBase.$);
    UltraCalcFunctionCombin._ak = null;
    UltraCalcFunctionCombin._al = null;
    return UltraCalcFunctionCombin;
}(BuiltInFunctionBase));
export { UltraCalcFunctionCombin };
//# sourceMappingURL=UltraCalcFunctionCombin.js.map