/*
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, IList_$type, runOn, String_$type, typeCast, Array_$type, markType } from "./type";
import { IDataSourceLocalDataProvider_$type } from "./IDataSourceLocalDataProvider";
import { DataSourceSchemaPropertyType_$type } from "./DataSourceSchemaPropertyType";
import { DefaultDataSourceSchema } from "./DefaultDataSourceSchema";
import { SortDescriptionCollection } from "./SortDescriptionCollection";
import { Dictionary$2 } from "./Dictionary$2";
import { FastReflectionHelper } from "./FastReflectionHelper";
import { DataSourceDataProviderSchemaChangedEventArgs } from "./DataSourceDataProviderSchemaChangedEventArgs";
import { List$1 } from "./List$1";
import { HashSet$1 } from "./HashSet$1";
import { stringStartsWith } from "./string";
/**
 * @hidden
 */
export let DataSourceLocalDataProvider = /*@__PURE__*/ (() => {
    class DataSourceLocalDataProvider extends Base {
        constructor() {
            super();
            this.k = null;
            this.j = null;
            this.e = null;
            this.l = null;
            this.d = 0;
            this.v = null;
            this.schemaChanged = null;
            this.q = new Dictionary$2(String_$type, FastReflectionHelper.$, 0);
            this.h = null;
            this.t = null;
            this.o = false;
            this.g = null;
            this.f = null;
            this.n = false;
            this.a = null;
            this.m = false;
            this.k = new SortDescriptionCollection();
            this.k.n(runOn(this, this.ai));
        }
        ai(a, b) {
            this.queueAutoRefresh();
        }
        get dataSource() {
            return this.v;
        }
        set dataSource(a) {
            let b = this.v;
            this.v = a;
            if (b != this.v) {
                this.ae("ItemsSource", b, this.v);
            }
        }
        getItemAtIndex(a) {
            if (this.t == null) {
                return null;
            }
            if (this.o) {
                return this.t[a];
            }
            return this.t.item(a);
        }
        getItemValue(a, b) {
            this.ab();
            let c;
            let d = this.q.tryGetValue(b, c);
            c = d.p1;
            if (c == null) {
                return null;
            }
            let val_ = c.getPropertyValue(a);
            if ((val_ === undefined)) {
                return null;
            }
            return val_;
        }
        ab() {
            if (this.h == null) {
                this.h = this.actualSchema;
                if (this.schemaChanged != null) {
                    this.schemaChanged(this, new DataSourceDataProviderSchemaChangedEventArgs(this.h, this.actualCount));
                }
            }
        }
        get actualCount() {
            if (this.t == null) {
                return 0;
            }
            if (this.o) {
                return (this.t.length);
            }
            return this.t.count;
        }
        get actualSchema() {
            if (this.h == null) {
                if (this.t == null) {
                    this.h = new DefaultDataSourceSchema([], [], null, null);
                }
                else {
                    this.h = this.i();
                }
            }
            return this.h;
        }
        i() {
            let a = this.w();
            if (a == null) {
                return new DefaultDataSourceSchema([], [], null, null);
            }
            this.q.clear();
            let b = new List$1(String_$type, 0);
            let c = new HashSet$1(String_$type, 0);
            let d = new List$1(DataSourceSchemaPropertyType_$type, 0);
            let e = new List$1(List$1.$.specialize(String_$type), 0);
            let f = this.r(a);
            for (let g = 0; g < f.count; g++) {
                let h = f._inner[g];
                if (stringStartsWith(h, "_")) {
                    continue;
                }
                if (h == "$hashCode") {
                    continue;
                }
                if (c.contains(h)) {
                    continue;
                }
                else {
                    b.add(h);
                    c.add_1(h);
                    this.q.addItem(h, new FastReflectionHelper(false, h));
                    d.add(this.b(a, h));
                    e.add(this.s(a, h));
                }
            }
            let i = new Array(e.count);
            for (let j = 0; j < e.count; j++) {
                i[j] = e._inner[j].toArray();
            }
            return new DefaultDataSourceSchema(b.toArray(), d.toArray(), null, i);
        }
        resolveSchemaPropertyType(a) {
            let b = a.split('.');
            if (b.length == 1) {
                for (let c = 0; c < this.actualSchema.propertyNames.length; c++) {
                    if (this.actualSchema.propertyNames[c] == b[0]) {
                        return this.actualSchema.propertyTypes[c];
                    }
                }
                return 11;
            }
            else {
                let d = this.w();
                let e = this.getItemValue(d, a);
                if (e == null) {
                    return 11;
                }
                else {
                    return DataSourceLocalDataProvider.c(e);
                }
            }
        }
        r(schemaItem_) {
            let propertyKey_ = null;
            let ret_ = new List$1(String_$type, 0);
            let exclusions_ = {};
            for (propertyKey_ in schemaItem_) {
                if (!isNaN(propertyKey_)) {
                    continue;
                }
                if (!exclusions_.hasOwnProperty(propertyKey_)) {
                    ret_.add(propertyKey_);
                }
            }
            ;
            return ret_;
        }
        ad(a, item_, propertyName_, b) {
            if ((item_.__dataIntents !== undefined)) {
                if ((item_.__dataIntents[propertyName_] !== undefined)) {
                    this.ac(a, item_, propertyName_);
                }
                let selfName_ = "self." + propertyName_;
                if (!b && (item_.__dataIntents[selfName_] !== undefined)) {
                    this.ac(a, item_, selfName_);
                }
                let selfName2_ = "self_" + propertyName_;
                if (!b && (item_.__dataIntents[selfName2_] !== undefined)) {
                    this.ac(a, item_, selfName2_);
                }
            }
        }
        ac(a, item_, propertyName_) {
            let b = item_.__dataIntents[propertyName_];
            if (typeCast(Array_$type, b) !== null) {
                let c = b;
                for (let d = 0; d < c.length; d++) {
                    if (c[d] != null) {
                        a.add(c[d].toString());
                    }
                }
            }
        }
        s(item_, propertyName_) {
            let a = new List$1(String_$type, 0);
            let list_ = this.t;
            let dataIntents_ = null;
            this.ad(a, item_, propertyName_, false);
            this.ad(a, list_, propertyName_, true);
            return a;
        }
        b(item_, propertyName_) {
            if (item_ == null) {
                return 11;
            }
            if ((item_[propertyName_] == undefined)) {
                return 11;
            }
            let value_ = item_[propertyName_];
            return DataSourceLocalDataProvider.c(value_);
        }
        static c(value_) {
            if (value_ == null) {
                return 11;
            }
            if ((typeof value_ == 'number')) {
                return 5;
            }
            if ((typeof value_ == 'boolean')) {
                return 2;
            }
            if ((typeof value_ == 'string')) {
                return 0;
            }
            if ((typeof value_ == 'object')) {
                if ((value_ instanceof Date)) {
                    return 8;
                }
            }
            return 11;
        }
        w() {
            let a = 0;
            if (this.o) {
                a = (this.t.length);
            }
            else {
                a = this.t.count;
            }
            for (let b = 0; b < a; b++) {
                let c;
                if (this.o) {
                    c = this.t[b];
                }
                else {
                    c = this.t.item(b);
                }
                if (c != null) {
                    return c;
                }
            }
            return null;
        }
        ae(a, b, c) {
            this.af(a, b, c);
        }
        get u() {
            return this.t;
        }
        af(a, b, c) {
            switch (a) {
                case "ItemsSource":
                    this.h = null;
                    if (this.dataSource != null) {
                        this.o = false;
                        if (typeCast(Array_$type, this.dataSource) !== null) {
                            this.o = true;
                            this.t = this.dataSource;
                        }
                        else if (typeCast(IList_$type, this.dataSource) !== null) {
                            this.t = this.dataSource;
                        }
                    }
                    else {
                        this.t = null;
                    }
                    this.ab();
                    break;
                case "DeferAutoRefresh":
                    if (!c) {
                        this.queueAutoRefresh();
                    }
                    break;
            }
        }
        get executionContext() {
            return this.g;
        }
        set executionContext(a) {
            this.g = a;
        }
        get p() {
            return false;
        }
        notifySetItem(a, b, c) {
            if (this.p) {
                if (this.o) {
                    this.t[a] = c;
                }
                else {
                    this.t.item(a, c);
                }
            }
            if (this.updateNotifier != null) {
                this.updateNotifier.notifySetItem(a, b, c);
            }
        }
        notifyClearItems() {
            if (this.p) {
                if (this.o) {
                    this.t.length = 0;
                }
                else {
                    this.t.clear();
                }
            }
            if (this.updateNotifier != null) {
                this.updateNotifier.notifyClearItems();
            }
        }
        notifyInsertItem(a, b) {
            if (this.p) {
                if (this.o) {
                    this.t.splice(a, 0, b);
                }
                else {
                    this.t.insert(a, b);
                }
            }
            let c = 0;
            if (this.o) {
                c = (this.t.length);
            }
            else {
                c = this.t.count;
            }
            if (this.t != null && c == 1 && (this.actualSchema == null || this.actualSchema.propertyNames.length == 0)) {
                this.h = null;
                this.ab();
            }
            if (this.updateNotifier != null) {
                this.updateNotifier.notifyInsertItem(a, b);
            }
        }
        notifyRemoveItem(a, b) {
            if (this.p) {
                if (this.o) {
                    this.t.splice(a, 1);
                }
                else {
                    this.t.remove(a);
                }
            }
            if (this.updateNotifier != null) {
                this.updateNotifier.notifyRemoveItem(a, b);
            }
        }
        get updateNotifier() {
            return this.f;
        }
        set updateNotifier(a) {
            this.f = a;
        }
        get deferAutoRefresh() {
            return this.n;
        }
        set deferAutoRefresh(a) {
            let b = this.n;
            this.n = a;
            if (this.n != b) {
                this.ae("DeferAutoRefresh", b, this.n);
            }
        }
        get isSortingSupported() {
            return false;
        }
        get isFilteringSupported() {
            return false;
        }
        get sortDescriptions() {
            return this.k;
        }
        get filterExpressions() {
            return this.e;
        }
        get summaryDescriptions() {
            return this.l;
        }
        get summaryScope() {
            return this.d;
        }
        set summaryScope(a) {
            this.d = a;
        }
        get notifyUsingSourceIndexes() {
            return true;
        }
        get propertiesRequested() {
            return this.a;
        }
        set propertiesRequested(a) {
            this.a = a;
        }
        get isItemIndexLookupSupported() {
            return true;
        }
        get isKeyIndexLookupSupported() {
            return false;
        }
        get isGroupingSupported() {
            return false;
        }
        get groupDescriptions() {
            return this.j;
        }
        queueAutoRefresh() {
            if (this.deferAutoRefresh) {
                return;
            }
            if (this.m) {
                return;
            }
            if (this.executionContext != null) {
                this.m = true;
                this.executionContext.enqueueAction(runOn(this, this.aa));
            }
        }
        aa() {
            if (this.deferAutoRefresh) {
                this.m = false;
                return;
            }
            if (!this.m) {
                return;
            }
            this.m = false;
            this.ag();
        }
        ag() {
            this.ah();
        }
        ah() {
        }
        flushAutoRefresh() {
            this.aa();
        }
        refresh() {
            this.ag();
        }
        indexOfItem(a) {
            if (this.t == null) {
                return -1;
            }
            return this.t.indexOf(a);
        }
        indexOfKey(a) {
            return -1;
        }
    }
    DataSourceLocalDataProvider.$t = /*@__PURE__*/ markType(DataSourceLocalDataProvider, 'DataSourceLocalDataProvider', Base.$, [IDataSourceLocalDataProvider_$type]);
    return DataSourceLocalDataProvider;
})();
//# sourceMappingURL=DataSourceLocalDataProvider.js.map
