/*
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 { MathUtilities } from "./MathUtilities";
import { Base, markType } from "igniteui-angular-core";
/**
 * @hidden
 */
var UltraCalcFunctionPercentRankBase = /** @class */ (function (_super) {
    tslib_1.__extends(UltraCalcFunctionPercentRankBase, _super);
    function UltraCalcFunctionPercentRankBase() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    UltraCalcFunctionPercentRankBase._ak = function (a, b, c) {
        var d = 3;
        if (b == 3) {
            var e = a.pop();
            d = e._toInt();
            if (d < 1) {
                return new ExcelCalcValue(new ExcelCalcErrorValue(4));
            }
        }
        var f = a.pop();
        var g = a.pop();
        if (f.isError) {
            return new ExcelCalcValue(f.toErrorValue());
        }
        var h = f._toDouble3();
        var i = g.toArrayProxy();
        var j = i.getLength(0);
        var k = i.getLength(1);
        var l, m, n;
        l = m = n = 0;
        var o = Number.POSITIVE_INFINITY;
        var p = Number.NEGATIVE_INFINITY;
        var q, r;
        q = r = 0;
        for (var s = 0; s < j; s++) {
            for (var t = 0; t < k; t++) {
                var u = i.item(s, t);
                if (u.isError) {
                    return new ExcelCalcValue(u.toErrorValue());
                }
                if (u.isNull || u.isString || u.isBoolean) {
                    l++;
                }
                else {
                    var v = u._toDouble3();
                    var w = Base.compareSimple(h, v);
                    if (w == 0) {
                        if (n == 0) {
                            m += r;
                            r = q = 0;
                        }
                        n++;
                    }
                    else if (w < 0) {
                        if (n == 0) {
                            w = Base.compareSimple(o, v);
                            if (w == 0) {
                                q++;
                            }
                            else if (w > 0) {
                                q = 1;
                                o = v;
                            }
                        }
                    }
                    else {
                        if (n != 0) {
                            m++;
                        }
                        else {
                            w = Base.compareSimple(v, p);
                            if (w < 0) {
                                m++;
                            }
                            else if (w == 0) {
                                r++;
                            }
                            else {
                                m += r;
                                r = 1;
                                p = v;
                            }
                        }
                    }
                }
            }
        }
        var x = (k * j) - l;
        if (x == 0) {
            return new ExcelCalcValue(new ExcelCalcErrorValue(3));
        }
        if (x == n) {
            return new ExcelCalcValue(x == 1 ? 1 : 0);
        }
        var y;
        if (c) {
            m++;
            x += 2;
        }
        if (n == 0) {
            if (q == 0 || r == 0) {
                return new ExcelCalcValue(new ExcelCalcErrorValue(3));
            }
            var z = (m + r - 1) / (x - 1);
            var aa = (m + r) / (x - 1);
            var ab = (h - p) / (o - p);
            y = z + ab * (aa - z);
        }
        else if (m == 0) {
            y = 0;
        }
        else {
            y = m / (x - 1);
        }
        y = MathUtilities.k(y, d);
        return new ExcelCalcValue(y);
    };
    UltraCalcFunctionPercentRankBase.prototype.get_maxArgs = function () {
        return 3;
    };
    Object.defineProperty(UltraCalcFunctionPercentRankBase.prototype, "maxArgs", {
        get: function () {
            return this.get_maxArgs();
        },
        enumerable: true,
        configurable: true
    });
    UltraCalcFunctionPercentRankBase.prototype.get_minArgs = function () {
        return 2;
    };
    Object.defineProperty(UltraCalcFunctionPercentRankBase.prototype, "minArgs", {
        get: function () {
            return this.get_minArgs();
        },
        enumerable: true,
        configurable: true
    });
    UltraCalcFunctionPercentRankBase.$t = markType(UltraCalcFunctionPercentRankBase, 'UltraCalcFunctionPercentRankBase', BuiltInFunctionBase.$);
    return UltraCalcFunctionPercentRankBase;
}(BuiltInFunctionBase));
export { UltraCalcFunctionPercentRankBase };
//# sourceMappingURL=UltraCalcFunctionPercentRankBase.js.map