/*
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, __values } from "tslib";
import { LegendBase } from "./LegendBase";
import { IChartItemLegend_$type } from "igniteui-angular-core";
import { LegendBaseView } from "./LegendBaseView";
import { fromEn, delegateCombine, typeCast, fromEnum, markType } from "igniteui-angular-core";
import { ObservableCollection$1 } from "igniteui-angular-core";
import { UIElement } from "igniteui-angular-core";
import { ContentControl } from "igniteui-angular-core";
import { ILegendContext_$type } from "igniteui-angular-core";
import { LegendItemInfo } from "./LegendItemInfo";
import { LegendVisualData } from "./LegendVisualData";
import { LegendItemVisualData } from "./LegendItemVisualData";
import { RectData } from "igniteui-angular-core";
/**
 * @hidden
 */
var ItemLegend = /** @class */ /*@__PURE__*/ (function (_super) {
    __extends(ItemLegend, _super);
    function ItemLegend() {
        var _this = _super.call(this) || this;
        _this.bb = null;
        _this.bd = null;
        _this.ab = ItemLegend.$;
        _this.bb = _this.ak;
        var a = _this.at;
        a.collectionChanged = delegateCombine(a.collectionChanged, function (a, b) {
            var e_1, _a, e_2, _b;
            if (b.oldItems != null) {
                try {
                    for (var _c = __values(fromEn(b.oldItems)), _d = _c.next(); !_d.done; _d = _c.next()) {
                        var c = _d.value;
                        _this.bb.an(c);
                    }
                }
                catch (e_1_1) {
                    e_1 = { error: e_1_1 };
                }
                finally {
                    try {
                        if (_d && !_d.done && (_a = _c.return))
                            _a.call(_c);
                    }
                    finally {
                        if (e_1)
                            throw e_1.error;
                    }
                }
            }
            if (b.newItems != null) {
                try {
                    for (var _e = __values(fromEn(b.newItems)), _f = _e.next(); !_f.done; _f = _e.next()) {
                        var d = _f.value;
                        _this.bb.x(d);
                    }
                }
                catch (e_2_1) {
                    e_2 = { error: e_2_1 };
                }
                finally {
                    try {
                        if (_f && !_f.done && (_b = _e.return))
                            _b.call(_e);
                    }
                    finally {
                        if (e_2)
                            throw e_2.error;
                    }
                }
            }
        });
        return _this;
    }
    ItemLegend.prototype.aj = function () {
        return new ItemLegendView(this);
    };
    ItemLegend.prototype.a8 = function (a) {
        _super.prototype.a8.call(this, a);
        this.bb = a;
    };
    ItemLegend.prototype.addChildInOrder = function (a, b) {
        if (!this.ak.m()) {
            return;
        }
        this.bi(b);
    };
    ItemLegend.prototype.get_isItemwise = function () {
        return true;
    };
    ItemLegend.prototype.createItemwiseLegendItems = function (a, b) {
        this.a0(a, b);
    };
    ItemLegend.prototype.a0 = function (a, b) {
        var e_3, _a;
        this.ak.af();
        this.clearLegendItemsForSeries(b);
        if (b == null || a == null || a.count == 0) {
            return;
        }
        try {
            for (var _b = __values(fromEnum(a)), _c = _b.next(); !_c.done; _c = _b.next()) {
                var c = _c.value;
                var d = typeCast(ContentControl.$, c);
                if (d != null && d.content != null) {
                    var e = typeCast(ILegendContext_$type, d.content);
                    if (e != null && !this.be(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 (_c && !_c.done && (_a = _b.return))
                    _a.call(_b);
            }
            finally {
                if (e_3)
                    throw e_3.error;
            }
        }
        this.ak.ag();
    };
    ItemLegend.prototype.createLegendItemsInsert = function (a, b) {
        this.bh(a, b);
    };
    ItemLegend.prototype.bh = function (a, b) {
        var e_4, _a;
        this.ak.af();
        try {
            var c = this.bf(b);
            if (b == null || a == null || a.count == 0) {
                return;
            }
            try {
                for (var _b = __values(fromEnum(a)), _c = _b.next(); !_c.done; _c = _b.next()) {
                    var d = _c.value;
                    var e = typeCast(ContentControl.$, d);
                    if (e != null && e.content != null) {
                        var f = typeCast(ILegendContext_$type, e.content);
                        if (f != null && !this.be(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 (_c && !_c.done && (_a = _b.return))
                        _a.call(_b);
                }
                finally {
                    if (e_4)
                        throw e_4.error;
                }
            }
        }
        finally {
            this.ak.ag();
        }
    };
    ItemLegend.prototype.renderItemwiseContent = function (a) {
        this.bi(a);
    };
    ItemLegend.prototype.bi = 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.be(d)) {
                        _this.at.add(b);
                        var e = new LegendItemInfo();
                        e.b = d;
                        e.c = b;
                        e.a = a;
                        e.d = d.itemLabel;
                    }
                }
            });
        }
    };
    ItemLegend.prototype.bf = function (a) {
        var e_5, _a, e_6, _b;
        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 _c = __values(fromEnum(this.at)), _d = _c.next(); !_d.done; _d = _c.next()) {
                var e = _d.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 (_d && !_d.done && (_a = _c.return))
                    _a.call(_c);
            }
            finally {
                if (e_5)
                    throw e_5.error;
            }
        }
        try {
            for (var _e = __values(fromEnum(b)), _f = _e.next(); !_f.done; _f = _e.next()) {
                var h = _f.value;
                this.at.remove(h);
            }
        }
        catch (e_6_1) {
            e_6 = { error: e_6_1 };
        }
        finally {
            try {
                if (_f && !_f.done && (_b = _e.return))
                    _b.call(_e);
            }
            finally {
                if (e_6)
                    throw e_6.error;
            }
        }
        if (c == -1) {
            return this.at.count;
        }
        return c;
    };
    ItemLegend.prototype.be = function (a) {
        return this.bb.ap(a);
    };
    Object.defineProperty(ItemLegend.prototype, "bc", {
        get: function () {
            return this.ak.k ? 0 : 1;
        },
        set: function (a) {
            this.ak.k = a == 0;
        },
        enumerable: false,
        configurable: true
    });
    ItemLegend.$t = markType(ItemLegend, 'ItemLegend', LegendBase.$, [IChartItemLegend_$type]);
    return ItemLegend;
}(LegendBase));
export { ItemLegend };
/**
 * @hidden
 */
var ItemLegendView = /** @class */ /*@__PURE__*/ (function (_super) {
    __extends(ItemLegendView, _super);
    function ItemLegendView(a) {
        var _this = _super.call(this, a) || this;
        _this.ao = null;
        _this.ao = a;
        return _this;
    }
    ItemLegendView.prototype.ak = function () {
        _super.prototype.ak.call(this);
    };
    ItemLegendView.prototype.get_j = function () {
        return true;
    };
    ItemLegendView.prototype.ap = function (a) {
        return this.c.e(a);
    };
    ItemLegendView.prototype.f = function () {
        var a = new LegendVisualData();
        var b = this.c.g.rootWrapper;
        b = b.getChildAt(0);
        var c = b.getChildCount();
        for (var d = 0; d < c; d++) {
            var e = new LegendItemVisualData();
            var f = b.getChildAt(d);
            e.label = f.getText();
            e.label = e.label != null ? e.label.trim() : null;
            e.bounds = new RectData(f.getOffset().left, f.getOffset().top, f.width(), f.height());
            e.labelBounds = this.h(f);
            e.badgeBounds = this.g(f);
            a.items.add(e);
        }
        if (c == a.items.count) {
            for (var g = 0; g < c; g++) {
                a.items._inner[g].appearance.fill = this.a(g);
            }
        }
        return a;
    };
    ItemLegendView.$t = markType(ItemLegendView, 'ItemLegendView', LegendBaseView.$);
    return ItemLegendView;
}(LegendBaseView));
export { ItemLegendView };
