/*
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 * as tslib_1 from "tslib";
import { DomWrapper_$type, Base, typeCast, runOn, fromEnum, markType, INotifyPropertyChanged_$type, PropertyChangedEventArgs } from "igniteui-react-core/ES5/type";
import { Dictionary$2 } from "igniteui-react-core/ES5/Dictionary$2";
import { RenderingContext } from "igniteui-react-core/ES5/RenderingContext";
import { ContentControl } from "igniteui-react-core/ES5/ContentControl";
import { ILegendContext_$type } from "igniteui-react-core/ES5/ILegendContext";
import { DataTemplatePassInfo } from "igniteui-react-core/ES5/DataTemplatePassInfo";
import { DataTemplateMeasureInfo } from "igniteui-react-core/ES5/DataTemplateMeasureInfo";
import { DataTemplateRenderInfo } from "igniteui-react-core/ES5/DataTemplateRenderInfo";
import { CanvasViewRenderer } from "igniteui-react-core/ES5/CanvasViewRenderer";
import { Size } from "igniteui-react-core/ES5/Size";
import { FontUtil } from "igniteui-react-core/ES5/FontUtil";
import { LegendItemHelper } from "igniteui-react-core/ES5/LegendItemHelper";
import { isNaN_ } from "igniteui-react-core/ES5/number";
import { IChartLegend_$type } from "igniteui-react-core/ES5/IChartLegend";
import { ObservableCollection$1 } from "igniteui-react-core/ES5/ObservableCollection$1";
import { UIElement } from "igniteui-react-core/ES5/UIElement";
import { ILegendSeries_$type } from "igniteui-react-core/ES5/ILegendSeries";
import { PropertyUpdatedEventArgs } from "igniteui-react-core/ES5/PropertyUpdatedEventArgs";
import { LegendMouseButtonEventArgs } from "./LegendMouseButtonEventArgs";
import { LegendMouseEventArgs } from "./LegendMouseEventArgs";
import { LegendVisualData } from "./LegendVisualData";
import { LegendItemVisualData } from "./LegendItemVisualData";
import { AppearanceHelper } from "igniteui-react-core/ES5/AppearanceHelper";
import { stringIsNullOrEmpty } from "igniteui-react-core/ES5/string";
import { MouseButtonEventArgs } from "igniteui-react-core/ES5/MouseButtonEventArgs";
import { MouseEventArgs } from "igniteui-react-core/ES5/MouseEventArgs";
var LegendBaseViewManager = /** @class */ (function (_super) {
    tslib_1.__extends(LegendBaseViewManager, _super);
    function LegendBaseViewManager(a) {
        var _this = _super.call(this) || this;
        _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();
        return _this;
    }
    LegendBaseViewManager.prototype.p = function () {
        var _this = this;
        if (this.g == null) {
            this.g = function (a) {
                var 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);
    };
    LegendBaseViewManager.prototype.s = function (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);
    };
    LegendBaseViewManager.prototype.j = function () {
        return this.h.createElement("table");
    };
    LegendBaseViewManager.prototype.i = function () {
        var a = this.h.createElement("tr");
        a.addClass(this.a.o);
        var 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;
    };
    LegendBaseViewManager.prototype.k = function (a, b) {
        var c = typeCast(ContentControl.$, a);
        var d = this.i();
        if (c != null) {
            var e = typeCast(ILegendContext_$type, c.content);
            if (e != null && c.ah != null) {
                var f = new DataTemplatePassInfo();
                f.context = d;
                f.passID = "LegendItem";
                var g = new DataTemplateMeasureInfo();
                g.passInfo = f;
                g.width = NaN;
                g.height = NaN;
                g.data = e;
                g.context = d;
                var 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);
                var i = d.findByClass(".ui-legend-item-badge");
                for (var 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 (var 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;
    };
    LegendBaseViewManager.prototype.o = function (a, b) {
        var 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 {
            var d = this.l.getChildAt(b);
            d.before(c);
        }
    };
    LegendBaseViewManager.prototype.u = function (a) {
    };
    LegendBaseViewManager.prototype.v = function (a) {
        var _this = this;
        var b;
        if (((function () { var c = _this.f.tryGetValue(a, b); b = c.p1; return c.ret; })())) {
            b.unlistenAll();
            b.remove();
            this.f.removeItem(a);
        }
    };
    LegendBaseViewManager.prototype.n = function (visual_) {
        try {
            for (var _a = tslib_1.__values(fromEnum(this.f)), _b = _a.next(); !_b.done; _b = _a.next()) {
                var pair_ = _b.value;
                if (pair_.value.getNativeElement() === visual_) {
                    return pair_.key;
                }
                ;
            }
        }
        catch (e_1_1) { e_1 = { error: e_1_1 }; }
        finally {
            try {
                if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
            }
            finally { if (e_1) throw e_1.error; }
        }
        return null;
        var e_1, _c;
    };
    LegendBaseViewManager.prototype.e = function (a) {
        try {
            for (var _a = tslib_1.__values(fromEnum(this.f.keys)), _b = _a.next(); !_b.done; _b = _a.next()) {
                var b = _b.value;
                var c = typeCast(ContentControl.$, b);
                if (c != null && c.content != null) {
                    var 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;
                    }
                }
            }
        }
        catch (e_2_1) { e_2 = { error: e_2_1 }; }
        finally {
            try {
                if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
            }
            finally { if (e_2) throw e_2.error; }
        }
        return false;
        var e_2, _c;
    };
    LegendBaseViewManager.prototype.b = function (a) {
        var b = this.h.rootWrapper.width();
        var c = this.h.rootWrapper.height();
        this.l.remove();
        var 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;
    };
    LegendBaseViewManager.prototype.z = function () {
        return new Size(1, this.h.rootWrapper.width(), Math.max(10, this.h.rootWrapper.height() - 15));
    };
    LegendBaseViewManager.prototype.w = function (a, b, c, d) {
        var e = a.g();
        e.beginPath();
        e.moveTo(b.aj._inner[0].x, b.aj._inner[0].y);
        for (var 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);
        var g = e.createLinearGradient(d.left, d.top, d.left, d.top + d.height);
        try {
            for (var _a = tslib_1.__values(fromEnum(c.a)), _b = _a.next(); !_b.done; _b = _a.next()) {
                var h = _b.value;
                g.addColorStop(h.a, h.b._fill);
            }
        }
        catch (e_3_1) { e_3 = { error: e_3_1 }; }
        finally {
            try {
                if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
            }
            finally { if (e_3) throw e_3.error; }
        }
        e.fillStyle = g;
        e.fill();
        var e_3, _c;
    };
    LegendBaseViewManager.prototype.q = function () {
    };
    LegendBaseViewManager.prototype.r = function () {
    };
    LegendBaseViewManager.prototype.t = function (a) {
        var b = this.l == null ? 0 : this.l.getChildCount();
        for (var c = 0; c < b; c++) {
            var 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" : "");
        }
    };
    LegendBaseViewManager.prototype.x = function (a) {
        this.l.setStyleProperty("color", a);
    };
    LegendBaseViewManager.prototype.y = function (a) {
        var 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');
    return LegendBaseViewManager;
}(Base));
export { LegendBaseViewManager };
var LegendBase = /** @class */ (function (_super) {
    tslib_1.__extends(LegendBase, _super);
    function LegendBase() {
        var _this = _super.call(this) || this;
        _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;
        var a = _this.aj();
        _this.a5(a);
        a.ad();
        _this.at = new ObservableCollection$1(UIElement.$, 0);
        return _this;
    }
    LegendBase.prototype.aj = function () {
        return new LegendBaseView(this);
    };
    LegendBase.prototype.a5 = function (a) {
        this.ak = a;
    };
    LegendBase.prototype.addChildInOrder = function (a, b) {
    };
    LegendBase.prototype.az = function (a, b) {
    };
    Object.defineProperty(LegendBase.prototype, "seriesOwner", {
        get: function () {
            return this._seriesOwner;
        },
        set: function (a) {
            this._seriesOwner = a;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(LegendBase.prototype, "chartOwner", {
        get: function () {
            return this._chartOwner;
        },
        set: function (a) {
            this._chartOwner = a;
        },
        enumerable: true,
        configurable: true
    });
    LegendBase.prototype.get_isItemwise = function () {
        return false;
    };
    Object.defineProperty(LegendBase.prototype, "isItemwise", {
        get: function () {
            return this.get_isItemwise();
        },
        enumerable: true,
        configurable: true
    });
    LegendBase.prototype.get_isScale = function () {
        return false;
    };
    Object.defineProperty(LegendBase.prototype, "isScale", {
        get: function () {
            return this.get_isScale();
        },
        enumerable: true,
        configurable: true
    });
    LegendBase.prototype.clearLegendItemsForSeries = function (a) {
        if (a == null || this.at == null || this.at.count == 0) {
            return;
        }
        var b = new ObservableCollection$1(UIElement.$, 0);
        try {
            for (var _a = tslib_1.__values(fromEnum(this.at)), _b = _a.next(); !_b.done; _b = _a.next()) {
                var c = _b.value;
                var d = typeCast(ContentControl.$, c);
                if (d != null && d.content != null) {
                    var e = typeCast(ILegendContext_$type, d.content);
                    if (e != null && e.series == a) {
                        b.add(c);
                    }
                }
            }
        }
        catch (e_4_1) { e_4 = { error: e_4_1 }; }
        finally {
            try {
                if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
            }
            finally { if (e_4) throw e_4.error; }
        }
        try {
            for (var _d = tslib_1.__values(fromEnum(b)), _e = _d.next(); !_e.done; _e = _d.next()) {
                var f = _e.value;
                this.at.remove(f);
            }
        }
        catch (e_5_1) { e_5 = { error: e_5_1 }; }
        finally {
            try {
                if (_e && !_e.done && (_f = _d.return)) _f.call(_d);
            }
            finally { if (e_5) throw e_5.error; }
        }
        var e_4, _c, e_5, _f;
    };
    LegendBase.prototype.containsChild = function (a) {
        return this.at.contains(a);
    };
    LegendBase.prototype.removeChild = function (a) {
        this.at.remove(a);
    };
    Object.defineProperty(LegendBase.prototype, "ai", {
        get: function () {
            if (this.seriesOwner != null) {
                return this.seriesOwner;
            }
            else {
                return this.chartOwner;
            }
        },
        enumerable: true,
        configurable: true
    });
    LegendBase.prototype.a7 = function (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));
        }
    };
    LegendBase.prototype.a2 = function (a) {
        if (this.legendItemMouseLeftButtonDown != null) {
            this.legendItemMouseLeftButtonDown(this, a);
        }
    };
    LegendBase.prototype.a3 = function (a) {
        if (this.legendItemMouseLeftButtonUp != null) {
            this.legendItemMouseLeftButtonUp(this, a);
        }
    };
    LegendBase.prototype.a0 = function (a) {
        if (this.legendItemMouseEnter != null) {
            this.legendItemMouseEnter(this, a);
        }
    };
    LegendBase.prototype.a1 = function (a) {
        if (this.legendItemMouseLeave != null) {
            this.legendItemMouseLeave(this, a);
        }
    };
    LegendBase.prototype.a4 = function (a) {
        if (this.legendItemMouseMove != null) {
            this.legendItemMouseMove(this, a);
        }
    };
    LegendBase.prototype.provideContainer = function (a) {
        this.ak.ac(a);
    };
    Object.defineProperty(LegendBase.prototype, "av", {
        get: function () {
            return this.ak.n;
        },
        set: function (a) {
            this.ak.n = a;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(LegendBase.prototype, "aw", {
        get: function () {
            return this.ak.o;
        },
        set: function (a) {
            this.ak.o = a;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(LegendBase.prototype, "au", {
        get: function () {
            return this.ak.m;
        },
        set: function (a) {
            this.ak.m = a;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(LegendBase.prototype, "ax", {
        get: function () {
            return this.ak.p;
        },
        set: function (a) {
            this.ak.p = a;
        },
        enumerable: true,
        configurable: true
    });
    LegendBase.prototype.ar = function () {
        var a = new LegendVisualData();
        try {
            for (var _a = tslib_1.__values(fromEnum(this.at)), _b = _a.next(); !_b.done; _b = _a.next()) {
                var b = _b.value;
                var c = typeCast(ContentControl.$, b);
                var d = new LegendItemVisualData();
                var 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);
            }
        }
        catch (e_6_1) { e_6 = { error: e_6_1 }; }
        finally {
            try {
                if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
            }
            finally { if (e_6) throw e_6.error; }
        }
        return a;
        var e_6, _c;
    };
    LegendBase.$t = markType(LegendBase, 'LegendBase', ContentControl.$, [INotifyPropertyChanged_$type, IChartLegend_$type]);
    return LegendBase;
}(ContentControl));
export { LegendBase };
var LegendBaseView = /** @class */ (function (_super) {
    tslib_1.__extends(LegendBaseView, _super);
    function LegendBaseView(a) {
        var _this = _super.call(this) || this;
        _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);
        return _this;
    }
    LegendBaseView.prototype.ad = function () {
    };
    LegendBaseView.prototype.get_f = function () {
        return false;
    };
    Object.defineProperty(LegendBaseView.prototype, "f", {
        get: function () {
            return this.get_f();
        },
        enumerable: true,
        configurable: true
    });
    LegendBaseView.prototype.c = function (a) {
        var b;
        var c;
        var d;
        var e = this.v(a, b, c, d);
        b = e.p1;
        c = e.p2;
        d = e.p3;
        var f = new MouseButtonEventArgs();
        var g = new LegendMouseButtonEventArgs(b, c, d, f, a);
        return g;
    };
    LegendBaseView.prototype.d = function (a) {
        var b;
        var c;
        var d;
        var e = this.v(a, b, c, d);
        b = e.p1;
        c = e.p2;
        d = e.p3;
        var f = new MouseEventArgs();
        var g = new LegendMouseEventArgs(b, c, d, f, a);
        return g;
    };
    LegendBaseView.prototype.v = function (a, b, c, d) {
        b = this.a.ai;
        c = null;
        d = null;
        if (a != null) {
            var e = typeCast(ContentControl.$, a);
            if (e != null && e.content != null && typeCast(ILegendContext_$type, e.content) !== null) {
                var 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
        };
    };
    LegendBaseView.prototype.u = function () {
    };
    LegendBaseView.prototype.i = function () {
        return true;
    };
    LegendBaseView.prototype.t = function (a) {
    };
    LegendBaseView.prototype.af = function (a) {
    };
    LegendBaseView.prototype.ac = function (a) {
        this.b.s(a);
    };
    LegendBaseView.prototype.ag = function (a) {
        this.b.v(a);
    };
    LegendBaseView.prototype.s = function (a) {
        var b = this.a.at.indexOf(a);
        this.b.o(a, b);
    };
    LegendBaseView.prototype.aa = function () {
        this.b.q();
    };
    LegendBaseView.prototype.ab = function () {
        this.b.r();
    };
    Object.defineProperty(LegendBaseView.prototype, "g", {
        get: function () {
            return this.e;
        },
        set: function (a) {
            this.e = a;
            this.b.t(this.e);
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(LegendBaseView.prototype, "q", {
        get: function () {
            return this.k;
        },
        set: function (a) {
            this.k = a;
            this.b.x(this.k);
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(LegendBaseView.prototype, "r", {
        get: function () {
            return this.l;
        },
        set: function (a) {
            this.l = a;
            this.b.y(this.l);
        },
        enumerable: true,
        configurable: true
    });
    LegendBaseView.prototype.ae = function () {
    };
    LegendBaseView.prototype.j = function (e_) {
        return this.b.n(e_);
    };
    LegendBaseView.prototype.y = function (a) {
        if (!this.h) {
            this.a.a0(this.d(this.j(a)));
        }
        this.h = true;
    };
    LegendBaseView.prototype.x = function (a) {
        if (this.h) {
            this.a.a1(this.d(this.j(a)));
        }
        this.h = false;
    };
    LegendBaseView.prototype.w = function (a) {
        this.a.a2(this.c(this.j(a)));
    };
    LegendBaseView.prototype.z = function (a) {
        this.a.a3(this.c(this.j(a)));
    };
    LegendBaseView.$t = markType(LegendBaseView, 'LegendBaseView');
    return LegendBaseView;
}(Base));
export { LegendBaseView };
