/*
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, __values } from "tslib";
import { BuiltInFunctionBase } from "./BuiltInFunctionBase";
import { ExcelCalcValue } from "./ExcelCalcValue";
import { fromEnum, markType } from "igniteui-angular-core";
import { UltraCalcFunctionWorkDay } from "./UltraCalcFunctionWorkDay";
import { UltraCalcFunctionDateDiff } from "./UltraCalcFunctionDateDiff";
import { intDivide } from "igniteui-angular-core";
import { dateGetDate } from "igniteui-angular-core";
/**
 * @hidden
 */
var UltraCalcFunctionNetWorkDays = /** @class */ /*@__PURE__*/ (function (_super) {
    __extends(UltraCalcFunctionNetWorkDays, _super);
    function UltraCalcFunctionNetWorkDays() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    UltraCalcFunctionNetWorkDays.prototype.evaluate = function (a, b) {
        var e_1, _a;
        var c = this._getArguments(a, b - 2, true);
        var d;
        var e;
        var f = ((function () { var g = UltraCalcFunctionWorkDay._ak(c, d, e); d = g.p1; e = g.p2; return g.ret; })());
        if (f == false) {
            return e;
        }
        var g = dateGetDate(a.pop()._toDateTime1());
        var h = dateGetDate(a.pop()._toDateTime1());
        var i = 1;
        if (h > g) {
            i = -1;
            var j = h;
            h = g;
            g = j;
        }
        var k = UltraCalcFunctionDateDiff._am(a._w, 4, h, g);
        k += 1;
        if (k > 0) {
            var l = (intDivide(k, 7));
            var m = (k % 7);
            k -= (l * 2);
            switch (h.getDay()) {
                case 1:
                case 2:
                case 3:
                case 4:
                case 5:
                case 6:
                    var n = 8 - h.getDay();
                    if (m >= n) {
                        k -= Math.min(2, n);
                    }
                    else if (m == (n - 1)) {
                        k -= Math.min(1, n);
                    }
                    break;
                case 0:
                    if (m >= 1) {
                        k -= 1;
                    }
                    break;
                default: break;
            }
            if (d != null) {
                try {
                    for (var _b = __values(fromEnum(d.keys)), _c = _b.next(); !_c.done; _c = _b.next()) {
                        var o = _c.value;
                        if (o >= h && o <= g) {
                            k -= 1;
                        }
                    }
                }
                catch (e_1_1) {
                    e_1 = { error: e_1_1 };
                }
                finally {
                    try {
                        if (_c && !_c.done && (_a = _b.return))
                            _a.call(_b);
                    }
                    finally {
                        if (e_1)
                            throw e_1.error;
                    }
                }
            }
            k *= i;
        }
        return new ExcelCalcValue(k);
    };
    UltraCalcFunctionNetWorkDays.prototype.canParameterBeEnumerable = function (a) {
        return a == 2;
    };
    UltraCalcFunctionNetWorkDays.prototype.get_name = function () {
        return "networkdays";
    };
    Object.defineProperty(UltraCalcFunctionNetWorkDays.prototype, "name", {
        get: function () {
            return this.get_name();
        },
        enumerable: false,
        configurable: true
    });
    UltraCalcFunctionNetWorkDays.prototype.get_minArgs = function () {
        return 2;
    };
    Object.defineProperty(UltraCalcFunctionNetWorkDays.prototype, "minArgs", {
        get: function () {
            return this.get_minArgs();
        },
        enumerable: false,
        configurable: true
    });
    UltraCalcFunctionNetWorkDays.prototype.get_maxArgs = function () {
        return 3;
    };
    Object.defineProperty(UltraCalcFunctionNetWorkDays.prototype, "maxArgs", {
        get: function () {
            return this.get_maxArgs();
        },
        enumerable: false,
        configurable: true
    });
    UltraCalcFunctionNetWorkDays.$t = markType(UltraCalcFunctionNetWorkDays, 'UltraCalcFunctionNetWorkDays', BuiltInFunctionBase.$);
    return UltraCalcFunctionNetWorkDays;
}(BuiltInFunctionBase));
export { UltraCalcFunctionNetWorkDays };
