/*
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 { Base, DomWrapper_$type, typeCast, runOn, fromEnum, markType, INotifyPropertyChanged_$type, PropertyChangedEventArgs } from "igniteui-react-core/ES2015/type";
import { Dictionary$2 } from "igniteui-react-core/ES2015/Dictionary$2";
import { RenderingContext } from "igniteui-react-core/ES2015/RenderingContext";
import { ContentControl } from "igniteui-react-core/ES2015/ContentControl";
import { ILegendContext_$type } from "igniteui-react-core/ES2015/ILegendContext";
import { DataTemplatePassInfo } from "igniteui-react-core/ES2015/DataTemplatePassInfo";
import { DataTemplateMeasureInfo } from "igniteui-react-core/ES2015/DataTemplateMeasureInfo";
import { DataTemplateRenderInfo } from "igniteui-react-core/ES2015/DataTemplateRenderInfo";
import { CanvasViewRenderer } from "igniteui-react-core/ES2015/CanvasViewRenderer";
import { Size } from "igniteui-react-core/ES2015/Size";
import { Rect } from "igniteui-react-core/ES2015/Rect";
import { FontUtil } from "igniteui-react-core/ES2015/FontUtil";
import { LegendItemHelper } from "igniteui-react-core/ES2015/LegendItemHelper";
import { isNaN_ } from "igniteui-react-core/ES2015/number";
import { IChartLegend_$type } from "igniteui-react-core/ES2015/IChartLegend";
import { ObservableCollection$1 } from "igniteui-react-core/ES2015/ObservableCollection$1";
import { UIElement } from "igniteui-react-core/ES2015/UIElement";
import { ILegendSeries_$type } from "igniteui-react-core/ES2015/ILegendSeries";
import { PropertyUpdatedEventArgs } from "igniteui-react-core/ES2015/PropertyUpdatedEventArgs";
import { LegendMouseButtonEventArgs } from "./LegendMouseButtonEventArgs";
import { LegendMouseEventArgs } from "./LegendMouseEventArgs";
import { LegendVisualData } from "./LegendVisualData";
import { MouseButtonEventArgs } from "igniteui-react-core/ES2015/MouseButtonEventArgs";
import { MouseEventArgs } from "igniteui-react-core/ES2015/MouseEventArgs";
import { RectData } from "igniteui-react-core/ES2015/RectData";
import { AppearanceHelper } from "igniteui-react-core/ES2015/AppearanceHelper";
import { LegendItemVisualData } from "./LegendItemVisualData";
import { stringIsNullOrEmpty } from "igniteui-react-core/ES2015/string";
/**
 * @hidden
 */
export class LegendBaseViewManager extends Base {
    constructor(a) {
        super();
        this.a = null;
        this.g = null;
        this.m = null;
        this.h = null;
        this.d = false;
        this.f = null;
        this.n = null;
        this.c = null;
        this.a = a;
        this.f = new Dictionary$2(Base.$, DomWrapper_$type, 0);
        this.q();
    }
    q() {
        if (this.g == null) {
            this.g = (a) => {
                let b = LegendItemHelper.b;
                if (window.devicePixelRatio != undefined && (isNaN_(b) || window.devicePixelRatio != b)) {
                    LegendItemHelper.b = window.devicePixelRatio;
                    this.a.aj();
                }
            };
            window.addEventListener("resize", this.g, false);
        }
        this.g(null);
    }
    t(a) {
        if (a == null) {
            if (this.g != null) {
                window.removeEventListener("resize", this.g, false);
                this.g = null;
            }
            this.m = null;
            this.h = null;
            return;
        }
        this.h = a;
        this.m = this.j();
        this.m.addClass(this.a.s);
        this.h.append(this.m);
    }
    j() {
        return this.h.createElement("table");
    }
    i() {
        let a = this.h.createElement("tr");
        a.addClass(this.a.t);
        let b = this.a.k;
        if (b) {
            a.setStyleProperty("display", "block");
            a.setStyleProperty("float", "left");
            a.setStyleProperty("margin", "0px");
            a.setStyleProperty("margin-right", "8px");
        }
        return a;
    }
    k(a) {
        return this.l(a, this.m);
    }
    l(a, b) {
        let c = typeCast(ContentControl.$, a);
        let d = this.i();
        if (c != null) {
            let e = typeCast(ILegendContext_$type, c.content);
            if (e != null && c.ah != null) {
                let f = new DataTemplatePassInfo();
                f.context = d;
                f.passID = "LegendItem";
                let g = new DataTemplateMeasureInfo();
                g.passInfo = f;
                g.width = NaN;
                g.height = NaN;
                g.data = e;
                g.context = d;
                let h = new DataTemplateRenderInfo();
                h.passInfo = f;
                if (c.ah.measure != null) {
                    c.ah.measure(g);
                }
                h.context = this.h.getSubRenderer(d);
                h.availableWidth = g.width;
                h.availableHeight = g.height;
                h.data = e;
                h.xPosition = 0;
                h.yPosition = 0;
                c.ah.render(h);
                let i = d.findByClass(".ui-legend-item-badge");
                for (let j = 0; j < i.length; j++) {
                    i[j].removeClass("ui-legend-item-badge").addClass(this.a.r);
                }
                i = d.findByClass(".ui-legend-item-text");
                for (let k = 0; k < i.length; k++) {
                    if (this.a.k) {
                        i[k].setStyleProperty("white-space", "nowrap");
                    }
                    i[k].removeClass("ui-legend-item-text").addClass(this.a.u);
                }
            }
        }
        return d;
    }
    p(a, b) {
        let c = this.l(a, this.m);
        c.listen("mouseup", runOn(this.a, this.a.ae));
        c.listen("mousedown", runOn(this.a, this.a.ab));
        c.listen("mouseleave", runOn(this.a, this.a.ac));
        c.listen("mousemove", runOn(this.a, this.a.ad));
        this.f.addItem(a, c);
        if ((this.m.getChildCount() == 0) || (b >= this.m.getChildCount())) {
            this.m.append(c);
        }
        else {
            let d = this.m.getChildAt(b);
            d.before(c);
        }
    }
    v(a) {
    }
    w(a) {
        let b;
        if (((() => { let c = this.f.tryGetValue(a, b); b = c.p1; return c.ret; })())) {
            b.unlistenAll();
            b.remove();
            this.f.removeItem(a);
        }
    }
    o(visual_) {
        for (let pair_ of fromEnum(this.f)) {
            if (pair_.value.getNativeElement() === visual_) {
                return pair_.key;
            }
            ;
        }
        return null;
    }
    e(a) {
        for (let b of fromEnum(this.f.keys)) {
            let c = typeCast(ContentControl.$, b);
            if (c != null && c.content != null) {
                let d = typeCast(ILegendContext_$type, c.content);
                if (d != null && d.itemLabel == a.itemLabel && d.legendLabel == a.legendLabel && d.series == a.series && d.itemBrush == a.itemBrush) {
                    return true;
                }
            }
        }
        return false;
    }
    b(a) {
        let b = this.h.rootWrapper.width();
        let c = this.h.rootWrapper.height();
        this.m.remove();
        let d = this.h.createElement("div");
        d.setStyleProperty("position", "relative");
        this.n = this.h.createElement("<canvas style=\"position : absolute; top : 0; left : 0\" />");
        this.n.setStyleProperty("position", "absolute");
        this.n.setStyleProperty("top", "0px");
        this.n.setStyleProperty("left", "0px");
        this.h.append(d);
        d.append(this.n);
        this.c = new RenderingContext(new CanvasViewRenderer(), this.h.get2DCanvasContext(this.n));
        this.n.setAttribute("width", b.toString());
        this.n.setAttribute("height", c.toString());
        return this.c;
    }
    aa() {
        return new Size(1, this.h.rootWrapper.width(), Math.max(10, this.h.rootWrapper.height() - 15));
    }
    x(a, b, c, d) {
        let e = a.g();
        e.beginPath();
        e.moveTo(b.aj._inner[0].x, b.aj._inner[0].y);
        for (let f = 1; f < b.aj.count; f++) {
            e.lineTo(b.aj._inner[f].x, b.aj._inner[f].y);
        }
        e.lineTo(b.aj._inner[0].x, b.aj._inner[0].y);
        let g = e.createLinearGradient(d.left, d.top, d.left, d.top + d.height);
        for (let h of fromEnum(c.a)) {
            g.addColorStop(h.a, h.b._fill);
        }
        e.fillStyle = g;
        e.fill();
    }
    r() {
    }
    s() {
    }
    u(a) {
        let b = this.m == null ? 0 : this.m.getChildCount();
        for (let c = 0; c < b; c++) {
            let d = this.m.getChildAt(c);
            d.setStyleProperty("display", a ? "block" : "");
            d.setStyleProperty("float", a ? "left" : "");
            d.setStyleProperty("margin", a ? "0px" : "");
            d.setStyleProperty("margin-right", a ? "8px" : "");
            d.getChildAt(1).setStyleProperty("white-space", a ? "nowrap" : "");
        }
    }
    y(a) {
        this.m.setStyleProperty("color", a);
    }
    z(a) {
        let b = FontUtil.getFontInfoFromString(this.h, a);
        this.m.setStyleProperty("font-family", b.n);
        this.m.setStyleProperty("font-size", b.d + "px");
        this.m.setStyleProperty("font-style", b.q);
    }
}
LegendBaseViewManager.$t = markType(LegendBaseViewManager, 'LegendBaseViewManager');
/**
 * @hidden
 */
export class LegendBase extends ContentControl {
    constructor() {
        super();
        this.ak = null;
        this.at = null;
        this._seriesOwner = null;
        this._chartOwner = null;
        this.propertyChanged = null;
        this.propertyUpdated = null;
        this.legendItemMouseLeftButtonDown = null;
        this.legendItemMouseLeftButtonUp = null;
        this.legendItemMouseEnter = null;
        this.legendItemMouseLeave = null;
        this.legendItemMouseMove = null;
        let a = this.aj();
        this.a6(a);
        a.ai();
        this.at = new ObservableCollection$1(UIElement.$, 0);
        this.aw = "ui-chart-legend-items-list";
        this.ax = "ui-chart-legend-item";
        this.av = "ui-chart-legend-item-badge";
        this.ay = "ui-chart-legend-item-text";
    }
    aj() {
        return new LegendBaseView(this);
    }
    a6(a) {
        this.ak = a;
    }
    addChildInOrder(a, b) {
    }
    a0(a, b) {
    }
    get seriesOwner() {
        return this._seriesOwner;
    }
    set seriesOwner(a) {
        this._seriesOwner = a;
    }
    get chartOwner() {
        return this._chartOwner;
    }
    set chartOwner(a) {
        this._chartOwner = a;
    }
    get_isItemwise() {
        return false;
    }
    get isItemwise() {
        return this.get_isItemwise();
    }
    get_isScale() {
        return false;
    }
    get isScale() {
        return this.get_isScale();
    }
    clearLegendItemsForSeries(a) {
        if (a == null || this.at == null || this.at.count == 0) {
            return;
        }
        let b = new ObservableCollection$1(UIElement.$, 0);
        for (let c of fromEnum(this.at)) {
            let d = typeCast(ContentControl.$, c);
            if (d != null && d.content != null) {
                let e = typeCast(ILegendContext_$type, d.content);
                if (e != null && e.series == a) {
                    b.add(c);
                }
            }
        }
        for (let f of fromEnum(b)) {
            this.at.remove(f);
        }
    }
    containsChild(a) {
        return this.at.contains(a);
    }
    removeChild(a) {
        this.at.remove(a);
    }
    get ai() {
        if (this.seriesOwner != null) {
            return this.seriesOwner;
        }
        else {
            return this.chartOwner;
        }
    }
    a7(a, b, c) {
        if (this.propertyChanged != null) {
            this.propertyChanged(this, new PropertyChangedEventArgs(a));
        }
        if (this.propertyUpdated != null) {
            this.propertyUpdated(this, new PropertyUpdatedEventArgs(a, b, c));
        }
    }
    a3(a) {
        if (this.legendItemMouseLeftButtonDown != null) {
            this.legendItemMouseLeftButtonDown(this, a);
        }
    }
    a4(a) {
        if (this.legendItemMouseLeftButtonUp != null) {
            this.legendItemMouseLeftButtonUp(this, a);
        }
    }
    a1(a) {
        if (this.legendItemMouseEnter != null) {
            this.legendItemMouseEnter(this, a);
        }
    }
    a2(a) {
        if (this.legendItemMouseLeave != null) {
            this.legendItemMouseLeave(this, a);
        }
    }
    a5(a) {
        if (this.legendItemMouseMove != null) {
            this.legendItemMouseMove(this, a);
        }
    }
    provideContainer(a) {
        this.ak.ah(a);
    }
    get aw() {
        return this.ak.s;
    }
    set aw(a) {
        this.ak.s = a;
    }
    get ax() {
        return this.ak.t;
    }
    set ax(a) {
        this.ak.t = a;
    }
    get av() {
        return this.ak.r;
    }
    set av(a) {
        this.ak.r = a;
    }
    get ay() {
        return this.ak.u;
    }
    set ay(a) {
        this.ak.u = a;
    }
    exportVisualData() {
        return this.ak.f();
    }
    exportSerializedVisualData() {
        return this.exportVisualData().serialize();
    }
    get_isFinancial() {
        return false;
    }
    get isFinancial() {
        return this.get_isFinancial();
    }
}
LegendBase.$t = markType(LegendBase, 'LegendBase', ContentControl.$, [INotifyPropertyChanged_$type, IChartLegend_$type]);
/**
 * @hidden
 */
export class LegendBaseView extends Base {
    constructor(a) {
        super();
        this.c = null;
        this.b = null;
        this.s = null;
        this.t = null;
        this.r = null;
        this.u = null;
        this.l = false;
        this.i = false;
        this.p = null;
        this.q = null;
        this.b = a;
        this.c = new LegendBaseViewManager(this);
    }
    ai() {
    }
    get_j() {
        return false;
    }
    get j() {
        return this.get_j();
    }
    d(a) {
        let b;
        let c;
        let d;
        let e = this.aa(a, b, c, d);
        b = e.p1;
        c = e.p2;
        d = e.p3;
        let f = new MouseButtonEventArgs();
        let g = new LegendMouseButtonEventArgs(b, c, d, f, a);
        return g;
    }
    e(a) {
        let b;
        let c;
        let d;
        let e = this.aa(a, b, c, d);
        b = e.p1;
        c = e.p2;
        d = e.p3;
        let f = new MouseEventArgs();
        let g = new LegendMouseEventArgs(b, c, d, f, a);
        return g;
    }
    aa(a, b, c, d) {
        b = this.b.ai;
        c = null;
        d = null;
        if (a != null) {
            let e = typeCast(ContentControl.$, a);
            if (e != null && e.content != null && typeCast(ILegendContext_$type, e.content) !== null) {
                let f = typeCast(ILegendContext_$type, e.content);
                c = typeCast(ILegendSeries_$type, f.series);
                if (c != null) {
                    b = c.container;
                }
                d = f.item;
            }
        }
        return {
            p1: b,
            p2: c,
            p3: d
        };
    }
    z() {
    }
    m() {
        return true;
    }
    y(a) {
    }
    ak(a) {
    }
    ah(a) {
        this.c.t(a);
    }
    al(a) {
        this.c.w(a);
    }
    x(a) {
        let b = this.b.at.indexOf(a);
        this.c.p(a, b);
    }
    af() {
        this.c.r();
    }
    ag() {
        this.c.s();
    }
    get k() {
        return this.i;
    }
    set k(a) {
        this.i = a;
        this.c.u(this.i);
    }
    get v() {
        return this.p;
    }
    set v(a) {
        this.p = a;
        this.c.y(this.p);
    }
    get w() {
        return this.q;
    }
    set w(a) {
        this.q = a;
        this.c.z(this.q);
    }
    aj() {
    }
    n(e_) {
        let ev_ = e_.originalEvent;
        return ev_.currentTarget;
    }
    o(e_) {
        return this.c.o(this.n(e_));
    }
    ad(a) {
        if (!this.l) {
            this.b.a1(this.e(this.o(a)));
        }
        this.l = true;
    }
    ac(a) {
        if (this.l) {
            this.b.a2(this.e(this.o(a)));
        }
        this.l = false;
    }
    ab(a) {
        this.b.a3(this.d(this.o(a)));
    }
    ae(a) {
        this.b.a4(this.d(this.o(a)));
    }
    h(a) {
        let b = a.querySelectorAll("span *");
        let c = Rect.empty;
        for (let e = 0; e < b.length; e++) {
            let d = b[e];
            if (stringIsNullOrEmpty(d.getText())) {
                continue;
            }
            let f = new Rect(0, d.getOffset().left, d.getOffset().top, d.width(), d.height());
            f.union(c);
            c = f;
        }
        return RectData.b(c);
    }
    g(a) {
        let b = a.querySelectorAll("canvas");
        let c = b.length > 0 ? b[0] : null;
        return c != null ? new RectData(c.getOffset().left, c.getOffset().top, c.width(), c.height()) : null;
    }
    a(a) {
        if (this.b == null || this.b.at == null || this.b.at.count <= a) {
            return null;
        }
        let b = this.b.at._inner[a];
        if (b == null) {
            return null;
        }
        let c = b.content;
        if (c == null) {
            return null;
        }
        let d = c.actualItemBrush != null ? c.actualItemBrush : c.itemBrush;
        return AppearanceHelper.b(d);
    }
    f() {
        let a = new LegendVisualData();
        let b = this.c.m;
        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 < this.b.at.count; g++) {
            a.items._inner[g].appearance.fill = this.a(g);
        }
        return a;
    }
}
LegendBaseView.$t = markType(LegendBaseView, 'LegendBaseView');
