/*
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 { Regex } from "igniteui-angular-core";
import { toNullable, Number_$type, markType } from "igniteui-angular-core";
import { ExcelCalcValue } from "./ExcelCalcValue";
import { ExcelCalcErrorValue } from "./ExcelCalcErrorValue";
import { ValueFormatter } from "./ValueFormatter";
import { StringUtilities } from "./StringUtilities";
import { dateTryParse } from "igniteui-angular-core";
import { tryParseInt32_1 } from "igniteui-angular-core";
import { startsWith1, stringFormat, stringFormat1 } from "igniteui-angular-core";
/**
 * @hidden
 */
var UltraCalcFunctionText = /** @class */ (function (_super) {
    tslib_1.__extends(UltraCalcFunctionText, _super);
    function UltraCalcFunctionText() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    UltraCalcFunctionText.prototype.evaluate = function (a, b) {
        var c = a.pop();
        var d = a.pop();
        if (d.isError) {
            return d;
        }
        if (c.isError) {
            return c;
        }
        if (c.isBoolean) {
            return new ExcelCalcValue(new ExcelCalcErrorValue(1));
        }
        var e = d.toString();
        var f = c.toString();
        var g;
        var h = ((function () { var i = d._toDouble1(g); g = i.p0; return i.ret; })());
        if (h == false && d.isString) {
            var i = this._ak(e);
            if (i.hasValue) {
                g = i.value;
                h = true;
            }
            else {
                var j_1;
                if (((function () { var k = dateTryParse(e, j_1); j_1 = k.p1; return k.ret; })())) {
                    var k = ExcelCalcValue._dateTimeToExcelDate(d._o, j_1);
                    if (k.hasValue) {
                        g = k.value;
                        h = true;
                    }
                }
            }
        }
        var l = NaN;
        if (h) {
            l = g;
        }
        var m = new ValueFormatter(0, d._o, f, a._w);
        var n;
        if (((function () { var o = m.v(l, e, n); n = o.p2; return o.ret; })()) == false) {
            return new ExcelCalcValue(new ExcelCalcErrorValue(1));
        }
        return new ExcelCalcValue(n);
    };
    UltraCalcFunctionText.prototype.get_name = function () {
        return "text";
    };
    Object.defineProperty(UltraCalcFunctionText.prototype, "name", {
        get: function () {
            return this.get_name();
        },
        enumerable: true,
        configurable: true
    });
    UltraCalcFunctionText.prototype.get_minArgs = function () {
        return 2;
    };
    Object.defineProperty(UltraCalcFunctionText.prototype, "minArgs", {
        get: function () {
            return this.get_minArgs();
        },
        enumerable: true,
        configurable: true
    });
    UltraCalcFunctionText.prototype.get_maxArgs = function () {
        return 2;
    };
    Object.defineProperty(UltraCalcFunctionText.prototype, "maxArgs", {
        get: function () {
            return this.get_maxArgs();
        },
        enumerable: true,
        configurable: true
    });
    UltraCalcFunctionText.prototype._ak = function (a) {
        var b = UltraCalcFunctionText._am.i(a);
        if (b.i == false) {
            return toNullable(Number_$type, null);
        }
        var c = 0;
        var d = 0;
        var e = 0;
        var f = 0;
        var g = b.m.item1("Hours");
        if (g.d != 0) {
            if (((function () { var h = tryParseInt32_1(g.g, c); c = h.p1; return h.ret; })()) == false) {
            }
        }
        var h = b.m.item1("Minutes");
        if (h.d != 0) {
            if (((function () { var i = tryParseInt32_1(h.g, d); d = i.p1; return i.ret; })()) == false) {
            }
        }
        var i = b.m.item1("Seconds");
        if (i.d != 0) {
            if (((function () { var j = tryParseInt32_1(i.g, e); e = j.p1; return j.ret; })()) == false) {
            }
        }
        var j = b.m.item1("Milliseconds");
        if (j.d != 0) {
            var k_1 = j.g;
            while (k_1.length < 4) {
                k_1 += "0";
            }
            if (((function () { var l = tryParseInt32_1(k_1, f); f = l.p1; return l.ret; })()) == false) {
            }
        }
        var l = b.m.item1("AMPM");
        if (l.d != 0) {
            if (c >= 12) {
                return toNullable(Number_$type, null);
            }
            if (startsWith1(l.g, "P", StringUtilities.j)) {
                c += 12;
            }
        }
        return toNullable(Number_$type, (c / 24) + (d / 1440) + (e / 86400) + (f / 864000000));
    };
    Object.defineProperty(UltraCalcFunctionText, "_am", {
        get: function () {
            if (UltraCalcFunctionText._al == null) {
                var a = "(?'Hours'[0-9]{1,4})\\s*";
                var b = "\\s*(?'Minutes'[0-9]?[0-9])\\s*";
                var c = "\\s*(?'Seconds'[0-9]?[0-9])\\s*";
                var d = ".\\s*(?'Milliseconds'[0-9]{0,4})[0-9]*\\s*";
                var e = "(\\s+(?'AMPM'(a|p)m?)\\s*)";
                var f = stringFormat("({0}{1})", a, e);
                var g = stringFormat1("({0}:{1}(:{2}({3})?)?{4}?)", a, b, c, d, e);
                var h = stringFormat1("({0}:{1}{2}{3}?)", b, c, d, e);
                UltraCalcFunctionText._al = new Regex(stringFormat("^({0}|{1}|{2})$", f, g, h), 8 | 1 | 32 | 512);
            }
            return UltraCalcFunctionText._al;
        },
        enumerable: true,
        configurable: true
    });
    UltraCalcFunctionText.prototype.doesParameterAllowIntermediateResultArray = function (a, b) {
        return true;
    };
    UltraCalcFunctionText.$t = markType(UltraCalcFunctionText, 'UltraCalcFunctionText', BuiltInFunctionBase.$);
    UltraCalcFunctionText._al = null;
    return UltraCalcFunctionText;
}(BuiltInFunctionBase));
export { UltraCalcFunctionText };
//# sourceMappingURL=UltraCalcFunctionText.js.map