/*
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 * as tslib_1 from "tslib";
import { LegendBase } from "./LegendBase";
import { IChartItemLegend_$type } from "igniteui-react-core/ES5/IChartItemLegend";
import { LegendBaseView } from "./LegendBaseView";
import { fromEn, delegateCombine, typeCast, fromEnum, markType } from "igniteui-react-core/ES5/type";
import { ObservableCollection$1 } from "igniteui-react-core/ES5/ObservableCollection$1";
import { UIElement } from "igniteui-react-core/ES5/UIElement";
import { ContentControl } from "igniteui-react-core/ES5/ContentControl";
import { ILegendContext_$type } from "igniteui-react-core/ES5/ILegendContext";
import { LegendItemInfo } from "./LegendItemInfo";
var ItemLegend = /** @class */ (function (_super) {
    tslib_1.__extends(ItemLegend, _super);
    function ItemLegend() {
        var _this = _super.call(this) || this;
        _this.a9 = null;
        _this.ba = null;
        _this.ab = ItemLegend.$;
        var a = _this.at;
        a.collectionChanged = delegateCombine(a.collectionChanged, function (a, b) {
            if (b.oldItems != null) {
                try {
                    for (var _a = tslib_1.__values(fromEn(b.oldItems)), _b = _a.next(); !_b.done; _b = _a.next()) {
                        var c = _b.value;
                        _this.a9.ag(c);
                    }
                }
                catch (e_1_1) { e_1 = { error: e_1_1 }; }
                finally {
                    try {
                        if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
                    }
                    finally { if (e_1) throw e_1.error; }
                }
            }
            if (b.newItems != null) {
                try {
                    for (var _d = tslib_1.__values(fromEn(b.newItems)), _e = _d.next(); !_e.done; _e = _d.next()) {
                        var d = _e.value;
                        _this.a9.s(d);
                    }
                }
                catch (e_2_1) { e_2 = { error: e_2_1 }; }
                finally {
                    try {
                        if (_e && !_e.done && (_f = _d.return)) _f.call(_d);
                    }
                    finally { if (e_2) throw e_2.error; }
                }
            }
            var e_1, _c, e_2, _f;
        });
        return _this;
    }
    ItemLegend.prototype.aj = function () {
        return new ItemLegendView(this);
    };
    ItemLegend.prototype.a5 = function (a) {
        _super.prototype.a5.call(this, a);
        this.a9 = a;
    };
    ItemLegend.prototype.addChildInOrder = function (a, b) {
        if (!this.ak.i()) {
            return;
        }
        this.be(b);
    };
    ItemLegend.prototype.get_isItemwise = function () {
        return true;
    };
    Object.defineProperty(ItemLegend.prototype, "isItemwise", {
        get: function () {
            return this.get_isItemwise();
        },
        enumerable: true,
        configurable: true
    });
    ItemLegend.prototype.createItemwiseLegendItems = function (a, b) {
        this.az(a, b);
    };
    ItemLegend.prototype.az = function (a, b) {
        this.ak.aa();
        this.clearLegendItemsForSeries(b);
        if (b == null || a == null || a.count == 0) {
            return;
        }
        try {
            for (var _a = tslib_1.__values(fromEnum(a)), _b = _a.next(); !_b.done; _b = _a.next()) {
                var c = _b.value;
                var d = typeCast(ContentControl.$, c);
                if (d != null && d.content != null) {
                    var e = typeCast(ILegendContext_$type, d.content);
                    if (e != null && !this.bb(e)) {
                        this.at.add(c);
                        var f = new LegendItemInfo();
                        f.b = e;
                        f.c = c;
                        f.a = b;
                        if (e.legendLabel != null) {
                            f.d = e.legendLabel;
                        }
                        else {
                            f.d = e.itemLabel;
                        }
                    }
                }
            }
        }
        catch (e_3_1) { e_3 = { error: e_3_1 }; }
        finally {
            try {
                if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
            }
            finally { if (e_3) throw e_3.error; }
        }
        this.ak.ab();
        var e_3, _c;
    };
    ItemLegend.prototype.createLegendItemsInsert = function (a, b) {
        this.bd(a, b);
    };
    ItemLegend.prototype.bd = function (a, b) {
        this.ak.aa();
        var c = this.bc(b);
        if (b == null || a == null || a.count == 0) {
            return;
        }
        try {
            for (var _a = tslib_1.__values(fromEnum(a)), _b = _a.next(); !_b.done; _b = _a.next()) {
                var d = _b.value;
                var e = typeCast(ContentControl.$, d);
                if (e != null && e.content != null) {
                    var f = typeCast(ILegendContext_$type, e.content);
                    if (f != null && !this.bb(f)) {
                        this.at.insert(c, d);
                        c++;
                        var g = new LegendItemInfo();
                        g.b = f;
                        g.c = d;
                        g.a = b;
                        g.d = f.legendLabel != null ? f.legendLabel.toString() : f.itemLabel;
                    }
                }
            }
        }
        catch (e_4_1) { e_4 = { error: e_4_1 }; }
        finally {
            try {
                if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
            }
            finally { if (e_4) throw e_4.error; }
        }
        this.ak.ab();
        var e_4, _c;
    };
    ItemLegend.prototype.renderItemwiseContent = function (a) {
        this.be(a);
    };
    ItemLegend.prototype.be = function (a) {
        var _this = this;
        this.clearLegendItemsForSeries(a);
        if (a.hasSubItems) {
            a.forSubItems(function (b) {
                var c = typeCast(ContentControl.$, b);
                if (c != null && c.content != null) {
                    var d = typeCast(ILegendContext_$type, c.content);
                    if (d != null && !_this.bb(d)) {
                        _this.at.add(b);
                        var e = new LegendItemInfo();
                        e.b = d;
                        e.c = b;
                        e.a = a;
                        e.d = d.itemLabel;
                    }
                }
            });
        }
    };
    ItemLegend.prototype.bc = function (a) {
        if (a == null || this.at == null || this.at.count == 0) {
            return 0;
        }
        var b = new ObservableCollection$1(UIElement.$, 0);
        var c = -1;
        var d = 0;
        try {
            for (var _a = tslib_1.__values(fromEnum(this.at)), _b = _a.next(); !_b.done; _b = _a.next()) {
                var e = _b.value;
                var f = typeCast(ContentControl.$, e);
                if (f != null && f.content != null) {
                    var g = typeCast(ILegendContext_$type, f.content);
                    if (g != null && g.series == a) {
                        if (c == -1) {
                            c = d;
                        }
                        b.add(e);
                    }
                }
                d++;
            }
        }
        catch (e_5_1) { e_5 = { error: e_5_1 }; }
        finally {
            try {
                if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
            }
            finally { if (e_5) throw e_5.error; }
        }
        try {
            for (var _d = tslib_1.__values(fromEnum(b)), _e = _d.next(); !_e.done; _e = _d.next()) {
                var h = _e.value;
                this.at.remove(h);
            }
        }
        catch (e_6_1) { e_6 = { error: e_6_1 }; }
        finally {
            try {
                if (_e && !_e.done && (_f = _d.return)) _f.call(_d);
            }
            finally { if (e_6) throw e_6.error; }
        }
        if (c == -1) {
            return this.at.count;
        }
        return c;
        var e_5, _c, e_6, _f;
    };
    ItemLegend.prototype.bb = function (a) {
        return this.a9.ai(a);
    };
    ItemLegend.$t = markType(ItemLegend, 'ItemLegend', LegendBase.$, [IChartItemLegend_$type]);
    return ItemLegend;
}(LegendBase));
export { ItemLegend };
var ItemLegendView = /** @class */ (function (_super) {
    tslib_1.__extends(ItemLegendView, _super);
    function ItemLegendView(a) {
        var _this = _super.call(this, a) || this;
        _this.ah = null;
        _this.ah = a;
        return _this;
    }
    ItemLegendView.prototype.ad = function () {
        _super.prototype.ad.call(this);
    };
    ItemLegendView.prototype.get_f = function () {
        return true;
    };
    Object.defineProperty(ItemLegendView.prototype, "f", {
        get: function () {
            return this.get_f();
        },
        enumerable: true,
        configurable: true
    });
    ItemLegendView.prototype.ai = function (a) {
        return this.b.e(a);
    };
    ItemLegendView.$t = markType(ItemLegendView, 'ItemLegendView', LegendBaseView.$);
    return ItemLegendView;
}(LegendBaseView));
export { ItemLegendView };
