/*
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 * as tslib_1 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 { truncate, intDivide } from "./number";
/**
 * @hidden
 */
var VirtualDataSource = /** @class */ /*@__PURE__*/ (function (_super) {
    tslib_1.__extends(VirtualDataSource, _super);
    function VirtualDataSource() {
        var _this = _super.call(this) || this;
        _this.cg = null;
        _this.cx = false;
        _this.dj = 0;
        _this.cr = null;
        _this.ck = null;
        _this.cj = null;
        _this.di = 50;
        _this.dh = 200;
        _this.actualPageSizePopulated = false;
        _this.c3 = 50;
        _this.cv = false;
        _this.co = null;
        _this.cu = new LRUCache$2(Base.$, IDataSourcePage_$type, 0, 200);
        _this.c1 = new Dictionary$2(Base.$, Array_$type, 0);
        _this.c2 = new Dictionary$2(Number_$type, Number_$type, 0);
        _this.cw = false;
        _this.ce = null;
        _this.ch = null;
        _this.ct = new SectionMap();
        _this.cf = null;
        _this.db = -1;
        _this.c4 = -1;
        _this.dd = -1;
        _this.c6 = -1;
        _this.de = -1;
        _this.c7 = -1;
        _this.dc = -1;
        _this.c5 = -1;
        _this.dg = -1;
        _this.df = -1;
        _this.c9 = -1;
        _this.c8 = -1;
        _this.da = -1;
        _this.c0 = new Dictionary$2(String_$type, Boolean_$type, 0);
        _this.cr = new PagePredictionEngine();
        _this.cr.e = _this;
        _this.cr.ag = _this.actualPageSize;
        _this.cr.f = _this.ct;
        _this.executionContext = new DataSourceExecutionContext();
        _this.cr.b = _this.executionContext;
        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: true,
        configurable: true
    });
    Object.defineProperty(VirtualDataSource.prototype, "dataProvider", {
        get: function () {
            return this.ck;
        },
        set: function (a) {
            var b = this.ck;
            this.ck = a;
            if (b != this.ck) {
                this.onPropertyUpdated("DataProvider", b, this.ck);
            }
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(VirtualDataSource.prototype, "actualDataProvider", {
        get: function () {
            if (this.cj == null) {
                this.actualDataProvider = this.resolveDataProvider();
            }
            return this.cj;
        },
        set: function (a) {
            var b = this.cj;
            this.cj = a;
            if (b != this.cj) {
                this.onPropertyUpdated("ActualDataProvider", b, this.cj);
            }
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(VirtualDataSource.prototype, "pageSizeRequested", {
        get: function () {
            return this.di;
        },
        set: function (a) {
            var b = this.di;
            this.di = a;
            if (b != this.di) {
                this.onPropertyUpdated("PageSizeRequested", b, this.di);
            }
        },
        enumerable: true,
        configurable: true
    });
    VirtualDataSource.prototype.resetCache = function () {
        if (this.cp != null) {
            this.cp.resetCache();
        }
        this.resetCacheOverride();
    };
    VirtualDataSource.prototype.resetCacheOverride = function () {
        this.cu.i();
    };
    Object.defineProperty(VirtualDataSource.prototype, "maxCachedPages", {
        get: function () {
            return this.dh;
        },
        set: function (a) {
            var b = this.dh;
            this.dh = a;
            if (b != this.dh) {
                this.onPropertyUpdated("MaxCachedPages", b, this.dh);
            }
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(VirtualDataSource.prototype, "actualPageSize", {
        get: function () {
            return this.c3;
        },
        set: function (a) {
            var b = this.c3;
            this.c3 = a;
            if (b != this.c3) {
                this.onPropertyUpdated("ActualPageSize", b, this.c3);
            }
        },
        enumerable: true,
        configurable: true
    });
    VirtualDataSource.prototype.get_isSortingSupported = function () {
        if (this.externalDataSource != null && !this.cv) {
            this.cv = true;
            var a = this.externalDataSource.isSortingSupportedOverride;
            this.cv = false;
            return a;
        }
        return false;
    };
    VirtualDataSource.prototype.get_isFilteringSupported = function () {
        if (this.externalDataSource != null && !this.cv) {
            this.cv = true;
            var a = this.externalDataSource.isFilteringSupportedOverride;
            this.cv = false;
            return a;
        }
        return false;
    };
    VirtualDataSource.prototype.get_isGroupingSupported = function () {
        if (this.externalDataSource != null && !this.cv) {
            this.cv = true;
            var a = this.externalDataSource.isGroupingSupportedOverride;
            this.cv = false;
            return a;
        }
        return false;
    };
    Object.defineProperty(VirtualDataSource.prototype, "cp", {
        get: function () {
            return this.co;
        },
        set: function (a) {
            this.co = a;
            this.actualDataProvider = this.resolveDataProvider();
        },
        enumerable: true,
        configurable: true
    });
    VirtualDataSource.prototype.resolveDataProvider = function () {
        if (this.cp == null) {
            return null;
        }
        return this.cp.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.cr.b = this.executionContext;
                break;
            case "ActualDataProvider":
                this.actualDataProvider.pageSizeRequested = this.pageSizeRequested;
                this.actualDataProvider.pageLoaded = runOn(this, this.d0);
                this.queueAutoRefresh();
                break;
            case "PageSizeRequested":
                this.actualPageSize = this.pageSizeRequested;
                if (this.actualDataProvider != null) {
                    this.actualDataProvider.pageSizeRequested = this.pageSizeRequested;
                }
                this.queueAutoRefresh();
                break;
            case "ActualPageSize":
                this.cr.ag = this.actualPageSize;
                this.queueAutoRefresh();
                break;
            case "FirstVisibleIndexRequested":
                this.cr.aa = this.firstVisibleIndexRequested;
                break;
            case "LastVisibleIndexRequested":
                this.cr.ae = this.lastVisibleIndexRequested;
                break;
            case "ActualCount":
                if (this.actualDataProvider != null && !this.ct.q) {
                    this.cr.ab = this.actualDataProvider.actualCount;
                }
                else {
                    this.cr.ab = this.actualCount;
                }
                this.queueAutoRefresh();
                break;
            case "MaxCachedPages":
                this.cu.h = this.maxCachedPages;
                break;
        }
    };
    VirtualDataSource.prototype.dm = function (a) {
        if (this.aj && this.ct.q) {
            var b = this.cs(a);
            if (b == null) {
                b = this.ct.d();
            }
            return a - b.u;
        }
        return a;
    };
    VirtualDataSource.prototype.d0 = function (a, b, c) {
        var _this = this;
        var d = this.executionContext;
        if (d != null) {
            d.execute(function () { return _this.d6(a, b, c); });
        }
        else {
            this.d6(a, b, c);
        }
    };
    VirtualDataSource.prototype.d6 = 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.d1(d);
        this.cu.item(d, a);
        var e = a.schema();
        if (e != null) {
            this.actualSchema = e;
        }
        var f = a.getSummaryInformation();
        if (this.ch == null && f != null) {
            this.ch = f;
            for (var g = 0; g < this.summaryDescriptions.k.count; g++) {
                this.ch[g].summaryIndex = g;
            }
            var h = SectionData.s(this.ch);
            this.dj = h.count;
        }
        var i = a.getGroupInformation();
        if (this.ce == null && i != null) {
            this.ce = i;
            this.d5();
            this.cx = false;
        }
        b = this.resolveFullCount(b);
        if (this.actualCount != b) {
            this.actualCount = b;
        }
        this.d4(a);
    };
    VirtualDataSource.prototype.resolveFullCount = function (a) {
        if (this.ce != null) {
            return this.ct.u + this.dj;
        }
        return a + this.dj;
    };
    VirtualDataSource.prototype.d5 = function () {
        var a = 0;
        var b = 0;
        var c = 0;
        if (this.shouldEmitSectionHeaders) {
            switch (this.sectionHeaderDisplayMode) {
                case 0:
                    c++;
                    break;
                case 1:
                    c += this.groupDescriptions.k.count;
                    break;
            }
        }
        if (this.shouldEmitSectionFooters) {
            c++;
        }
        var d = this.shouldEmitSectionHeaders;
        var e = this.shouldEmitSectionFooters;
        for (var f = 0; f < this.ce.length; f++) {
            var g = new SectionData();
            if (d) {
                switch (this.sectionHeaderDisplayMode) {
                    case 0:
                        b++;
                        break;
                    case 1:
                        b += this.groupDescriptions.k.count;
                        break;
                }
            }
            var h = this.ce[f].endIndex - this.ce[f].startIndex;
            g.af = a;
            g.ae = f;
            g.d = [h + 1];
            g.ad = a + h;
            g.ad += c;
            g.u = b;
            g.e = this.ce[f].groupKeyProperties;
            g.f = this.ce[f].groupKeyValues;
            g.ai(this.sectionHeaderDisplayMode, this.isSectionExpandedDefault);
            if (e) {
                g.v = g.c.length;
            }
            var i = 0;
            g.i = this.ce[f].summaryResults;
            if (g.i != null) {
                for (var j = 0; j < g.i.length; j++) {
                    g.i[j].summaryIndex = j;
                }
                i = SectionData.s(g.i).count;
            }
            if (this.includeSummaryRowsInSection) {
                if (this.isSectionSummaryRowsAtBottom) {
                    g.ag = i;
                }
                else {
                    g.ah = i;
                    g.u += i;
                }
                b += i;
                g.ad += i;
            }
            if (e) {
                b++;
            }
            for (var k = 0; k < g.c.length; k++) {
                var l = this.dw(g, k);
                g.a[k] = l;
                if (this.c0.containsKey(l)) {
                    g.c[k] = this.c0.item(l);
                }
                else {
                    g.c[k] = this.isSectionExpandedDefault;
                }
                if (!g.c[k]) {
                    var m = g.c.length - 1 - k;
                    g.ad -= m;
                    b -= m;
                    if (e) {
                        b--;
                        g.v--;
                    }
                    k += m;
                }
            }
            if (!g.q) {
                g.ad -= g.d[0] + i;
                b -= g.d[0] + i;
            }
            a = g.ad + 1;
            this.ct.t.add(g);
        }
    };
    VirtualDataSource.prototype.d4 = 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.dl(b);
            var d = this.dl(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.dl = function (a) {
        if (this.aj && this.ct.q) {
            var b = this.ct.g(a);
            if (b == null) {
                b = this.ct.d();
            }
            if (!b.q) {
                return b.af;
            }
            return a + b.u;
        }
        return a;
    };
    VirtualDataSource.prototype.d2 = function (a) {
        a();
    };
    VirtualDataSource.prototype.get_isVirtual = function () {
        return true;
    };
    VirtualDataSource.prototype.isPlaceholderItem = function (a) {
        var b = this.ci(a);
        var c = b[0];
        if (!this.cu.d(c)) {
            if (b[1] == -1) {
                return false;
            }
            return true;
        }
        return false;
    };
    VirtualDataSource.prototype.getItemAtIndex = function (a) {
        if (a >= this.actualCount - this.dj) {
            return this.dr(a);
        }
        var b = this.ci(a);
        var c = b[0];
        if (!this.cu.d(c)) {
            if (b[1] == -1) {
                return this.dr(a);
            }
            return null;
        }
        var d = this.cu.item(c);
        var e = b[1];
        if (e == -1) {
            return this.dr(a);
        }
        return d.getItemAtIndex(e);
    };
    VirtualDataSource.prototype.dr = function (a) {
        if (a >= this.actualCount - this.dj) {
            var b = new DataSourceSpecialRow();
            b.rowType = 4;
            b.level = 0;
            var c = a - (this.actualCount - this.dj);
            var d = SectionData.s(this.ch);
            var e = new Array(d._inner[c].count);
            d._inner[c].values.copyTo(e, 0);
            b.summaryResults = e;
            return b;
        }
        var f = this.cs(a);
        if (f != null) {
            var g = f.k(a);
            if (g == 1 || g == 2) {
                var h = new DataSourceSpecialRow();
                h.rowType = g;
                h.summaryResults = f.i;
                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]);
                }
                h.level = g == 1 ? a - f.af : (f.v - 1) - (a - (f.ad - f.v + 1));
                return h;
            }
            else if (g == 5) {
                var j = new DataSourceSpecialRow();
                j.rowType = 5;
                j.level = f.c.length;
                var k = f.y(a);
                var l = SectionData.s(f.i);
                var m = new Array(l._inner[k].count);
                l._inner[k].values.copyTo(m, 0);
                j.summaryResults = m;
                return j;
            }
        }
        return null;
    };
    VirtualDataSource.prototype.ci = function (a) {
        if (this.aj && this.ct.q) {
            var b = false;
            var c = this.cs(a);
            if (c == null) {
                c = this.ct.d();
                if (this.ch.length > 0 && a > c.ad) {
                    b = true;
                }
            }
            var d = c.af;
            if (a >= d && a < d + c.c.length && this.shouldEmitSectionHeaders) {
                b = true;
            }
            if (a - (d + c.c.length - 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.u;
            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.cs = function (a) {
        return this.ct.e(a);
    };
    VirtualDataSource.prototype.getItemPropertyAtIndex = function (a, b) {
        if (a > (this.actualCount - 1) - this.dj) {
            return this.ds(a, b);
        }
        var c = this.ci(a);
        var d = c[0];
        if (!this.cu.d(d)) {
            if (c[1] == -1) {
                return this.ds(a, b);
            }
            return null;
        }
        var e = this.cu.item(d);
        var f = c[1];
        if (f == -1) {
            return this.ds(a, b);
        }
        return e.getItemValueAtIndex(f, b);
    };
    VirtualDataSource.prototype.ds = function (a, b) {
        var c = this.dr(a);
        if (typeCast(DataSourceSpecialRow.$, c) !== null) {
            return c.getValue(b);
        }
        return null;
    };
    VirtualDataSource.prototype.d3 = function (a) {
        if (!this.c2.containsKey(a)) {
            this.c2.addItem(a, 1);
        }
        else {
            this.c2.item(a, this.c2.item(a) + 1);
        }
    };
    VirtualDataSource.prototype.d1 = function (a) {
        if (this.c2.containsKey(a)) {
            this.c2.item(a, this.c2.item(a) - 1);
            if (this.c2.item(a) <= 0) {
                this.c2.removeItem(a);
                if (this.actualDataProvider != null) {
                    this.actualDataProvider.removePageRequest(a);
                }
            }
        }
    };
    VirtualDataSource.prototype.addCandidate = function (a, b) {
        var c = false;
        if (!this.c1.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.cu.d(g)) {
                    d.add(g);
                    this.d3(g);
                    var h = this.c2.item(g);
                    if (h == 1 && this.actualDataProvider != null) {
                        this.actualDataProvider.addPageRequest(g, b);
                    }
                    c = true;
                }
            }
            if (d.count > 0) {
                this.c1.addItem(a, d.toArray());
            }
        }
        return c;
    };
    VirtualDataSource.prototype.removeCandidate = function (a) {
        if (this.c1.containsKey(a)) {
            for (var b = 0; b < this.c1.item(a).length; b++) {
                var c = this.c1.item(a)[b];
                this.d1(c);
            }
            this.c1.removeItem(a);
        }
    };
    VirtualDataSource.prototype.transformPage = function (a) {
        var b = [a];
        if (this.aj && this.ct.q) {
            var c = a * this.actualPageSize;
            var d = c + this.actualPageSize - 1;
            var e = this.cs(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.ct.t.count - 1) {
                        e = this.ct.t._inner[e.ae + 1];
                    }
                    else {
                        break;
                    }
                }
                if (!e.q || (g >= e.af && g <= e.af + (e.c.length - 1) + e.aa)) {
                    continue;
                }
                var h = g - e.u;
                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.cu.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.ci(a);
        var e = d[0];
        if (this.cu.d(e)) {
            this.cu.j(e);
        }
    };
    VirtualDataSource.prototype.refreshInternalOverride = function () {
        _super.prototype.refreshInternalOverride.call(this);
        this.actualPageSizePopulated = false;
        var a = this.actualCount != 0;
        this.cw = true;
        if (!this.cx) {
            this.resetCache();
            if (a) {
                this.onClearItems();
            }
            this.ce = null;
            this.ch = null;
            this.dj = 0;
            this.ct.v();
        }
        else {
            this.cx = false;
            if (a) {
                this.onClearItems();
            }
        }
        this.d7();
        this.cr.au();
        this.cw = false;
    };
    VirtualDataSource.prototype.d7 = function () {
        var e_1, _a;
        if (this.actualDataProvider != null) {
            try {
                for (var _b = tslib_1.__values(fromEnum(this.c1.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.cw) {
            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.cu.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.cz(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.cz = 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.cq();
        if (this.aj && this.shouldEmitSectionHeaders && !this.isSectionContentVisible && this.isSectionHeaderNormalRow) {
            if (this.ct.t.count > 0) {
                for (var d = 0; d < this.ct.t.count; d++) {
                    var e = this.ct.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.cu.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.cq = 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.cg, 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.cs(a);
        var d = this.cs(b);
        if (this.ct.t.count > 0) {
            if (this.ct.t.count == 1) {
                c = this.ct.t._inner[0];
                d = this.ct.t._inner[0];
            }
            else {
                c = this.ct.e(a);
                d = this.ct.e(b);
                if (b >= this.actualCount - this.dj) {
                    d = this.ct.d();
                }
            }
        }
        else if (this.dj > 0) {
            var e = new List$1(Number_$type, 0);
            for (var f = this.actualCount - this.dj; f < this.actualCount; f++) {
                e.add(f);
            }
            return e.toArray();
        }
        if (c == null || d == null) {
            return null;
        }
        if (c.ae == this.db && c.aa == this.dd && c.af == this.de && c.ad == this.dc && c.ah == this.dg && c.ag == this.df && d.ae == this.c4 && d.aa == this.c6 && d.af == this.c7 && d.ad == this.c5 && d.ah == this.c9 && d.ag == this.c8 && this.dj == this.da) {
            return this.cf;
        }
        this.db = c.ae;
        this.c4 = d.ae;
        this.dd = c.aa;
        this.c6 = d.aa;
        this.de = c.af;
        this.c7 = d.af;
        this.dc = c.ad;
        this.c5 = d.ad;
        this.c9 = d.ah;
        this.c8 = d.ag;
        this.dg = c.ah;
        this.df = c.ag;
        this.da = this.dj;
        var g = new List$1(Number_$type, 0);
        for (var h = c.ae; h <= d.ae; h++) {
            var i = this.ct.t._inner[h];
            var j = i.c.length;
            if (this.shouldEmitSectionHeaders && this.aj) {
                for (var k = 0; k < i.c.length; k++) {
                    g.add(i.af + k);
                    if (!i.c[k]) {
                        break;
                    }
                }
            }
            if (i.q) {
                for (var l = 0; l < i.ah; l++) {
                    g.add(i.af + l + j);
                }
                if (this.shouldEmitShiftedRows) {
                    for (var m = 0; m < i.aa; m++) {
                        g.add(i.af + m + j);
                    }
                }
                for (var n = i.ag - 1; n >= 0; n--) {
                    if (this.shouldEmitSectionFooters) {
                        g.add(i.ad - j - n);
                    }
                    else {
                        g.add(i.ad - n);
                    }
                }
            }
            if (this.shouldEmitSectionFooters && this.aj) {
                for (var o = i.c.length - 1; o >= 0; o--) {
                    if (!i.c[o]) {
                        break;
                    }
                    g.add(i.ad - o);
                }
            }
        }
        for (var p = this.actualCount - this.dj; p < this.actualCount; p++) {
            g.add(p);
        }
        this.cf = g.toArray();
        return this.cf;
    };
    VirtualDataSource.prototype.isExclusivelySticky = function (a) {
        return this.getRowType(a) == 1 || this.getRowType(a) == 2;
    };
    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.ct.e(a);
        if (b == null) {
            b = this.ct.d();
        }
        for (var c = 0; c < b.c.length; c++) {
            if (a == b.af + c) {
                return b.c[c];
            }
        }
        return _super.prototype.getIsRowExpandedAtIndex.call(this, a);
    };
    VirtualDataSource.prototype.setIsRowExpandedAtIndex = function (a, b) {
        var c = this.ct.e(a);
        if (c == null) {
            c = this.ct.d();
        }
        for (var d = 0; d < c.c.length; d++) {
            if (a == c.af + d) {
                var e = false;
                var f = c.a[d];
                if (this.c0.containsKey(f) && this.c0.item(f) != b) {
                    var g = this.c0.item(f);
                    if (b == this.isSectionExpandedDefault) {
                        this.c0.removeItem(f);
                    }
                    else {
                        this.c0.item(f, b);
                    }
                    e = true;
                    this.onRowExpansionChanged(a, g, b);
                }
                else if (b != this.isSectionExpandedDefault) {
                    this.c0.addItem(f, b);
                    e = true;
                    this.onRowExpansionChanged(a, this.isSectionExpandedDefault, b);
                }
                if (e) {
                    this.ct.v();
                    this.d5();
                    this.cx = true;
                    if (this.actualCount != this.ct.u) {
                        this.actualCount = this.ct.u + this.dj;
                    }
                    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.dj;
    };
    VirtualDataSource.prototype.dv = function (a) {
        var b = "";
        for (var c = 0; c < a.e.length; c++) {
            b += a.e[c] + ":" + a.f[c];
        }
        return b;
    };
    VirtualDataSource.prototype.dw = function (a, b) {
        if (this.sectionHeaderDisplayMode == 0) {
            return this.dv(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.ch;
    };
    VirtualDataSource.prototype.getSectionSummaryResults = function (a) {
        if (this.ct.q && a >= 0 && a < this.ct.t.count) {
            if (this.ct.t._inner[a].i != null) {
                var b = new Array(1);
                b[0] = this.ct.t._inner[a].i;
                return b;
            }
        }
        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;
        }
    };
    VirtualDataSource.$t = markType(VirtualDataSource, 'VirtualDataSource', BaseDataSource.$, [IPageCandidatesSink_$type]);
    return VirtualDataSource;
}(BaseDataSource));
export { VirtualDataSource };
//# sourceMappingURL=VirtualDataSource.js.map
