/*
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 } from "tslib";
import { BuiltInFunctionBase } from "./BuiltInFunctionBase";
import { ExcelCalcValue } from "./ExcelCalcValue";
import { RefBase } from "./RefBase";
import { typeCast, String_$type, markType } from "igniteui-angular-core";
import { ExcelCalcErrorValue } from "./ExcelCalcErrorValue";
import { CellAddress } from "./CellAddress";
import { WorksheetCell } from "./WorksheetCell";
import { GetFormulaStringInfo } from "./GetFormulaStringInfo";
import { MathUtilities } from "./MathUtilities";
import { FormattedString } from "./FormattedString";
import { nullableEquals } from "igniteui-angular-core";
import { stringStartsWith, stringEndsWith, stringIsNullOrEmpty } from "igniteui-angular-core";
/**
 * @hidden
 */
var UltraCalcFunctionCell = /** @class */ /*@__PURE__*/ (function (_super) {
    __extends(UltraCalcFunctionCell, _super);
    function UltraCalcFunctionCell() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    UltraCalcFunctionCell.prototype.evaluate = function (a, b) {
        var c = null;
        if (b == 2) {
            c = a.pop();
        }
        var d = a.pop();
        if (d.isError) {
            return new ExcelCalcValue(d.toErrorValue());
        }
        var e = d.toString().toLowerCase();
        var f = null;
        var g;
        var h;
        var i, j;
        if (c == null) {
            var k = a._r._u;
            if (k.b == null || k.b.workbook == null) {
                k.e();
            }
            if (k.b == null || k.b.workbook == null) {
                return new ExcelCalcValue(new ExcelCalcErrorValue(1));
            }
            h = k.b;
            j = k.c;
            i = k.d;
            g = a._r._bb.i(h.index);
        }
        else {
            f = typeCast(RefBase.$, c.toReference());
            if (f == null) {
                return new ExcelCalcValue(new ExcelCalcErrorValue(1));
            }
            g = f._q;
            h = f._u;
            j = f._ao;
            i = f._as;
        }
        var l = h != null ? h._rows$i._aj(i) : null;
        var m;
        switch (e) {
            case "address":
                {
                    var n = CellAddress.u(i, j, h._o, false, false, -1, -1, false, h._c);
                    if (WorksheetCell.l_op_Equality(a.owningCell, null) || h != a.owningCell.worksheet) {
                        var o = g.o(GetFormulaStringInfo.a, true);
                        if (stringStartsWith(o, "'") && stringEndsWith(o, "'!")) {
                            o = o.substr(1, o.length - 3);
                            var p = o.indexOf("[");
                            if (p > 0) {
                                o = o.substr(p);
                            }
                            o += "!";
                        }
                        n = o + n;
                    }
                    m = n;
                    break;
                }
            case "col":
                {
                    m = j + 1;
                    break;
                }
            case "contents":
            case "type":
                {
                    m = UltraCalcFunctionCell._ak(c, f, g, j, l, false);
                    if (e == "type") {
                        if (m == null) {
                            m = "b";
                        }
                        else {
                            var q = typeCast(String_$type, m);
                            if (q != null) {
                                m = "l";
                            }
                            else {
                                m = "v";
                            }
                        }
                    }
                    break;
                }
            case "filename":
                {
                    if (!g.j && stringIsNullOrEmpty(g.e.x)) {
                        m = "";
                    }
                    else {
                        var r = g.o(GetFormulaStringInfo.a, true);
                        if (r != null && r.length > 0) {
                            if (r.charAt(r.length - 1) == '!') {
                                r = r.substr(0, r.length - 1);
                            }
                            if (stringStartsWith(r, "'") && stringEndsWith(r, "'")) {
                                r = r.substr(1, r.length - 2);
                            }
                        }
                        m = r;
                    }
                    break;
                }
            case "color":
            case "format":
            case "parentheses":
            case "prefix":
            case "protect":
                {
                    if (g.j) {
                        m = new ExcelCalcErrorValue(3);
                    }
                    else {
                        var s = h._b4(l, j);
                        switch (e) {
                            case "color":
                                {
                                    var t = s._cs;
                                    var u = h.workbook._b2.a(t);
                                    var v = u.c(1);
                                    m = v != null && !stringIsNullOrEmpty(v.color) ? 1 : 0;
                                    break;
                                }
                            case "parentheses":
                                {
                                    var w = s._cs;
                                    var x = h.workbook._b2.a(w);
                                    var y = x.c(0);
                                    m = y != null && y.hasParentheses && !y.hasVerbatimString ? 1 : 0;
                                    break;
                                }
                            case "prefix":
                                m = UltraCalcFunctionCell._ak(c, f, g, j, l, true);
                                if (typeof m === 'string') {
                                    switch (s._a4) {
                                        case 1:
                                        case 7:
                                        case 5:
                                        case 0:
                                            m = "'";
                                            break;
                                        case 2:
                                        case 6:
                                            m = "^";
                                            break;
                                        case 3:
                                            m = "'";
                                            break;
                                        case 4:
                                            m = "\\";
                                            break;
                                        default:
                                            m = "";
                                            break;
                                    }
                                }
                                else {
                                    m = "";
                                }
                                break;
                            case "protect":
                                m = nullableEquals(s._c5, false) ? 0 : 1;
                                break;
                            default:
                            case "format":
                                {
                                    var z = s._cs;
                                    var aa = h.workbook._b2.a(z);
                                    m = aa.ah();
                                    break;
                                }
                        }
                    }
                    break;
                }
            case "row":
                {
                    m = i + 1;
                    break;
                }
            case "width":
                {
                    if (g.j) {
                        m = new ExcelCalcErrorValue(3);
                    }
                    else {
                        var ab = h._fp(j, false);
                        m = MathUtilities.f(h._fn(ab));
                    }
                    break;
                }
            default:
                m = new ExcelCalcErrorValue(1);
                break;
        }
        return new ExcelCalcValue(m);
    };
    UltraCalcFunctionCell._ak = function (a, b, c, d, e, f) {
        var g;
        if (e == null) {
            if (a != null && c.j) {
                if (b.value != null) {
                    g = b.value.value;
                }
                else {
                    g = null;
                }
            }
            else {
                g = null;
            }
        }
        else {
            if (!e._bt(d)) {
                g = null;
            }
            else {
                if (f) {
                    var h = e.getCellFormula(d);
                    if (h != null) {
                        return h;
                    }
                }
                var i = null;
                if (b == null) {
                    b = e._as(d);
                    if (b._af && b._formula != null) {
                        i = b._formula.evaluateCore(e.worksheet.workbook);
                    }
                }
                if (i == null) {
                    i = new ExcelCalcValue(b);
                }
                var j = i._bn(false, false);
                if (typeCast(FormattedString.$, j) !== null) {
                    g = j.unformattedString;
                }
                else {
                    g = j;
                }
            }
        }
        return g;
    };
    UltraCalcFunctionCell.prototype.get_name = function () {
        return "cell";
    };
    Object.defineProperty(UltraCalcFunctionCell.prototype, "name", {
        get: function () {
            return this.get_name();
        },
        enumerable: false,
        configurable: true
    });
    UltraCalcFunctionCell.prototype.get_minArgs = function () {
        return 1;
    };
    Object.defineProperty(UltraCalcFunctionCell.prototype, "minArgs", {
        get: function () {
            return this.get_minArgs();
        },
        enumerable: false,
        configurable: true
    });
    UltraCalcFunctionCell.prototype.get_maxArgs = function () {
        return 2;
    };
    Object.defineProperty(UltraCalcFunctionCell.prototype, "maxArgs", {
        get: function () {
            return this.get_maxArgs();
        },
        enumerable: false,
        configurable: true
    });
    UltraCalcFunctionCell.$t = markType(UltraCalcFunctionCell, 'UltraCalcFunctionCell', BuiltInFunctionBase.$);
    return UltraCalcFunctionCell;
}(BuiltInFunctionBase));
export { UltraCalcFunctionCell };
