/*
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 { ScatterBase } from "./ScatterBase";
import { IScaleLegendSeries_$type } from "igniteui-react-core/ES5/IScaleLegendSeries";
import { List$1 } from "igniteui-react-core/ES5/List$1";
import { UIElement } from "igniteui-react-core/ES5/UIElement";
import { Base, Type, typeCast, Point_$type, fromEnum, markType, enumGetBox, runOn } from "igniteui-react-core/ES5/type";
import { DependencyProperty } from "igniteui-react-core/ES5/DependencyProperty";
import { SizeScale } from "./SizeScale";
import { BrushScale } from "./BrushScale";
import { ScatterFrame } from "./ScatterFrame";
import { IChartLegend_$type } from "igniteui-react-core/ES5/IChartLegend";
import { ValueBrushScale } from "./ValueBrushScale";
import { IFastItemsSource_$type } from "igniteui-react-core/ES5/IFastItemsSource";
import { ScatterBaseView } from "./ScatterBaseView";
import { ScalerParams } from "./ScalerParams";
import { Dictionary$2 } from "igniteui-react-core/ES5/Dictionary$2";
import { OwnedPoint } from "./OwnedPoint";
import { BubbleMarkerManager } from "./BubbleMarkerManager";
import { ContentControl } from "igniteui-react-core/ES5/ContentControl";
import { DataContext } from "igniteui-react-core/ES5/DataContext";
import { DeviceUtils } from "igniteui-react-core/ES5/DeviceUtils";
import { PropertyMetadata } from "igniteui-react-core/ES5/PropertyMetadata";
import { isNaN_, isInfinity, logBase } from "igniteui-react-core/ES5/number";
import { stringIsNullOrEmpty } from "igniteui-react-core/ES5/string";
import { BrushSelectionMode_$type } from "./BrushSelectionMode";
import { ScatterTrendLineManager } from "./ScatterTrendLineManager";
var BubbleSeries = /** @class */ (function (_super) {
    tslib_1.__extends(BubbleSeries, _super);
    function BubbleSeries() {
        var _this = _super.call(this) || this;
        _this.pn = null;
        _this.pm = null;
        _this.oy = null;
        _this.ot = null;
        _this.os = null;
        _this.or = null;
        _this.ab = BubbleSeries.$;
        _this.m0 = new ScatterFrame();
        _this.m2 = new ScatterFrame();
        _this.mz = new ScatterFrame();
        return _this;
    }
    BubbleSeries.prototype.bf = function () {
        return new BubbleSeriesView(this);
    };
    BubbleSeries.prototype.il = function (a) {
        _super.prototype.il.call(this, a);
        this.op = a;
    };
    BubbleSeries.prototype.isAttachedTo = function (a) {
        return this.aw == a;
    };
    BubbleSeries.prototype.ci = function () {
        return true;
    };
    BubbleSeries.prototype.hz = function (a) {
        _super.prototype.hz.call(this, a);
        if (this.ow != null && this.oy != null && this.oy.count > 0) {
            for (var b = 0; b < this.oy.count; b++) {
                a(this.oy._inner[b]);
            }
        }
    };
    Object.defineProperty(BubbleSeries.prototype, "ov", {
        get: function () {
            return this.ox;
        },
        enumerable: true,
        configurable: true
    });
    BubbleSeries.prototype.pl = function (a) {
        var b = typeCast(CustomPaletteBrushScale.$, this.oo);
        var c = typeCast(ValueBrushScale.$, this.oo);
        var d = this.oo;
        if (b != null && this.bz != null) {
            return b.getBrush1(a, this.bz.count);
        }
        else if (c != null) {
            return c.getBrushByIndex(a, this.ou);
        }
        else if (d != null) {
            return d.getBrush(a);
        }
        return null;
    };
    Object.defineProperty(BubbleSeries.prototype, "legendReady", {
        get: function () {
            return this.ox != null && this.ox.count != 0;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(BubbleSeries.prototype, "minScaleText", {
        get: function () {
            return (Math.round(this.ox.minimum * 1000) / 1000).toString();
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(BubbleSeries.prototype, "maxScaleText", {
        get: function () {
            return (Math.round(this.ox.maximum * 1000) / 1000).toString();
        },
        enumerable: true,
        configurable: true
    });
    BubbleSeries.prototype.forScaleColors = function (a) {
        for (var b = 0; b < this.ox.count; b++) {
            if (this.oo == null || !this.oo.isReady) {
                return false;
            }
            var c = NaN;
            if (this.ou != null) {
                c = (this.ou.item(b) - this.ou.minimum) / (this.ou.maximum - this.ou.minimum);
            }
            var d = (this.ox.item(b) - this.ox.minimum) / (this.ox.maximum - this.ox.minimum);
            if (isNaN_(c)) {
                c = 0;
            }
            if (isNaN_(d)) {
                d = c;
            }
            var e = this.mm;
            var f = this.pl(b);
            var g = f != null ? f : e;
            a(g, d);
        }
        return true;
    };
    Object.defineProperty(BubbleSeries.prototype, "ox", {
        get: function () {
            return this.ot;
        },
        set: function (a) {
            if (this.ot != a) {
                var b = this.ox;
                this.ot = a;
                this.raisePropertyChanged("RadiusColumn", b, this.ox);
            }
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(BubbleSeries.prototype, "ow", {
        get: function () {
            return this.os;
        },
        set: function (a) {
            if (this.os != a) {
                var b = this.ow;
                this.os = a;
                this.raisePropertyChanged("LabelColumn", b, this.ow);
            }
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(BubbleSeries.prototype, "ou", {
        get: function () {
            return this.or;
        },
        set: function (a) {
            if (this.or != a) {
                var b = this.ou;
                this.or = a;
                this.raisePropertyChanged("FillColumn", b, this.ou);
            }
        },
        enumerable: true,
        configurable: true
    });
    BubbleSeries.prototype.np = function (a, b, c, d, e) {
        if (c <= this.nb) {
            b.g = new Dictionary$2(Base.$, OwnedPoint.$, 1, c);
        }
        var f = this.nb;
        var g = this.bz;
        var h;
        var i;
        var j = this.getEffectiveViewport1(a);
        var k = new ScalerParams(0, d, e, this.mv.d, j);
        var l = new ScalerParams(0, d, e, this.mv.e, j);
        var _loop_1 = function (m) {
            h = this_1.m4.item(m);
            i = this_1.m6.item(m);
            var n = { $type: Point_$type, x: this_1.mv.a.dt(h, k), y: this_1.mv.b.dt(i, l) };
            if (!isInfinity(n.x) && !isInfinity(n.y)) {
                var o_1 = g.item(m);
                if (c <= f) {
                    if (!b.g.containsKey(o_1)) {
                        var p_1 = { $type: Point_$type, x: h, y: i };
                        b.g.addItem(o_1, ((function () {
                            var $ret = new OwnedPoint();
                            $ret.a = o_1;
                            $ret.b = p_1;
                            $ret.c = n;
                            return $ret;
                        })()));
                    }
                }
            }
        };
        var this_1 = this;
        for (var m = 0; m < c; m++) {
            _loop_1(m);
        }
    };
    BubbleSeries.prototype.nt = function (a, b) {
        var c = typeCast(BubbleSeriesView.$, b);
        var d = c.cr;
        d.m = this.ox;
        _super.prototype.nt.call(this, a, b);
        var e = Math.min(this.m4 != null ? this.m4.count : 0, this.m6 != null ? this.m6.count : 0);
        if (e < 1) {
            return;
        }
        c.dc();
        c.dd();
    };
    BubbleSeries.prototype.pd = function () {
        if (this.bs == null) {
            return;
        }
        if (this.aw == null) {
            return;
        }
        if (this.aw.isItemwise) {
            var a = this.aw;
            this.aw.clearLegendItemsForSeries(this);
            this.pc();
            a.renderItemwiseContent(this);
        }
        if (this.aw.isScale) {
            var b = this.aw;
            b.restoreOriginalState();
            b.initializeLegend(this);
        }
    };
    BubbleSeries.oz = function (a, b, c, d, e) {
        if (e <= a || isNaN_(e) || isInfinity(e)) {
            return c;
        }
        if (e >= b) {
            return d;
        }
        var f = c + ((d - c) / (b - a)) * (e - a);
        return f;
    };
    BubbleSeries.o0 = function (a, b, c, d, logBase_, e) {
        var f = logBase(e, logBase_);
        var g = logBase(a, logBase_);
        var h = logBase(b, logBase_);
        return BubbleSeries.oz(g, h, c, d, f);
    };
    BubbleSeries.prototype.du = function (a, b, c) {
        var d = _super.prototype.du.call(this, a, b, c);
        if (this.ox == null || this.bz == null || this.ox.count == 0 || this.bz.count != this.ox.count) {
            d = false;
        }
        return d;
    };
    BubbleSeries.prototype.m9 = function (a, b) {
        if (b != 3) {
            return true;
        }
        if (a == null) {
            return true;
        }
        if (this.ni == a || this.nm == a || this.o9 == a) {
            return true;
        }
        return false;
    };
    BubbleSeries.prototype.pc = function () {
        var _this = this;
        var a = typeCast(IChartLegend_$type, this.aw);
        if (a == null || this.bz == null) {
            return;
        }
        this.oy = new List$1(UIElement.$, 0);
        var b = this.bz;
        var _loop_2 = function (c) {
            var d = null;
            if (typeCast(ValueBrushScale.$, this_2.oo) !== null && this_2.ou != null) {
                d = this_2.oo.getBrushByIndex(c, this_2.ou);
            }
            else if (typeCast(CustomPaletteBrushScale.$, this_2.oo) !== null) {
                d = this_2.oo.getBrush1(c, b.count);
            }
            else if (this_2.oo != null) {
                d = this_2.oo.getBrush(c);
            }
            var e = new ContentControl();
            var f = this_2.ow != null && this_2.ow.item(c) != null ? this_2.ow.item(c).toString() : "";
            e.content = ((function () {
                var $ret = new DataContext();
                $ret.series = _this;
                $ret.item = b.item(c);
                $ret.itemBrush = d;
                $ret.itemLabel = f;
                return $ret;
            })());
            var g = this_2.jp;
            if (g == null) {
                g = this_2.bi.bi(this_2.bk);
            }
            e.ah = g;
            this_2.oy.add(e);
        };
        var this_2 = this;
        for (var c = 0; c < b.count; c++) {
            _loop_2(c);
        }
    };
    BubbleSeries.prototype.hr = function (a, b, c, d) {
        _super.prototype.hr.call(this, a, b, c, d);
        this.pd();
    };
    BubbleSeries.prototype.h5 = function () {
        _super.prototype.h5.call(this);
        this.pd();
    };
    BubbleSeries.prototype.ia = function () {
        _super.prototype.ia.call(this);
        this.pd();
    };
    BubbleSeries.prototype.ir = function (a, b, c, d) {
        var e_1, _a;
        _super.prototype.ir.call(this, a, b, c, d);
        switch (b) {
            case "FastItemsSource":
                if (typeCast(IFastItemsSource_$type, c) != null) {
                    c.deregisterColumn(this.ox);
                    c.deregisterColumn(this.ou);
                    c.deregisterColumn(this.ow);
                    this.ox = null;
                    this.ou = null;
                    this.ow = null;
                }
                if (typeCast(IFastItemsSource_$type, d) != null) {
                    this.ox = this.bw(this.o9);
                    if (!stringIsNullOrEmpty(this.o2)) {
                        this.ou = this.bw(this.o2);
                    }
                    this.ow = this.by(this.o6);
                }
                this.i4(false);
                this.pd();
                break;
            case BubbleSeries.$$p[3]:
                if (this.bz != null) {
                    this.bz.deregisterColumn(this.ox);
                    this.ox = this.bw(this.o9);
                    this.pd();
                }
                break;
            case "RadiusColumn":
                this.mw.ct.g$i();
                this.i4(false);
                this.ic();
                break;
            case BubbleSeries.$$p[4]:
                this.i4(false);
                this.ib();
                break;
            case "DiscreteLegendItemTemplate":
                this.pd();
                break;
            case BubbleSeries.$$p[1]:
                this.i4(false);
                this.pd();
                this.ib();
                break;
            case "FillColumn":
                this.i4(false);
                this.ib();
                break;
            case BubbleSeries.$$p[0]:
                if (this.bz != null) {
                    this.bz.deregisterColumn(this.ou);
                    this.ou = this.bw(this.o2);
                    this.pd();
                }
                break;
            case BubbleSeries.$$p[2]:
                if (this.bz != null) {
                    this.bz.deregisterColumn(this.ow);
                    this.ow = this.by(this.o6);
                    this.pd();
                }
                break;
            case "ActualLegend":
                var e = typeCast(IChartLegend_$type, c);
                if (e != null) {
                    e.clearLegendItemsForSeries(this);
                }
                if (e != null && e.isScale) {
                    var f = e;
                    var g = true;
                    var h = null;
                    if (this.bs != null) {
                        try {
                            for (var _b = tslib_1.__values(fromEnum(this.bs.series)), _c = _b.next(); !_c.done; _c = _b.next()) {
                                var i = _c.value;
                                if (i.legend == f) {
                                    h = i;
                                    g = false;
                                }
                            }
                        }
                        catch (e_1_1) { e_1 = { error: e_1_1 }; }
                        finally {
                            try {
                                if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
                            }
                            finally { if (e_1) throw e_1.error; }
                        }
                    }
                    if (g) {
                        f.restoreOriginalState();
                    }
                    else {
                        f.initializeLegend(typeCast(IScaleLegendSeries_$type, h));
                    }
                }
                this.pd();
                break;
            case "SeriesViewer":
                this.pd();
                break;
        }
    };
    BubbleSeries.prototype.pf = function (a, b, c, d) {
        var e = this.ox.minimum;
        var f = this.ox.maximum;
        var g = c.width;
        if (!this.bi.br.isEmpty) {
            g = this.bi.br.width;
        }
        else if (this.bs != null && !this.bs.m0.isEmpty) {
            g = this.bs.m0.width;
        }
        var h = DeviceUtils.c(g);
        var i = this.oq || ((function () {
            var $ret = new SizeScale();
            $ret.minimumValue = h / 10;
            $ret.maximumValue = h / 5;
            return $ret;
        })());
        if (i != null) {
            var j = i.minimumValue;
            var k = i.maximumValue;
            var l = i.logarithmBase;
            if (!i.l.contains(this)) {
                i.l.add(this);
            }
            if (i.isLogarithmic) {
                for (var m = 0; m < b.count; m++) {
                    b._inner[m] = BubbleSeries.o0(e, f, j, k, l, b._inner[m]);
                }
            }
            else {
                for (var n = 0; n < b.count; n++) {
                    b._inner[n] = BubbleSeries.oz(e, f, j, k, b._inner[n]);
                }
            }
        }
        var o = 1;
        o = DeviceUtils.d();
        var p = this.oq == null;
        if (d) {
            var q = c.width / g;
            for (var r = 0; r < b.count; r++) {
                if (p) {
                    b._inner[r] = b._inner[r] / o;
                }
                b._inner[r] = b._inner[r] * q;
            }
        }
        for (var s = 0; s < a.count; s++) {
            var t = a._inner[s];
            if (p && !d) {
                t.width = Math.max(0, b._inner[s] / o);
                t.height = Math.max(0, b._inner[s] / o);
            }
            else {
                t.width = Math.max(0, b._inner[s]);
                t.height = Math.max(0, b._inner[s]);
            }
        }
    };
    BubbleSeries.prototype.pe = function (a) {
        if (this.oo != null && !this.oo.n.contains(this)) {
            this.oo.n.add(this);
        }
        var b = typeCast(CustomPaletteBrushScale.$, this.oo);
        var c = typeCast(ValueBrushScale.$, this.oo);
        var d = this.oo;
        var e = this.oo == null || !this.oo.isReady || (c != null && this.o2 == null);
        if (e) {
            this.op.db();
            var f = typeCast(BubbleSeriesView.$, this.bh);
            if (f != null) {
                f.db();
            }
            return;
        }
        var g = a.count;
        var h = this.bz;
        for (var i = 0; i < g; i++) {
            var j = a._inner[i];
            var k = typeCast(DataContext.$, j.content);
            if (k != null) {
                var l = null;
                var m = h.indexOf(k.item);
                if (b != null) {
                    l = b.getBrush1(m, h.count);
                }
                else if (c != null) {
                    l = c.getBrushByIndex(m, this.ou);
                }
                else if (d != null) {
                    l = d.getBrush(m);
                }
                k.itemBrush = l;
            }
        }
    };
    BubbleSeries.prototype.get_cy = function () {
        return false;
    };
    Object.defineProperty(BubbleSeries.prototype, "actualMarkerBrush", {
        get: function () {
            return this.mm;
        },
        enumerable: true,
        configurable: true
    });
    BubbleSeries.$t = markType(BubbleSeries, 'BubbleSeries', ScatterBase.$, [IScaleLegendSeries_$type]);
    return BubbleSeries;
}(ScatterBase));
export { BubbleSeries };
Type.dep(DependencyProperty, PropertyMetadata, BubbleSeries, 'raisePropertyChanged', ['FillMemberPath:o2:pg', [2], 'FillScale:oo:ph', [BrushScale.$], 'LabelMemberPath:o6:pi', [2], 'RadiusMemberPath:o9:pj', [2, null], 'RadiusScale:oq:pk', [SizeScale.$]]);
var CustomPaletteBrushScale = /** @class */ (function (_super) {
    tslib_1.__extends(CustomPaletteBrushScale, _super);
    function CustomPaletteBrushScale() {
        return _super.call(this) || this;
    }
    CustomPaletteBrushScale.prototype.getBrush1 = function (a, b) {
        if (this.brushes == null || this.brushes.count == 0) {
            return null;
        }
        if (this.brushSelectionMode == 0) {
            return _super.prototype.getBrush.call(this, a % this.brushes.count);
        }
        var c = BubbleSeries.oz(0, b - 1, 0, this.brushes.count - 1, a);
        return this.u(c);
    };
    CustomPaletteBrushScale.prototype.get_isReady = function () {
        return this.brushes != null && this.brushes.count > 0;
    };
    CustomPaletteBrushScale.$t = markType(CustomPaletteBrushScale, 'CustomPaletteBrushScale', BrushScale.$);
    return CustomPaletteBrushScale;
}(BrushScale));
export { CustomPaletteBrushScale };
Type.dep(DependencyProperty, PropertyMetadata, CustomPaletteBrushScale, 'q', ['BrushSelectionMode', [BrushSelectionMode_$type, enumGetBox(BrushSelectionMode_$type, 0)]]);
var BubbleSeriesView = /** @class */ (function (_super) {
    tslib_1.__extends(BubbleSeriesView, _super);
    function BubbleSeriesView(a) {
        var _this = _super.call(this, a) || this;
        _this.da = null;
        _this.da = a;
        _this.ct = new ScatterTrendLineManager();
        return _this;
    }
    BubbleSeriesView.prototype.ap = function () {
        _super.prototype.ap.call(this);
        if (!this.n) {
            this.by.ls = 2;
            this.e.bj = 4;
        }
    };
    BubbleSeriesView.prototype.cq = function () {
        var _this = this;
        return new BubbleMarkerManager(function (a) { return _this.cu.item(a); }, function (a) { return _this.cs.mv.c.item(a); }, runOn(this, this.c4), runOn(this, this.cn), runOn(this, this.cv));
    };
    BubbleSeriesView.prototype.dc = function () {
        var a = this.cr;
        this.da.pf(a.n, a.o, this.br, this == this.e.bh);
        this.an();
    };
    BubbleSeriesView.prototype.dd = function () {
        var a = this.cr;
        this.da.pe(a.n);
    };
    BubbleSeriesView.prototype.db = function () {
        var e_2, _a;
        var a = this.cr;
        try {
            for (var _b = tslib_1.__values(fromEnum(a.n)), _c = _b.next(); !_c.done; _c = _b.next()) {
                var b = _c.value;
                var c = typeCast(DataContext.$, b.content);
                if (c != null) {
                    c.itemBrush = null;
                }
            }
        }
        catch (e_2_1) { e_2 = { error: e_2_1 }; }
        finally {
            try {
                if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
            }
            finally { if (e_2) throw e_2.error; }
        }
    };
    BubbleSeriesView.prototype.getDefaultTooltipTemplate = function () {
        var a = "<div class='ui-chart-default-tooltip-content'><span";
        var b = this.e.a0.a$q(this.e);
        if (!stringIsNullOrEmpty(b)) {
            a += " style='color:" + b + "'";
        }
        a += ">" + this.da.fr + "</span><br/><span>" + "(${item." + this.da.ni + "}, ${item." + this.da.nm + "})</span>";
        if (!stringIsNullOrEmpty(this.da.o9)) {
            a += "<span>, Radius: ${item." + this.da.o9 + "}</span>";
        }
        a += "</div>";
        return a;
    };
    BubbleSeriesView.$t = markType(BubbleSeriesView, 'BubbleSeriesView', ScatterBaseView.$);
    return BubbleSeriesView;
}(ScatterBaseView));
export { BubbleSeriesView };
