/*
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-react-core/ES2015/IChartItemLegend";
import { LegendBaseView } from "./LegendBaseView";
import { fromEn, delegateCombine, typeCast, fromEnum, markType } from "igniteui-react-core/ES2015/type";
import { ObservableCollection$1 } from "igniteui-react-core/ES2015/ObservableCollection$1";
import { UIElement } from "igniteui-react-core/ES2015/UIElement";
import { ContentControl } from "igniteui-react-core/ES2015/ContentControl";
import { ILegendContext_$type } from "igniteui-react-core/ES2015/ILegendContext";
import { LegendItemInfo } from "./LegendItemInfo";
import { LegendVisualData } from "./LegendVisualData";
import { LegendItemVisualData } from "./LegendItemVisualData";
import { RectData } from "igniteui-react-core/ES2015/RectData";
/**
 * @hidden
 */
export class ItemLegend extends LegendBase {
    constructor() {
        super();
        this.a9 = null;
        this.ba = null;
        this.ab = ItemLegend.$;
        this.a9 = 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.a9.al(c);
                }
            }
            if (b.newItems != null) {
                for (let d of fromEn(b.newItems)) {
                    this.a9.x(d);
                }
            }
        });
    }
    aj() {
        return new ItemLegendView(this);
    }
    a6(a) {
        super.a6(a);
        this.a9 = a;
    }
    addChildInOrder(a, b) {
        if (!this.ak.m()) {
            return;
        }
        this.be(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.bb(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.bd(a, b);
    }
    bd(a, b) {
        this.ak.af();
        try {
            let c = this.bc(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.bb(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.be(a);
    }
    be(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.bb(d)) {
                        this.at.add(b);
                        let e = new LegendItemInfo();
                        e.b = d;
                        e.c = b;
                        e.a = a;
                        e.d = d.itemLabel;
                    }
                }
            });
        }
    }
    bc(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;
    }
    bb(a) {
        return this.a9.an(a);
    }
}
ItemLegend.$t = markType(ItemLegend, 'ItemLegend', LegendBase.$, [IChartItemLegend_$type]);
/**
 * @hidden
 */
export class ItemLegendView extends LegendBaseView {
    constructor(a) {
        super(a);
        this.am = null;
        this.am = a;
    }
    ai() {
        super.ai();
    }
    get_j() {
        return true;
    }
    an(a) {
        return this.c.e(a);
    }
    f() {
        let a = new LegendVisualData();
        let b = this.c.h.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);
        }
        for (let g = 0; g < c; g++) {
            a.items._inner[g].appearance.fill = this.a(g);
        }
        return a;
    }
}
ItemLegendView.$t = markType(ItemLegendView, 'ItemLegendView', LegendBaseView.$);
