/*
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 { Base, runOn, Array_$type, Number_$type, typeCast, fromEnum, typeGetValue, String_$type, Boolean_$type, markType } from "./type";
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 { DataSourcePropertiesComparer } from "./DataSourcePropertiesComparer";
import { TransactionState } from "./TransactionState";
import { TransactionType } from "./TransactionType";
import { DataSourceBatchStartedEventArgs } from "./DataSourceBatchStartedEventArgs";
import { DataSourceBatchCompletedEventArgs } from "./DataSourceBatchCompletedEventArgs";
import { truncate, intDivide } from "./number";
/**
 * @hidden
 */
export let VirtualDataSource = /*@__PURE__*/ (() => {
    class VirtualDataSource extends BaseDataSource {
        constructor() {
            super();
            this.dk = false;
            this.d7 = 0;
            this.de = null;
            this.c5 = null;
            this.c4 = null;
            this.d6 = 50;
            this.d5 = 200;
            this.actualPageSizePopulated = false;
            this.dr = 50;
            this.di = false;
            this.c9 = null;
            this.c0 = null;
            this.c1 = null;
            this.dh = new LRUCache$2(Base.$, IDataSourcePage_$type, 0, 200);
            this.dp = new Dictionary$2(Base.$, Array_$type, 0);
            this.dq = new Dictionary$2(Number_$type, Number_$type, 0);
            this.dj = false;
            this.cy = null;
            this.c2 = null;
            this.dg = new SectionMap();
            this.cz = null;
            this.dz = -1;
            this.ds = -1;
            this.d1 = -1;
            this.du = -1;
            this.d2 = -1;
            this.dv = -1;
            this.d0 = -1;
            this.dt = -1;
            this.d4 = -1;
            this.d3 = -1;
            this.dx = -1;
            this.dw = -1;
            this.dy = -1;
            this.dn = new Dictionary$2(String_$type, Boolean_$type, 0);
            this._concurrencyTag = null;
            this.batchStarted = null;
            this.batchCompleted = null;
            this.de = new PagePredictionEngine();
            this.de.e = this;
            this.de.ag = this.actualPageSize;
            this.de.f = this.dg;
            this.executionContext = new DataSourceExecutionContext();
            this.de.b = this.executionContext;
            this.isReadOnly = true;
        }
        get_isSectionCollapsable() {
            if (!this.isSectionContentVisible) {
                return true;
            }
            return super.get_isSectionCollapsable();
        }
        set_isSectionCollapsable(a) {
            super.set_isSectionCollapsable(a);
        }
        get_isSectionExpandedDefault() {
            if (!this.isSectionContentVisible) {
                return false;
            }
            if (this.isSectionCollapsable) {
                return super.get_isSectionExpandedDefault();
            }
            return true;
        }
        set_isSectionExpandedDefault(a) {
            super.set_isSectionExpandedDefault(a);
        }
        get_actualBaseDataProvider() {
            return this.actualDataProvider;
        }
        get actualBaseDataProvider() {
            return this.get_actualBaseDataProvider();
        }
        get dataProvider() {
            return this.c5;
        }
        set dataProvider(a) {
            let b = this.c5;
            this.c5 = a;
            if (b != this.c5) {
                this.onPropertyUpdated("DataProvider", b, this.c5);
            }
        }
        get actualDataProvider() {
            if (this.c4 == null) {
                this.actualDataProvider = this.resolveDataProvider();
            }
            return this.c4;
        }
        set actualDataProvider(a) {
            let b = this.c4;
            this.c4 = a;
            if (b != this.c4) {
                this.onPropertyUpdated("ActualDataProvider", b, this.c4);
            }
        }
        get pageSizeRequested() {
            return this.d6;
        }
        set pageSizeRequested(a) {
            let b = this.d6;
            this.d6 = a;
            if (b != this.d6) {
                this.onPropertyUpdated("PageSizeRequested", b, this.d6);
            }
        }
        resetCache() {
            if (this.da != null) {
                this.da.resetCache();
            }
            this.resetCacheOverride();
        }
        resetCacheOverride() {
            this.dh.i();
        }
        get maxCachedPages() {
            return this.d5;
        }
        set maxCachedPages(a) {
            let b = this.d5;
            this.d5 = a;
            if (b != this.d5) {
                this.onPropertyUpdated("MaxCachedPages", b, this.d5);
            }
        }
        get actualPageSize() {
            return this.dr;
        }
        set actualPageSize(a) {
            let b = this.dr;
            this.dr = a;
            if (b != this.dr) {
                this.onPropertyUpdated("ActualPageSize", b, this.dr);
            }
        }
        get_isSortingSupported() {
            if (this.externalDataSource != null && !this.di) {
                this.di = true;
                let a = this.externalDataSource.isSortingSupportedOverride;
                this.di = false;
                return a;
            }
            return false;
        }
        get_isFilteringSupported() {
            if (this.externalDataSource != null && !this.di) {
                this.di = true;
                let a = this.externalDataSource.isFilteringSupportedOverride;
                this.di = false;
                return a;
            }
            return false;
        }
        get_isGroupingSupported() {
            if (this.externalDataSource != null && !this.di) {
                this.di = true;
                let a = this.externalDataSource.isGroupingSupportedOverride;
                this.di = false;
                return a;
            }
            return false;
        }
        get da() {
            return this.c9;
        }
        set da(a) {
            this.c9 = a;
            this.actualDataProvider = this.resolveDataProvider();
        }
        resolveDataProvider() {
            if (this.da == null) {
                return null;
            }
            return this.da.resolveDataProvider();
        }
        propertyUpdatedOverride(a, b, c) {
            super.propertyUpdatedOverride(a, b, c);
            switch (a) {
                case "DataProvider":
                    this.actualDataProvider = this.dataProvider;
                    this.queueAutoRefresh();
                    break;
                case "ExecutionContext":
                    this.de.b = this.executionContext;
                    break;
                case "ActualDataProvider":
                    this.actualDataProvider.pageSizeRequested = this.pageSizeRequested;
                    this.actualDataProvider.pageLoaded = runOn(this, this.es);
                    this.actualDataProvider.batchCompleted = runOn(this, this.er);
                    this.queueAutoRefresh();
                    break;
                case "PageSizeRequested":
                    this.actualPageSize = this.pageSizeRequested;
                    if (this.actualDataProvider != null) {
                        this.actualDataProvider.pageSizeRequested = this.pageSizeRequested;
                    }
                    this.queueAutoRefresh();
                    break;
                case "ActualPageSize":
                    this.de.ag = this.actualPageSize;
                    this.queueAutoRefresh();
                    break;
                case "FirstVisibleIndexRequested":
                    this.de.aa = this.firstVisibleIndexRequested;
                    break;
                case "LastVisibleIndexRequested":
                    this.de.ae = this.lastVisibleIndexRequested;
                    break;
                case "ActualCount":
                    if (this.actualDataProvider != null && !this.dg.q) {
                        this.de.ab = this.actualDataProvider.actualCount;
                    }
                    else {
                        this.de.ab = this.actualCount;
                    }
                    this.queueAutoRefresh();
                    break;
                case "MaxCachedPages":
                    this.dh.h = this.maxCachedPages;
                    break;
            }
        }
        eb(a) {
            if (this.aq && this.dg.q) {
                let b = this.df(a);
                if (b == null) {
                    b = this.dg.d();
                }
                return a - b.t;
            }
            return a;
        }
        es(a, b, c) {
            let d = this.executionContext;
            if (d != null) {
                d.execute(() => this.ey(a, b, c));
            }
            else {
                this.ey(a, b, c);
            }
        }
        ey(a, b, c) {
            if (a == null) {
                b = this.resolveFullCount(b);
                if (this.actualCount != b) {
                    this.actualCount = b;
                }
                return;
            }
            if (c != this.actualPageSize && !this.actualPageSizePopulated) {
                this.actualPageSize = c;
            }
            this.actualPageSizePopulated = true;
            let d = a.pageIndex();
            this.et(d);
            this.dh.item(d, a);
            let e = a.schema();
            if (e != null) {
                this.actualSchema = e;
            }
            let f = a.getSummaryInformation();
            if (this.summaryDescriptions.k.count > 0 && this.c2 == null && f != null) {
                this.c2 = f;
                for (let g = 0; g < this.c2.length; g++) {
                    this.c2[g].summaryIndex = this.d9(this.c2[g]);
                }
                let h = SectionData.r(this.c2);
                this.d7 = h.count;
            }
            let i = a.getGroupInformation();
            if (this.groupDescriptions.k.count > 0 && this.cy == null && i != null) {
                this.cy = i;
                this.ex();
                this.dk = false;
            }
            b = this.resolveFullCount(b);
            if (this.actualCount != b) {
                this.actualCount = b;
            }
            this.ew(a);
        }
        d9(a) {
            for (let b = 0; b < this.summaryDescriptions.k.count; b++) {
                let c = this.summaryDescriptions.k.item(b);
                if (c.propertyName == a.propertyName && c.operand == a.operand) {
                    return b;
                }
            }
            return -1;
        }
        resolveFullCount(a) {
            if (this.cy != null) {
                return this.dg.u + this.getRootSummaryRowCount();
            }
            return a + this.getRootSummaryRowCount();
        }
        ex() {
            let a = 0;
            let b = 0;
            let c = this.shouldEmitSectionHeaders;
            let d = this.shouldEmitSectionFooters;
            for (let e = 0; e < this.cy.length; e++) {
                let f = new SectionData();
                let g = b;
                let h = this.cy[e].endIndex - this.cy[e].startIndex;
                f.af = a;
                f.ae = e;
                f.d = new Array(this.groupDescriptions.k.count);
                for (let i = 0; i < this.groupDescriptions.k.count; i++) {
                    f.d[i] = 0;
                }
                f.ad = a + h;
                f.t = 0;
                f.e = this.cy[e].groupKeyProperties;
                f.f = this.cy[e].groupKeyValues;
                f.h = new Array(0);
                f.ai(this.sectionHeaderDisplayMode, this.isSectionExpandedDefault);
                this.eq(f, e == 0);
                if (d) {
                    f.u = f.c.length;
                }
                let j = 0;
                f.i = new Array(this.groupDescriptions.k.count);
                if (this.summaryScope == 0 || this.summaryScope == 2) {
                    if (this.cy[e].summaryResults != null) {
                        f.i[this.groupDescriptions.k.count - 1] = this.cy[e].summaryResults;
                        if (f.i != null) {
                            for (let k = 0; k < f.i[this.groupDescriptions.k.count - 1].length; k++) {
                                let l = f.i[this.groupDescriptions.k.count - 1][k];
                                l.summaryIndex = this.d9(l);
                                l.groupKey = f.f;
                            }
                            j = SectionData.r(f.i[this.groupDescriptions.k.count - 1]).count;
                        }
                    }
                }
                if (this.includeSummaryRowsInSection && this.shouldEmitSummaryRows) {
                    if (this.isSectionSummaryRowsAtBottom) {
                        f.ag = j;
                    }
                    else {
                        f.ah = j;
                        g += j;
                    }
                    b += j;
                    f.ad = f.ad + j;
                }
                if (d) {
                    b++;
                }
                if (c) {
                    let m = 0;
                    f.b = new Array(f.c.length);
                    if (f.z > -1) {
                        let n = this.dg.t._inner[f.z];
                        for (let o = 0; o < n.f.length; o++) {
                            m = o;
                            if (!Base.equalsStatic(n.f[o], f.f[o])) {
                                break;
                            }
                        }
                    }
                    for (let p = m; p < f.c.length; p++) {
                        b++;
                        g++;
                        f.ad = f.ad + 1;
                        let q = this.ek(f, p);
                        f.a[p] = q;
                        f.b[p] = p - m;
                        if (this.dn.containsKey(q)) {
                            f.c[p] = this.dn.item(q);
                        }
                        else {
                            f.c[p] = this.isSectionExpandedDefault;
                        }
                        if (!f.c[p]) {
                            let r = 0;
                            let s = f.c.length - 1 - p;
                            f.ad = f.ad - r;
                            b -= r;
                            if (d) {
                                b--;
                                f.u = f.u - 1;
                            }
                            p += s;
                        }
                    }
                }
                f.y = c ? f.v() : 0;
                f.t = g;
                this.dg.z(f, h + 1);
                if (!f.p) {
                    let t = f.w();
                    f.ad = f.ad - (f.d[t] + j);
                    b -= f.d[t] + j;
                }
                a = f.ad + 1;
                this.dg.t.add(f);
                f.q = this.dg.r(f);
            }
            let u = 0;
            for (let v = 0; v < this.dg.t.count; v++) {
                let w = this.dg.t._inner[v];
                if (this.shouldEmitSectionHeaders) {
                    if (!w.q) {
                        let x = w.ad - w.af + 1;
                        this.dg.ae(w.ae);
                        this.dg.ab(w, -x, -x);
                        v--;
                        u++;
                        continue;
                    }
                }
                if (w.z > -1) {
                    w.z -= u;
                }
            }
        }
        eq(a, b) {
            if (b) {
                this.c0 = new Array(this.groupDescriptions.k.count);
                for (let c = 0; c < this.c0.length; c++) {
                    this.c0[c] = a;
                }
            }
            if (this.sectionHeaderDisplayMode == 1) {
                let d = false;
                let e = a.e.length;
                for (let f = 0; f < e; f++) {
                    if (d) {
                        this.c0[f] = a;
                        continue;
                    }
                    let g = false;
                    if (this.c0[f].f[f] == null) {
                        g = a.f[f] != null;
                    }
                    else {
                        g = !Base.equalsStatic(this.c0[f].f[f], a.f[f]);
                    }
                    if (g) {
                        this.c0[f] = a;
                        if (f - 1 > -1) {
                            let h = f - 1;
                            while (h >= 0 && this.c0[h].o) {
                                h = h - 1;
                            }
                            a.z = this.c0[h].ae;
                        }
                        d = true;
                    }
                }
            }
            else {
                let i = false;
                for (let j = 0; j < a.e.length; j++) {
                    if (this.c0[0].f[j] == null) {
                        if (this.c0[0].f[j] != a.f[j]) {
                            i = true;
                            break;
                        }
                        continue;
                    }
                    if (!Base.equalsStatic(this.c0[0].f[j], a.f[j])) {
                        i = true;
                        break;
                    }
                }
                if (i) {
                    this.c0[0] = a;
                }
            }
        }
        ep(a) {
        }
        ew(a) {
            if (this.updateNotifier != null) {
                let b = a.pageIndex() * this.actualPageSize;
                if (this.actualDataProvider != null) {
                    if (b > this.actualDataProvider.actualCount) {
                        return;
                    }
                }
                let c = this.ea(b);
                let d = this.ea(b + a.count() - 1);
                if (c > 0 && this.getRowType(c - 1) == 1) {
                    c--;
                }
                if (this.actualCount == 0 && a.count() == 0) {
                    this.onClearItems();
                }
                else {
                    this.updateNotifier.rangeActualized(c, d);
                }
            }
        }
        ea(a) {
            if (this.aq && this.dg.q) {
                let b = this.dg.g(a);
                if (b == null) {
                    b = this.dg.d();
                }
                if (!b.p) {
                    return b.af;
                }
                return a + b.t;
            }
            return a;
        }
        eu(a) {
            a();
        }
        get_isVirtual() {
            return true;
        }
        isPlaceholderItem(a) {
            let b = this.c3(a);
            let c = b[0];
            if (!this.dh.d(c)) {
                if (b[1] == -1) {
                    return false;
                }
                return true;
            }
            return false;
        }
        getItemAtIndex(a) {
            if (a >= this.actualCount - this.getRootSummaryRowCount()) {
                return this.ef(a);
            }
            let b = this.c3(a);
            let c = b[0];
            if (!this.dh.d(c)) {
                if (b[1] == -1) {
                    return this.ef(a);
                }
                return null;
            }
            let d = this.dh.item(c);
            let e = b[1];
            if (e == -1) {
                return this.ef(a);
            }
            return d.getItemAtIndex(e);
        }
        getItemFromKey(a) {
            let b = this.indexOfKey(a);
            if (b >= 0) {
                return this.getItemAtIndex(b);
            }
            return null;
        }
        ef(a) {
            if (a >= this.actualCount - this.getRootSummaryRowCount()) {
                let b = new DataSourceSpecialRow();
                b.rowType = 4;
                b.level = 0;
                let c = a - (this.actualCount - this.getRootSummaryRowCount());
                let d = SectionData.r(this.c2);
                let e = new Array(d._inner[c].count);
                d._inner[c].values.copyTo(e, 0);
                b.summaryResults = e;
                return b;
            }
            let f = this.df(a);
            if (f != null) {
                let g = f.j(a);
                if (g == 1 || g == 2) {
                    let h = new DataSourceSpecialRow();
                    h.rowType = g;
                    h.summaryResults = f.i[f.i.length - 1];
                    h.level = g == 1 ? f.w() + (a - f.af) : (f.u - 1) - (f.w() + (a - (f.ad - f.u + 1)));
                    if (this.sectionHeaderDisplayMode == 0) {
                        for (let i = 0; i < this.groupDescriptions.k.count; i++) {
                            h.setSectionValue(this.groupDescriptions.k.item(i).propertyName, f.f[i]);
                            h.setValue(this.groupDescriptions.k.item(i).propertyName, f.f[i]);
                        }
                    }
                    else {
                        if (h.level >= 0 && h.level < this.groupDescriptions.k.count) {
                            h.setSectionValue(this.groupDescriptions.k.item(h.level).propertyName, f.f[h.level]);
                            h.setValue(this.groupDescriptions.k.item(h.level).propertyName, f.f[h.level]);
                        }
                    }
                    return h;
                }
                else if (g == 5) {
                    let j = new DataSourceSpecialRow();
                    j.rowType = 5;
                    j.level = f.c.length;
                    let k = f.x(a);
                    let l = SectionData.r(f.i[f.i.length - 1]);
                    let m = new Array(l._inner[k].count);
                    l._inner[k].values.copyTo(m, 0);
                    j.summaryResults = m;
                    return j;
                }
            }
            return null;
        }
        c3(a) {
            if (this.aq && this.dg.q) {
                let b = false;
                let c = this.df(a);
                if (c == null) {
                    c = this.dg.d();
                    if (this.c2.length > 0 && a > c.ad) {
                        b = true;
                    }
                }
                let d = c.af;
                if (this.shouldEmitSectionHeaders && a >= d && a < d + c.v()) {
                    b = true;
                }
                if (a - (d + c.v() - 1) <= c.aa + c.ah) {
                    b = true;
                }
                if (this.shouldEmitSectionFooters) {
                    if (a <= c.ad && a >= c.ad - c.c.length - c.ag) {
                        b = true;
                    }
                }
                else if (c.ag > 0) {
                    if (a > c.ad - c.ag && a <= c.ad) {
                        b = true;
                    }
                }
                let e = a - c.t;
                let f = truncate(Math.floor(e / this.actualPageSize));
                let g = e % this.actualPageSize;
                if (b) {
                    g = -1;
                }
                let h = new Array(2);
                h[0] = f;
                h[1] = g;
                return h;
            }
            else {
                let i = truncate(Math.floor(a / this.actualPageSize));
                let j = a % this.actualPageSize;
                let k = new Array(2);
                k[0] = i;
                k[1] = j;
                return k;
            }
        }
        df(a) {
            return this.dg.e(a);
        }
        getItemPropertyAtIndex(a, b) {
            if (a > (this.actualCount - 1) - this.getRootSummaryRowCount()) {
                return this.eg(a, b);
            }
            let c = this.c3(a);
            let d = c[0];
            if (!this.dh.d(d)) {
                if (c[1] == -1) {
                    return this.eg(a, b);
                }
                return null;
            }
            let e = this.dh.item(d);
            let f = c[1];
            if (f == -1) {
                return this.eg(a, b);
            }
            if (this.ar()) {
                let g = e.getItemAtIndex(f);
                let h = this.ao(g, b);
                if (h) {
                    let i = this.a2(g, b);
                    return i;
                }
            }
            return e.getItemValueAtIndex(f, b);
        }
        eg(a, b) {
            let c = this.ef(a);
            if (typeCast(DataSourceSpecialRow.$, c) !== null) {
                return c.getValue(b);
            }
            return null;
        }
        ev(a) {
            if (!this.dq.containsKey(a)) {
                this.dq.addItem(a, 1);
            }
            else {
                this.dq.item(a, this.dq.item(a) + 1);
            }
        }
        et(a) {
            if (this.dq.containsKey(a)) {
                this.dq.item(a, this.dq.item(a) - 1);
                if (this.dq.item(a) <= 0) {
                    this.dq.removeItem(a);
                    if (this.actualDataProvider != null) {
                        this.actualDataProvider.removePageRequest(a);
                    }
                }
            }
        }
        addCandidate(a, b) {
            let c = false;
            if (!this.dp.containsKey(a)) {
                let d = new List$1(Number_$type, 0);
                let e = this.transformPage(a);
                for (let f = 0; f < e.length; f++) {
                    let g = e[f];
                    if (!this.dh.d(g)) {
                        d.add(g);
                        this.ev(g);
                        let h = this.dq.item(g);
                        if (h == 1 && this.actualDataProvider != null) {
                            this.actualDataProvider.addPageRequest(g, b);
                        }
                        c = true;
                    }
                }
                if (d.count > 0) {
                    this.dp.addItem(a, d.toArray());
                }
            }
            return c;
        }
        removeCandidate(a) {
            if (this.dp.containsKey(a)) {
                for (let b = 0; b < this.dp.item(a).length; b++) {
                    let c = this.dp.item(a)[b];
                    this.et(c);
                }
                this.dp.removeItem(a);
            }
        }
        transformPage(a) {
            let b = [a];
            if (this.aq && this.dg.q) {
                let c = a * this.actualPageSize;
                let d = c + this.actualPageSize - 1;
                let e = this.df(c);
                let f = new List$1(Number_$type, 0);
                for (let g = c; g <= d; g++) {
                    if (e == null) {
                        break;
                    }
                    if (g > e.ad) {
                        if (e.ae < this.dg.t.count - 1) {
                            e = this.dg.t._inner[e.ae + 1];
                        }
                        else {
                            break;
                        }
                    }
                    if (!e.p || (g >= e.af && g <= e.af + (e.v() - 1) + e.aa)) {
                        continue;
                    }
                    let h = g - e.t;
                    let i = intDivide(h, this.actualPageSize);
                    if (!f.contains(i)) {
                        f.add(i);
                    }
                }
                b = f.toArray();
            }
            return b;
        }
        getUnrealizedCount() {
            let a = this.dh.g * this.actualPageSize;
            let b = this.actualCount - a;
            return b;
        }
        onSetItemOverride(a, b, c) {
            super.onSetItemOverride(a, b, c);
            let d = this.c3(a);
            let e = d[0];
            if (this.dh.d(e)) {
                this.dh.j(e);
            }
        }
        refreshInternalOverride() {
            super.refreshInternalOverride();
            this.actualPageSizePopulated = false;
            let a = this.actualCount != 0;
            this.dj = true;
            if (!this.dk) {
                this.resetCache();
                if (a) {
                    this.onClearItems();
                }
                this.cy = null;
                this.c2 = null;
                this.d7 = 0;
                this.dg.v();
            }
            else {
                this.dk = false;
                if (a) {
                    this.onClearItems();
                }
            }
            this.ez();
            this.de.au();
            this.dj = false;
        }
        ez() {
            if (this.actualDataProvider != null) {
                for (let a of fromEnum(this.dp.values)) {
                    for (let c = 0; c < a.length; c++) {
                        let b = a[c];
                        this.actualDataProvider.addPageRequest(b, 1);
                    }
                }
            }
        }
        onClearItemsOverride() {
            super.onClearItemsOverride();
            if (!this.dj) {
                this.queueAutoRefresh();
            }
        }
        onInsertItemOverride(a, b) {
            super.onInsertItemOverride(a, b);
            this.queueAutoRefresh();
        }
        onRemoveItemOverride(a, b) {
            super.onRemoveItemOverride(a, b);
            this.queueAutoRefresh();
        }
        get_isItemIndexLookupSupported() {
            return true;
        }
        get_isKeyIndexLookupSupported() {
            return true;
        }
        indexOfItem(a) {
            let b = this.dh.b;
            let c = b.o;
            let d = b.q;
            let e = d.count;
            let f = b.p;
            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.dm(h.getItemAtIndex(j), a)) {
                        let k = typeGetValue(c._inner[g]);
                        let l = k * this.actualPageSize;
                        return l + j;
                    }
                }
            }
            if (this.actualDataProvider != null && this.actualDataProvider.isItemIndexLookupSupported) {
                return this.actualDataProvider.indexOfItem(a);
            }
            return -1;
        }
        dm(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.actualBaseDataProvider == null) {
                return -1;
            }
            this.ensureComparables(this.actualSchema);
            let c = this.dd();
            if (this.aq && this.shouldEmitSectionHeaders && !this.isSectionContentVisible && this.isSectionHeaderNormalRow) {
                if (this.dg.t.count > 0) {
                    for (let d = 0; d < this.dg.t.count; d++) {
                        let e = this.dg.t._inner[d];
                        let f = new DataSourceSpecialRow();
                        for (let g = 0; g < e.e.length; g++) {
                            let h = e.e[g];
                            let i = e.f[g];
                            f.setSectionValue(h, i);
                        }
                        if (c.i(f, a) == 0) {
                            return d;
                        }
                    }
                }
            }
            let j = this.dh.b;
            let k = j.o;
            let l = j.q;
            let m = l.count;
            let n = j.p;
            for (let o = 0; o < m; o++) {
                if (n._inner[o]) {
                    continue;
                }
                let p = l._inner[o].b;
                let q = p.count();
                for (let r = 0; r < q; r++) {
                    if (c.i(p.getItemAtIndex(r), a) == 0) {
                        let s = typeGetValue(k._inner[o]);
                        let t = s * this.actualPageSize;
                        return t + r;
                    }
                }
            }
            if (this.actualDataProvider != null && this.actualDataProvider.isKeyIndexLookupSupported) {
                return this.actualDataProvider.indexOfKey(a);
            }
            return -1;
        }
        dd() {
            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.d, this);
        }
        getStickyRowPriority(a) {
            let b = this.getRowType(a);
            switch (b) {
                case 1: return 100 - this.getRowLevel(a);
                case 2: return 99 - this.getRowLevel(a);
                case 3:
                case 5: return 2;
                case 4: return 3;
            }
            return 1;
        }
        unpinRow(a) {
            super.unpinRow(a);
        }
        pinRow(a) {
            super.pinRow(a);
        }
        isRowPinned(a) {
            return super.isRowPinned(a);
        }
        getStickyRowsInRange(a, b) {
            if ((this.groupDescriptions == null || this.groupDescriptions.k.count == 0) && (this.summaryDescriptions == null || this.summaryDescriptions.k.count == 0)) {
                return null;
            }
            let c = this.df(a);
            let d = this.df(b);
            if (this.dg.t.count > 0) {
                if (this.dg.t.count == 1) {
                    c = this.dg.t._inner[0];
                    d = this.dg.t._inner[0];
                }
                else {
                    c = this.dg.e(a);
                    if (c != null) {
                        while (c.z != -1) {
                            c = this.dg.t._inner[c.z];
                        }
                    }
                    d = this.dg.e(b);
                    if (b >= this.actualCount - this.getRootSummaryRowCount()) {
                        d = this.dg.d();
                    }
                }
            }
            else if (this.getRootSummaryRowCount() > 0) {
                let e = new List$1(Number_$type, 0);
                for (let f = this.actualCount - this.getRootSummaryRowCount(); f < this.actualCount; f++) {
                    e.add(f);
                }
                return e.toArray();
            }
            if (c == null || d == null) {
                return null;
            }
            if (c.ae == this.dz && c.aa == this.d1 && c.af == this.d2 && c.ad == this.d0 && c.ah == this.d4 && c.ag == this.d3 && d.ae == this.ds && d.aa == this.du && d.af == this.dv && d.ad == this.dt && d.ah == this.dx && d.ag == this.dw && this.getRootSummaryRowCount() == this.dy) {
                return this.cz;
            }
            this.dz = c.ae;
            this.ds = d.ae;
            this.d1 = c.aa;
            this.du = d.aa;
            this.d2 = c.af;
            this.dv = d.af;
            this.d0 = c.ad;
            this.dt = d.ad;
            this.dx = d.ah;
            this.dw = d.ag;
            this.d4 = c.ah;
            this.d3 = c.ag;
            this.dy = this.getRootSummaryRowCount();
            let g = new List$1(Number_$type, 0);
            for (let h = c.ae; h <= d.ae; h++) {
                let i = this.dg.t._inner[h];
                let j = i.c.length;
                let k = i.v();
                if (this.shouldEmitSectionHeaders && this.aq) {
                    for (let l = 0; l < i.c.length; l++) {
                        if (i.a[l] != null) {
                            let m = i.af + i.b[l];
                            g.add(m);
                        }
                        if (!i.c[l]) {
                            break;
                        }
                    }
                }
                if (i.p) {
                    for (let n = 0; n < i.ah; n++) {
                        g.add(i.af + n + k);
                    }
                    if (this.shouldEmitShiftedRows) {
                        for (let o = 0; o < i.aa; o++) {
                            g.add(i.af + o + j);
                        }
                    }
                    if (this.summaryScope == 0 || this.summaryScope == 2) {
                        if (this.includeSummaryRowsInSection && this.isSectionSummaryRowsAtBottom) {
                            for (let p = i.ag - 1; p >= 0; p--) {
                                if (this.shouldEmitSectionFooters) {
                                    g.add(i.ad - k - p);
                                }
                                else {
                                    g.add(i.ad - p);
                                }
                            }
                        }
                    }
                }
                if (this.shouldEmitSectionFooters && this.aq) {
                    for (let q = i.c.length - 1; q >= 0; q--) {
                        if (!i.c[q]) {
                            break;
                        }
                        g.add(i.ad - q);
                    }
                }
            }
            for (let r = this.actualCount - this.getRootSummaryRowCount(); r < this.actualCount; r++) {
                g.add(r);
            }
            this.cz = g.toArray();
            return this.cz;
        }
        isExclusivelySticky(a) {
            let b = this.getRowType(a);
            return b == 1 || b == 2 || b == 4 || (this.isSectionSummaryRowsAtBottom && b == 5);
        }
        getRowType(a) {
            if (a < 0) {
                return 0;
            }
            let b = this.getItemAtIndex(a);
            let item_ = b;
            if ((item_ != null && item_.$$isSpecialRow !== undefined)) {
                let c = b.rowType;
                if (this.isSectionHeaderNormalRow && c == 1) {
                    return 0;
                }
                return c;
            }
            return 0;
        }
        getIsRowExpandedAtIndex(a) {
            let b = this.dg.e(a);
            if (b == null) {
                b = this.dg.d();
            }
            for (let c = 0; c < b.a.length; c++) {
                if (b.a[c] != null) {
                    if (a == b.af + b.b[c]) {
                        return b.c[c];
                    }
                }
            }
            return super.getIsRowExpandedAtIndex(a);
        }
        setIsRowExpandedAtIndex(a, b) {
            let c = this.dg.e(a);
            if (c == null) {
                c = this.dg.d();
            }
            for (let d = 0; d < c.a.length; d++) {
                if (c.a[d] != null) {
                    if (a == c.af + c.b[d]) {
                        let e = false;
                        let f = c.a[d];
                        if (this.dn.containsKey(f) && this.dn.item(f) != b) {
                            let g = this.dn.item(f);
                            if (b == this.isSectionExpandedDefault) {
                                this.dn.removeItem(f);
                            }
                            else {
                                this.dn.item(f, b);
                            }
                            e = true;
                            this.onRowExpansionChanged(a, g, b);
                        }
                        else if (b != this.isSectionExpandedDefault) {
                            this.dn.addItem(f, b);
                            e = true;
                            this.onRowExpansionChanged(a, this.isSectionExpandedDefault, b);
                        }
                        if (e) {
                            this.dg.v();
                            this.ex();
                            this.dk = true;
                            if (this.actualCount != this.dg.u) {
                                this.actualCount = this.dg.u + this.getRootSummaryRowCount();
                            }
                            else {
                                this.queueAutoRefresh();
                            }
                        }
                        break;
                    }
                }
            }
        }
        getRowLevel(a) {
            if (a < 0) {
                return 0;
            }
            let b = this.getItemAtIndex(a);
            let item_ = b;
            if ((item_ != null && item_.$$isSpecialRow !== undefined)) {
                return b.level;
            }
            if (this.isGroupingSupported) {
                if (this.sectionHeaderDisplayMode == 1) {
                    return this.groupDescriptions.k.count;
                }
                else if (this.groupDescriptions.k.count > 0) {
                    return 1;
                }
            }
            return 0;
        }
        getRootSummaryRowCount() {
            return this.shouldEmitSummaryRows ? this.d7 : 0;
        }
        ej(a) {
            let b = "";
            for (let c = 0; c < a.e.length; c++) {
                let d = a.f[c].toString();
                b += a.e[c] + ":" + d;
            }
            return b;
        }
        ek(a, b) {
            if (this.sectionHeaderDisplayMode == 0) {
                return this.ej(a);
            }
            let c = "";
            for (let d = 0; d < a.e.length; d++) {
                let e = a.f[d].toString();
                c += a.e[d] + ":" + e + ":";
            }
            let f = b.toString();
            c += f;
            return c;
        }
        getRootSummaryResults() {
            return this.c2;
        }
        getSectionSummaryResults(a) {
            if (this.dg.t.count > 0 && a >= 0 && a < this.dg.t.count) {
                return this.dg.t._inner[a].i;
            }
            return null;
        }
        clone() {
            return null;
        }
        cloneProperties(a) {
            super.cloneProperties(a);
            let b = typeCast(VirtualDataSource.$, a);
            if (b != null) {
                b.pageSizeRequested = this.pageSizeRequested;
                b.maxCachedPages = this.maxCachedPages;
            }
        }
        get concurrencyTag() {
            return this._concurrencyTag;
        }
        set concurrencyTag(a) {
            this._concurrencyTag = a;
        }
        get_isBatchingEnabled() {
            return true;
        }
        set_isBatchingEnabled(a) {
            super.set_isBatchingEnabled(a);
        }
        updatePropertyAtKey(a, b, c, d = false) {
            let e = super.updatePropertyAtKey(a, b, c, d);
            if (this.concurrencyTag != null && this.ar()) {
                let f = this.indexOfKey(a);
                if (f > -1) {
                    let g = this.getItemAtIndex(f);
                    let h = this.actualDataProvider.getItemValue(g, this.concurrencyTag);
                    if (h != null) {
                        let i = this.v.g(a);
                        i.version = h;
                    }
                }
            }
            return e;
        }
        removeItemByKey(a) {
            super.removeItemByKey(a);
            if (this.concurrencyTag != null && this.ar()) {
                let b = this.indexOfKey(a);
                if (b > -1) {
                    let c = this.getItemAtIndex(b);
                    let d = this.actualDataProvider.getItemValue(c, this.concurrencyTag);
                    if (d != null) {
                        let e = this.v.g(a);
                        e.version = d;
                    }
                }
            }
        }
        cr(a, b) {
            if (a == 5) {
                let c = b;
                let d = c.a;
                let e = new List$1(TransactionState.$, 0);
                for (let f = 0; f < d.length; f++) {
                    let g = d[f].transactionType;
                    let h = null;
                    let i = null;
                    if (g == TransactionType.Delete || g == 1) {
                        let j = new Dictionary$2(String_$type, Base.$, 0);
                        let k = d[f].id;
                        for (let l = 0; l < this.actualPrimaryKey.length; l++) {
                            j.addItem(this.actualPrimaryKey[l], k[l]);
                        }
                        h = j;
                    }
                    if (g == 0) {
                        let m = new Dictionary$2(String_$type, Base.$, 0);
                        let n = d[f].id;
                        let o = n[0];
                        for (let p = 0; p < this.actualSchema.propertyNames.length; p++) {
                            let q = this.actualSchema.propertyNames[p];
                            m.addItem(q, this.actualDataProvider.getItemValue(o, q));
                        }
                        i = m;
                    }
                    else if (g == 1) {
                        i = d[f].value;
                    }
                    if (h != null) {
                        let r = {};
                        let s = h;
                        for (let t of fromEnum(s)) {
                            r[t.key] = t.value;
                        }
                        h = r;
                    }
                    if (i != null) {
                        let u = {};
                        let v = i;
                        for (let w of fromEnum(v)) {
                            u[w.key] = w.value;
                        }
                        i = u;
                    }
                    e.add(((() => {
                        let $ret = new TransactionState(h, g, i);
                        $ret.version = d[f].version;
                        return $ret;
                    })()));
                }
                let x = e.toArray();
                if (this.batchStarted != null) {
                    this.batchStarted(this, new DataSourceBatchStartedEventArgs(x));
                }
                this.actualDataProvider.createBatchRequest(x);
            }
        }
        er(a, b, c) {
            if (b) {
                this.queueAutoRefresh();
            }
            if (this.batchCompleted != null) {
                this.batchCompleted(this, new DataSourceBatchCompletedEventArgs(a, c));
            }
        }
    }
    VirtualDataSource.$t = markType(VirtualDataSource, 'VirtualDataSource', BaseDataSource.$, [IPageCandidatesSink_$type]);
    return VirtualDataSource;
})();
