/*
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 { markType } from "igniteui-angular-core";
/**
 * @hidden
 */
var UltraCalcFunctionIndex = /** @class */ (function (_super) {
    tslib_1.__extends(UltraCalcFunctionIndex, _super);
    function UltraCalcFunctionIndex() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    UltraCalcFunctionIndex.prototype.evaluate = function (a, b) {
        var c = null;
        if (b == 4) {
            c = a.pop();
        }
        var d = null;
        if (b >= 3) {
            d = a.pop();
        }
        var e = a.pop();
        var f = a.pop();
        if (f.isReference == false && f.isError) {
            return f;
        }
        if (e.isError) {
            return e;
        }
        if (d != null && d.isError) {
            return d;
        }
        if (c != null && c.isError) {
            return c;
        }
        if (f.isReference == false) {
            if (f.isString || f.isBoolean) {
                return new ExcelCalcValue(new ExcelCalcErrorValue(1));
            }
        }
        var g = e._toInt321() - 1;
        var h = (d == null ? 1 : d._toInt321()) - 1;
        var i = (c == null ? 1 : c._toInt321()) - 1;
        var j = f._b(false);
        if (j.length <= i) {
            return new ExcelCalcValue(new ExcelCalcErrorValue(0));
        }
        var k = j[i];
        var l = k.getLength(0);
        var m = k.getLength(1);
        if (d == null && l > 1) {
            if (m == 1) {
                h = g;
                g = 0;
            }
            else {
                return new ExcelCalcValue(new ExcelCalcErrorValue(0));
            }
        }
        if (l <= h || m <= g) {
            return new ExcelCalcValue(new ExcelCalcErrorValue(0));
        }
        if (h < -1 || g < -1) {
            return new ExcelCalcValue(new ExcelCalcErrorValue(1));
        }
        if (g == -1 || h == -1) {
            if (g == -1) {
                if (h == -1) {
                    return new ExcelCalcValue(k._a());
                }
                else {
                    var n = ((function () {
                        var $firstRank;
                        var $ret = new Array($firstRank = 1);
                        var $currRet = $ret;
                        for (var $rankInit = 0; $rankInit < $firstRank; $rankInit++) {
                            $currRet[$rankInit] = new Array(m);
                        }
                        return $ret;
                    })());
                    for (var o = 0; o < m; o++) {
                        n[0][o] = k.item(h, o);
                    }
                    return new ExcelCalcValue(n);
                }
            }
            else {
                var p = ((function () {
                    var $firstRank;
                    var $ret = new Array($firstRank = l);
                    var $currRet = $ret;
                    for (var $rankInit = 0; $rankInit < $firstRank; $rankInit++) {
                        $currRet[$rankInit] = new Array(1);
                    }
                    return $ret;
                })());
                for (var q = 0; q < l; q++) {
                    p[q][0] = k.item(q, g);
                }
                return new ExcelCalcValue(p);
            }
        }
        return k.item(h, g);
    };
    UltraCalcFunctionIndex.prototype.get_name = function () {
        return "index";
    };
    Object.defineProperty(UltraCalcFunctionIndex.prototype, "name", {
        get: function () {
            return this.get_name();
        },
        enumerable: true,
        configurable: true
    });
    UltraCalcFunctionIndex.prototype.get_minArgs = function () {
        return 2;
    };
    Object.defineProperty(UltraCalcFunctionIndex.prototype, "minArgs", {
        get: function () {
            return this.get_minArgs();
        },
        enumerable: true,
        configurable: true
    });
    UltraCalcFunctionIndex.prototype.get_maxArgs = function () {
        return 4;
    };
    Object.defineProperty(UltraCalcFunctionIndex.prototype, "maxArgs", {
        get: function () {
            return this.get_maxArgs();
        },
        enumerable: true,
        configurable: true
    });
    UltraCalcFunctionIndex.prototype._v = function (a) {
        if (a == 0) {
            return false;
        }
        return true;
    };
    UltraCalcFunctionIndex.$t = markType(UltraCalcFunctionIndex, 'UltraCalcFunctionIndex', BuiltInFunctionBase.$);
    return UltraCalcFunctionIndex;
}(BuiltInFunctionBase));
export { UltraCalcFunctionIndex };
//# sourceMappingURL=UltraCalcFunctionIndex.js.map