/*
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, INotifyPropertyChanged_$type, PropertyChangedEventArgs, runOn, delegateCombine, typeCast, delegateRemove, String_$type, fromEnum, Number_$type, Array_$type, markType } from "./type";
import { IDataSourceLocalDataProvider_$type } from "./IDataSourceLocalDataProvider";
import { DataSourceLocalDataProvider } from "./DataSourceLocalDataProvider";
import { List$1 } from "./List$1";
import { SummaryDescription } from "./SummaryDescription";
import { Tuple$2 } from "./Tuple$2";
import { AggregatedDataProviderLayerMap } from "./AggregatedDataProviderLayerMap";
import { AggregatedDataProviderLayerInfo } from "./AggregatedDataProviderLayerInfo";
import { IDataSourceUpdateNotifier_$type } from "./IDataSourceUpdateNotifier";
/**
 * @hidden
 */
export let AggregatedDataProvider = /*@__PURE__*/ (() => {
    class AggregatedDataProvider extends Base {
        constructor() {
            super();
            this.g = new DataSourceLocalDataProvider();
            this.c = null;
            this.r = null;
            this.j = false;
            this.q = null;
            this.i = false;
            this.schemaChanged = null;
            this.propertyChanged = null;
            this.h = false;
            this.k = true;
            this.o = new List$1(String_$type, 0);
            let a = this.g;
            a.schemaChanged = delegateCombine(a.schemaChanged, runOn(this, this.ab));
        }
        ab(a, b) {
            if (this.schemaChanged != null) {
                this.schemaChanged(this, b);
            }
        }
        get f() {
            return this.c;
        }
        set f(a) {
            let b = this.c;
            this.c = a;
            if (b != this.c) {
                this.ai("TargetDataSource", b, this.c);
            }
        }
        get w() {
            return this.r;
        }
        set w(a) {
            let b = this.r;
            this.r = a;
            if (b != this.r) {
                this.ai("SchemaHints", b, this.r);
            }
        }
        get m() {
            return this.j;
        }
        set m(a) {
            let b = this.j;
            this.j = a;
            if (b != this.j) {
                this.ai("IncludeParentSummary", b, this.r);
            }
        }
        get actualSchema() {
            return this.g.actualSchema;
        }
        get executionContext() {
            return this.g.executionContext;
        }
        set executionContext(a) {
            this.g.executionContext = a;
        }
        get updateNotifier() {
            return this.g.updateNotifier;
        }
        set updateNotifier(a) {
            this.g.updateNotifier = a;
        }
        get dataSource() {
            return this.q;
        }
        set dataSource(a) {
            let b = this.q;
            this.q = a;
            if (b != this.q) {
                this.ai("ItemsSource", b, this.q);
            }
        }
        get deferAutoRefresh() {
            return this.i;
        }
        set deferAutoRefresh(a) {
            let b = this.i;
            this.i = a;
            if (b != this.i) {
                this.ai("DeferAutoRefresh", b, this.i);
            }
        }
        get isSortingSupported() {
            return this.g.isSortingSupported;
        }
        get isGroupingSupported() {
            return this.g.isGroupingSupported;
        }
        get isFilteringSupported() {
            return this.g.isFilteringSupported;
        }
        get sortDescriptions() {
            return this.g.sortDescriptions;
        }
        get groupDescriptions() {
            return this.g.groupDescriptions;
        }
        get summaryDescriptions() {
            return this.g.summaryDescriptions;
        }
        get summaryScope() {
            return this.g.summaryScope;
        }
        set summaryScope(a) {
            this.g.summaryScope = a;
        }
        get filterExpressions() {
            return this.g.filterExpressions;
        }
        get propertiesRequested() {
            return this.g.propertiesRequested;
        }
        set propertiesRequested(a) {
            this.g.propertiesRequested = a;
        }
        get notifyUsingSourceIndexes() {
            return true;
        }
        get isItemIndexLookupSupported() {
            return this.g.isItemIndexLookupSupported;
        }
        get isKeyIndexLookupSupported() {
            return this.g.isKeyIndexLookupSupported;
        }
        get actualCount() {
            return this.g.actualCount;
        }
        ai(a, b, c) {
            if (this.propertyChanged != null) {
                this.propertyChanged(this, new PropertyChangedEventArgs(a));
            }
            this.ao(a, b, c);
        }
        ao(a, b, c) {
            switch (a) {
                case "ItemsSource":
                    this.g.dataSource = c;
                    break;
                case "DeferAutoRefresh":
                    if (!c) {
                        this.queueAutoRefresh();
                    }
                    break;
                case "TargetDataSource":
                    if (b != null) {
                        let d = b;
                        if (d.updateNotifier != null) {
                            if (typeCast(AggregatedDataProviderTargetDataSourceNotifier.$, d.updateNotifier) !== null) {
                                d.updateNotifier = d.updateNotifier.c;
                            }
                        }
                        d.schemaChanged = delegateRemove(d.schemaChanged, runOn(this, this.ah));
                    }
                    if (c != null) {
                        let e = c;
                        this.g.executionContext = e.executionContext;
                        let f = new AggregatedDataProviderTargetDataSourceNotifier(this, e.updateNotifier);
                        e.updateNotifier = f;
                        e.schemaChanged = delegateCombine(e.schemaChanged, runOn(this, this.ah));
                    }
                    this.queueAutoRefresh();
                    break;
            }
        }
        ah(a, b) {
            this.k = true;
            this.queueAutoRefresh();
        }
        addItem(a) {
            this.g.addItem(a);
        }
        queueAutoRefresh() {
            if (this.deferAutoRefresh) {
                return;
            }
            if (this.h) {
                return;
            }
            if (this.executionContext != null) {
                this.h = true;
                this.executionContext.enqueueAction(runOn(this, this.ae));
            }
        }
        ae() {
            if (this.deferAutoRefresh) {
                this.h = false;
                return;
            }
            if (!this.h) {
                return;
            }
            this.h = false;
            this.ap();
        }
        ap() {
            this.aq();
        }
        aq() {
            if (this.c == null) {
                return;
            }
            if (!this.k) {
                return;
            }
            this.k = false;
            let a = this.c.clone();
            let b = this.c.clone();
            a = this.d(a);
            b = this.e(b);
            a.flushAutoRefresh();
            b.flushAutoRefresh();
            this.o.clear();
            let c = this.a(b, a, 0, 0);
            this.t(c, this.o);
            this.dataSource = c;
        }
        t(a, b) {
            if (b.count == 0) {
                return null;
            }
            for (let d = 0; d < a.length; d++) {
                let c = a[d];
                let e = c;
                for (let f of fromEnum(b)) {
                    if (e[f] == null) {
                        e[f] = NaN;
                    }
                }
            }
            return null;
        }
        d(a) {
            a.shouldEmitSummaryRows = false;
            a.includeSummaryRowsInSection = false;
            a.summaryScope = 2;
            a.isSectionSummaryRowsAtBottom = true;
            a.sectionHeaderDisplayMode = 0;
            if (a.summaryDescriptions.k.count == 0) {
                for (let b = 1; b < a.groupDescriptions.k.count; b++) {
                    a.summaryDescriptions.k.add(new SummaryDescription(2, a.groupDescriptions.k.item(b).propertyName, 4));
                }
            }
            return a;
        }
        e(a) {
            a.shouldEmitSummaryRows = false;
            a.includeSummaryRowsInSection = false;
            a.summaryScope = 2;
            a.isSectionSummaryRowsAtBottom = true;
            a.sectionHeaderDisplayMode = 0;
            while (a.groupDescriptions.k.count > 1) {
                a.groupDescriptions.removeAt(a.groupDescriptions.k.count - 1);
            }
            if (a.summaryDescriptions.k.count == 0) {
                for (let b = 0; b < a.groupDescriptions.k.count; b++) {
                    a.summaryDescriptions.k.add(new SummaryDescription(2, a.groupDescriptions.k.item(b).propertyName, 4));
                }
            }
            return a;
        }
        flushAutoRefresh() {
            this.ae();
        }
        refresh() {
            this.ap();
        }
        getItemAtIndex(a) {
            return this.g.getItemAtIndex(a);
        }
        getItemValue(a, b) {
            return this.g.getItemValue(a, b);
        }
        indexOfItem(a) {
            return this.g.indexOfItem(a);
        }
        indexOfKey(a) {
            return this.g.indexOfKey(a);
        }
        removeItem(a) {
            this.g.removeItem(a);
        }
        resolveSchemaPropertyType(a) {
            return this.g.resolveSchemaPropertyType(a);
        }
        setItemValue(a, b, c) {
            this.g.setItemValue(a, b, c);
        }
        notifyClearItems() {
            if (this.f != null) {
                this.f.notifyClearItems();
            }
        }
        notifyInsertItem(a, b) {
            if (this.f != null) {
                this.f.notifyInsertItem(a, b);
            }
        }
        notifyRemoveItem(a, b) {
            if (this.f != null) {
                this.f.notifyRemoveItem(a, b);
            }
        }
        notifySetItem(a, b, c) {
            if (this.f != null) {
                this.f.notifySetItem(a, b, c);
            }
        }
        p(a) {
            return a.groupDescriptions.k.count;
        }
        a(a, b, c, d) {
            if (d == this.p(b)) {
                let e = this.z(b, c, d);
                return e.d;
            }
            if (d == 0) {
                let f = new AggregatedDataProviderLayerMap();
                let g = this.aa(f, a, c, d);
                g = this.aa(f, b, c, d);
                return g.d;
            }
            return new Array(0);
        }
        aa(a, b, c, d) {
            let e = false;
            let f = 0;
            let g = null;
            for (let h = c; h < b.actualCount; h++) {
                let i = b.getRowType(h);
                if (i == 3 || i == 6) {
                    continue;
                }
                if (!e) {
                    if (i == 0) {
                        e = true;
                    }
                    if (i == 1) {
                        e = true;
                        f = 0;
                        g = this.b(a, b, h, d);
                        this.ag(a, b, g, h, d);
                        continue;
                    }
                }
                let j = b.getItemAtIndex(h);
                if (g != null && i == 0) {
                    g.k.add1(j);
                }
                if (g != null && this.l(b, j, h)) {
                    if (b.getRowType(h) == 1) {
                        h--;
                    }
                    e = false;
                    g = null;
                }
            }
            return new Tuple$2(Number_$type, Array_$type, b.actualCount, a.a(this.o));
        }
        ag(a, b, c, d, e) {
            let f = b.getRowType(d);
            if (f == 1) {
                let g = b.getItemAtIndex(d);
                if (c.i.count == 1) {
                    this.ad(a, b, g, c);
                }
                else {
                    this.ac(b, g, c);
                }
            }
        }
        af(a, b, c, d, e) {
            let f = b.getRowType(d);
            if (f == 5) {
                let g = b.getItemAtIndex(d);
                if (this.n(g, c)) {
                    if (c.i.count == 1) {
                        this.ad(a, b, g, c);
                    }
                    else {
                        this.ac(b, g, c);
                    }
                }
            }
            let h = 1;
            while (d + h < b.actualCount && b.getRowType(d + h) == 5) {
                let i = b.getRowType(d + h);
                if (i == 5) {
                    let j = b.getItemAtIndex(d + h);
                    if (c.i.count == 1) {
                        this.ad(a, b, j, c);
                    }
                    else {
                        this.ac(b, j, c);
                    }
                }
                h++;
            }
        }
        ad(a, b, c, d) {
            let e = a.b(d.j._inner[0]);
            let f = c.summaryResults;
            if (e != null) {
                for (let g = 0; g < f.length; g++) {
                    let h = f[g];
                    e.y(h.propertyName, b.summaryDescriptions.k.item(g).alias, h.operand, h.value);
                }
            }
        }
        ac(a, b, c) {
            let d = b.summaryResults;
            for (let e = 0; e < d.length; e++) {
                let f = d[e];
                c.p(f.propertyName, a.summaryDescriptions.k.item(e).alias, f.operand, f.value);
            }
        }
        n(a, b) {
            return a.level == 1;
        }
        b(a, b, c, d) {
            let e = b.getItemAtIndex(c);
            let f = e.getSectionKeys();
            let g = new List$1(String_$type, 0);
            for (let h = 0; h < f.length; h++) {
                g.add(f[h]);
            }
            let i = new List$1(Base.$, 0);
            for (let j = 0; j < g.count; j++) {
                i.add1(e.getSectionValue(g._inner[j]));
            }
            let k = new AggregatedDataProviderLayerInfo();
            k.i = g;
            k.j = i;
            a.f(k);
            return k;
        }
        z(a, b, c) {
            let d = new List$1(Base.$, 0);
            let e = false;
            for (let f = b; f < a.actualCount; f++) {
                let g = a.getRowType(f);
                if (g == 3 || g == 6) {
                    continue;
                }
                if (!e) {
                    if (g == 0) {
                        e = true;
                    }
                    if (g == 1) {
                        e = true;
                        continue;
                    }
                }
                let h = a.getItemAtIndex(f);
                d.add1(h);
                if (this.l(a, h, f)) {
                    return new Tuple$2(Number_$type, Array_$type, f, d.toArray());
                }
            }
            return new Tuple$2(Number_$type, Array_$type, a.actualCount, d.toArray());
        }
        l(a, b, c) {
            if (a.getRowType(c) != 0) {
                return true;
            }
            return false;
        }
        aj() {
            this.k = true;
            this.queueAutoRefresh();
        }
        ak(a, b) {
            this.k = true;
            this.queueAutoRefresh();
        }
        al(a, b) {
            this.k = true;
            this.queueAutoRefresh();
        }
        am(a, b, c) {
            this.k = true;
            this.queueAutoRefresh();
        }
        an(a, b) {
            this.k = true;
            this.queueAutoRefresh();
        }
    }
    AggregatedDataProvider.$t = markType(AggregatedDataProvider, 'AggregatedDataProvider', Base.$, [IDataSourceLocalDataProvider_$type, INotifyPropertyChanged_$type]);
    return AggregatedDataProvider;
})();
/**
 * @hidden
 */
export let AggregatedDataProviderTargetDataSourceNotifier = /*@__PURE__*/ (() => {
    class AggregatedDataProviderTargetDataSourceNotifier extends Base {
        constructor(a, b) {
            super();
            this.a = null;
            this.b = null;
            this.a = a;
            this.b = b;
        }
        get c() {
            return this.b;
        }
        notifyClearItems() {
            this.a.aj();
            if (this.b != null) {
                this.b.notifyClearItems();
            }
        }
        notifyInsertItem(a, b) {
            this.a.ak(a, b);
            if (this.b != null) {
                this.b.notifyInsertItem(a, b);
            }
        }
        notifyRemoveItem(a, b) {
            this.a.al(a, b);
            if (this.b != null) {
                this.b.notifyRemoveItem(a, b);
            }
        }
        notifySetItem(a, b, c) {
            this.a.am(a, b, c);
            if (this.b != null) {
                this.b.notifySetItem(a, b, c);
            }
        }
        rangeActualized(a, b) {
            this.a.an(a, b);
            if (this.b != null) {
                this.b.rangeActualized(a, b);
            }
        }
    }
    AggregatedDataProviderTargetDataSourceNotifier.$t = markType(AggregatedDataProviderTargetDataSourceNotifier, 'AggregatedDataProviderTargetDataSourceNotifier', Base.$, [IDataSourceUpdateNotifier_$type]);
    return AggregatedDataProviderTargetDataSourceNotifier;
})();
