/*
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 { 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
 */
var DataSourceLocalDataProvider = /** @class */ (function (_super) {
    __extends(DataSourceLocalDataProvider, _super);
    function DataSourceLocalDataProvider() {
        var _this = _super.call(this) || this;
        _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));
        return _this;
    }
    DataSourceLocalDataProvider.prototype.ai = function (a, b) {
        this.queueAutoRefresh();
    };
    Object.defineProperty(DataSourceLocalDataProvider.prototype, "dataSource", {
        get: function () {
            return this.v;
        },
        set: function (a) {
            var b = this.v;
            this.v = a;
            if (b != this.v) {
                this.ae("ItemsSource", b, this.v);
            }
        },
        enumerable: true,
        configurable: true
    });
    DataSourceLocalDataProvider.prototype.getItemAtIndex = function (a) {
        if (this.t == null) {
            return null;
        }
        if (this.o) {
            return this.t[a];
        }
        return this.t.item(a);
    };
    DataSourceLocalDataProvider.prototype.getItemValue = function (a, b) {
        this.ab();
        var c;
        var d = this.q.tryGetValue(b, c);
        c = d.p1;
        if (c == null) {
            return null;
        }
        var val_ = c.getPropertyValue(a);
        if ((val_ === undefined)) {
            return null;
        }
        return val_;
    };
    DataSourceLocalDataProvider.prototype.ab = 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.t == null) {
                return 0;
            }
            if (this.o) {
                return (this.t.length);
            }
            return this.t.count;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(DataSourceLocalDataProvider.prototype, "actualSchema", {
        get: function () {
            if (this.h == null) {
                if (this.t == null) {
                    this.h = new DefaultDataSourceSchema([], [], null, null);
                }
                else {
                    this.h = this.i();
                }
            }
            return this.h;
        },
        enumerable: true,
        configurable: true
    });
    DataSourceLocalDataProvider.prototype.i = function () {
        var a = this.w();
        if (a == null) {
            return new DefaultDataSourceSchema([], [], null, null);
        }
        this.q.clear();
        var b = new List$1(String_$type, 0);
        var c = new HashSet$1(String_$type, 0);
        var d = new List$1(DataSourceSchemaPropertyType_$type, 0);
        var e = new List$1(List$1.$.specialize(String_$type), 0);
        var f = this.r(a);
        for (var g = 0; g < f.count; g++) {
            var 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));
            }
        }
        var i = new Array(e.count);
        for (var j = 0; j < e.count; j++) {
            i[j] = e._inner[j].toArray();
        }
        return new DefaultDataSourceSchema(b.toArray(), d.toArray(), null, i);
    };
    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.w();
            var e = this.getItemValue(d, a);
            if (e == null) {
                return 11;
            }
            else {
                return DataSourceLocalDataProvider.c(e);
            }
        }
    };
    DataSourceLocalDataProvider.prototype.r = 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.ad = function (a, item_, propertyName_, b) {
        if ((item_.__dataIntents !== undefined)) {
            if ((item_.__dataIntents[propertyName_] !== undefined)) {
                this.ac(a, item_, propertyName_);
            }
            var selfName_ = "self." + propertyName_;
            if (!b && (item_.__dataIntents[selfName_] !== undefined)) {
                this.ac(a, item_, selfName_);
            }
            var selfName2_ = "self_" + propertyName_;
            if (!b && (item_.__dataIntents[selfName2_] !== undefined)) {
                this.ac(a, item_, selfName2_);
            }
        }
    };
    DataSourceLocalDataProvider.prototype.ac = function (a, item_, propertyName_) {
        var b = item_.__dataIntents[propertyName_];
        if (typeCast(Array_$type, b) !== null) {
            var c = b;
            for (var d = 0; d < c.length; d++) {
                if (c[d] != null) {
                    a.add(c[d].toString());
                }
            }
        }
    };
    DataSourceLocalDataProvider.prototype.s = function (item_, propertyName_) {
        var a = new List$1(String_$type, 0);
        var list_ = this.t;
        var dataIntents_ = null;
        this.ad(a, item_, propertyName_, false);
        this.ad(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.w = function () {
        var a = 0;
        if (this.o) {
            a = (this.t.length);
        }
        else {
            a = this.t.count;
        }
        for (var b = 0; b < a; b++) {
            var c = void 0;
            if (this.o) {
                c = this.t[b];
            }
            else {
                c = this.t.item(b);
            }
            if (c != null) {
                return c;
            }
        }
        return null;
    };
    DataSourceLocalDataProvider.prototype.ae = function (a, b, c) {
        this.af(a, b, c);
    };
    Object.defineProperty(DataSourceLocalDataProvider.prototype, "u", {
        get: function () {
            return this.t;
        },
        enumerable: true,
        configurable: true
    });
    DataSourceLocalDataProvider.prototype.af = function (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;
        }
    };
    Object.defineProperty(DataSourceLocalDataProvider.prototype, "executionContext", {
        get: function () {
            return this.g;
        },
        set: function (a) {
            this.g = a;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(DataSourceLocalDataProvider.prototype, "p", {
        get: function () {
            return false;
        },
        enumerable: true,
        configurable: true
    });
    DataSourceLocalDataProvider.prototype.notifySetItem = function (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);
        }
    };
    DataSourceLocalDataProvider.prototype.notifyClearItems = function () {
        if (this.p) {
            if (this.o) {
                this.t.length = 0;
            }
            else {
                this.t.clear();
            }
        }
        if (this.updateNotifier != null) {
            this.updateNotifier.notifyClearItems();
        }
    };
    DataSourceLocalDataProvider.prototype.notifyInsertItem = function (a, b) {
        if (this.p) {
            if (this.o) {
                this.t.splice(a, 0, b);
            }
            else {
                this.t.insert(a, b);
            }
        }
        var 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);
        }
    };
    DataSourceLocalDataProvider.prototype.notifyRemoveItem = function (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);
        }
    };
    Object.defineProperty(DataSourceLocalDataProvider.prototype, "updateNotifier", {
        get: function () {
            return this.f;
        },
        set: function (a) {
            this.f = a;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(DataSourceLocalDataProvider.prototype, "deferAutoRefresh", {
        get: function () {
            return this.n;
        },
        set: function (a) {
            var b = this.n;
            this.n = a;
            if (this.n != b) {
                this.ae("DeferAutoRefresh", b, this.n);
            }
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(DataSourceLocalDataProvider.prototype, "isSortingSupported", {
        get: function () {
            return false;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(DataSourceLocalDataProvider.prototype, "isFilteringSupported", {
        get: function () {
            return false;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(DataSourceLocalDataProvider.prototype, "sortDescriptions", {
        get: function () {
            return this.k;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(DataSourceLocalDataProvider.prototype, "filterExpressions", {
        get: function () {
            return this.e;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(DataSourceLocalDataProvider.prototype, "summaryDescriptions", {
        get: function () {
            return this.l;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(DataSourceLocalDataProvider.prototype, "summaryScope", {
        get: function () {
            return this.d;
        },
        set: function (a) {
            this.d = a;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(DataSourceLocalDataProvider.prototype, "notifyUsingSourceIndexes", {
        get: function () {
            return true;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(DataSourceLocalDataProvider.prototype, "propertiesRequested", {
        get: function () {
            return this.a;
        },
        set: function (a) {
            this.a = a;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(DataSourceLocalDataProvider.prototype, "isItemIndexLookupSupported", {
        get: function () {
            return true;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(DataSourceLocalDataProvider.prototype, "isKeyIndexLookupSupported", {
        get: function () {
            return false;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(DataSourceLocalDataProvider.prototype, "isGroupingSupported", {
        get: function () {
            return false;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(DataSourceLocalDataProvider.prototype, "groupDescriptions", {
        get: function () {
            return this.j;
        },
        enumerable: true,
        configurable: true
    });
    DataSourceLocalDataProvider.prototype.queueAutoRefresh = function () {
        if (this.deferAutoRefresh) {
            return;
        }
        if (this.m) {
            return;
        }
        if (this.executionContext != null) {
            this.m = true;
            this.executionContext.enqueueAction(runOn(this, this.aa));
        }
    };
    DataSourceLocalDataProvider.prototype.aa = function () {
        if (this.deferAutoRefresh) {
            this.m = false;
            return;
        }
        if (!this.m) {
            return;
        }
        this.m = false;
        this.ag();
    };
    DataSourceLocalDataProvider.prototype.ag = function () {
        this.ah();
    };
    DataSourceLocalDataProvider.prototype.ah = function () {
    };
    DataSourceLocalDataProvider.prototype.flushAutoRefresh = function () {
        this.aa();
    };
    DataSourceLocalDataProvider.prototype.refresh = function () {
        this.ag();
    };
    DataSourceLocalDataProvider.prototype.indexOfItem = function (a) {
        if (this.t == null) {
            return -1;
        }
        return this.t.indexOf(a);
    };
    DataSourceLocalDataProvider.prototype.indexOfKey = function (a) {
        return -1;
    };
    DataSourceLocalDataProvider.$t = markType(DataSourceLocalDataProvider, 'DataSourceLocalDataProvider', Base.$, [IDataSourceLocalDataProvider_$type]);
    return DataSourceLocalDataProvider;
}(Base));
export { DataSourceLocalDataProvider };
