/*
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 { 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
 */
export let ItemLegend = /*@__PURE__*/ (() => {
    class ItemLegend extends LegendBase {
        constructor() {
            super();
            this.bb = null;
            this.bd = null;
            this.ab = ItemLegend.$;
            this.bb = this.ak;
            let a = this.at;
            a.collectionChanged = delegateCombine(a.collectionChanged, (a, b) => {
                if (b.oldItems != null) {
                    for (let c of fromEn(b.oldItems)) {
                        this.bb.an(c);
                    }
                }
                if (b.newItems != null) {
                    for (let d of fromEn(b.newItems)) {
                        this.bb.x(d);
                    }
                }
            });
        }
        aj() {
            return new ItemLegendView(this);
        }
        a8(a) {
            super.a8(a);
            this.bb = a;
        }
        addChildInOrder(a, b) {
            if (!this.ak.m()) {
                return;
            }
            this.bk(b);
        }
        get_isItemwise() {
            return true;
        }
        createItemwiseLegendItems(a, b) {
            this.a0(a, b);
        }
        a0(a, b) {
            this.ak.af();
            this.clearLegendItemsForSeries(b);
            if (b == null || a == null || a.count == 0) {
                return;
            }
            for (let c of fromEnum(a)) {
                let d = typeCast(ContentControl.$, c);
                if (d != null && d.content != null) {
                    let e = typeCast(ILegendContext_$type, d.content);
                    if (e != null && !this.be(e)) {
                        this.at.add(c);
                        let 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;
                        }
                    }
                }
            }
            this.ak.ag();
        }
        createLegendItemsInsert(a, b) {
            this.bj(a, b);
        }
        bj(a, b) {
            this.ak.af();
            try {
                let c = this.bf(b);
                if (b == null || a == null || a.count == 0) {
                    return;
                }
                for (let d of fromEnum(a)) {
                    let e = typeCast(ContentControl.$, d);
                    if (e != null && e.content != null) {
                        let f = typeCast(ILegendContext_$type, e.content);
                        if (f != null && !this.be(f)) {
                            this.at.insert(c, d);
                            c++;
                            let g = new LegendItemInfo();
                            g.b = f;
                            g.c = d;
                            g.a = b;
                            g.d = f.legendLabel != null ? f.legendLabel.toString() : f.itemLabel;
                        }
                    }
                }
            }
            finally {
                this.ak.ag();
            }
        }
        renderItemwiseContent(a) {
            this.bk(a);
        }
        bk(a) {
            this.clearLegendItemsForSeries(a);
            if (a.hasSubItems) {
                a.forSubItems((b) => {
                    let c = typeCast(ContentControl.$, b);
                    if (c != null && c.content != null) {
                        let d = typeCast(ILegendContext_$type, c.content);
                        if (d != null && !this.be(d)) {
                            this.at.add(b);
                            let e = new LegendItemInfo();
                            e.b = d;
                            e.c = b;
                            e.a = a;
                            e.d = d.itemLabel;
                        }
                    }
                });
            }
        }
        bf(a) {
            if (a == null || this.at == null || this.at.count == 0) {
                return 0;
            }
            let b = new ObservableCollection$1(UIElement.$, 0);
            let c = -1;
            let d = 0;
            for (let e of fromEnum(this.at)) {
                let f = typeCast(ContentControl.$, e);
                if (f != null && f.content != null) {
                    let g = typeCast(ILegendContext_$type, f.content);
                    if (g != null && g.series == a) {
                        if (c == -1) {
                            c = d;
                        }
                        b.add(e);
                    }
                }
                d++;
            }
            for (let h of fromEnum(b)) {
                this.at.remove(h);
            }
            if (c == -1) {
                return this.at.count;
            }
            return c;
        }
        be(a) {
            return this.bb.ap(a);
        }
        get bc() {
            return this.ak.k ? 0 : 1;
        }
        set bc(a) {
            this.ak.k = a == 0;
        }
        get bh() {
            return this.ak.v;
        }
        set bh(a) {
            this.ak.v = a;
        }
        get bi() {
            return this.ak.w;
        }
        set bi(a) {
            this.ak.w = a;
        }
    }
    ItemLegend.$t = markType(ItemLegend, 'ItemLegend', LegendBase.$, [IChartItemLegend_$type]);
    return ItemLegend;
})();
/**
 * @hidden
 */
export let ItemLegendView = /*@__PURE__*/ (() => {
    class ItemLegendView extends LegendBaseView {
        constructor(a) {
            super(a);
            this.ao = null;
            this.ao = a;
        }
        ak() {
            super.ak();
        }
        get_j() {
            return true;
        }
        ap(a) {
            return this.c.e(a);
        }
        f() {
            let a = new LegendVisualData();
            let b = this.c.g.rootWrapper;
            b = b.getChildAt(0);
            let c = b.getChildCount();
            for (let d = 0; d < c; d++) {
                let e = new LegendItemVisualData();
                let 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 (let g = 0; g < c; g++) {
                    a.items._inner[g].appearance.fill = this.a(g);
                }
            }
            return a;
        }
    }
    ItemLegendView.$t = markType(ItemLegendView, 'ItemLegendView', LegendBaseView.$);
    return ItemLegendView;
})();
