/*
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 } from "tslib";
import { BaseDataSource } from "./BaseDataSource";
import { Base, typeCast, Array_$type, Number_$type, markType } from "./type";
import { RearrangedListView } from "./RearrangedListView";
import { DataSourcePropertiesComparer } from "./DataSourcePropertiesComparer";
import { List$1 } from "./List$1";
import { DataSourceExecutionContext } from "./DataSourceExecutionContext";
import { DataSourceLocalDataProvider } from "./DataSourceLocalDataProvider";
import { DataSourceSpecialRow } from "./DataSourceSpecialRow";
import { ISupportsDataChangeNotifications_$type } from "./ISupportsDataChangeNotifications";
/**
 * @hidden
 */
var LocalDataSource = /** @class */ (function (_super) {
    __extends(LocalDataSource, _super);
    function LocalDataSource() {
        var _this = _super.call(this) || this;
        _this.b6 = null;
        _this.b5 = null;
        _this.c0 = null;
        _this.cb = null;
        _this.cf = null;
        _this.ch = null;
        _this.cd = null;
        _this.cr = new List$1(Array_$type, 0);
        _this.ci = false;
        _this.b4 = null;
        _this.cw = -1;
        _this.cs = -1;
        _this.cy = -1;
        _this.cu = -1;
        _this.cz = -1;
        _this.cv = -1;
        _this.cx = -1;
        _this.ct = -1;
        _this.executionContext = new DataSourceExecutionContext();
        return _this;
    }
    LocalDataSource.prototype.get_actualBaseDataProvider = function () {
        return this.actualDataProvider;
    };
    Object.defineProperty(LocalDataSource.prototype, "actualBaseDataProvider", {
        get: function () {
            return this.get_actualBaseDataProvider();
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(LocalDataSource.prototype, "dataProvider", {
        get: function () {
            return this.b6;
        },
        set: function (a) {
            var b = this.b6;
            this.b6 = a;
            if (b != this.b6) {
                this.onPropertyUpdated("DataProvider", b, this.b6);
            }
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(LocalDataSource.prototype, "actualDataProvider", {
        get: function () {
            if (this.b5 == null) {
                this.actualDataProvider = this.resolveDataProvider();
            }
            return this.b5;
        },
        set: function (a) {
            var b = this.b5;
            this.b5 = a;
            if (b != this.b5) {
                this.onPropertyUpdated("ActualDataProvider", b, this.b5);
            }
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(LocalDataSource.prototype, "itemsSource", {
        get: function () {
            return this.c0;
        },
        set: function (a) {
            var b = this.c0;
            this.c0 = a;
            if (b != this.c0) {
                this.onPropertyUpdated("ItemsSource", b, this.c0);
            }
        },
        enumerable: true,
        configurable: true
    });
    LocalDataSource.prototype.canInitialize = function () {
        var a = _super.prototype.canInitialize.call(this);
        if (!a) {
            return false;
        }
        return this.itemsSource != null;
    };
    Object.defineProperty(LocalDataSource.prototype, "cc", {
        get: function () {
            return this.cb;
        },
        set: function (a) {
            this.cb = a;
            this.actualDataProvider = this.resolveDataProvider();
        },
        enumerable: true,
        configurable: true
    });
    LocalDataSource.prototype.resolveDataProvider = function () {
        var provider_ = new DataSourceLocalDataProvider();
        provider_.executionContext = new DataSourceExecutionContext();
        return provider_;
    };
    LocalDataSource.prototype.propertyUpdatedOverride = function (a, b, c) {
        _super.prototype.propertyUpdatedOverride.call(this, a, b, c);
        switch (a) {
            case "DataProvider":
                this.actualDataProvider = this.dataProvider;
                break;
            case "ActualDataProvider":
                if (this.actualDataProvider != null) {
                    this.actualDataProvider.dataSource = this.itemsSource;
                    this.actualSchema = this.actualDataProvider.actualSchema;
                    this.actualCount = this.resolveFullCount(this.actualDataProvider.actualCount);
                }
                this.queueAutoRefresh();
                break;
            case "ItemsSource":
                if (this.actualDataProvider != null) {
                    this.actualDataProvider.dataSource = this.itemsSource;
                    this.actualSchema = this.actualDataProvider.actualSchema;
                    this.actualCount = this.resolveFullCount(this.actualDataProvider.actualCount);
                }
                this.queueAutoRefresh();
                break;
        }
    };
    LocalDataSource.prototype.getItemAtIndex = function (a) {
        if (this.cf != null && this.cf.ap != null) {
            return this.cf.ap.item(a);
        }
        else {
            return this.actualDataProvider.getItemAtIndex(a);
        }
    };
    LocalDataSource.prototype.getItemPropertyAtIndex = function (a, b) {
        var c;
        if (this.cf != null) {
            c = this.cf.ap.item(a);
        }
        else {
            c = this.actualDataProvider.getItemAtIndex(a);
        }
        var item_ = c;
        if ((item_ != null && item_.$$isSpecialRow !== undefined)) {
            return this.getSpecialRowItemValue(c, b);
        }
        return this.actualDataProvider.getItemValue(c, b);
    };
    LocalDataSource.prototype.ca = function () {
        return this.actualDataProvider;
    };
    LocalDataSource.prototype.ck = function () {
        return this.cr != null && this.cr.count > 0;
    };
    LocalDataSource.prototype.refreshInternalOverride = function () {
        _super.prototype.refreshInternalOverride.call(this);
        if (this.actualDataProvider != null) {
            if (this.cl() || this.cm() || this.cn() || this.co() || this.ck()) {
                if (this.cf == null) {
                    if (this.ch == null) {
                        this.ch = new LocalDataSourceViewUpdateNotifier(this);
                    }
                    this.cf = new RearrangedListView(this.actualDataProvider);
                    this.cf.r = this.ch;
                }
                this.ensureComparables(this.actualDataProvider.actualSchema);
                this.cf.ae = this.shouldEmitSectionHeaders && this.af;
                this.cf.ad = this.shouldEmitSectionFooters && this.af;
                this.cf.af = this.shouldEmitShiftedRows;
                this.cf.bi(this.cr);
                this.cf.al = this.cq();
                this.cf.ak = this.cp();
                this.cf.j = this.ce();
                this.cf.s = this.groupDescriptions;
                this.cf.t = this.sortDescriptions;
                this.cf.u = this.summaryDescriptions;
                this.cf.f = this.summaryScope;
                this.cf.g = this;
                this.cf.aa = this.groupDescriptions != null && this.groupDescriptions.k.count > 0;
                this.cf.l = this.cg();
                this.cf.be();
                this.actualSchema = this.actualDataProvider.actualSchema;
                this.actualCount = this.cf.ap.count;
            }
            else {
                var a = this.ca();
                this.actualSchema = this.actualDataProvider.actualSchema;
                if (a != null) {
                    this.actualCount = a.actualCount;
                }
                else {
                    this.actualCount = 0;
                }
                if (this.cf != null) {
                    this.cf.r = null;
                    this.cf = null;
                    this.onBroadcastClearItems();
                }
            }
        }
        else {
            this.actualCount = 0;
            this.actualSchema = null;
        }
    };
    LocalDataSource.prototype.get_isItemIndexLookupSupported = function () {
        return true;
    };
    LocalDataSource.prototype.get_isKeyIndexLookupSupported = function () {
        return true;
    };
    LocalDataSource.prototype.indexOfItem = function (a) {
        if (this.cf != null) {
            return this.cf.ap.indexOf(a);
        }
        if (this.actualDataProvider != null && this.actualDataProvider.isItemIndexLookupSupported) {
            return this.actualDataProvider.indexOfItem(a);
        }
        if (this.actualDataProvider != null) {
            var b = this.actualDataProvider.actualCount;
            var c = void 0;
            for (var d = 0; d < b; d++) {
                c = this.actualDataProvider.getItemAtIndex(d);
                if (this.cj(c, a)) {
                    return d;
                }
            }
        }
        return -1;
    };
    LocalDataSource.prototype.cj = function (a, b) {
        if (a == null && b == null) {
            return true;
        }
        if (a == null || b == null) {
            return false;
        }
        return Base.equalsStatic(a, b);
    };
    LocalDataSource.prototype.indexOfKey = function (a) {
        var b = this.actualPrimaryKey;
        if (b == null || b.length < 1 || this.actualBaseDataProvider == null) {
            return this.indexOfItem(a[0]);
        }
        var c = this.ce();
        var d;
        if (this.cf != null) {
            var e = this.cf.ap.count;
            for (var f = 0; f < e; f++) {
                d = this.cf.ap.item(f);
                if (!(typeCast(DataSourceSpecialRow.$, d) !== null) && c.i(d, a) == 0) {
                    return f;
                }
            }
            return -1;
        }
        if (this.actualDataProvider != null && this.actualDataProvider.isKeyIndexLookupSupported) {
            return this.actualDataProvider.indexOfKey(a);
        }
        if (this.actualDataProvider != null) {
            var g = this.actualDataProvider.actualCount;
            for (var h = 0; h < g; h++) {
                d = this.actualDataProvider.getItemAtIndex(h);
                if (c.i(d, a) == 0) {
                    return h;
                }
            }
        }
        return -1;
    };
    LocalDataSource.prototype.onActualPrimaryKeyChangedOverrride = function (a, b) {
        _super.prototype.onActualPrimaryKeyChangedOverrride.call(this, a, b);
        this.cd = null;
    };
    LocalDataSource.prototype.ce = function () {
        if (this.cd != null) {
            return this.cd;
        }
        if (this.actualPrimaryKey == null) {
            this.cd = new DataSourcePropertiesComparer(null, null, null, this);
            return this.cd;
        }
        var a = new Array(this.actualPrimaryKey.length);
        for (var b = 0; b < a.length; b++) {
            a[b] = 0;
        }
        this.cd = new DataSourcePropertiesComparer(this.actualPrimaryKey, a, this.d, this);
        return this.cd;
    };
    LocalDataSource.prototype.cg = function () {
        if (this.filterExpressions.j.count == 0) {
            return null;
        }
        var a = this.actualDataProvider;
        var b = this.actualDataProvider.actualSchema;
        var c = this.filterExpressions.j;
        var d = function (e) {
            for (var f = 0; f < c.count; f++) {
                if (!c.item(f).match(e, a, b)) {
                    return false;
                }
            }
            return true;
        };
        return d;
    };
    LocalDataSource.prototype.cq = function () {
        var a = 0;
        if (this.sortDescriptions.k.count == 0 && (this.groupDescriptions == null || this.groupDescriptions.k.count == 0)) {
            return null;
        }
        a = this.sortDescriptions.k.count;
        if (this.groupDescriptions != null) {
            a += this.groupDescriptions.k.count;
        }
        var b = new Array(a);
        var c = new Array(a);
        var d = 0;
        if (this.groupDescriptions != null) {
            for (var e = 0; e < this.groupDescriptions.k.count; e++) {
                b[e] = this.groupDescriptions.k.item(e).propertyName;
                c[e] = this.groupDescriptions.k.item(e).direction;
                d++;
            }
        }
        for (var f = 0; f < this.sortDescriptions.k.count; f++) {
            b[f + d] = this.sortDescriptions.k.item(f).propertyName;
            c[f + d] = this.sortDescriptions.k.item(f).direction;
        }
        return new DataSourcePropertiesComparer(b, c, this.d, this);
    };
    LocalDataSource.prototype.cp = function () {
        if (this.groupDescriptions == null || this.groupDescriptions.k.count < 1) {
            return null;
        }
        var a = 0;
        a = this.groupDescriptions.k.count;
        var b = new Array(a);
        var c = new Array(a);
        if (this.groupDescriptions != null) {
            for (var d = 0; d < this.groupDescriptions.k.count; d++) {
                b[d] = this.groupDescriptions.k.item(d).propertyName;
                c[d] = this.groupDescriptions.k.item(d).direction;
            }
        }
        return new DataSourcePropertiesComparer(b, c, this.d, this);
    };
    LocalDataSource.prototype.cm = function () {
        if (this.groupDescriptions == null || this.groupDescriptions.k.count < 1) {
            return false;
        }
        if (this.actualDataProvider == null) {
            return false;
        }
        if (this.actualDataProvider.isGroupingSupported) {
            return false;
        }
        return true;
    };
    LocalDataSource.prototype.cn = function () {
        if (this.sortDescriptions == null || this.sortDescriptions.k.count < 1) {
            return false;
        }
        if (this.actualDataProvider == null) {
            return false;
        }
        if (this.actualDataProvider.isSortingSupported) {
            return false;
        }
        return true;
    };
    LocalDataSource.prototype.cl = function () {
        return this.filterExpressions != null && this.filterExpressions.j.count > 0;
    };
    LocalDataSource.prototype.co = function () {
        if (this.summaryDescriptions == null || this.summaryDescriptions.k.count < 1) {
            return false;
        }
        return true;
    };
    LocalDataSource.prototype.onClearItemsOverride = function () {
        if (this.cf != null) {
            this.cf.notifyClearItems();
            this.queueAutoRefresh();
        }
        else {
            _super.prototype.onClearItemsOverride.call(this);
        }
    };
    LocalDataSource.prototype.onSetItemOverride = function (a, b, c) {
        if (this.cf != null) {
            this.cf.notifySetItem(a, b, c);
        }
        else {
            _super.prototype.onSetItemOverride.call(this, a, b, c);
        }
    };
    LocalDataSource.prototype.onInsertItemOverride = function (a, b) {
        if (this.cf != null) {
            this.cf.notifyInsertItem(a, b);
        }
        else {
            _super.prototype.onInsertItemOverride.call(this, a, b);
        }
    };
    LocalDataSource.prototype.onRemoveItemOverride = function (a, b) {
        if (this.cf != null) {
            this.cf.notifyRemoveItem(a, b);
        }
        else {
            _super.prototype.onRemoveItemOverride.call(this, a, b);
        }
    };
    LocalDataSource.prototype.c5 = function () {
        this.actualCount = this.cf.ap.count;
        this.onBroadcastClearItems();
    };
    LocalDataSource.prototype.c8 = function (a, b, c) {
        this.actualCount = this.cf.ap.count;
        this.onBroadcastSetItem(a, b, c);
    };
    LocalDataSource.prototype.c6 = function (a, b) {
        this.actualCount = this.cf.ap.count;
        this.onBroadcastInsertItem(a, b);
    };
    LocalDataSource.prototype.c7 = function (a, b) {
        this.actualCount = this.cf.ap.count;
        this.onBroadcastRemoveItem(a, b);
    };
    LocalDataSource.prototype.pinRow = function (a) {
        _super.prototype.pinRow.call(this, a);
        this.cw = -1;
        this.cs = -1;
        if (this.cf != null) {
            this.cr.add(a);
            this.cf.bd(a);
        }
        else {
            this.cr.add(a);
            this.queueAutoRefresh();
        }
    };
    LocalDataSource.prototype.unpinRow = function (a) {
        _super.prototype.unpinRow.call(this, a);
        var b = this.ce();
        if (this.cf != null) {
            for (var c = this.cr.count - 1; c >= 0; c--) {
                var d = this.cr._inner[c];
                if (d.length != a.length) {
                    continue;
                }
                if (b.h(d, a) == 0) {
                    this.cr.removeAt(c);
                    this.cf.bk(d);
                }
            }
        }
        else {
            for (var e = this.cr.count - 1; e >= 0; e--) {
                var f = this.cr._inner[e];
                if (f.length != a.length) {
                    continue;
                }
                if (b.h(f, a) == 0) {
                    this.cr.removeAt(e);
                }
            }
            this.queueAutoRefresh();
        }
        this.cw = -1;
        this.cs = -1;
    };
    LocalDataSource.prototype.clearPinnedRows = function () {
        for (var a = this.cr.count - 1; a >= 0; a--) {
            this.unpinRow(this.cr._inner[a]);
        }
    };
    LocalDataSource.prototype.isRowPinned = function (a) {
        if (this.cf == null) {
            return false;
        }
        var b = this.getItemAtIndex(a);
        if (b == null) {
            return false;
        }
        var item_ = b;
        if ((item_ != null && item_.$$isSpecialRow !== undefined)) {
            if (item_.rowType == 3) {
                return true;
            }
        }
        var c = this.ce();
        var d;
        if (this.actualPrimaryKey == null) {
            d = new Array(1);
            d[0] = b;
        }
        else {
            var e = this.actualPrimaryKey;
            var f = e.length;
            d = new Array(f);
            for (var g = 0; g < f; g++) {
                d[g] = this.getItemProperty(b, e[g]);
            }
        }
        for (var h = 0; h < this.cr.count; h++) {
            if (c.h(this.cr._inner[h], d) == 0) {
                return true;
            }
        }
        return false;
    };
    LocalDataSource.prototype.get_isSortingSupported = function () {
        if (this.o != null && !this.ci) {
            this.ci = true;
            var a = this.o.isSortingSupportedOverride;
            this.ci = false;
            return a;
        }
        return true;
    };
    LocalDataSource.prototype.get_isFilteringSupported = function () {
        if (this.o != null && !this.ci) {
            this.ci = true;
            var a = this.o.isFilteringSupportedOverride;
            this.ci = false;
            return a;
        }
        return true;
    };
    LocalDataSource.prototype.get_isGroupingSupported = function () {
        if (this.o != null && !this.ci) {
            this.ci = true;
            var a = this.o.isGroupingSupportedOverride;
            this.ci = false;
            return a;
        }
        return true;
    };
    LocalDataSource.prototype.getStickyRowPriority = function (a) {
        if (this.getRowType(a) == 1 || this.getRowType(a) == 2) {
            return 100 - this.getRowLevel(a);
        }
        else if (this.getRowType(a) == 3) {
            return 2;
        }
        return 1;
    };
    LocalDataSource.prototype.getStickyRowsInRange = function (a, b) {
        if (this.cf == null || ((this.groupDescriptions == null || this.groupDescriptions.k.count == 0) && !this.ck())) {
            return null;
        }
        var c = null;
        var d = null;
        if (this.cf.ao.count == 1) {
            c = this.cf.ao._inner[0];
            d = this.cf.ao._inner[0];
        }
        else {
            c = this.cf.n(a);
            d = this.cf.n(b);
        }
        if (c == null || d == null) {
            return null;
        }
        if (c.m == this.cw && c.h == this.cy && c.n == this.cz && c.l == this.cx && d.m == this.cs && d.h == this.cu && d.n == this.cv && d.l == this.ct) {
            return this.b4;
        }
        this.cw = c.m;
        this.cs = d.m;
        this.cy = c.h;
        this.cu = d.h;
        this.cz = c.n;
        this.cv = d.n;
        this.cx = c.l;
        this.ct = d.l;
        if (this.cf.ao.count == 1) {
            this.cs = d.l;
        }
        var e = 0;
        if (this.shouldEmitSectionHeaders && this.af) {
            e = 1;
        }
        var f = new List$1(Number_$type, 0);
        for (var g = c.m; g <= d.m; g++) {
            if (this.shouldEmitSectionHeaders && this.af) {
                for (var h = 0; h < this.cf.ao._inner[g].b.length; h++) {
                    f.add(this.cf.ao._inner[g].n + h);
                    if (!this.cf.ao._inner[g].b[h]) {
                        break;
                    }
                }
            }
            if (this.shouldEmitShiftedRows) {
                var i = this.cf.ao._inner[g].b.length;
                for (var j = 0; j < this.cf.ao._inner[g].h; j++) {
                    f.add(this.cf.ao._inner[g].n + j + i);
                }
            }
            if (this.shouldEmitSectionFooters && this.af) {
                for (var k = this.cf.ao._inner[g].b.length - 1; k >= 0; k--) {
                    if (!this.cf.ao._inner[g].b[k]) {
                        break;
                    }
                    f.add(this.cf.ao._inner[g].l - k);
                }
            }
        }
        this.b4 = f.toArray();
        return this.b4;
    };
    LocalDataSource.prototype.isExclusivelySticky = function (a) {
        return this.getRowType(a) == 1 || this.getRowType(a) == 2;
    };
    LocalDataSource.prototype.getRowType = function (a) {
        if (a < 0) {
            return 0;
        }
        var b = this.getItemAtIndex(a);
        var item_ = b;
        if ((item_ != null && item_.$$isSpecialRow !== undefined)) {
            return b.rowType;
        }
        return 0;
    };
    LocalDataSource.prototype.setIsRowExpandedAtIndex = function (a, b) {
        if (this.isSectionCollapsable && this.cf != null && this.cf.ap.count > 0 && this.cf.ap.count > a) {
            this.cf.bh(a, b);
        }
    };
    LocalDataSource.prototype.getIsRowExpandedAtIndex = function (a) {
        if (this.cf != null && this.cf.ap.count > 0 && this.cf.ap.count > a) {
            return this.cf.y(a);
        }
        return true;
    };
    LocalDataSource.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;
        }
        return 0;
    };
    LocalDataSource.$t = markType(LocalDataSource, 'LocalDataSource', BaseDataSource.$);
    return LocalDataSource;
}(BaseDataSource));
export { LocalDataSource };
/**
 * @hidden
 */
var LocalDataSourceViewUpdateNotifier = /** @class */ (function (_super) {
    __extends(LocalDataSourceViewUpdateNotifier, _super);
    function LocalDataSourceViewUpdateNotifier(a) {
        var _this = _super.call(this) || this;
        _this.a = null;
        _this.a = a;
        return _this;
    }
    LocalDataSourceViewUpdateNotifier.prototype.notifyClearItems = function () {
        this.a.c5();
    };
    LocalDataSourceViewUpdateNotifier.prototype.notifyInsertItem = function (a, b) {
        this.a.c6(a, b);
    };
    LocalDataSourceViewUpdateNotifier.prototype.notifyRemoveItem = function (a, b) {
        this.a.c7(a, b);
    };
    LocalDataSourceViewUpdateNotifier.prototype.notifySetItem = function (a, b, c) {
        this.a.c8(a, b, c);
    };
    LocalDataSourceViewUpdateNotifier.$t = markType(LocalDataSourceViewUpdateNotifier, 'LocalDataSourceViewUpdateNotifier', Base.$, [ISupportsDataChangeNotifications_$type]);
    return LocalDataSourceViewUpdateNotifier;
}(Base));
export { LocalDataSourceViewUpdateNotifier };
