/*
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 { Base, PropertyChangedEventArgs, runOn, delegateRemove, delegateCombine, enumGetBox, markType } from "./type";
import { IDataSource_$type } from "./IDataSource";
import { IDataSourceDataProviderUpdateNotifier_$type } from "./IDataSourceDataProviderUpdateNotifier";
import { SortDescriptionCollection } from "./SortDescriptionCollection";
import { FilterExpressionCollection } from "./FilterExpressionCollection";
import { SummaryDescriptionCollection } from "./SummaryDescriptionCollection";
import { DataSourceSummaryScope_$type } from "./DataSourceSummaryScope";
import { DataSourceSectionHeaderDisplayMode_$type } from "./DataSourceSectionHeaderDisplayMode";
import { DataSourceSchemaChangedEventArgs } from "./DataSourceSchemaChangedEventArgs";
/**
 * @hidden
 */
export class BaseDataSource extends Base {
    constructor() {
        super();
        this.af = false;
        this.am = null;
        this.o = null;
        this.q = null;
        this.p = null;
        this.g = null;
        this.r = null;
        this.h = null;
        this.m = null;
        this.ai = 0;
        this.aj = 0;
        this.ak = 0;
        this.n = null;
        this.propertyChanged = null;
        this.j = null;
        this.l = null;
        this.schemaChanged = null;
        this.v = false;
        this.f = 0;
        this.u = false;
        this.b = null;
        this.a = null;
        this.c = null;
        this.y = false;
        this.z = false;
        this.s = false;
        this.e = 0;
        this.w = false;
        this.x = true;
        this.ab = true;
        this.aa = false;
        this.ac = true;
        this.t = false;
        this.d = null;
        this.j = new DefaultDataSourceDataProviderUpdateNotifier(this);
        this.q = new SortDescriptionCollection();
        this.p = new SortDescriptionCollection();
        this.r = new SummaryDescriptionCollection();
        this.q.n(runOn(this, this.b2));
        this.p.n(runOn(this, this.bb));
        this.r.n(runOn(this, this.b3));
        this.g = new FilterExpressionCollection();
        this.g.m(runOn(this, this.a9));
    }
    b2(a, b) {
        this.onSortDescriptionsChanged(b);
    }
    bb(a, b) {
        this.onGroupDescriptionsChanged(b);
    }
    b3(a, b) {
        this.onSummaryDescriptionsChanged(b);
    }
    a9(a, b) {
        this.onFilterExpressionsChanged(b);
    }
    onSortDescriptionsChanged(a) {
        this.queueAutoRefresh();
    }
    onGroupDescriptionsChanged(a) {
        if (this.isGroupingSupported) {
            if (this.groupDescriptions != null && this.groupDescriptions.k.count > 0) {
                this.af = true;
            }
            else {
                this.af = false;
            }
            this.queueAutoRefresh();
        }
    }
    onSummaryDescriptionsChanged(a) {
        this.queueAutoRefresh();
    }
    onFilterExpressionsChanged(a) {
        this.queueAutoRefresh();
    }
    isPlaceholderItem(a) {
        return false;
    }
    get actualSchema() {
        return this.m;
    }
    set actualSchema(a) {
        let b = this.m;
        this.m = a;
        if (b != this.m) {
            this.onPropertyUpdated("ActualSchema", b, this.m);
        }
    }
    get actualCount() {
        return this.ai;
    }
    set actualCount(a) {
        let b = this.ai;
        this.ai = a;
        if (b != this.ai) {
            this.onPropertyUpdated("ActualCount", b, this.ai);
        }
    }
    get firstVisibleIndexRequested() {
        return this.aj;
    }
    set firstVisibleIndexRequested(a) {
        let b = this.aj;
        this.aj = a;
        if (b != this.aj) {
            this.onPropertyUpdated("FirstVisibleIndexRequested", b, this.aj);
        }
    }
    get lastVisibleIndexRequested() {
        return this.ak;
    }
    set lastVisibleIndexRequested(a) {
        let b = this.ak;
        this.ak = a;
        if (b != this.ak) {
            this.onPropertyUpdated("LastVisibleIndexRequested", b, this.ak);
        }
    }
    get updateNotifier() {
        return this.n;
    }
    set updateNotifier(a) {
        let b = this.n;
        this.n = a;
        if (b != this.n) {
            this.onPropertyUpdated("UpdateNotifier", b, this.n);
        }
    }
    getItemAtIndex(a) {
        return null;
    }
    getItemProperty(a, b) {
        if (this.actualBaseDataProvider != null) {
            let item_ = a;
            if ((item_ != null && item_.$$isSpecialRow !== undefined)) {
                return this.getSpecialRowItemValue(a, b);
            }
            return this.actualBaseDataProvider.getItemValue(a, b);
        }
        return null;
    }
    getSpecialRowItemValue(a, b) {
        if (a.rowType == 3) {
            return this.getItemProperty(a.targetRow, b);
        }
        return a.getValue(b);
    }
    getItemPropertyAtIndex(a, b) {
        return null;
    }
    get_fullSectionInformationWillForceLoad() {
        return false;
    }
    get fullSectionInformationWillForceLoad() {
        return this.get_fullSectionInformationWillForceLoad();
    }
    onPropertyUpdated(a, b, c) {
        if (this.propertyChanged != null) {
            this.propertyChanged(this, new PropertyChangedEventArgs(a));
        }
        this.propertyUpdatedOverride(a, b, c);
    }
    propertyUpdatedOverride(a, b, c) {
        switch (a) {
            case "PrimaryKey":
                this.actualPrimaryKey = this.primaryKey;
                break;
            case "ActualPrimaryKey":
                this.onActualPrimaryKeyChanged(b, c);
                break;
            case "ExecutionContext":
                if (this.actualBaseDataProvider != null) {
                    this.actualBaseDataProvider.executionContext = this.executionContext;
                }
                this.queueAutoRefresh();
                break;
            case "LastVisibleIndexRequested":
            case "FirstVisibleIndexRequested":
                this.onRequestedVisibleRangeChanged();
                break;
            case "ActualDataProvider":
                this.onActualDataProviderChanged(b, c);
                break;
            case "DeferAutoRefresh":
                if (this.actualBaseDataProvider != null) {
                    this.actualBaseDataProvider.deferAutoRefresh = this.deferAutoRefresh;
                }
                if (!c) {
                    this.queueAutoRefresh();
                }
                break;
            case "PropertiesRequested":
                if (this.actualBaseDataProvider != null) {
                    this.actualBaseDataProvider.propertiesRequested = this.propertiesRequested;
                }
                break;
            case "ShouldEmitSectionFooters":
            case "ShouldEmitSectionHeaders":
            case "ShouldEmitShiftedRows":
            case "SectionHeaderDisplayMode":
            case "IsSectionCollapsable":
                this.queueAutoRefresh();
                break;
            case "SummaryScope":
                if (this.actualBaseDataProvider != null) {
                    this.actualBaseDataProvider.summaryScope = this.summaryScope;
                }
                this.queueAutoRefresh();
                break;
        }
    }
    onActualPrimaryKeyChanged(a, b) {
        this.onActualPrimaryKeyChangedOverrride(a, b);
    }
    onActualPrimaryKeyChangedOverrride(a, b) {
    }
    get_isVirtual() {
        return false;
    }
    get isVirtual() {
        return this.get_isVirtual();
    }
    get dataProviderUpdateNotifier() {
        return this.j;
    }
    onActualDataProviderChanged(a, b) {
        let c = a;
        let d = b;
        if (c != null) {
            c.updateNotifier = null;
        }
        if (d != null) {
            d.updateNotifier = this.j;
        }
        if (c != null) {
            c.schemaChanged = delegateRemove(c.schemaChanged, runOn(this, this.a6));
        }
        if (d != null) {
            d.schemaChanged = delegateCombine(d.schemaChanged, runOn(this, this.a6));
            d.executionContext = this.executionContext;
            this.notifyUsingSourceIndexes = this.actualBaseDataProvider.notifyUsingSourceIndexes;
        }
        else {
            this.notifyUsingSourceIndexes = false;
        }
        if (c != null) {
            this.sortDescriptions.syncTarget = null;
            this.groupDescriptions.syncTarget = null;
            this.filterExpressions.syncTarget = null;
            this.summaryDescriptions.syncTarget = null;
        }
        if (d != null) {
            this.sortDescriptions.syncTarget = d.sortDescriptions;
            this.groupDescriptions.syncTarget = d.groupDescriptions;
            this.filterExpressions.syncTarget = d.filterExpressions;
            this.summaryDescriptions.syncTarget = d.summaryDescriptions;
            if (this.propertiesRequested != null) {
                d.propertiesRequested = this.propertiesRequested;
            }
            d.summaryScope = this.summaryScope;
        }
        this.queueAutoRefresh();
    }
    onRequestedVisibleRangeChanged() {
    }
    get executionContext() {
        return this.l;
    }
    set executionContext(a) {
        let b = this.l;
        this.l = a;
        if (this.l != b) {
            this.onPropertyUpdated("ExecutionContext", b, this.l);
        }
    }
    canInitialize() {
        if (this.l != null && this.actualBaseDataProvider != null) {
            return true;
        }
        return false;
    }
    get isInitializing() {
        return this.v;
    }
    get_isSortingSupported() {
        return false;
    }
    get isSortingSupported() {
        return this.get_isSortingSupported();
    }
    get_isFilteringSupported() {
        return false;
    }
    get isFilteringSupported() {
        return this.get_isFilteringSupported();
    }
    get sortDescriptions() {
        return this.q;
    }
    get groupDescriptions() {
        return this.p;
    }
    get filterExpressions() {
        return this.g;
    }
    get summaryDescriptions() {
        return this.r;
    }
    get summaryScope() {
        return this.f;
    }
    set summaryScope(a) {
        let b = this.f;
        this.f = a;
        if (this.f != b) {
            this.onPropertyUpdated("SummaryScope", enumGetBox(DataSourceSummaryScope_$type, b), enumGetBox(DataSourceSummaryScope_$type, this.f));
        }
    }
    get deferAutoRefresh() {
        return this.u;
    }
    set deferAutoRefresh(a) {
        let b = this.u;
        this.u = a;
        if (this.u != b) {
            this.onPropertyUpdated("DeferAutoRefresh", b, this.u);
        }
    }
    get primaryKey() {
        return this.b;
    }
    set primaryKey(a) {
        let b = this.b;
        this.b = a;
        if (this.b != b) {
            this.onPropertyUpdated("PrimaryKey", b, this.b);
        }
    }
    get actualPrimaryKey() {
        return this.a;
    }
    set actualPrimaryKey(a) {
        let b = this.a;
        this.a = a;
        if (this.a != b) {
            this.onPropertyUpdated("ActualPrimaryKey", b, this.a);
        }
    }
    get propertiesRequested() {
        return this.c;
    }
    set propertiesRequested(a) {
        let b = this.c;
        this.c = a;
        if (this.c != b) {
            this.onPropertyUpdated("PropertiesRequested", b, this.c);
        }
    }
    get notifyUsingSourceIndexes() {
        return this.y;
    }
    set notifyUsingSourceIndexes(a) {
        let b = this.y;
        this.y = a;
        if (this.y != b) {
            this.onPropertyUpdated("NotifyUsingSourceIndexes", b, this.y);
        }
    }
    get_isItemIndexLookupSupported() {
        return false;
    }
    get isItemIndexLookupSupported() {
        return this.get_isItemIndexLookupSupported();
    }
    get_isKeyIndexLookupSupported() {
        return false;
    }
    get isKeyIndexLookupSupported() {
        return this.get_isKeyIndexLookupSupported();
    }
    initializeOverride() {
    }
    resolveFullCount(a) {
        return a;
    }
    a6(a, b) {
        let c = this.executionContext;
        c.execute(() => {
            this.z = true;
            this.t = true;
            this.queueAutoRefresh();
            if (this.deferAutoRefresh) {
                this.actualSchema = b.schema;
                this.actualCount = this.resolveFullCount(b.count);
                this.bd();
            }
        });
    }
    bd() {
        if (this.schemaChanged != null) {
            this.schemaChanged(this, new DataSourceSchemaChangedEventArgs(this.actualSchema, this.actualCount));
        }
    }
    onSetItem(a, b, c) {
        this.onSetItemOverride(a, b, c);
    }
    onBroadcastSetItem(a, b, c) {
        if (this.updateNotifier != null) {
            this.updateNotifier.notifySetItem(a, b, c);
        }
    }
    onSetItemOverride(a, b, c) {
        if (this.actualBaseDataProvider != null) {
            this.actualCount = this.resolveFullCount(this.actualBaseDataProvider.actualCount);
        }
        this.onBroadcastSetItem(a, b, c);
    }
    onClearItems() {
        this.onClearItemsOverride();
    }
    onBroadcastClearItems() {
        if (this.updateNotifier != null) {
            this.updateNotifier.notifyClearItems();
        }
    }
    onClearItemsOverride() {
        if (this.actualBaseDataProvider != null) {
            this.actualCount = this.resolveFullCount(this.actualBaseDataProvider.actualCount);
        }
        this.onBroadcastClearItems();
    }
    onInsertItem(a, b) {
        this.onInsertItemOverride(a, b);
    }
    onBroadcastInsertItem(a, b) {
        if (this.updateNotifier != null) {
            this.updateNotifier.notifyInsertItem(a, b);
        }
    }
    onInsertItemOverride(a, b) {
        if (this.actualBaseDataProvider != null) {
            this.actualCount = this.resolveFullCount(this.actualBaseDataProvider.actualCount);
        }
        this.onBroadcastInsertItem(a, b);
    }
    onRemoveItem(a, b) {
        this.onRemoveItemOverride(a, b);
    }
    onBroadcastRemoveItem(a, b) {
        if (this.updateNotifier != null) {
            this.updateNotifier.notifyRemoveItem(a, b);
        }
    }
    onRemoveItemOverride(a, b) {
        if (this.actualBaseDataProvider != null) {
            this.actualCount = this.resolveFullCount(this.actualBaseDataProvider.actualCount);
        }
        this.onBroadcastRemoveItem(a, b);
    }
    notifySetItem(a, b, c) {
        if (this.actualBaseDataProvider != null) {
            this.actualBaseDataProvider.notifySetItem(a, b, c);
        }
    }
    notifyClearItems() {
        if (this.actualBaseDataProvider != null) {
            this.actualBaseDataProvider.notifyClearItems();
        }
    }
    notifyInsertItem(a, b) {
        if (this.actualBaseDataProvider != null) {
            this.actualBaseDataProvider.notifyInsertItem(a, b);
        }
        ;
    }
    notifyRemoveItem(a, b) {
        if (this.actualBaseDataProvider != null) {
            this.actualBaseDataProvider.notifyRemoveItem(a, b);
        }
        ;
    }
    queueAutoRefresh() {
        if (this.deferAutoRefresh) {
            return;
        }
        if (this.actualBaseDataProvider != null) {
            this.actualBaseDataProvider.queueAutoRefresh();
        }
        if (this.s) {
            return;
        }
        if (this.executionContext != null) {
            this.s = true;
            this.executionContext.enqueueAction(runOn(this, this.a7));
        }
    }
    a7() {
        if (this.deferAutoRefresh) {
            this.s = false;
            return;
        }
        if (this.actualBaseDataProvider != null) {
            this.actualBaseDataProvider.flushAutoRefresh();
        }
        if (!this.s) {
            return;
        }
        this.s = false;
        this.b0();
    }
    b0() {
        if (this.actualBaseDataProvider != null) {
            this.actualSchema = this.actualBaseDataProvider.actualSchema;
            this.actualCount = this.resolveFullCount(this.actualBaseDataProvider.actualCount);
        }
        if (this.actualSchema != null && this.primaryKey == null) {
            this.actualPrimaryKey = this.actualSchema.primaryKey;
        }
        this.refreshInternalOverride();
        if (this.z) {
            this.z = false;
            this.bd();
        }
    }
    refreshInternalOverride() {
    }
    flushAutoRefresh() {
        this.a7();
    }
    refresh() {
        if (this.actualBaseDataProvider != null) {
            this.actualBaseDataProvider.refresh();
        }
        this.b0();
    }
    indexOfItem(a) {
        return -1;
    }
    indexOfKey(a) {
        return -1;
    }
    getPrimaryKeyValue(a, b) {
        let c = this.actualPrimaryKey;
        if (c == null || c.length < 1 || this.actualBaseDataProvider == null) {
            return;
        }
        for (let d = 0; d < c.length; d++) {
            b[d] = this.actualBaseDataProvider.getItemValue(a, c[d]);
        }
    }
    getRowType(a) {
        return 0;
    }
    getMainValuePath(a) {
        return "SectionInformation";
    }
    getStickyRows() {
        return this.getStickyRowsInRange(this.firstVisibleIndexRequested, this.lastVisibleIndexRequested);
    }
    getStickyRowsInRange(a, b) {
        return null;
    }
    isRowSpanning(a) {
        return a == 1 || a == 2;
    }
    getStickyRowPriority(a) {
        return -1;
    }
    isExclusivelySticky(a) {
        return false;
    }
    pinRow(a) {
    }
    unpinRow(a) {
    }
    clearPinnedRows() {
    }
    isRowPinned(a) {
        return false;
    }
    get_isGroupingSupported() {
        return false;
    }
    get isGroupingSupported() {
        return this.get_isGroupingSupported();
    }
    get_sectionHeaderDisplayMode() {
        return this.e;
    }
    set_sectionHeaderDisplayMode(a) {
        let b = this.e;
        this.e = a;
        if (b != this.e) {
            this.onPropertyUpdated("SectionHeaderDisplayMode", enumGetBox(DataSourceSectionHeaderDisplayMode_$type, b), enumGetBox(DataSourceSectionHeaderDisplayMode_$type, this.e));
        }
    }
    get sectionHeaderDisplayMode() {
        return this.get_sectionHeaderDisplayMode();
    }
    set sectionHeaderDisplayMode(a) {
        this.set_sectionHeaderDisplayMode(a);
    }
    get_isSectionCollapsable() {
        return this.w;
    }
    set_isSectionCollapsable(a) {
        let b = this.w;
        this.w = a;
        if (this.w != b) {
            this.onPropertyUpdated("IsSectionCollapsable", b, this.w);
        }
    }
    get isSectionCollapsable() {
        return this.get_isSectionCollapsable();
    }
    set isSectionCollapsable(a) {
        this.set_isSectionCollapsable(a);
    }
    get_isSectionExpandedDefault() {
        return this.x;
    }
    set_isSectionExpandedDefault(a) {
        this.x = a;
    }
    get isSectionExpandedDefault() {
        return this.get_isSectionExpandedDefault();
    }
    set isSectionExpandedDefault(a) {
        this.set_isSectionExpandedDefault(a);
    }
    get shouldEmitSectionHeaders() {
        return this.ab;
    }
    set shouldEmitSectionHeaders(a) {
        let b = this.ab;
        this.ab = a;
        if (this.ab != b) {
            this.onPropertyUpdated("ShouldEmitSectionHeaders", b, this.ab);
        }
    }
    get shouldEmitSectionFooters() {
        return this.aa;
    }
    set shouldEmitSectionFooters(a) {
        let b = this.aa;
        this.aa = a;
        if (this.aa != b) {
            this.onPropertyUpdated("ShouldEmitSectionFooters", b, this.aa);
        }
    }
    get shouldEmitShiftedRows() {
        return this.ac;
    }
    set shouldEmitShiftedRows(a) {
        let b = this.ac;
        this.ac = a;
        if (this.ac != b) {
            this.onPropertyUpdated("ShouldEmitShiftedRows", b, this.ac);
        }
    }
    ensureComparables(a) {
        if (this.t && this.actualCount > 0) {
            this.d = new Array(a.propertyNames.length);
            this.t = false;
        }
    }
    resolveSchemaPropertyType(a) {
        return this.actualBaseDataProvider.resolveSchemaPropertyType(a);
    }
    setIsRowExpandedAtIndex(a, b) {
    }
    getIsRowExpandedAtIndex(a) {
        return true;
    }
    getRowLevel(a) {
        return 0;
    }
}
BaseDataSource.$t = markType(BaseDataSource, 'BaseDataSource', Base.$, [IDataSource_$type]);
/**
 * @hidden
 */
export class DefaultDataSourceDataProviderUpdateNotifier extends Base {
    constructor(a) {
        super();
        this.a = null;
        this.a = a;
    }
    notifySetItem(a, b, c) {
        this.a.onSetItem(a, b, c);
    }
    notifyClearItems() {
        this.a.onClearItems();
    }
    notifyInsertItem(a, b) {
        this.a.onInsertItem(a, b);
    }
    notifyRemoveItem(a, b) {
        this.a.onRemoveItem(a, b);
    }
}
DefaultDataSourceDataProviderUpdateNotifier.$t = markType(DefaultDataSourceDataProviderUpdateNotifier, 'DefaultDataSourceDataProviderUpdateNotifier', Base.$, [IDataSourceDataProviderUpdateNotifier_$type]);
