/*
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 { BaseDataSource } from "./BaseDataSource";
import { IPageCandidatesSink_$type } from "./IPageCandidatesSink";
import { PagePredictionEngine } from "./PagePredictionEngine";
import { LRUCache$2 } from "./LRUCache$2";
import { IDataSourcePage_$type } from "./IDataSourcePage";
import { Dictionary$2 } from "./Dictionary$2";
import { SectionMap } from "./SectionMap";
import { DataSourceExecutionContext } from "./DataSourceExecutionContext";
import { SectionData } from "./SectionData";
import { List$1 } from "./List$1";
import { DataSourceSpecialRow } from "./DataSourceSpecialRow";
import { Base, runOn, Number_$type, typeCast, fromEnum, typeGetValue, markType } from "./type";
import { DataSourcePropertiesComparer } from "./DataSourcePropertiesComparer";
import { truncate } from "./number";
export class VirtualDataSource extends BaseDataSource {
    constructor() {
        super();
        this.bw = null;
        this.b6 = null;
        this.bz = null;
        this.by = null;
        this.cj = 50;
        this.ci = 200;
        this.cc = false;
        this.cf = 50;
        this.ca = false;
        this.b3 = null;
        this.b9 = new LRUCache$2(Base.$, IDataSourcePage_$type, 0, 200);
        this.ce = new Dictionary$2(Base.$, Number_$type, 0);
        this.cb = false;
        this.bu = null;
        this.b8 = new SectionMap();
        this.bv = null;
        this.ch = -1;
        this.cg = -1;
        this.b6 = new PagePredictionEngine();
        this.b6.e = this;
        this.b6.ag = this.ck;
        this.b6.f = this.b8;
        this.executionContext = new DataSourceExecutionContext();
        this.b6.b = this.executionContext;
    }
    get_g() {
        return this.b0;
    }
    get g() {
        return this.get_g();
    }
    get b1() {
        return this.bz;
    }
    set b1(a) {
        let b = this.bz;
        this.bz = a;
        if (b != this.bz) {
            this.bj("DataProvider", b, this.bz);
        }
    }
    get b0() {
        if (this.by == null) {
            this.b0 = this.b2();
        }
        return this.by;
    }
    set b0(a) {
        let b = this.by;
        this.by = a;
        if (b != this.by) {
            this.bj("ActualDataProvider", b, this.by);
        }
    }
    get cp() {
        return this.cj;
    }
    set cp(a) {
        let b = this.cj;
        this.cj = a;
        if (b != this.cj) {
            this.bj("PageSizeRequested", b, this.cj);
        }
    }
    c3() {
        if (this.b4 != null) {
            this.b4.resetCache();
        }
        this.c4();
    }
    c4() {
        this.b9.i();
    }
    get co() {
        return this.ci;
    }
    set co(a) {
        let b = this.ci;
        this.ci = a;
        if (b != this.ci) {
            this.bj("MaxCachedPages", b, this.ci);
        }
    }
    get ck() {
        return this.cf;
    }
    set ck(a) {
        let b = this.cf;
        this.cf = a;
        if (b != this.cf) {
            this.bj("ActualPageSize", b, this.cf);
        }
    }
    get_isSortingSupported() {
        if (this.m != null && !this.ca) {
            this.ca = true;
            let a = this.m.isSortingSupportedOverride;
            this.ca = false;
            return a;
        }
        return false;
    }
    get isSortingSupported() {
        return this.get_isSortingSupported();
    }
    get_isFilteringSupported() {
        if (this.m != null && !this.ca) {
            this.ca = true;
            let a = this.m.isFilteringSupportedOverride;
            this.ca = false;
            return a;
        }
        return false;
    }
    get isFilteringSupported() {
        return this.get_isFilteringSupported();
    }
    get_isGroupingSupported() {
        if (this.m != null && !this.ca) {
            this.ca = true;
            let a = this.m.isGroupingSupportedOverride;
            this.ca = false;
            return a;
        }
        return false;
    }
    get isGroupingSupported() {
        return this.get_isGroupingSupported();
    }
    get b4() {
        return this.b3;
    }
    set b4(a) {
        this.b3 = a;
        this.b0 = this.b2();
    }
    b2() {
        if (this.b4 == null) {
            return null;
        }
        return this.b4.resolveDataProvider();
    }
    bq(a, b, c) {
        super.bq(a, b, c);
        switch (a) {
            case "DataProvider":
                this.b0 = this.b1;
                this.queueAutoRefresh();
                break;
            case "ExecutionContext":
                this.b6.b = this.executionContext;
                break;
            case "ActualDataProvider":
                this.b0.pageSizeRequested = this.cp;
                this.b0.pageLoaded = runOn(this, this.cx);
                this.queueAutoRefresh();
                break;
            case "PageSizeRequested":
                this.ck = this.cp;
                if (this.b0 != null) {
                    this.b0.pageSizeRequested = this.cp;
                }
                this.queueAutoRefresh();
                break;
            case "ActualPageSize":
                this.b6.ag = this.ck;
                this.queueAutoRefresh();
                break;
            case "FirstVisibleIndexRequested":
                this.b6.aa = this.cm(this.firstVisibleIndexRequested);
                break;
            case "LastVisibleIndexRequested":
                this.b6.ae = this.cm(this.lastVisibleIndexRequested);
                break;
            case "ActualCount":
                if (this.b0 != null) {
                    this.b6.ab = this.b0.actualCount;
                }
                else {
                    this.b6.ab = this.actualCount;
                }
                this.queueAutoRefresh();
                break;
            case "MaxCachedPages":
                this.b9.h = this.co;
                break;
        }
    }
    cm(a) {
        if (this.aa && this.b8.l) {
            let b = this.b7(a);
            if (b == null) {
                b = this.b8.c();
            }
            return a - b.c;
        }
        return a;
    }
    cx(a, b, c) {
        let d = this.executionContext;
        if (d != null) {
            d.execute(() => this.c1(a, b, c));
        }
        else {
            this.c1(a, b, c);
        }
    }
    c1(a, b, c) {
        if (a == null) {
            b = this.ag(b);
            if (this.actualCount != b) {
                this.actualCount = b;
            }
            return;
        }
        if (c != this.ck && !this.cc) {
            this.ck = c;
        }
        this.cc = true;
        let d = a.pageIndex();
        this.b6.a1(d);
        if (this.ce.containsKey(d)) {
            this.ce.removeItem(d);
        }
        this.b9.item(d, a);
        let e = a.schema();
        if (e != null) {
            this.actualSchema = e;
        }
        let f = a.getGroupInformation();
        if (this.bu == null && f != null) {
            this.bu = f;
            this.c0();
        }
        b = this.ag(b);
        if (this.actualCount != b) {
            this.actualCount = b;
        }
        this.cz(a);
    }
    ag(a) {
        if (this.bu != null) {
            return this.b8.o;
        }
        return a;
    }
    c0() {
        let a = 0;
        let b = 0;
        let c = 0;
        if (this.shouldEmitSectionHeaders) {
            c++;
        }
        if (this.shouldEmitSectionFooters) {
            c++;
        }
        let d = this.shouldEmitSectionHeaders;
        let e = this.shouldEmitSectionFooters;
        for (let f = 0; f < this.bu.length; f++) {
            let g = new SectionData();
            if (d) {
                b++;
            }
            g.g = a;
            g.f = f;
            g.e = a + (this.bu[f].endIndex - this.bu[f].startIndex);
            g.e += c;
            g.c = b;
            g.a = this.bu[f].groupKeyProperties;
            g.b = this.bu[f].groupKeyValues;
            if (e) {
                b++;
            }
            a = g.e + 1;
            this.b8.n.add(g);
        }
    }
    cz(a) {
        if (this.updateNotifier != null) {
            let b = a.pageIndex() * this.ck;
            if (this.b0 != null) {
                if (b > this.b0.actualCount) {
                    return;
                }
            }
            b = this.cl(b);
            let c = b + a.count() - 1;
            c = this.cl(c);
            if (b > 0 && this.getRowType(b - 1) == 1) {
                b--;
            }
            if (this.actualCount == 0 && a.count() == 0) {
                this.bd();
            }
            else {
                this.updateNotifier.rangeActualized(b, c);
            }
        }
    }
    cl(a) {
        if (this.aa && this.b8.l) {
            let b = this.b8.f(a);
            return a + b.c;
        }
        return a;
    }
    cy(a) {
        a();
    }
    get_isVirtual() {
        return true;
    }
    get isVirtual() {
        return this.get_isVirtual();
    }
    isPlaceholderItem(a) {
        let b = this.bx(a);
        let c = b[0];
        if (!this.b9.d(c)) {
            return true;
        }
        return false;
    }
    getItemAtIndex(a) {
        let b = this.bx(a);
        let c = b[0];
        if (!this.b9.d(c)) {
            if (b[1] == -1) {
                return this.cq(a);
            }
            return null;
        }
        let d = this.b9.item(c);
        let e = b[1];
        if (e == -1) {
            return this.cq(a);
        }
        return d.getItemAtIndex(e);
    }
    cq(a) {
        let b = this.b7(a);
        if (a == b.g) {
            let c = new DataSourceSpecialRow();
            c.a = 1;
            for (let d = 0; d < this.groupDescriptions.j.count; d++) {
                c.g(this.groupDescriptions.j.item(d).g, b.b[d]);
            }
            return c;
        }
        if (a == b.e) {
            let e = new DataSourceSpecialRow();
            e.a = 2;
            for (let f = 0; f < this.groupDescriptions.j.count; f++) {
                e.g(this.groupDescriptions.j.item(f).g, b.b[f]);
            }
            return e;
        }
        return null;
    }
    bx(a) {
        if (this.aa && this.b8.l) {
            let b = this.b7(a);
            let c = b.g;
            let d = false;
            if (a == c && this.shouldEmitSectionHeaders) {
                d = true;
            }
            if (a - c <= b.d) {
                d = true;
            }
            let e = a - b.c;
            let f = truncate(Math.floor(e / this.ck));
            let g = e % this.ck;
            if (d) {
                g = -1;
            }
            let h = new Array(2);
            h[0] = f;
            h[1] = g;
            return h;
        }
        else {
            let i = truncate(Math.floor(a / this.ck));
            let j = a % this.ck;
            let k = new Array(2);
            k[0] = i;
            k[1] = j;
            return k;
        }
    }
    b7(a) {
        return this.b8.d(a);
    }
    getItemPropertyAtIndex(a, b) {
        let c = this.bx(a);
        let d = c[0];
        if (!this.b9.d(d)) {
            if (c[1] == -1) {
                return this.cr(a, b);
            }
            return null;
        }
        let e = this.b9.item(d);
        let f = c[1];
        if (f == -1) {
            return this.cr(a, b);
        }
        return e.getItemValueAtIndex(f, b);
    }
    cr(a, b) {
        let c = this.cq(a);
        if (typeCast(DataSourceSpecialRow.$, c) !== null) {
            return c.e(b);
        }
        return null;
    }
    addCandidate(a, b) {
        if (!this.ce.containsKey(a) && !this.b9.d(a)) {
            this.ce.addItem(a, a);
            if (this.b0 != null) {
                this.b0.addPageRequest(a, b);
            }
            return true;
        }
        return false;
    }
    removeCandidate(a) {
        if (this.ce.containsKey(a)) {
            this.ce.removeItem(a);
            if (this.b0 != null) {
                this.b0.removePageRequest(a);
            }
        }
    }
    cn() {
        let a = this.b9.g * this.ck;
        let b = this.actualCount - a;
        return b;
    }
    bo(a, b, c) {
        super.bo(a, b, c);
        let d = this.bx(a);
        let e = d[0];
        if (this.b9.d(e)) {
            this.b9.j(e);
        }
    }
    bs() {
        super.bs();
        this.cc = false;
        let a = this.actualCount != 0;
        this.c3();
        this.cb = true;
        if (a) {
            this.bd();
        }
        this.bu = null;
        this.b8.p();
        this.c2();
        this.b6.au();
        this.cb = false;
    }
    c2() {
        if (this.b0 != null) {
            for (let a of fromEnum(this.ce.values)) {
                this.b0.addPageRequest(a, 1);
            }
        }
    }
    be() {
        super.be();
        if (!this.cb) {
            this.queueAutoRefresh();
        }
    }
    bi(a, b) {
        super.bi(a, b);
        this.queueAutoRefresh();
    }
    bl(a, b) {
        super.bl(a, b);
        this.queueAutoRefresh();
    }
    get_isItemIndexLookupSupported() {
        return true;
    }
    get isItemIndexLookupSupported() {
        return this.get_isItemIndexLookupSupported();
    }
    get_isKeyIndexLookupSupported() {
        return true;
    }
    get isKeyIndexLookupSupported() {
        return this.get_isKeyIndexLookupSupported();
    }
    indexOfItem(a) {
        let b = this.b9.b;
        let c = b.l;
        let d = b.n;
        let e = d.count;
        let f = b.m;
        for (let g = 0; g < e; g++) {
            if (f._inner[g]) {
                continue;
            }
            let h = d._inner[g].b;
            let i = h.count();
            for (let j = 0; j < i; j++) {
                if (this.cd(h.getItemAtIndex(j), a)) {
                    let k = typeGetValue(c._inner[g]);
                    let l = k * this.ck;
                    return l + j;
                }
            }
        }
        if (this.b0 != null && this.b0.isItemIndexLookupSupported) {
            return this.b0.indexOfItem(a);
        }
        return -1;
    }
    cd(a, b) {
        if (a == null && b == null) {
            return true;
        }
        if (a == null || b == null) {
            return false;
        }
        return Base.equalsStatic(a, b);
    }
    indexOfKey(a) {
        let b = this.actualPrimaryKey;
        if (b == null || b.length < 1 || this.g == null) {
            return -1;
        }
        this.a0(this.actualSchema);
        let c = this.b5();
        let d = this.b9.b;
        let e = d.l;
        let f = d.n;
        let g = f.count;
        let h = d.m;
        for (let i = 0; i < g; i++) {
            if (h._inner[i]) {
                continue;
            }
            let j = f._inner[i].b;
            let k = j.count();
            for (let l = 0; l < k; l++) {
                if (c.i(j.getItemAtIndex(l), a) == 0) {
                    let m = typeGetValue(e._inner[i]);
                    let n = m * this.ck;
                    return n + l;
                }
            }
        }
        if (this.b0 != null && this.b0.isKeyIndexLookupSupported) {
            return this.b0.indexOfKey(a);
        }
        return -1;
    }
    b5() {
        let a = new Array(this.actualPrimaryKey.length);
        for (let b = 0; b < a.length; b++) {
            a[b] = 0;
        }
        return new DataSourcePropertiesComparer(this.actualPrimaryKey, a, this.bw, this);
    }
    getStickyRowPriority(a) {
        if (this.getRowType(a) == 1 || this.getRowType(a) == 2) {
            return 3;
        }
        else if (this.getRowType(a) == 3) {
            return 2;
        }
        return 1;
    }
    unpinRow(a) {
        super.unpinRow(a);
    }
    pinRow(a) {
        super.pinRow(a);
    }
    isRowPinned(a) {
        return super.isRowPinned(a);
    }
    getStickyRows() {
        if (this.groupDescriptions == null || this.groupDescriptions.j.count == 0) {
            return null;
        }
        let a = this.b7(this.firstVisibleIndexRequested);
        let b = this.b7(this.lastVisibleIndexRequested);
        if (a == null || b == null) {
            return null;
        }
        if (a.f == this.ch && b.f == this.cg) {
            return this.bv;
        }
        this.cg = b.f;
        this.ch = a.f;
        let c = new List$1(Number_$type, 0);
        for (let d = a.f; d <= b.f; d++) {
            if (this.shouldEmitSectionHeaders) {
                c.add(this.b8.n._inner[d].g);
            }
            if (this.shouldEmitShiftedRows) {
                for (let e = 0; e < this.b8.n._inner[d].d; e++) {
                    c.add(this.b8.n._inner[d].g + e + 1);
                }
            }
            if (this.shouldEmitSectionFooters) {
                c.add(this.b8.n._inner[d].e);
            }
        }
        this.bv = c.toArray();
        return this.bv;
    }
    isExclusivelySticky(a) {
        return this.getRowType(a) == 1 || this.getRowType(a) == 2;
    }
    getRowType(a) {
        if (a < 0) {
            return 0;
        }
        let b = this.getItemAtIndex(a);
        let item_ = b;
        if ((item_ != null && item_.$$isSpecialRow !== undefined)) {
            return b.a;
        }
        return 0;
    }
}
VirtualDataSource.$t = markType(VirtualDataSource, 'VirtualDataSource', BaseDataSource.$, [IPageCandidatesSink_$type]);
//# sourceMappingURL=VirtualDataSource.js.map