/*
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 { DomWrapper_$type, Base, 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 { 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 { LegendItemVisualData } from "./LegendItemVisualData";
import { AppearanceHelper } from "igniteui-react-core/ES2015/AppearanceHelper";
import { stringIsNullOrEmpty } from "igniteui-react-core/ES2015/string";
import { MouseButtonEventArgs } from "igniteui-react-core/ES2015/MouseButtonEventArgs";
import { MouseEventArgs } from "igniteui-react-core/ES2015/MouseEventArgs";
export class LegendBaseViewManager extends Base {
    constructor(a) {
        super();
        this.a = null;
        this.g = null;
        this.l = null;
        this.h = null;
        this.d = false;
        this.f = null;
        this.m = null;
        this.c = null;
        this.a = a;
        this.f = new Dictionary$2(Base.$, DomWrapper_$type, 0);
        this.p();
    }
    p() {
        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.ae();
                }
            };
            window.addEventListener("resize", this.g, false);
        }
        this.g(null);
    }
    s(a) {
        if (a == null) {
            if (this.g != null) {
                window.removeEventListener("resize", this.g, false);
                this.g = null;
            }
            this.l = null;
            this.h = null;
            return;
        }
        this.h = a;
        this.l = this.j();
        this.l.addClass(this.a.n);
        this.h.append(this.l);
    }
    j() {
        return this.h.createElement("table");
    }
    i() {
        let a = this.h.createElement("tr");
        a.addClass(this.a.o);
        let b = this.a.g;
        if (b) {
            a.setStyleProperty("display", "block");
            a.setStyleProperty("float", "left");
            a.setStyleProperty("margin", "0px");
            a.setStyleProperty("margin-right", "8px");
        }
        return a;
    }
    k(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.m);
                }
                i = d.findByClass(".ui-legend-item-text");
                for (let k = 0; k < i.length; k++) {
                    if (this.a.g) {
                        i[k].setStyleProperty("white-space", "nowrap");
                    }
                    i[k].removeClass("ui-legend-item-text").addClass(this.a.p);
                }
            }
        }
        return d;
    }
    o(a, b) {
        let c = this.k(a, this.l);
        c.listen("mouseup", runOn(this.a, this.a.z));
        c.listen("mousedown", runOn(this.a, this.a.w));
        c.listen("mouseleave", runOn(this.a, this.a.x));
        c.listen("mousemove", runOn(this.a, this.a.y));
        this.f.addItem(a, c);
        if ((this.l.getChildCount() == 0) || (b >= this.l.getChildCount())) {
            this.l.append(c);
        }
        else {
            let d = this.l.getChildAt(b);
            d.before(c);
        }
    }
    u(a) {
    }
    v(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);
        }
    }
    n(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.l.remove();
        let d = this.h.createElement("div");
        d.setStyleProperty("position", "relative");
        this.m = this.h.createElement("<canvas style=\"position : absolute; top : 0; left : 0\" />");
        this.m.setStyleProperty("position", "absolute");
        this.m.setStyleProperty("top", "0px");
        this.m.setStyleProperty("left", "0px");
        this.h.append(d);
        d.append(this.m);
        this.c = new RenderingContext(new CanvasViewRenderer(), this.h.get2DCanvasContext(this.m));
        this.m.setAttribute("width", b.toString());
        this.m.setAttribute("height", c.toString());
        return this.c;
    }
    z() {
        return new Size(1, this.h.rootWrapper.width(), Math.max(10, this.h.rootWrapper.height() - 15));
    }
    w(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();
    }
    q() {
    }
    r() {
    }
    t(a) {
        let b = this.l == null ? 0 : this.l.getChildCount();
        for (let c = 0; c < b; c++) {
            let d = this.l.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" : "");
        }
    }
    x(a) {
        this.l.setStyleProperty("color", a);
    }
    y(a) {
        let b = FontUtil.getFontInfoFromString(this.h, a);
        this.l.setStyleProperty("font-family", b.n);
        this.l.setStyleProperty("font-size", b.d + "px");
        this.l.setStyleProperty("font-style", b.q);
    }
}
LegendBaseViewManager.$t = markType(LegendBaseViewManager, 'LegendBaseViewManager');
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.a5(a);
        a.ad();
        this.at = new ObservableCollection$1(UIElement.$, 0);
    }
    aj() {
        return new LegendBaseView(this);
    }
    a5(a) {
        this.ak = a;
    }
    addChildInOrder(a, b) {
    }
    az(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));
        }
    }
    a2(a) {
        if (this.legendItemMouseLeftButtonDown != null) {
            this.legendItemMouseLeftButtonDown(this, a);
        }
    }
    a3(a) {
        if (this.legendItemMouseLeftButtonUp != null) {
            this.legendItemMouseLeftButtonUp(this, a);
        }
    }
    a0(a) {
        if (this.legendItemMouseEnter != null) {
            this.legendItemMouseEnter(this, a);
        }
    }
    a1(a) {
        if (this.legendItemMouseLeave != null) {
            this.legendItemMouseLeave(this, a);
        }
    }
    a4(a) {
        if (this.legendItemMouseMove != null) {
            this.legendItemMouseMove(this, a);
        }
    }
    provideContainer(a) {
        this.ak.ac(a);
    }
    get av() {
        return this.ak.n;
    }
    set av(a) {
        this.ak.n = a;
    }
    get aw() {
        return this.ak.o;
    }
    set aw(a) {
        this.ak.o = a;
    }
    get au() {
        return this.ak.m;
    }
    set au(a) {
        this.ak.m = a;
    }
    get ax() {
        return this.ak.p;
    }
    set ax(a) {
        this.ak.p = a;
    }
    ar() {
        let a = new LegendVisualData();
        for (let b of fromEnum(this.at)) {
            let c = typeCast(ContentControl.$, b);
            let d = new LegendItemVisualData();
            let e = typeCast(ILegendContext_$type, c.content);
            if (e != null) {
                if (e.itemLabel != null && !stringIsNullOrEmpty(e.itemLabel.toString())) {
                    d.label = e.itemLabel.toString();
                }
                if (e.legendLabel != null && !stringIsNullOrEmpty(e.legendLabel.toString())) {
                    d.label = e.legendLabel.toString();
                }
                if (e.actualItemBrush != null) {
                    d.appearance.fill = AppearanceHelper.b(e.actualItemBrush);
                }
                else {
                    d.appearance.fill = AppearanceHelper.b(e.itemBrush);
                }
            }
            a.items.add(d);
        }
        return a;
    }
}
LegendBase.$t = markType(LegendBase, 'LegendBase', ContentControl.$, [INotifyPropertyChanged_$type, IChartLegend_$type]);
export class LegendBaseView extends Base {
    constructor(a) {
        super();
        this.b = null;
        this.a = null;
        this.n = null;
        this.o = null;
        this.m = null;
        this.p = null;
        this.h = false;
        this.e = false;
        this.k = null;
        this.l = null;
        this.a = a;
        this.b = new LegendBaseViewManager(this);
    }
    ad() {
    }
    get_f() {
        return false;
    }
    get f() {
        return this.get_f();
    }
    c(a) {
        let b;
        let c;
        let d;
        let e = this.v(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;
    }
    d(a) {
        let b;
        let c;
        let d;
        let e = this.v(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;
    }
    v(a, b, c, d) {
        b = this.a.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
        };
    }
    u() {
    }
    i() {
        return true;
    }
    t(a) {
    }
    af(a) {
    }
    ac(a) {
        this.b.s(a);
    }
    ag(a) {
        this.b.v(a);
    }
    s(a) {
        let b = this.a.at.indexOf(a);
        this.b.o(a, b);
    }
    aa() {
        this.b.q();
    }
    ab() {
        this.b.r();
    }
    get g() {
        return this.e;
    }
    set g(a) {
        this.e = a;
        this.b.t(this.e);
    }
    get q() {
        return this.k;
    }
    set q(a) {
        this.k = a;
        this.b.x(this.k);
    }
    get r() {
        return this.l;
    }
    set r(a) {
        this.l = a;
        this.b.y(this.l);
    }
    ae() {
    }
    j(e_) {
        return this.b.n(e_);
    }
    y(a) {
        if (!this.h) {
            this.a.a0(this.d(this.j(a)));
        }
        this.h = true;
    }
    x(a) {
        if (this.h) {
            this.a.a1(this.d(this.j(a)));
        }
        this.h = false;
    }
    w(a) {
        this.a.a2(this.c(this.j(a)));
    }
    z(a) {
        this.a.a3(this.c(this.j(a)));
    }
}
LegendBaseView.$t = markType(LegendBaseView, 'LegendBaseView');
