/*
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 { 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
 */
var VirtualDataSource = /** @class */ /*@__PURE__*/ (function (_super) {
    __extends(VirtualDataSource, _super);
    function VirtualDataSource() {
        var _this = _super.call(this) || this;
        _this.cx = null;
        _this.dg = false;
        _this.d3 = 0;
        _this.da = null;
        _this.c1 = null;
        _this.c0 = null;
        _this.d2 = 50;
        _this.d1 = 200;
        _this.actualPageSizePopulated = false;
        _this.dm = 50;
        _this.de = false;
        _this.c5 = null;
        _this.cv = null;
        _this.cw = null;
        _this.dd = new LRUCache$2(Base.$, IDataSourcePage_$type, 0, 200);
        _this.dk = new Dictionary$2(Base.$, Array_$type, 0);
        _this.dl = new Dictionary$2(Number_$type, Number_$type, 0);
        _this.df = false;
        _this.ct = null;
        _this.cy = null;
        _this.dc = new SectionMap();
        _this.cu = null;
        _this.dv = -1;
        _this.dn = -1;
        _this.dx = -1;
        _this.dq = -1;
        _this.dy = -1;
        _this.dr = -1;
        _this.dw = -1;
        _this.dp = -1;
        _this.d0 = -1;
        _this.dz = -1;
        _this.dt = -1;
        _this.ds = -1;
        _this.du = -1;
        _this.dj = new Dictionary$2(String_$type, Boolean_$type, 0);
        _this._concurrencyTag = null;
        _this.batchStarted = null;
        _this.batchCompleted = null;
        _this.da = new PagePredictionEngine();
        _this.da.e = _this;
        _this.da.ag = _this.actualPageSize;
        _this.da.f = _this.dc;
        _this.executionContext = new DataSourceExecutionContext();
        _this.da.b = _this.executionContext;
        _this.isReadOnly = true;
        return _this;
    }
    VirtualDataSource.prototype.get_isSectionCollapsable = function () {
        if (!this.isSectionContentVisible) {
            return true;
        }
        return _super.prototype.get_isSectionCollapsable.call(this);
    };
    VirtualDataSource.prototype.set_isSectionCollapsable = function (a) {
        _super.prototype.set_isSectionCollapsable.call(this, a);
    };
    VirtualDataSource.prototype.get_isSectionExpandedDefault = function () {
        if (!this.isSectionContentVisible) {
            return false;
        }
        if (this.isSectionCollapsable) {
            return _super.prototype.get_isSectionExpandedDefault.call(this);
        }
        return true;
    };
    VirtualDataSource.prototype.set_isSectionExpandedDefault = function (a) {
        _super.prototype.set_isSectionExpandedDefault.call(this, a);
    };
    VirtualDataSource.prototype.get_actualBaseDataProvider = function () {
        return this.actualDataProvider;
    };
    Object.defineProperty(VirtualDataSource.prototype, "actualBaseDataProvider", {
        get: function () {
            return this.get_actualBaseDataProvider();
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(VirtualDataSource.prototype, "dataProvider", {
        get: function () {
            return this.c1;
        },
        set: function (a) {
            var b = this.c1;
            this.c1 = a;
            if (b != this.c1) {
                this.onPropertyUpdated("DataProvider", b, this.c1);
            }
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(VirtualDataSource.prototype, "actualDataProvider", {
        get: function () {
            if (this.c0 == null) {
                this.actualDataProvider = this.resolveDataProvider();
            }
            return this.c0;
        },
        set: function (a) {
            var b = this.c0;
            this.c0 = a;
            if (b != this.c0) {
                this.onPropertyUpdated("ActualDataProvider", b, this.c0);
            }
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(VirtualDataSource.prototype, "pageSizeRequested", {
        get: function () {
            return this.d2;
        },
        set: function (a) {
            var b = this.d2;
            this.d2 = a;
            if (b != this.d2) {
                this.onPropertyUpdated("PageSizeRequested", b, this.d2);
            }
        },
        enumerable: false,
        configurable: true
    });
    VirtualDataSource.prototype.resetCache = function () {
        if (this.c6 != null) {
            this.c6.resetCache();
        }
        this.resetCacheOverride();
    };
    VirtualDataSource.prototype.resetCacheOverride = function () {
        this.dd.i();
    };
    Object.defineProperty(VirtualDataSource.prototype, "maxCachedPages", {
        get: function () {
            return this.d1;
        },
        set: function (a) {
            var b = this.d1;
            this.d1 = a;
            if (b != this.d1) {
                this.onPropertyUpdated("MaxCachedPages", b, this.d1);
            }
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(VirtualDataSource.prototype, "actualPageSize", {
        get: function () {
            return this.dm;
        },
        set: function (a) {
            var b = this.dm;
            this.dm = a;
            if (b != this.dm) {
                this.onPropertyUpdated("ActualPageSize", b, this.dm);
            }
        },
        enumerable: false,
        configurable: true
    });
    VirtualDataSource.prototype.get_isSortingSupported = function () {
        if (this.externalDataSource != null && !this.de) {
            this.de = true;
            var a = this.externalDataSource.isSortingSupportedOverride;
            this.de = false;
            return a;
        }
        return false;
    };
    VirtualDataSource.prototype.get_isFilteringSupported = function () {
        if (this.externalDataSource != null && !this.de) {
            this.de = true;
            var a = this.externalDataSource.isFilteringSupportedOverride;
            this.de = false;
            return a;
        }
        return false;
    };
    VirtualDataSource.prototype.get_isGroupingSupported = function () {
        if (this.externalDataSource != null && !this.de) {
            this.de = true;
            var a = this.externalDataSource.isGroupingSupportedOverride;
            this.de = false;
            return a;
        }
        return false;
    };
    Object.defineProperty(VirtualDataSource.prototype, "c6", {
        get: function () {
            return this.c5;
        },
        set: function (a) {
            this.c5 = a;
            this.actualDataProvider = this.resolveDataProvider();
        },
        enumerable: false,
        configurable: true
    });
    VirtualDataSource.prototype.resolveDataProvider = function () {
        if (this.c6 == null) {
            return null;
        }
        return this.c6.resolveDataProvider();
    };
    VirtualDataSource.prototype.propertyUpdatedOverride = function (a, b, c) {
        _super.prototype.propertyUpdatedOverride.call(this, a, b, c);
        switch (a) {
            case "DataProvider":
                this.actualDataProvider = this.dataProvider;
                this.queueAutoRefresh();
                break;
            case "ExecutionContext":
                this.da.b = this.executionContext;
                break;
            case "ActualDataProvider":
                this.actualDataProvider.pageSizeRequested = this.pageSizeRequested;
                this.actualDataProvider.pageLoaded = runOn(this, this.eo);
                this.actualDataProvider.batchCompleted = runOn(this, this.en);
                this.queueAutoRefresh();
                break;
            case "PageSizeRequested":
                this.actualPageSize = this.pageSizeRequested;
                if (this.actualDataProvider != null) {
                    this.actualDataProvider.pageSizeRequested = this.pageSizeRequested;
                }
                this.queueAutoRefresh();
                break;
            case "ActualPageSize":
                this.da.ag = this.actualPageSize;
                this.queueAutoRefresh();
                break;
            case "FirstVisibleIndexRequested":
                this.da.aa = this.firstVisibleIndexRequested;
                break;
            case "LastVisibleIndexRequested":
                this.da.ae = this.lastVisibleIndexRequested;
                break;
            case "ActualCount":
                if (this.actualDataProvider != null && !this.dc.q) {
                    this.da.ab = this.actualDataProvider.actualCount;
                }
                else {
                    this.da.ab = this.actualCount;
                }
                this.queueAutoRefresh();
                break;
            case "MaxCachedPages":
                this.dd.h = this.maxCachedPages;
                break;
        }
    };
    VirtualDataSource.prototype.d7 = function (a) {
        if (this.ao && this.dc.q) {
            var b = this.db(a);
            if (b == null) {
                b = this.dc.d();
            }
            return a - b.t;
        }
        return a;
    };
    VirtualDataSource.prototype.eo = function (a, b, c) {
        var _this = this;
        var d = this.executionContext;
        if (d != null) {
            d.execute(function () { return _this.eu(a, b, c); });
        }
        else {
            this.eu(a, b, c);
        }
    };
    VirtualDataSource.prototype.eu = function (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;
        var d = a.pageIndex();
        this.ep(d);
        this.dd.item(d, a);
        var e = a.schema();
        if (e != null) {
            this.actualSchema = e;
        }
        var f = a.getSummaryInformation();
        if (this.summaryDescriptions.k.count > 0 && this.cy == null && f != null) {
            this.cy = f;
            for (var g = 0; g < this.cy.length; g++) {
                this.cy[g].summaryIndex = this.d5(this.cy[g]);
            }
            var h = SectionData.r(this.cy);
            this.d3 = h.count;
        }
        var i = a.getGroupInformation();
        if (this.groupDescriptions.k.count > 0 && this.ct == null && i != null) {
            this.ct = i;
            this.et();
            this.dg = false;
        }
        b = this.resolveFullCount(b);
        if (this.actualCount != b) {
            this.actualCount = b;
        }
        this.es(a);
    };
    VirtualDataSource.prototype.d5 = function (a) {
        for (var b = 0; b < this.summaryDescriptions.k.count; b++) {
            var c = this.summaryDescriptions.k.item(b);
            if (c.propertyName == a.propertyName && c.operand == a.operand) {
                return b;
            }
        }
        return -1;
    };
    VirtualDataSource.prototype.resolveFullCount = function (a) {
        if (this.ct != null) {
            return this.dc.u + this.d3;
        }
        return a + this.d3;
    };
    VirtualDataSource.prototype.et = function () {
        var a = 0;
        var b = 0;
        var c = this.shouldEmitSectionHeaders;
        var d = this.shouldEmitSectionFooters;
        for (var e = 0; e < this.ct.length; e++) {
            var f = new SectionData();
            var g = b;
            var h = this.ct[e].endIndex - this.ct[e].startIndex;
            f.af = a;
            f.ae = e;
            f.d = new Array(this.groupDescriptions.k.count);
            for (var i = 0; i < this.groupDescriptions.k.count; i++) {
                f.d[i] = 0;
            }
            f.ad = a + h;
            f.t = 0;
            f.e = this.ct[e].groupKeyProperties;
            f.f = this.ct[e].groupKeyValues;
            f.h = new Array(0);
            f.ai(this.sectionHeaderDisplayMode, this.isSectionExpandedDefault);
            this.em(f, e == 0);
            if (d) {
                f.u = f.c.length;
            }
            var j = 0;
            f.i = new Array(this.groupDescriptions.k.count);
            if (this.summaryScope == 0 || this.summaryScope == 2) {
                if (this.ct[e].summaryResults != null) {
                    f.i[this.groupDescriptions.k.count - 1] = this.ct[e].summaryResults;
                    if (f.i != null) {
                        for (var k = 0; k < f.i[this.groupDescriptions.k.count - 1].length; k++) {
                            var l = f.i[this.groupDescriptions.k.count - 1][k];
                            l.summaryIndex = this.d5(l);
                        }
                        j = SectionData.r(f.i[this.groupDescriptions.k.count - 1]).count;
                    }
                }
            }
            if (this.includeSummaryRowsInSection) {
                if (this.isSectionSummaryRowsAtBottom) {
                    f.ag = j;
                }
                else {
                    f.ah = j;
                    g += j;
                }
                b += j;
                f.ad += j;
            }
            if (d) {
                b++;
            }
            var m = 0;
            f.b = new Array(f.c.length);
            if (f.z > -1) {
                var n = this.dc.t._inner[f.z];
                for (var o = 0; o < n.f.length; o++) {
                    m = o;
                    if (!Base.equalsStatic(n.f[o], f.f[o])) {
                        break;
                    }
                }
            }
            for (var p = m; p < f.c.length; p++) {
                b++;
                g++;
                f.ad++;
                var q = this.eg(f, p);
                f.a[p] = q;
                f.b[p] = p - m;
                if (this.dj.containsKey(q)) {
                    f.c[p] = this.dj.item(q);
                }
                else {
                    f.c[p] = this.isSectionExpandedDefault;
                }
                if (!f.c[p]) {
                    var r = 0;
                    var s = f.c.length - 1 - p;
                    f.ad -= r;
                    b -= r;
                    if (d) {
                        b--;
                        f.u--;
                    }
                    p += s;
                }
            }
            f.y = f.v();
            f.t = g;
            this.dc.z(f, h + 1);
            if (!f.p) {
                var t = f.w();
                f.ad -= f.d[t] + j;
                b -= f.d[t] + j;
            }
            a = f.ad + 1;
            this.dc.t.add(f);
            f.q = this.dc.r(f);
        }
        var u = 0;
        for (var v = 0; v < this.dc.t.count; v++) {
            var w = this.dc.t._inner[v];
            if (this.shouldEmitSectionHeaders) {
                if (!w.q) {
                    var x = w.ad - w.af + 1;
                    this.dc.ae(w.ae);
                    this.dc.ab(w, -x, -x);
                    v--;
                    u++;
                    continue;
                }
            }
            if (w.z > -1) {
                w.z -= u;
            }
        }
    };
    VirtualDataSource.prototype.em = function (a, b) {
        if (b) {
            this.cv = new Array(this.groupDescriptions.k.count);
            for (var c = 0; c < this.cv.length; c++) {
                this.cv[c] = a;
            }
        }
        if (this.sectionHeaderDisplayMode == 1) {
            var d = false;
            var e = a.e.length;
            for (var f = 0; f < e; f++) {
                if (d) {
                    this.cv[f] = a;
                    continue;
                }
                var g = false;
                if (this.cv[f].f[f] == null) {
                    g = a.f[f] != null;
                }
                else {
                    g = !Base.equalsStatic(this.cv[f].f[f], a.f[f]);
                }
                if (g) {
                    this.cv[f] = a;
                    if (f - 1 > -1) {
                        var h = f - 1;
                        while (h >= 0 && this.cv[h].o) {
                            h = h - 1;
                        }
                        a.z = this.cv[h].ae;
                    }
                    d = true;
                }
            }
        }
        else {
            var i = false;
            for (var j = 0; j < a.e.length; j++) {
                if (this.cv[0].f[j] == null) {
                    if (this.cv[0].f[j] != a.f[j]) {
                        i = true;
                        break;
                    }
                    continue;
                }
                if (!Base.equalsStatic(this.cv[0].f[j], a.f[j])) {
                    i = true;
                    break;
                }
            }
            if (i) {
                this.cv[0] = a;
            }
        }
    };
    VirtualDataSource.prototype.el = function (a) {
    };
    VirtualDataSource.prototype.es = function (a) {
        if (this.updateNotifier != null) {
            var b = a.pageIndex() * this.actualPageSize;
            if (this.actualDataProvider != null) {
                if (b > this.actualDataProvider.actualCount) {
                    return;
                }
            }
            var c = this.d6(b);
            var d = this.d6(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);
            }
        }
    };
    VirtualDataSource.prototype.d6 = function (a) {
        if (this.ao && this.dc.q) {
            var b = this.dc.g(a);
            if (b == null) {
                b = this.dc.d();
            }
            if (!b.p) {
                return b.af;
            }
            return a + b.t;
        }
        return a;
    };
    VirtualDataSource.prototype.eq = function (a) {
        a();
    };
    VirtualDataSource.prototype.get_isVirtual = function () {
        return true;
    };
    VirtualDataSource.prototype.isPlaceholderItem = function (a) {
        var b = this.cz(a);
        var c = b[0];
        if (!this.dd.d(c)) {
            if (b[1] == -1) {
                return false;
            }
            return true;
        }
        return false;
    };
    VirtualDataSource.prototype.getItemAtIndex = function (a) {
        if (a >= this.actualCount - this.d3) {
            return this.eb(a);
        }
        var b = this.cz(a);
        var c = b[0];
        if (!this.dd.d(c)) {
            if (b[1] == -1) {
                return this.eb(a);
            }
            return null;
        }
        var d = this.dd.item(c);
        var e = b[1];
        if (e == -1) {
            return this.eb(a);
        }
        return d.getItemAtIndex(e);
    };
    VirtualDataSource.prototype.getItemFromKey = function (a) {
        var b = this.indexOfKey(a);
        if (b >= 0) {
            return this.getItemAtIndex(b);
        }
        return null;
    };
    VirtualDataSource.prototype.eb = function (a) {
        if (a >= this.actualCount - this.d3) {
            var b = new DataSourceSpecialRow();
            b.rowType = 4;
            b.level = 0;
            var c = a - (this.actualCount - this.d3);
            var d = SectionData.r(this.cy);
            var e = new Array(d._inner[c].count);
            d._inner[c].values.copyTo(e, 0);
            b.summaryResults = e;
            return b;
        }
        var f = this.db(a);
        if (f != null) {
            var g = f.j(a);
            if (g == 1 || g == 2) {
                var 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 (var 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) {
                var j = new DataSourceSpecialRow();
                j.rowType = 5;
                j.level = f.c.length;
                var k = f.x(a);
                var l = SectionData.r(f.i[f.i.length - 1]);
                var m = new Array(l._inner[k].count);
                l._inner[k].values.copyTo(m, 0);
                j.summaryResults = m;
                return j;
            }
        }
        return null;
    };
    VirtualDataSource.prototype.cz = function (a) {
        if (this.ao && this.dc.q) {
            var b = false;
            var c = this.db(a);
            if (c == null) {
                c = this.dc.d();
                if (this.cy.length > 0 && a > c.ad) {
                    b = true;
                }
            }
            var 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;
                }
            }
            var e = a - c.t;
            var f = truncate(Math.floor(e / this.actualPageSize));
            var g = e % this.actualPageSize;
            if (b) {
                g = -1;
            }
            var h = new Array(2);
            h[0] = f;
            h[1] = g;
            return h;
        }
        else {
            var i = truncate(Math.floor(a / this.actualPageSize));
            var j = a % this.actualPageSize;
            var k = new Array(2);
            k[0] = i;
            k[1] = j;
            return k;
        }
    };
    VirtualDataSource.prototype.db = function (a) {
        return this.dc.e(a);
    };
    VirtualDataSource.prototype.getItemPropertyAtIndex = function (a, b) {
        if (a > (this.actualCount - 1) - this.d3) {
            return this.ec(a, b);
        }
        var c = this.cz(a);
        var d = c[0];
        if (!this.dd.d(d)) {
            if (c[1] == -1) {
                return this.ec(a, b);
            }
            return null;
        }
        var e = this.dd.item(d);
        var f = c[1];
        if (f == -1) {
            return this.ec(a, b);
        }
        if (this.ap()) {
            var g = e.getItemAtIndex(f);
            var h = this.am(g, b);
            if (h) {
                var i = this.a0(g, b);
                return i;
            }
        }
        return e.getItemValueAtIndex(f, b);
    };
    VirtualDataSource.prototype.ec = function (a, b) {
        var c = this.eb(a);
        if (typeCast(DataSourceSpecialRow.$, c) !== null) {
            return c.getValue(b);
        }
        return null;
    };
    VirtualDataSource.prototype.er = function (a) {
        if (!this.dl.containsKey(a)) {
            this.dl.addItem(a, 1);
        }
        else {
            this.dl.item(a, this.dl.item(a) + 1);
        }
    };
    VirtualDataSource.prototype.ep = function (a) {
        if (this.dl.containsKey(a)) {
            this.dl.item(a, this.dl.item(a) - 1);
            if (this.dl.item(a) <= 0) {
                this.dl.removeItem(a);
                if (this.actualDataProvider != null) {
                    this.actualDataProvider.removePageRequest(a);
                }
            }
        }
    };
    VirtualDataSource.prototype.addCandidate = function (a, b) {
        var c = false;
        if (!this.dk.containsKey(a)) {
            var d = new List$1(Number_$type, 0);
            var e = this.transformPage(a);
            for (var f = 0; f < e.length; f++) {
                var g = e[f];
                if (!this.dd.d(g)) {
                    d.add(g);
                    this.er(g);
                    var h = this.dl.item(g);
                    if (h == 1 && this.actualDataProvider != null) {
                        this.actualDataProvider.addPageRequest(g, b);
                    }
                    c = true;
                }
            }
            if (d.count > 0) {
                this.dk.addItem(a, d.toArray());
            }
        }
        return c;
    };
    VirtualDataSource.prototype.removeCandidate = function (a) {
        if (this.dk.containsKey(a)) {
            for (var b = 0; b < this.dk.item(a).length; b++) {
                var c = this.dk.item(a)[b];
                this.ep(c);
            }
            this.dk.removeItem(a);
        }
    };
    VirtualDataSource.prototype.transformPage = function (a) {
        var b = [a];
        if (this.ao && this.dc.q) {
            var c = a * this.actualPageSize;
            var d = c + this.actualPageSize - 1;
            var e = this.db(c);
            var f = new List$1(Number_$type, 0);
            for (var g = c; g <= d; g++) {
                if (e == null) {
                    break;
                }
                if (g > e.ad) {
                    if (e.ae < this.dc.t.count - 1) {
                        e = this.dc.t._inner[e.ae + 1];
                    }
                    else {
                        break;
                    }
                }
                if (!e.p || (g >= e.af && g <= e.af + (e.v() - 1) + e.aa)) {
                    continue;
                }
                var h = g - e.t;
                var i = intDivide(h, this.actualPageSize);
                if (!f.contains(i)) {
                    f.add(i);
                }
            }
            b = f.toArray();
        }
        return b;
    };
    VirtualDataSource.prototype.getUnrealizedCount = function () {
        var a = this.dd.g * this.actualPageSize;
        var b = this.actualCount - a;
        return b;
    };
    VirtualDataSource.prototype.onSetItemOverride = function (a, b, c) {
        _super.prototype.onSetItemOverride.call(this, a, b, c);
        var d = this.cz(a);
        var e = d[0];
        if (this.dd.d(e)) {
            this.dd.j(e);
        }
    };
    VirtualDataSource.prototype.refreshInternalOverride = function () {
        _super.prototype.refreshInternalOverride.call(this);
        this.actualPageSizePopulated = false;
        var a = this.actualCount != 0;
        this.df = true;
        if (!this.dg) {
            this.resetCache();
            if (a) {
                this.onClearItems();
            }
            this.ct = null;
            this.cy = null;
            this.d3 = 0;
            this.dc.v();
        }
        else {
            this.dg = false;
            if (a) {
                this.onClearItems();
            }
        }
        this.ev();
        this.da.au();
        this.df = false;
    };
    VirtualDataSource.prototype.ev = function () {
        var e_1, _a;
        if (this.actualDataProvider != null) {
            try {
                for (var _b = __values(fromEnum(this.dk.values)), _c = _b.next(); !_c.done; _c = _b.next()) {
                    var a = _c.value;
                    for (var c = 0; c < a.length; c++) {
                        var b = a[c];
                        this.actualDataProvider.addPageRequest(b, 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;
                }
            }
        }
    };
    VirtualDataSource.prototype.onClearItemsOverride = function () {
        _super.prototype.onClearItemsOverride.call(this);
        if (!this.df) {
            this.queueAutoRefresh();
        }
    };
    VirtualDataSource.prototype.onInsertItemOverride = function (a, b) {
        _super.prototype.onInsertItemOverride.call(this, a, b);
        this.queueAutoRefresh();
    };
    VirtualDataSource.prototype.onRemoveItemOverride = function (a, b) {
        _super.prototype.onRemoveItemOverride.call(this, a, b);
        this.queueAutoRefresh();
    };
    VirtualDataSource.prototype.get_isItemIndexLookupSupported = function () {
        return true;
    };
    VirtualDataSource.prototype.get_isKeyIndexLookupSupported = function () {
        return true;
    };
    VirtualDataSource.prototype.indexOfItem = function (a) {
        var b = this.dd.b;
        var c = b.o;
        var d = b.q;
        var e = d.count;
        var f = b.p;
        for (var g = 0; g < e; g++) {
            if (f._inner[g]) {
                continue;
            }
            var h = d._inner[g].b;
            var i = h.count();
            for (var j = 0; j < i; j++) {
                if (this.di(h.getItemAtIndex(j), a)) {
                    var k = typeGetValue(c._inner[g]);
                    var l = k * this.actualPageSize;
                    return l + j;
                }
            }
        }
        if (this.actualDataProvider != null && this.actualDataProvider.isItemIndexLookupSupported) {
            return this.actualDataProvider.indexOfItem(a);
        }
        return -1;
    };
    VirtualDataSource.prototype.di = function (a, b) {
        if (a == null && b == null) {
            return true;
        }
        if (a == null || b == null) {
            return false;
        }
        return Base.equalsStatic(a, b);
    };
    VirtualDataSource.prototype.indexOfKey = function (a) {
        var b = this.actualPrimaryKey;
        if (b == null || b.length < 1 || this.actualBaseDataProvider == null) {
            return -1;
        }
        this.ensureComparables(this.actualSchema);
        var c = this.c9();
        if (this.ao && this.shouldEmitSectionHeaders && !this.isSectionContentVisible && this.isSectionHeaderNormalRow) {
            if (this.dc.t.count > 0) {
                for (var d = 0; d < this.dc.t.count; d++) {
                    var e = this.dc.t._inner[d];
                    var f = new DataSourceSpecialRow();
                    for (var g = 0; g < e.e.length; g++) {
                        var h = e.e[g];
                        var i = e.f[g];
                        f.setSectionValue(h, i);
                    }
                    if (c.i(f, a) == 0) {
                        return d;
                    }
                }
            }
        }
        var j = this.dd.b;
        var k = j.o;
        var l = j.q;
        var m = l.count;
        var n = j.p;
        for (var o = 0; o < m; o++) {
            if (n._inner[o]) {
                continue;
            }
            var p = l._inner[o].b;
            var q = p.count();
            for (var r = 0; r < q; r++) {
                if (c.i(p.getItemAtIndex(r), a) == 0) {
                    var s = typeGetValue(k._inner[o]);
                    var t = s * this.actualPageSize;
                    return t + r;
                }
            }
        }
        if (this.actualDataProvider != null && this.actualDataProvider.isKeyIndexLookupSupported) {
            return this.actualDataProvider.indexOfKey(a);
        }
        return -1;
    };
    VirtualDataSource.prototype.c9 = function () {
        var a = new Array(this.actualPrimaryKey.length);
        for (var b = 0; b < a.length; b++) {
            a[b] = 0;
        }
        return new DataSourcePropertiesComparer(this.actualPrimaryKey, a, this.cx, this);
    };
    VirtualDataSource.prototype.getStickyRowPriority = function (a) {
        var 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;
    };
    VirtualDataSource.prototype.unpinRow = function (a) {
        _super.prototype.unpinRow.call(this, a);
    };
    VirtualDataSource.prototype.pinRow = function (a) {
        _super.prototype.pinRow.call(this, a);
    };
    VirtualDataSource.prototype.isRowPinned = function (a) {
        return _super.prototype.isRowPinned.call(this, a);
    };
    VirtualDataSource.prototype.getStickyRowsInRange = function (a, b) {
        if ((this.groupDescriptions == null || this.groupDescriptions.k.count == 0) && (this.summaryDescriptions == null || this.summaryDescriptions.k.count == 0)) {
            return null;
        }
        var c = this.db(a);
        var d = this.db(b);
        if (this.dc.t.count > 0) {
            if (this.dc.t.count == 1) {
                c = this.dc.t._inner[0];
                d = this.dc.t._inner[0];
            }
            else {
                c = this.dc.e(a);
                if (c != null) {
                    while (c.z != -1) {
                        c = this.dc.t._inner[c.z];
                    }
                }
                d = this.dc.e(b);
                if (b >= this.actualCount - this.d3) {
                    d = this.dc.d();
                }
            }
        }
        else if (this.d3 > 0) {
            var e = new List$1(Number_$type, 0);
            for (var f = this.actualCount - this.d3; f < this.actualCount; f++) {
                e.add(f);
            }
            return e.toArray();
        }
        if (c == null || d == null) {
            return null;
        }
        if (c.ae == this.dv && c.aa == this.dx && c.af == this.dy && c.ad == this.dw && c.ah == this.d0 && c.ag == this.dz && d.ae == this.dn && d.aa == this.dq && d.af == this.dr && d.ad == this.dp && d.ah == this.dt && d.ag == this.ds && this.d3 == this.du) {
            return this.cu;
        }
        this.dv = c.ae;
        this.dn = d.ae;
        this.dx = c.aa;
        this.dq = d.aa;
        this.dy = c.af;
        this.dr = d.af;
        this.dw = c.ad;
        this.dp = d.ad;
        this.dt = d.ah;
        this.ds = d.ag;
        this.d0 = c.ah;
        this.dz = c.ag;
        this.du = this.d3;
        var g = new List$1(Number_$type, 0);
        for (var h = c.ae; h <= d.ae; h++) {
            var i = this.dc.t._inner[h];
            var j = i.c.length;
            var k = i.v();
            if (this.shouldEmitSectionHeaders && this.ao) {
                for (var l = 0; l < i.c.length; l++) {
                    if (i.a[l] != null) {
                        var m = i.af + i.b[l];
                        g.add(m);
                    }
                    if (!i.c[l]) {
                        break;
                    }
                }
            }
            if (i.p) {
                for (var n = 0; n < i.ah; n++) {
                    g.add(i.af + n + k);
                }
                if (this.shouldEmitShiftedRows) {
                    for (var 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 (var 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.ao) {
                for (var q = i.c.length - 1; q >= 0; q--) {
                    if (!i.c[q]) {
                        break;
                    }
                    g.add(i.ad - q);
                }
            }
        }
        for (var r = this.actualCount - this.d3; r < this.actualCount; r++) {
            g.add(r);
        }
        this.cu = g.toArray();
        return this.cu;
    };
    VirtualDataSource.prototype.isExclusivelySticky = function (a) {
        var b = this.getRowType(a);
        return b == 1 || b == 2 || b == 4 || (this.isSectionSummaryRowsAtBottom && b == 5);
    };
    VirtualDataSource.prototype.getRowType = function (a) {
        if (a < 0) {
            return 0;
        }
        var b = this.getItemAtIndex(a);
        var item_ = b;
        if ((item_ != null && item_.$$isSpecialRow !== undefined)) {
            var c = b.rowType;
            if (this.isSectionHeaderNormalRow && c == 1) {
                return 0;
            }
            return c;
        }
        return 0;
    };
    VirtualDataSource.prototype.getIsRowExpandedAtIndex = function (a) {
        var b = this.dc.e(a);
        if (b == null) {
            b = this.dc.d();
        }
        for (var 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.prototype.getIsRowExpandedAtIndex.call(this, a);
    };
    VirtualDataSource.prototype.setIsRowExpandedAtIndex = function (a, b) {
        var c = this.dc.e(a);
        if (c == null) {
            c = this.dc.d();
        }
        for (var d = 0; d < c.a.length; d++) {
            if (c.a[d] != null) {
                if (a == c.af + c.b[d]) {
                    var e = false;
                    var f = c.a[d];
                    if (this.dj.containsKey(f) && this.dj.item(f) != b) {
                        var g = this.dj.item(f);
                        if (b == this.isSectionExpandedDefault) {
                            this.dj.removeItem(f);
                        }
                        else {
                            this.dj.item(f, b);
                        }
                        e = true;
                        this.onRowExpansionChanged(a, g, b);
                    }
                    else if (b != this.isSectionExpandedDefault) {
                        this.dj.addItem(f, b);
                        e = true;
                        this.onRowExpansionChanged(a, this.isSectionExpandedDefault, b);
                    }
                    if (e) {
                        this.dc.v();
                        this.et();
                        this.dg = true;
                        if (this.actualCount != this.dc.u) {
                            this.actualCount = this.dc.u + this.d3;
                        }
                        else {
                            this.queueAutoRefresh();
                        }
                    }
                    break;
                }
            }
        }
    };
    VirtualDataSource.prototype.getRowLevel = function (a) {
        if (a < 0) {
            return 0;
        }
        var b = this.getItemAtIndex(a);
        var 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;
    };
    VirtualDataSource.prototype.getRootSummaryRowCount = function () {
        return this.d3;
    };
    VirtualDataSource.prototype.ef = function (a) {
        var b = "";
        for (var c = 0; c < a.e.length; c++) {
            b += a.e[c] + ":" + a.f[c];
        }
        return b;
    };
    VirtualDataSource.prototype.eg = function (a, b) {
        if (this.sectionHeaderDisplayMode == 0) {
            return this.ef(a);
        }
        var c = "";
        for (var d = 0; d < a.e.length; d++) {
            c += a.e[d] + ":" + a.f[d] + ":";
        }
        c += b;
        return c;
    };
    VirtualDataSource.prototype.getRootSummaryResults = function () {
        return this.cy;
    };
    VirtualDataSource.prototype.getSectionSummaryResults = function (a) {
        if (this.dc.t.count > 0 && a >= 0 && a < this.dc.t.count) {
            return this.dc.t._inner[a].i;
        }
        return null;
    };
    VirtualDataSource.prototype.clone = function () {
        return null;
    };
    VirtualDataSource.prototype.cloneProperties = function (a) {
        _super.prototype.cloneProperties.call(this, a);
        var b = typeCast(VirtualDataSource.$, a);
        if (b != null) {
            b.pageSizeRequested = this.pageSizeRequested;
            b.maxCachedPages = this.maxCachedPages;
        }
    };
    Object.defineProperty(VirtualDataSource.prototype, "concurrencyTag", {
        get: function () {
            return this._concurrencyTag;
        },
        set: function (a) {
            this._concurrencyTag = a;
        },
        enumerable: false,
        configurable: true
    });
    VirtualDataSource.prototype.get_isBatchingEnabled = function () {
        return true;
    };
    VirtualDataSource.prototype.set_isBatchingEnabled = function (a) {
        _super.prototype.set_isBatchingEnabled.call(this, a);
    };
    VirtualDataSource.prototype.updatePropertyAtKey = function (a, b, c, d) {
        if (d === void 0) {
            d = false;
        }
        var e = _super.prototype.updatePropertyAtKey.call(this, a, b, c, d);
        if (this.concurrencyTag != null && this.ap()) {
            var f = this.indexOfKey(a);
            if (f > -1) {
                var g = this.getItemAtIndex(f);
                var h = this.actualDataProvider.getItemValue(g, this.concurrencyTag);
                if (h != null) {
                    var i = this.u.g(a);
                    i.version = h;
                }
            }
        }
        return e;
    };
    VirtualDataSource.prototype.removeItemByKey = function (a) {
        _super.prototype.removeItemByKey.call(this, a);
        if (this.concurrencyTag != null && this.ap()) {
            var b = this.indexOfKey(a);
            if (b > -1) {
                var c = this.getItemAtIndex(b);
                var d = this.actualDataProvider.getItemValue(c, this.concurrencyTag);
                if (d != null) {
                    var e = this.u.g(a);
                    e.version = d;
                }
            }
        }
    };
    VirtualDataSource.prototype.cm = function (a, b) {
        if (a == 5) {
            var c = b;
            var d_1 = c.a;
            var e = new List$1(TransactionState.$, 0);
            var _loop_1 = function (f) {
                var e_2, _a, e_3, _b;
                var g = d_1[f].transactionType;
                var h = null;
                var i = null;
                if (g == TransactionType.Delete || g == 1) {
                    var j = new Dictionary$2(String_$type, Base.$, 0);
                    var k = d_1[f].id;
                    for (var l = 0; l < this_1.actualPrimaryKey.length; l++) {
                        j.addItem(this_1.actualPrimaryKey[l], k[l]);
                    }
                    h = j;
                }
                if (g == 0) {
                    var m = new Dictionary$2(String_$type, Base.$, 0);
                    var n = d_1[f].id[0];
                    for (var o = 0; o < this_1.actualSchema.propertyNames.length; o++) {
                        var p = this_1.actualSchema.propertyNames[o];
                        m.addItem(p, this_1.actualDataProvider.getItemValue(n, p));
                    }
                    i = m;
                }
                else if (g == 1) {
                    i = d_1[f].value;
                }
                if (h != null) {
                    var q = {};
                    var r = h;
                    try {
                        for (var _c = (e_2 = void 0, __values(fromEnum(r))), _d = _c.next(); !_d.done; _d = _c.next()) {
                            var s = _d.value;
                            q[s.key] = s.value;
                        }
                    }
                    catch (e_2_1) {
                        e_2 = { error: e_2_1 };
                    }
                    finally {
                        try {
                            if (_d && !_d.done && (_a = _c.return))
                                _a.call(_c);
                        }
                        finally {
                            if (e_2)
                                throw e_2.error;
                        }
                    }
                    h = q;
                }
                if (i != null) {
                    var t = {};
                    var u = i;
                    try {
                        for (var _e = (e_3 = void 0, __values(fromEnum(u))), _f = _e.next(); !_f.done; _f = _e.next()) {
                            var v = _f.value;
                            t[v.key] = v.value;
                        }
                    }
                    catch (e_3_1) {
                        e_3 = { error: e_3_1 };
                    }
                    finally {
                        try {
                            if (_f && !_f.done && (_b = _e.return))
                                _b.call(_e);
                        }
                        finally {
                            if (e_3)
                                throw e_3.error;
                        }
                    }
                    i = t;
                }
                e.add(((function () {
                    var $ret = new TransactionState(h, g, i);
                    $ret.version = d_1[f].version;
                    return $ret;
                })()));
            };
            var this_1 = this;
            for (var f = 0; f < d_1.length; f++) {
                _loop_1(f);
            }
            var w = e.toArray();
            if (this.batchStarted != null) {
                this.batchStarted(this, new DataSourceBatchStartedEventArgs(w));
            }
            this.actualDataProvider.createBatchRequest(w);
        }
    };
    VirtualDataSource.prototype.en = function (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;
}(BaseDataSource));
export { VirtualDataSource };
