/*
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 { Base, Type, markType } from "igniteui-angular-core";
import { WorkItem_ExceptionInfo } from "./WorkItem_ExceptionInfo";
import { InvalidOperationException } from "igniteui-angular-core";
import { Queue$1 } from "igniteui-angular-core";
import { dateNow } from "igniteui-angular-core";
import { timeSpanTicks } from "igniteui-angular-core";
/**
 * @hidden
 */
var WorkItem = /** @class */ /*@__PURE__*/ (function (_super) {
    __extends(WorkItem, _super);
    function WorkItem(a, b, c) {
        var _this = _super.call(this) || this;
        _this.j = null;
        _this.n = false;
        _this.o = false;
        _this.a = null;
        _this.b = null;
        _this.k = 0;
        _this.k = a;
        _this.o = c;
        if (b != null) {
            b.ab(_this);
        }
        return _this;
    }
    WorkItem.c = function (a, b) {
        if (b === void 0) {
            b = 0;
        }
        return new WorkItem_WorkItemSync(a, b, null, true);
    };
    WorkItem.d = function (a, b) {
        if (b === void 0) {
            b = 0;
        }
        return new WorkItem_WorkItemAsync(a, b, null, true);
    };
    WorkItem.e = function ($tResult, a, b) {
        if (b === void 0) {
            b = 0;
        }
        return new WorkItem_WorkItemSync$1($tResult, a, b, null, true);
    };
    WorkItem.f = function ($tResult, a, b) {
        if (b === void 0) {
            b = 0;
        }
        return new WorkItem_WorkItemAsync$1($tResult, a, b, null, true);
    };
    WorkItem.prototype.w = function (a) {
        var _this = this;
        try {
            if (this.r) {
                return;
            }
            if (this.j != null && this.o) {
                if (((function () { var b = _this.u(_this.j); _this.j = b.p0; return b.ret; })())) {
                    this.ac();
                }
                this.r = true;
            }
            else if (this.p(a)) {
                this.r = true;
            }
        }
        catch (b) {
            this.j = new WorkItem_ExceptionInfo(b);
            this.r = true;
        }
        finally {
            if (this.r) {
                this.z(a);
            }
        }
    };
    WorkItem.prototype.q = function (a, b) {
        if (a == null) {
            return true;
        }
        a.ab(this);
        if (a.t == false) {
            b.f(a);
        }
        else {
            a.w(b);
        }
        return false;
    };
    WorkItem.g = function ($t, a) {
        return new WorkItem_WorkItemSync$1($t, function () { return a; }, 1, null, true);
    };
    WorkItem.h = function ($t, a) {
        var b = WorkItem.g($t, a);
        b.w(WorkItemScheduler.c());
        return b;
    };
    WorkItem.prototype.x = function (a) {
        a.ab(null);
        this.j = a.j;
    };
    WorkItem.prototype.y = function () {
        if (this.j == null) {
            return;
        }
        this.j.c();
    };
    WorkItem.prototype.z = function (a) {
        var b = this.a;
        if (b != null) {
            b.x(this);
            if (b.t == false) {
                a.f(b);
            }
            else {
                b.w(a);
            }
        }
        else {
            this.y();
        }
    };
    WorkItem.prototype.aa = function (a) {
        this.j = a;
    };
    WorkItem.prototype.ab = function (a) {
        if (this.a == a) {
            return;
        }
        if (a == null) {
            this.a.b = null;
        }
        else {
            if (this.a != null) {
                throw new InvalidOperationException(1, "This work item has other work already scheduled after it.");
            }
            a.b = this.i;
        }
        this.a = a;
    };
    WorkItem.prototype.ac = function () {
        var a = this;
        var b = this.a;
        while (b != null && b.s) {
            b.x(a);
            b.r = true;
            a = b;
            b = b.a;
        }
    };
    WorkItem.prototype.u = function (a) {
        return {
            ret: false,
            p0: a
        };
    };
    Object.defineProperty(WorkItem.prototype, "v", {
        get: function () {
            if (this.j == null) {
                return null;
            }
            return this.j.b;
        },
        enumerable: false,
        configurable: true
    });
    WorkItem.prototype.get_s = function () {
        return false;
    };
    Object.defineProperty(WorkItem.prototype, "s", {
        get: function () {
            return this.get_s();
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(WorkItem.prototype, "r", {
        get: function () {
            return this.n;
        },
        set: function (a) {
            this.n = a;
        },
        enumerable: false,
        configurable: true
    });
    WorkItem.prototype.get_t = function () {
        return false;
    };
    Object.defineProperty(WorkItem.prototype, "t", {
        get: function () {
            return this.get_t();
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(WorkItem.prototype, "i", {
        get: function () {
            return this.b || this;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(WorkItem.prototype, "l", {
        get: function () {
            return this.k;
        },
        enumerable: false,
        configurable: true
    });
    WorkItem.$t = markType(WorkItem, 'WorkItem');
    WorkItem.m = function () {
    };
    return WorkItem;
}(Base));
export { WorkItem };
/**
 * @hidden
 */
var WorkItem_WorkItemSync = /** @class */ /*@__PURE__*/ (function (_super) {
    __extends(WorkItem_WorkItemSync, _super);
    function WorkItem_WorkItemSync(a, b, c, d) {
        var _this = _super.call(this, b, c, d) || this;
        _this.ad = null;
        _this.ad = a;
        return _this;
    }
    WorkItem_WorkItemSync.prototype.p = function (a) {
        this.ad();
        return true;
    };
    WorkItem_WorkItemSync.$t = markType(WorkItem_WorkItemSync, 'WorkItem_WorkItemSync', WorkItem.$);
    return WorkItem_WorkItemSync;
}(WorkItem));
export { WorkItem_WorkItemSync };
/**
 * @hidden
 */
var WorkItem$1 = /** @class */ /*@__PURE__*/ (function (_super) {
    __extends(WorkItem$1, _super);
    function WorkItem$1($tResult, a, b, c) {
        var _this = _super.call(this, a, b, c) || this;
        _this.$tResult = null;
        _this.ad = null;
        _this.$tResult = $tResult;
        _this.$type = _this.$type.specialize(_this.$tResult);
        return _this;
    }
    WorkItem$1.$t = markType(WorkItem$1, 'WorkItem$1', WorkItem.$);
    return WorkItem$1;
}(WorkItem));
export { WorkItem$1 };
/**
 * @hidden
 */
var WorkItem_WorkItemSync$1 = /** @class */ /*@__PURE__*/ (function (_super) {
    __extends(WorkItem_WorkItemSync$1, _super);
    function WorkItem_WorkItemSync$1($tResult, a, b, c, d) {
        var _this = _super.call(this, $tResult, b, c, d) || this;
        _this.$tResult = null;
        _this.ae = null;
        _this.$tResult = $tResult;
        _this.$type = _this.$type.specialize(_this.$tResult);
        _this.ae = a;
        return _this;
    }
    WorkItem_WorkItemSync$1.prototype.p = function (a) {
        this.ad = this.ae();
        return true;
    };
    WorkItem_WorkItemSync$1.$t = markType(WorkItem_WorkItemSync$1, 'WorkItem_WorkItemSync$1', WorkItem$1.$.specialize(0));
    return WorkItem_WorkItemSync$1;
}(WorkItem$1));
export { WorkItem_WorkItemSync$1 };
/**
 * @hidden
 */
var WorkItem_WorkItemAsync = /** @class */ /*@__PURE__*/ (function (_super) {
    __extends(WorkItem_WorkItemAsync, _super);
    function WorkItem_WorkItemAsync(a, b, c, d) {
        var _this = _super.call(this, b, c, d) || this;
        _this.ad = false;
        _this.ae = null;
        _this.ae = a;
        return _this;
    }
    WorkItem_WorkItemAsync.prototype.p = function (a) {
        if (this.ad == false) {
            this.ad = true;
            return this.q(this.ae(), a);
        }
        return true;
    };
    WorkItem_WorkItemAsync.$t = markType(WorkItem_WorkItemAsync, 'WorkItem_WorkItemAsync', WorkItem.$);
    return WorkItem_WorkItemAsync;
}(WorkItem));
export { WorkItem_WorkItemAsync };
/**
 * @hidden
 */
var WorkItem_WorkItemAsync$1 = /** @class */ /*@__PURE__*/ (function (_super) {
    __extends(WorkItem_WorkItemAsync$1, _super);
    function WorkItem_WorkItemAsync$1($tResult, a, b, c, d) {
        var _this = _super.call(this, $tResult, b, c, d) || this;
        _this.$tResult = null;
        _this.af = false;
        _this.ag = null;
        _this.ae = null;
        _this.$tResult = $tResult;
        _this.$type = _this.$type.specialize(_this.$tResult);
        _this.ag = a;
        return _this;
    }
    WorkItem_WorkItemAsync$1.prototype.p = function (a) {
        if (this.af == false) {
            this.af = true;
            var b = this.ag();
            this.ae = b;
            return this.q(b, a);
        }
        return true;
    };
    WorkItem_WorkItemAsync$1.prototype.x = function (a) {
        if (this.ae != null) {
            if (this.ae.v == null) {
                this.ad = WorkItemExtensions.m(this.$tResult, this.ae);
            }
            this.ae = null;
        }
        _super.prototype.x.call(this, a);
    };
    WorkItem_WorkItemAsync$1.$t = markType(WorkItem_WorkItemAsync$1, 'WorkItem_WorkItemAsync$1', WorkItem$1.$.specialize(0));
    return WorkItem_WorkItemAsync$1;
}(WorkItem$1));
export { WorkItem_WorkItemAsync$1 };
/**
 * @hidden
 */
var WorkItemScheduler = /** @class */ /*@__PURE__*/ (function (_super) {
    __extends(WorkItemScheduler, _super);
    function WorkItemScheduler() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    WorkItemScheduler.b = function () {
        return WorkItemScheduler.d();
    };
    WorkItemScheduler.c = function () {
        return new WorkItemScheduler_SynchronousImpl();
    };
    WorkItemScheduler.d = function () {
        return new WorkItemScheduler_TimeSlicedImpl(1, 0);
    };
    WorkItemScheduler.a = function (a, b) {
        return new WorkItemScheduler_TimeSlicedImpl(a, b);
    };
    WorkItemScheduler.$t = markType(WorkItemScheduler, 'WorkItemScheduler');
    return WorkItemScheduler;
}(Base));
export { WorkItemScheduler };
/**
 * @hidden
 */
var WorkItemScheduler_SynchronousImpl = /** @class */ /*@__PURE__*/ (function (_super) {
    __extends(WorkItemScheduler_SynchronousImpl, _super);
    function WorkItemScheduler_SynchronousImpl() {
        var _this = _super.call(this) || this;
        _this.h = null;
        _this.h = new Queue$1(WorkItem.$);
        return _this;
    }
    WorkItemScheduler_SynchronousImpl.prototype.e = function (a) {
        if (a == null) {
            return;
        }
        this.f(a);
        while (this.h.count != 0) {
            var b = this.h.b();
            if (b != null) {
                b.w(this);
            }
        }
    };
    WorkItemScheduler_SynchronousImpl.prototype.f = function (a) {
        this.h.g(a.i);
    };
    WorkItemScheduler_SynchronousImpl.prototype.g = function () {
    };
    WorkItemScheduler_SynchronousImpl.$t = markType(WorkItemScheduler_SynchronousImpl, 'WorkItemScheduler_SynchronousImpl', WorkItemScheduler.$);
    return WorkItemScheduler_SynchronousImpl;
}(WorkItemScheduler));
export { WorkItemScheduler_SynchronousImpl };
/**
 * @hidden
 */
var WorkItemScheduler_TimeSlicedImpl = /** @class */ /*@__PURE__*/ (function (_super) {
    __extends(WorkItemScheduler_TimeSlicedImpl, _super);
    function WorkItemScheduler_TimeSlicedImpl(a, b) {
        var _this = _super.call(this) || this;
        _this.j = null;
        _this.l = 0;
        _this.n = 0;
        _this.k = new Date();
        _this.h = null;
        _this.i = false;
        _this.m = 0;
        _this.j = new Queue$1(WorkItem.$);
        _this.l = a;
        _this.n = b * (10000);
        _this.k = dateNow();
        return _this;
    }
    WorkItemScheduler_TimeSlicedImpl.prototype.o = function () {
        this.k = dateNow();
        var a = this.h;
        this.h = null;
        this.e(a);
    };
    WorkItemScheduler_TimeSlicedImpl.prototype.e = function (a) {
        if (a == null) {
            return;
        }
        this.j.g(a.i);
        while (this.j.count != 0) {
            var b = this.j.b();
            if (b != null) {
                b.w(this);
            }
        }
    };
    WorkItemScheduler_TimeSlicedImpl.prototype.f = function (a) {
        var _this = this;
        var b;
        if (this.i) {
            b = false;
        }
        else if (a.l == 2) {
            b = true;
        }
        else if (this.n > 0 && timeSpanTicks((+(dateNow()) - +(this.k))) >= this.n) {
            b = true;
        }
        else {
            b = false;
        }
        if (b) {
            this.h = a;
            this.m = (setTimeout(function () { return _this.o(); }, this.l));
        }
        else {
            this.j.g(a.i);
        }
    };
    WorkItemScheduler_TimeSlicedImpl.prototype.g = function () {
        this.i = true;
        if (this.h != null) {
            clearTimeout(this.m);
            this.o();
        }
    };
    WorkItemScheduler_TimeSlicedImpl.$t = markType(WorkItemScheduler_TimeSlicedImpl, 'WorkItemScheduler_TimeSlicedImpl', WorkItemScheduler.$);
    return WorkItemScheduler_TimeSlicedImpl;
}(WorkItemScheduler));
export { WorkItemScheduler_TimeSlicedImpl };
/**
 * @hidden
 */
var WorkItemExtensions = /** @class */ /*@__PURE__*/ (function (_super) {
    __extends(WorkItemExtensions, _super);
    function WorkItemExtensions() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    WorkItemExtensions.o = function (a) {
        if (a != null) {
            WorkItemScheduler.c().e(a);
        }
    };
    WorkItemExtensions.p = function (a) {
        WorkItemScheduler.b().e(WorkItemExtensions.b(a, function (b) { return b.y(); }, 1));
    };
    WorkItemExtensions.n = function (a, b, c) {
        var d = WorkItemScheduler.a(b, c);
        d.e(WorkItemExtensions.b(a, function (e) { return e.y(); }, 1));
        return d;
    };
    WorkItemExtensions.m = function ($tResult, a) {
        if (a == null) {
            return Type.getDefaultValue($tResult);
        }
        if (a.r == false) {
            WorkItemScheduler.c().e(a);
        }
        a.y();
        return a.ad;
    };
    WorkItemExtensions.a = function (a, b, c) {
        if (c === void 0) {
            c = 0;
        }
        return new WorkItem_WorkItemSync(b, c, a, true);
    };
    WorkItemExtensions.b = function (a, b, c) {
        if (c === void 0) {
            c = 0;
        }
        return new WorkItem_WorkItemSync(function () { return b(a || WorkItemExtensions_WorkItemCompleted.ad); }, c, a, false);
    };
    WorkItemExtensions.e = function ($t, a, b, c) {
        if (c === void 0) {
            c = 0;
        }
        return new WorkItem_WorkItemSync(function () { return b(a || WorkItemExtensions_WorkItemCompleted$1.$t.specialize($t).getStaticFields().ae); }, c, a, false);
    };
    WorkItemExtensions.c = function (a, b, c) {
        if (c === void 0) {
            c = 0;
        }
        return new WorkItem_WorkItemAsync(b, c, a, true);
    };
    WorkItemExtensions.d = function (a, b, c) {
        if (c === void 0) {
            c = 0;
        }
        return new WorkItem_WorkItemAsync(function () { return b(a || WorkItemExtensions_WorkItemCompleted.ad); }, c, a, false);
    };
    WorkItemExtensions.f = function ($t, a, b, c) {
        if (c === void 0) {
            c = 0;
        }
        return new WorkItem_WorkItemAsync(function () { return b(a || WorkItemExtensions_WorkItemCompleted$1.$t.specialize($t).getStaticFields().ae); }, c, a, false);
    };
    WorkItemExtensions.g = function ($tResult, a, b, c) {
        if (c === void 0) {
            c = 0;
        }
        return new WorkItem_WorkItemSync$1($tResult, b, c, a, true);
    };
    WorkItemExtensions.h = function ($tResult, a, b, c) {
        if (c === void 0) {
            c = 0;
        }
        return new WorkItem_WorkItemSync$1($tResult, function () { return b(a || WorkItemExtensions_WorkItemCompleted.ad); }, c, a, false);
    };
    WorkItemExtensions.k = function ($t, $tResult, a, b, c) {
        if (c === void 0) {
            c = 0;
        }
        return new WorkItem_WorkItemSync$1($tResult, function () { return b(a || WorkItemExtensions_WorkItemCompleted$1.$t.specialize($t).getStaticFields().ae); }, c, a, false);
    };
    WorkItemExtensions.i = function ($tResult, a, b, c) {
        if (c === void 0) {
            c = 0;
        }
        return new WorkItem_WorkItemAsync$1($tResult, b, c, a, true);
    };
    WorkItemExtensions.j = function ($tResult, a, b, c) {
        if (c === void 0) {
            c = 0;
        }
        return new WorkItem_WorkItemAsync$1($tResult, function () { return b(a || WorkItemExtensions_WorkItemCompleted.ad); }, c, a, false);
    };
    WorkItemExtensions.l = function ($t, $tResult, a, b, c) {
        if (c === void 0) {
            c = 0;
        }
        return new WorkItem_WorkItemAsync$1($tResult, function () { return b(a || WorkItemExtensions_WorkItemCompleted$1.$t.specialize($t).getStaticFields().ae); }, c, a, false);
    };
    WorkItemExtensions.$t = markType(WorkItemExtensions, 'WorkItemExtensions');
    return WorkItemExtensions;
}(Base));
export { WorkItemExtensions };
/**
 * @hidden
 */
var WorkItemExtensions_WorkItemCompleted = /** @class */ /*@__PURE__*/ (function (_super) {
    __extends(WorkItemExtensions_WorkItemCompleted, _super);
    function WorkItemExtensions_WorkItemCompleted() {
        var _this = _super.call(this, 1, null, true) || this;
        _this.r = true;
        return _this;
    }
    WorkItemExtensions_WorkItemCompleted.prototype.p = function (a) {
        return true;
    };
    WorkItemExtensions_WorkItemCompleted.prototype.x = function (a) {
        _super.prototype.x.call(this, a);
    };
    WorkItemExtensions_WorkItemCompleted.$t = markType(WorkItemExtensions_WorkItemCompleted, 'WorkItemExtensions_WorkItemCompleted', WorkItem.$);
    WorkItemExtensions_WorkItemCompleted.ad = new WorkItemExtensions_WorkItemCompleted();
    return WorkItemExtensions_WorkItemCompleted;
}(WorkItem));
export { WorkItemExtensions_WorkItemCompleted };
/**
 * @hidden
 */
var WorkItemExtensions_WorkItemCompleted$1 = /** @class */ /*@__PURE__*/ (function (_super) {
    __extends(WorkItemExtensions_WorkItemCompleted$1, _super);
    function WorkItemExtensions_WorkItemCompleted$1($t) {
        var _this = _super.call(this, $t, 1, null, true) || this;
        _this.$t = null;
        _this.$t = $t;
        _this.$type = _this.$type.specialize(_this.$t);
        _this.r = true;
        return _this;
    }
    WorkItemExtensions_WorkItemCompleted$1.prototype.p = function (a) {
        return true;
    };
    WorkItemExtensions_WorkItemCompleted$1.prototype.x = function (a) {
        _super.prototype.x.call(this, a);
    };
    WorkItemExtensions_WorkItemCompleted$1.$t = markType(WorkItemExtensions_WorkItemCompleted$1, 'WorkItemExtensions_WorkItemCompleted$1', WorkItem$1.$.specialize(0), null, function () {
        this.ae = new WorkItemExtensions_WorkItemCompleted$1(arguments[0]);
    });
    return WorkItemExtensions_WorkItemCompleted$1;
}(WorkItem$1));
export { WorkItemExtensions_WorkItemCompleted$1 };
