/*
THIS INFRAGISTICS ULTIMATE SOFTWARE LICENSE  AGREEMENT ("AGREEMENT") LOCATED HERE:
https://www.infragistics.com/legal/license/igultimate-la
https://www.infragistics.com/legal/license/igultimate-eula
GOVERNS THE LICENSING, INSTALLATION AND USE OF INFRAGISTICS SOFTWARE. BY DOWNLOADING AND/OR INSTALLING AND USING INFRAGISTICS SOFTWARE:  you are indicating that you have read and understand this Agreement, and agree to be legally bound by it on behalf of the yourself and your company.
*/
import { BaseDataSource } from "./BaseDataSource";
import { 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
 */
export class LocalDataSource extends BaseDataSource {
    constructor() {
        super();
        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();
    }
    get_actualBaseDataProvider() {
        return this.actualDataProvider;
    }
    get actualBaseDataProvider() {
        return this.get_actualBaseDataProvider();
    }
    get dataProvider() {
        return this.b6;
    }
    set dataProvider(a) {
        let b = this.b6;
        this.b6 = a;
        if (b != this.b6) {
            this.onPropertyUpdated("DataProvider", b, this.b6);
        }
    }
    get actualDataProvider() {
        if (this.b5 == null) {
            this.actualDataProvider = this.resolveDataProvider();
        }
        return this.b5;
    }
    set actualDataProvider(a) {
        let b = this.b5;
        this.b5 = a;
        if (b != this.b5) {
            this.onPropertyUpdated("ActualDataProvider", b, this.b5);
        }
    }
    get itemsSource() {
        return this.c0;
    }
    set itemsSource(a) {
        let b = this.c0;
        this.c0 = a;
        if (b != this.c0) {
            this.onPropertyUpdated("ItemsSource", b, this.c0);
        }
    }
    canInitialize() {
        let a = super.canInitialize();
        if (!a) {
            return false;
        }
        return this.itemsSource != null;
    }
    get cc() {
        return this.cb;
    }
    set cc(a) {
        this.cb = a;
        this.actualDataProvider = this.resolveDataProvider();
    }
    resolveDataProvider() {
        let provider_ = new DataSourceLocalDataProvider();
        provider_.executionContext = new DataSourceExecutionContext();
        return provider_;
    }
    propertyUpdatedOverride(a, b, c) {
        super.propertyUpdatedOverride(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;
        }
    }
    getItemAtIndex(a) {
        if (this.cf != null && this.cf.ap != null) {
            return this.cf.ap.item(a);
        }
        else {
            return this.actualDataProvider.getItemAtIndex(a);
        }
    }
    getItemPropertyAtIndex(a, b) {
        let c;
        if (this.cf != null) {
            c = this.cf.ap.item(a);
        }
        else {
            c = this.actualDataProvider.getItemAtIndex(a);
        }
        let item_ = c;
        if ((item_ != null && item_.$$isSpecialRow !== undefined)) {
            return this.getSpecialRowItemValue(c, b);
        }
        return this.actualDataProvider.getItemValue(c, b);
    }
    ca() {
        return this.actualDataProvider;
    }
    ck() {
        return this.cr != null && this.cr.count > 0;
    }
    refreshInternalOverride() {
        super.refreshInternalOverride();
        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 {
                let 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;
        }
    }
    get_isItemIndexLookupSupported() {
        return true;
    }
    get_isKeyIndexLookupSupported() {
        return true;
    }
    indexOfItem(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) {
            let b = this.actualDataProvider.actualCount;
            let c;
            for (let d = 0; d < b; d++) {
                c = this.actualDataProvider.getItemAtIndex(d);
                if (this.cj(c, a)) {
                    return d;
                }
            }
        }
        return -1;
    }
    cj(a, b) {
        if (a == null && b == null) {
            return true;
        }
        if (a == null || b == null) {
            return false;
        }
        return Base.equalsStatic(a, b);
    }
    indexOfKey(a) {
        let b = this.actualPrimaryKey;
        if (b == null || b.length < 1 || this.actualBaseDataProvider == null) {
            return this.indexOfItem(a[0]);
        }
        let c = this.ce();
        let d;
        if (this.cf != null) {
            let e = this.cf.ap.count;
            for (let 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) {
            let g = this.actualDataProvider.actualCount;
            for (let h = 0; h < g; h++) {
                d = this.actualDataProvider.getItemAtIndex(h);
                if (c.i(d, a) == 0) {
                    return h;
                }
            }
        }
        return -1;
    }
    onActualPrimaryKeyChangedOverrride(a, b) {
        super.onActualPrimaryKeyChangedOverrride(a, b);
        this.cd = null;
    }
    ce() {
        if (this.cd != null) {
            return this.cd;
        }
        if (this.actualPrimaryKey == null) {
            this.cd = new DataSourcePropertiesComparer(null, null, null, this);
            return this.cd;
        }
        let a = new Array(this.actualPrimaryKey.length);
        for (let b = 0; b < a.length; b++) {
            a[b] = 0;
        }
        this.cd = new DataSourcePropertiesComparer(this.actualPrimaryKey, a, this.d, this);
        return this.cd;
    }
    cg() {
        if (this.filterExpressions.j.count == 0) {
            return null;
        }
        let a = this.actualDataProvider;
        let b = this.actualDataProvider.actualSchema;
        let c = this.filterExpressions.j;
        let d = (e) => {
            for (let f = 0; f < c.count; f++) {
                if (!c.item(f).match(e, a, b)) {
                    return false;
                }
            }
            return true;
        };
        return d;
    }
    cq() {
        let 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;
        }
        let b = new Array(a);
        let c = new Array(a);
        let d = 0;
        if (this.groupDescriptions != null) {
            for (let 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 (let 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);
    }
    cp() {
        if (this.groupDescriptions == null || this.groupDescriptions.k.count < 1) {
            return null;
        }
        let a = 0;
        a = this.groupDescriptions.k.count;
        let b = new Array(a);
        let c = new Array(a);
        if (this.groupDescriptions != null) {
            for (let 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);
    }
    cm() {
        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;
    }
    cn() {
        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;
    }
    cl() {
        return this.filterExpressions != null && this.filterExpressions.j.count > 0;
    }
    co() {
        if (this.summaryDescriptions == null || this.summaryDescriptions.k.count < 1) {
            return false;
        }
        return true;
    }
    onClearItemsOverride() {
        if (this.cf != null) {
            this.cf.notifyClearItems();
            this.queueAutoRefresh();
        }
        else {
            super.onClearItemsOverride();
        }
    }
    onSetItemOverride(a, b, c) {
        if (this.cf != null) {
            this.cf.notifySetItem(a, b, c);
        }
        else {
            super.onSetItemOverride(a, b, c);
        }
    }
    onInsertItemOverride(a, b) {
        if (this.cf != null) {
            this.cf.notifyInsertItem(a, b);
        }
        else {
            super.onInsertItemOverride(a, b);
        }
    }
    onRemoveItemOverride(a, b) {
        if (this.cf != null) {
            this.cf.notifyRemoveItem(a, b);
        }
        else {
            super.onRemoveItemOverride(a, b);
        }
    }
    c5() {
        this.actualCount = this.cf.ap.count;
        this.onBroadcastClearItems();
    }
    c8(a, b, c) {
        this.actualCount = this.cf.ap.count;
        this.onBroadcastSetItem(a, b, c);
    }
    c6(a, b) {
        this.actualCount = this.cf.ap.count;
        this.onBroadcastInsertItem(a, b);
    }
    c7(a, b) {
        this.actualCount = this.cf.ap.count;
        this.onBroadcastRemoveItem(a, b);
    }
    pinRow(a) {
        super.pinRow(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();
        }
    }
    unpinRow(a) {
        super.unpinRow(a);
        let b = this.ce();
        if (this.cf != null) {
            for (let c = this.cr.count - 1; c >= 0; c--) {
                let 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 (let e = this.cr.count - 1; e >= 0; e--) {
                let 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;
    }
    clearPinnedRows() {
        for (let a = this.cr.count - 1; a >= 0; a--) {
            this.unpinRow(this.cr._inner[a]);
        }
    }
    isRowPinned(a) {
        if (this.cf == null) {
            return false;
        }
        let b = this.getItemAtIndex(a);
        if (b == null) {
            return false;
        }
        let item_ = b;
        if ((item_ != null && item_.$$isSpecialRow !== undefined)) {
            if (item_.rowType == 3) {
                return true;
            }
        }
        let c = this.ce();
        let d;
        if (this.actualPrimaryKey == null) {
            d = new Array(1);
            d[0] = b;
        }
        else {
            let e = this.actualPrimaryKey;
            let f = e.length;
            d = new Array(f);
            for (let g = 0; g < f; g++) {
                d[g] = this.getItemProperty(b, e[g]);
            }
        }
        for (let h = 0; h < this.cr.count; h++) {
            if (c.h(this.cr._inner[h], d) == 0) {
                return true;
            }
        }
        return false;
    }
    get_isSortingSupported() {
        if (this.o != null && !this.ci) {
            this.ci = true;
            let a = this.o.isSortingSupportedOverride;
            this.ci = false;
            return a;
        }
        return true;
    }
    get_isFilteringSupported() {
        if (this.o != null && !this.ci) {
            this.ci = true;
            let a = this.o.isFilteringSupportedOverride;
            this.ci = false;
            return a;
        }
        return true;
    }
    get_isGroupingSupported() {
        if (this.o != null && !this.ci) {
            this.ci = true;
            let a = this.o.isGroupingSupportedOverride;
            this.ci = false;
            return a;
        }
        return true;
    }
    getStickyRowPriority(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;
    }
    getStickyRowsInRange(a, b) {
        if (this.cf == null || ((this.groupDescriptions == null || this.groupDescriptions.k.count == 0) && !this.ck())) {
            return null;
        }
        let c = null;
        let 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;
        }
        let e = 0;
        if (this.shouldEmitSectionHeaders && this.af) {
            e = 1;
        }
        let f = new List$1(Number_$type, 0);
        for (let g = c.m; g <= d.m; g++) {
            if (this.shouldEmitSectionHeaders && this.af) {
                for (let 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) {
                let i = this.cf.ao._inner[g].b.length;
                for (let 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 (let 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;
    }
    isExclusivelySticky(a) {
        return this.getRowType(a) == 1 || this.getRowType(a) == 2;
    }
    getRowType(a) {
        if (a < 0) {
            return 0;
        }
        let b = this.getItemAtIndex(a);
        let item_ = b;
        if ((item_ != null && item_.$$isSpecialRow !== undefined)) {
            return b.rowType;
        }
        return 0;
    }
    setIsRowExpandedAtIndex(a, b) {
        if (this.isSectionCollapsable && this.cf != null && this.cf.ap.count > 0 && this.cf.ap.count > a) {
            this.cf.bh(a, b);
        }
    }
    getIsRowExpandedAtIndex(a) {
        if (this.cf != null && this.cf.ap.count > 0 && this.cf.ap.count > a) {
            return this.cf.y(a);
        }
        return true;
    }
    getRowLevel(a) {
        if (a < 0) {
            return 0;
        }
        let b = this.getItemAtIndex(a);
        let item_ = b;
        if ((item_ != null && item_.$$isSpecialRow !== undefined)) {
            return b.level;
        }
        return 0;
    }
}
LocalDataSource.$t = markType(LocalDataSource, 'LocalDataSource', BaseDataSource.$);
/**
 * @hidden
 */
export class LocalDataSourceViewUpdateNotifier extends Base {
    constructor(a) {
        super();
        this.a = null;
        this.a = a;
    }
    notifyClearItems() {
        this.a.c5();
    }
    notifyInsertItem(a, b) {
        this.a.c6(a, b);
    }
    notifyRemoveItem(a, b) {
        this.a.c7(a, b);
    }
    notifySetItem(a, b, c) {
        this.a.c8(a, b, c);
    }
}
LocalDataSourceViewUpdateNotifier.$t = markType(LocalDataSourceViewUpdateNotifier, 'LocalDataSourceViewUpdateNotifier', Base.$, [ISupportsDataChangeNotifications_$type]);
