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