/*
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 { Base, runOn, typeCast, markType } from "./type";
import { AsyncDataSourcePageRequest } from "./AsyncDataSourcePageRequest";
import { WeakReference } from "./WeakReference";
import { IDataSourceExecutionContext_$type } from "./IDataSourceExecutionContext";
import { LinkedList$1 } from "./LinkedList$1";
import { List$1 } from "./List$1";
export class AsyncVirtualDataSourceProviderWorker extends Base {
    constructor(a) {
        super();
        this.q = new LinkedList$1(AsyncDataSourcePageRequest.$);
        this.o = new List$1(AsyncDataSourcePageRequest.$, 0);
        this.ab = {};
        this.h = true;
        this.t = 0;
        this.s = 0;
        this.u = 0;
        this.c = null;
        this.r = -1;
        this.ap = null;
        this.ao = null;
        this.g = false;
        this.m = false;
        this.f = false;
        this.h = true;
        this.t = a.c;
        this.s = this.t;
        this.u = a.d;
        this.ap = new WeakReference(a.a);
        this.ao = new WeakReference(a.b);
    }
    get p() {
        return this.o;
    }
    get a() {
        if (this.ap == null) {
            return null;
        }
        return this.ap.c;
    }
    get b() {
        if (this.ao == null) {
            return null;
        }
        return this.ao.c;
    }
    get w() {
        return this.s;
    }
    set w(a) {
        this.s = a;
    }
    get v() {
        return this.r;
    }
    set v(a) {
        this.r = a;
    }
    get d() {
        return this.c;
    }
    set d(a) {
        this.c = a;
    }
    get n() {
        return this.g;
    }
    set n(a) {
        this.g = a;
    }
    get l() {
        return this.h;
    }
    set l(a) {
        this.h = a;
    }
    i(a, b) {
        return this.j(a, b, 200);
    }
    j(a, b, c) {
        if (this.m) {
            return false;
        }
        if (b == 2) {
            this.q.e(new AsyncDataSourcePageRequest(a, c));
        }
        else {
            this.q.f(new AsyncDataSourcePageRequest(a, c));
        }
        return true;
    }
    ak(a) {
        let b = null;
        let c = this.q.c;
        while (c != null) {
            let d = c.c;
            if (d.g == a) {
                b = c;
                break;
            }
            c = c.a;
        }
        if (b != null) {
            this.q.h(b);
        }
    }
    ai() {
        this.q.g();
    }
    an() {
        this.ap = null;
        this.ao = null;
        this.m = true;
        this.h = false;
    }
    af() {
    }
    e() {
        return null;
    }
    ae(a) {
    }
    aj(a, b) {
    }
    ad(a, b) {
    }
    am() {
        for (let a = 0; a < this.o.count; a++) {
            if (!this.o.item(a).b.a.isRunning && !this.o.item(a).d && !this.o.item(a).b.a.isCompleted && !this.o.item(a).b.a.isCancelled) {
                this.o.item(a).b.a.isRunning = true;
                this.o.item(a).b.a.run();
                break;
            }
        }
    }
    x(a) {
        this.am();
        for (let b = 0; b < a.length; b++) {
            if (a[b].d || a[b].b.a.isCompleted || a[b].b.a.isCancelled) {
                return b;
            }
        }
        return -1;
    }
    ac() {
        if (!this.f) {
            this.f = true;
            this.af();
        }
        {
            let a = null;
            let b = null;
            let c = this.o.count;
            if (this.q.c != null && c < 2) {
                let d = this.q.c;
                this.q.i();
                this.ag(d.c, d.c.h);
                c = this.o.count;
            }
            if (c > 0) {
                a = new Array(c);
                for (let e = 0; e < c; e++) {
                    a[e] = this.o.item(e);
                }
                b = this.e();
                this.ae(b);
            }
            if (a != null && a.length > 0) {
                let f = this.x(a);
                if (!this.h) {
                    return;
                }
                if (f >= 0) {
                    let g = a[f];
                    this.ad(b, f);
                    this.o.remove(g);
                    this.aj(b, f);
                    this.ah(g.b, g.h, g.g, b);
                }
            }
            else {
            }
            if (!this.h) {
                return;
            }
            if (this.h) {
                window.setTimeout(runOn(this, this.ac), 100);
            }
        }
    }
    ag(a, b) {
    }
    ah(a, b, c, d) {
    }
    al(a, b) {
        let c = b * 2;
        if (b > 20000) {
            this.ak(a);
        }
        if (this.ao == null) {
            this.an();
            return;
        }
        let d = typeCast(IDataSourceExecutionContext_$type, this.ao.c);
        if (d == null) {
            this.an();
            return;
        }
        d.executeDelayed(() => this.j(a, 0, c), b);
    }
    k(a) {
        if (this.r < 0) {
            return false;
        }
        if ((this.r - (a * this.s)) < this.s) {
            return true;
        }
        return false;
    }
}
AsyncVirtualDataSourceProviderWorker.$t = markType(AsyncVirtualDataSourceProviderWorker, 'AsyncVirtualDataSourceProviderWorker');
//# sourceMappingURL=AsyncVirtualDataSourceProviderWorker.js.map