/*
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 { LegendSizeChangedEventArgs } from "./LegendSizeChangedEventArgs";
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.bd = null;
        _this.bf = null;
        _this.ab = ItemLegend.$;
        _this.bd = _this.ak;
        var a = _this.au;
        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.bd.ao(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.bd.y(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.ba = function (a) {
        _super.prototype.ba.call(this, a);
        this.bd = a;
    };
    ItemLegend.prototype.addChildInOrder = function (a, b) {
        if (!this.ak.n()) {
            return;
        }
        this.bn(b);
    };
    ItemLegend.prototype.get_isItemwise = function () {
        return true;
    };
    ItemLegend.prototype.createItemwiseLegendItems = function (a, b) {
        this.a1(a, b);
    };
    ItemLegend.prototype.a1 = function (a, b) {
        var e_3, _a;
        this.ak.ag();
        this.clearLegendItemsForSeries(b);
        if (b == null || a == null || a.count == 0) {
            return;
        }
        var c = false;
        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.bg(f)) {
                        this.au.add(d);
                        c = true;
                        var g = new LegendItemInfo();
                        g.b = f;
                        g.c = d;
                        g.a = b;
                        if (f.legendLabel != null) {
                            g.d = f.legendLabel;
                        }
                        else {
                            g.d = f.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;
            }
        }
        if (c) {
            this.a9(new LegendSizeChangedEventArgs());
        }
        this.ak.ah();
    };
    ItemLegend.prototype.createLegendItemsInsert = function (a, b) {
        this.bm(a, b);
    };
    ItemLegend.prototype.bm = function (a, b) {
        var e_4, _a;
        this.ak.ag();
        try {
            var c = this.bh(b);
            if (b == null || a == null || a.count == 0) {
                return;
            }
            var d = false;
            try {
                for (var _b = __values(fromEnum(a)), _c = _b.next(); !_c.done; _c = _b.next()) {
                    var e = _c.value;
                    var f = typeCast(ContentControl.$, e);
                    if (f != null && f.content != null) {
                        var g = typeCast(ILegendContext_$type, f.content);
                        if (g != null && !this.bg(g)) {
                            this.au.insert(c, e);
                            d = true;
                            c++;
                            var h = new LegendItemInfo();
                            h.b = g;
                            h.c = e;
                            h.a = b;
                            h.d = g.legendLabel != null ? g.legendLabel.toString() : g.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;
                }
            }
            if (d) {
                this.a9(new LegendSizeChangedEventArgs());
            }
        }
        finally {
            this.ak.ah();
        }
    };
    ItemLegend.prototype.renderItemwiseContent = function (a) {
        this.bn(a);
    };
    ItemLegend.prototype.bn = function (a) {
        var _this = this;
        this.clearLegendItemsForSeries(a);
        if (a.hasSubItems) {
            var b_1 = this.bi(a);
            var c_1 = false;
            a.forSubItems(function (d) {
                var e = typeCast(ContentControl.$, d);
                if (e != null && e.content != null) {
                    var f = typeCast(ILegendContext_$type, e.content);
                    if (f != null && !_this.bg(f)) {
                        _this.au.insert(b_1, d);
                        c_1 = true;
                        var g = new LegendItemInfo();
                        g.b = f;
                        g.c = d;
                        g.a = a;
                        g.d = f.legendLabel != null ? f.legendLabel.toString() : f.itemLabel;
                        b_1++;
                    }
                }
            });
            if (c_1) {
                this.a9(new LegendSizeChangedEventArgs());
            }
        }
    };
    ItemLegend.prototype.bi = function (a) {
        var e_5, _a;
        var b = 0;
        var c = a.resolveLegendIndex();
        try {
            for (var _b = __values(fromEnum(this.au)), _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);
                    var g = f.series;
                    var h = g.resolveLegendIndex();
                    if (c == h) {
                        if (!a.isLayer) {
                            return b;
                        }
                    }
                    else if (h > c) {
                        return b;
                    }
                }
                b++;
            }
        }
        catch (e_5_1) {
            e_5 = { error: e_5_1 };
        }
        finally {
            try {
                if (_c && !_c.done && (_a = _b.return))
                    _a.call(_b);
            }
            finally {
                if (e_5)
                    throw e_5.error;
            }
        }
        return b;
    };
    ItemLegend.prototype.bh = function (a) {
        var e_6, _a, e_7, _b;
        if (a == null || this.au == null || this.au.count == 0) {
            return 0;
        }
        var b = new ObservableCollection$1(UIElement.$, 0);
        var c = -1;
        var d = 0;
        try {
            for (var _c = __values(fromEnum(this.au)), _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_6_1) {
            e_6 = { error: e_6_1 };
        }
        finally {
            try {
                if (_d && !_d.done && (_a = _c.return))
                    _a.call(_c);
            }
            finally {
                if (e_6)
                    throw e_6.error;
            }
        }
        try {
            for (var _e = __values(fromEnum(b)), _f = _e.next(); !_f.done; _f = _e.next()) {
                var h = _f.value;
                this.au.remove(h);
            }
        }
        catch (e_7_1) {
            e_7 = { error: e_7_1 };
        }
        finally {
            try {
                if (_f && !_f.done && (_b = _e.return))
                    _b.call(_e);
            }
            finally {
                if (e_7)
                    throw e_7.error;
            }
        }
        if (b.count > 0) {
            this.a9(new LegendSizeChangedEventArgs());
        }
        if (c == -1) {
            return this.au.count;
        }
        return c;
    };
    ItemLegend.prototype.bg = function (a) {
        return this.bd.aq(a);
    };
    Object.defineProperty(ItemLegend.prototype, "be", {
        get: function () {
            return this.ak.l ? 0 : 1;
        },
        set: function (a) {
            var b = this.ak.l;
            this.ak.l = a == 0;
            if (b != this.ak.l) {
                this.a9(new LegendSizeChangedEventArgs());
            }
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(ItemLegend.prototype, "bk", {
        get: function () {
            return this.ak.w;
        },
        set: function (a) {
            this.ak.w = a;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(ItemLegend.prototype, "bl", {
        get: function () {
            return this.ak.x;
        },
        set: function (a) {
            var b = this.ak.x;
            this.ak.x = a;
            if (b != a) {
                this.a9(new LegendSizeChangedEventArgs());
            }
        },
        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.ap = null;
        _this.ap = a;
        return _this;
    }
    ItemLegendView.prototype.al = function () {
        _super.prototype.al.call(this);
    };
    ItemLegendView.prototype.get_k = function () {
        return true;
    };
    ItemLegendView.prototype.aq = function (a) {
        return this.d.e(a);
    };
    ItemLegendView.prototype.g = function () {
        var a = new LegendVisualData();
        var b = this.d.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.i(f);
            e.badgeBounds = this.h(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);
                a.items._inner[g].appearance.stroke = this.b(g);
            }
        }
        return a;
    };
    ItemLegendView.$t = markType(ItemLegendView, 'ItemLegendView', LegendBaseView.$);
    return ItemLegendView;
}(LegendBaseView));
export { ItemLegendView };
