/*
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 { 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
 */
export let ItemLegend = /*@__PURE__*/ (() => {
    class ItemLegend extends LegendBase {
        constructor() {
            super();
            this.bd = null;
            this.bf = null;
            this.ab = ItemLegend.$;
            this.bd = this.ak;
            let a = this.au;
            a.collectionChanged = delegateCombine(a.collectionChanged, (a, b) => {
                if (b.oldItems != null) {
                    for (let c of fromEn(b.oldItems)) {
                        this.bd.ao(c);
                    }
                }
                if (b.newItems != null) {
                    for (let d of fromEn(b.newItems)) {
                        this.bd.y(d);
                    }
                }
            });
        }
        aj() {
            return new ItemLegendView(this);
        }
        ba(a) {
            super.ba(a);
            this.bd = a;
        }
        addChildInOrder(a, b) {
            if (!this.ak.n()) {
                return;
            }
            this.bn(b);
        }
        get_isItemwise() {
            return true;
        }
        createItemwiseLegendItems(a, b) {
            this.a1(a, b);
        }
        a1(a, b) {
            this.ak.ag();
            this.clearLegendItemsForSeries(b);
            if (b == null || a == null || a.count == 0) {
                return;
            }
            let c = false;
            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.bg(f)) {
                        this.au.add(d);
                        c = true;
                        let 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;
                        }
                    }
                }
            }
            if (c) {
                this.a9(new LegendSizeChangedEventArgs());
            }
            this.ak.ah();
        }
        createLegendItemsInsert(a, b) {
            this.bm(a, b);
        }
        bm(a, b) {
            this.ak.ag();
            try {
                let c = this.bh(b);
                if (b == null || a == null || a.count == 0) {
                    return;
                }
                let d = false;
                for (let e of fromEnum(a)) {
                    let f = typeCast(ContentControl.$, e);
                    if (f != null && f.content != null) {
                        let g = typeCast(ILegendContext_$type, f.content);
                        if (g != null && !this.bg(g)) {
                            this.au.insert(c, e);
                            d = true;
                            c++;
                            let h = new LegendItemInfo();
                            h.b = g;
                            h.c = e;
                            h.a = b;
                            h.d = g.legendLabel != null ? g.legendLabel.toString() : g.itemLabel;
                        }
                    }
                }
                if (d) {
                    this.a9(new LegendSizeChangedEventArgs());
                }
            }
            finally {
                this.ak.ah();
            }
        }
        renderItemwiseContent(a) {
            this.bn(a);
        }
        bn(a) {
            this.clearLegendItemsForSeries(a);
            if (a.hasSubItems) {
                let b = this.bi(a);
                let c = false;
                a.forSubItems((d) => {
                    let e = typeCast(ContentControl.$, d);
                    if (e != null && e.content != null) {
                        let f = typeCast(ILegendContext_$type, e.content);
                        if (f != null && !this.bg(f)) {
                            this.au.insert(b, d);
                            c = true;
                            let g = new LegendItemInfo();
                            g.b = f;
                            g.c = d;
                            g.a = a;
                            g.d = f.legendLabel != null ? f.legendLabel.toString() : f.itemLabel;
                            b++;
                        }
                    }
                });
                if (c) {
                    this.a9(new LegendSizeChangedEventArgs());
                }
            }
        }
        bi(a) {
            let b = 0;
            let c = a.resolveLegendIndex();
            for (let d of fromEnum(this.au)) {
                let e = typeCast(ContentControl.$, d);
                if (e != null && e.content != null) {
                    let f = typeCast(ILegendContext_$type, e.content);
                    let g = f.series;
                    let h = g.resolveLegendIndex();
                    if (c == h) {
                        if (!a.isLayer) {
                            return b;
                        }
                    }
                    else if (h > c) {
                        return b;
                    }
                }
                b++;
            }
            return b;
        }
        bh(a) {
            if (a == null || this.au == null || this.au.count == 0) {
                return 0;
            }
            let b = new ObservableCollection$1(UIElement.$, 0);
            let c = -1;
            let d = 0;
            for (let e of fromEnum(this.au)) {
                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.au.remove(h);
            }
            if (b.count > 0) {
                this.a9(new LegendSizeChangedEventArgs());
            }
            if (c == -1) {
                return this.au.count;
            }
            return c;
        }
        bg(a) {
            return this.bd.aq(a);
        }
        get be() {
            return this.ak.l ? 0 : 1;
        }
        set be(a) {
            let b = this.ak.l;
            this.ak.l = a == 0;
            if (b != this.ak.l) {
                this.a9(new LegendSizeChangedEventArgs());
            }
        }
        get bk() {
            return this.ak.w;
        }
        set bk(a) {
            this.ak.w = a;
        }
        get bl() {
            return this.ak.x;
        }
        set bl(a) {
            let b = this.ak.x;
            this.ak.x = a;
            if (b != a) {
                this.a9(new LegendSizeChangedEventArgs());
            }
        }
    }
    ItemLegend.$t = markType(ItemLegend, 'ItemLegend', LegendBase.$, [IChartItemLegend_$type]);
    return ItemLegend;
})();
/**
 * @hidden
 */
export let ItemLegendView = /*@__PURE__*/ (() => {
    class ItemLegendView extends LegendBaseView {
        constructor(a) {
            super(a);
            this.ap = null;
            this.ap = a;
        }
        al() {
            super.al();
        }
        get_k() {
            return true;
        }
        aq(a) {
            return this.d.e(a);
        }
        g() {
            let a = new LegendVisualData();
            let b = this.d.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.i(f);
                e.badgeBounds = this.h(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);
                    a.items._inner[g].appearance.stroke = this.b(g);
                }
            }
            return a;
        }
    }
    ItemLegendView.$t = markType(ItemLegendView, 'ItemLegendView', LegendBaseView.$);
    return ItemLegendView;
})();
