/*
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";
/**
 * @hidden
 */
var BubbleSeries = /** @class */ (function (_super) {
    tslib_1.__extends(BubbleSeries, _super);
    function BubbleSeries() {
        var _this = _super.call(this) || this;
        _this.pp = null;
        _this.po = null;
        _this.o0 = null;
        _this.ov = null;
        _this.ou = null;
        _this.ot = null;
        _this.ab = BubbleSeries.$;
        _this.m2 = new ScatterFrame();
        _this.m4 = new ScatterFrame();
        _this.m1 = new ScatterFrame();
        return _this;
    }
    BubbleSeries.prototype.bf = function () {
        return new BubbleSeriesView(this);
    };
    BubbleSeries.prototype.im = function (a) {
        _super.prototype.im.call(this, a);
        this.or = a;
    };
    BubbleSeries.prototype.isAttachedTo = function (a) {
        return this.aw == a;
    };
    BubbleSeries.prototype.ci = function () {
        return true;
    };
    BubbleSeries.prototype.h0 = function (a) {
        _super.prototype.h0.call(this, a);
        if (this.oy != null && this.o0 != null && this.o0.count > 0) {
            for (var b = 0; b < this.o0.count; b++) {
                a(this.o0._inner[b]);
            }
        }
    };
    Object.defineProperty(BubbleSeries.prototype, "ox", {
        get: function () {
            return this.oz;
        },
        enumerable: true,
        configurable: true
    });
    BubbleSeries.prototype.pn = function (a) {
        var b = typeCast(CustomPaletteBrushScale.$, this.oq);
        var c = typeCast(ValueBrushScale.$, this.oq);
        var d = this.oq;
        if (b != null && this.bz != null) {
            return b.getBrush1(a, this.bz.count);
        }
        else if (c != null) {
            return c.getBrushByIndex(a, this.ow);
        }
        else if (d != null) {
            return d.getBrush(a);
        }
        return null;
    };
    Object.defineProperty(BubbleSeries.prototype, "legendReady", {
        get: function () {
            return this.oz != null && this.oz.count != 0;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(BubbleSeries.prototype, "minScaleText", {
        get: function () {
            return (Math.round(this.oz.minimum * 1000) / 1000).toString();
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(BubbleSeries.prototype, "maxScaleText", {
        get: function () {
            return (Math.round(this.oz.maximum * 1000) / 1000).toString();
        },
        enumerable: true,
        configurable: true
    });
    BubbleSeries.prototype.forScaleColors = function (a) {
        for (var b = 0; b < this.oz.count; b++) {
            if (this.oq == null || !this.oq.isReady) {
                return false;
            }
            var c = NaN;
            if (this.ow != null) {
                c = (this.ow.item(b) - this.ow.minimum) / (this.ow.maximum - this.ow.minimum);
            }
            var d = (this.oz.item(b) - this.oz.minimum) / (this.oz.maximum - this.oz.minimum);
            if (isNaN_(c)) {
                c = 0;
            }
            if (isNaN_(d)) {
                d = c;
            }
            var e = this.mo;
            var f = this.pn(b);
            var g = f != null ? f : e;
            a(g, d);
        }
        return true;
    };
    Object.defineProperty(BubbleSeries.prototype, "oz", {
        get: function () {
            return this.ov;
        },
        set: function (a) {
            if (this.ov != a) {
                var b = this.oz;
                this.ov = a;
                this.raisePropertyChanged("RadiusColumn", b, this.oz);
            }
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(BubbleSeries.prototype, "oy", {
        get: function () {
            return this.ou;
        },
        set: function (a) {
            if (this.ou != a) {
                var b = this.oy;
                this.ou = a;
                this.raisePropertyChanged("LabelColumn", b, this.oy);
            }
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(BubbleSeries.prototype, "ow", {
        get: function () {
            return this.ot;
        },
        set: function (a) {
            if (this.ot != a) {
                var b = this.ow;
                this.ot = a;
                this.raisePropertyChanged("FillColumn", b, this.ow);
            }
        },
        enumerable: true,
        configurable: true
    });
    BubbleSeries.prototype.nr = function (a, b, c, d, e) {
        if (c <= this.nd) {
            b.g = new Dictionary$2(Base.$, OwnedPoint.$, 1, c);
        }
        var f = this.nd;
        var g = this.bz;
        var h;
        var i;
        var j = this.getEffectiveViewport1(a);
        var k = new ScalerParams(0, d, e, this.mx.d, j);
        var l = new ScalerParams(0, d, e, this.mx.e, j);
        var _loop_1 = function (m) {
            h = this_1.m6.item(m);
            i = this_1.m8.item(m);
            var n = { $type: Point_$type, x: this_1.mx.a.dt(h, k), y: this_1.mx.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.nv = function (a, b) {
        var c = typeCast(BubbleSeriesView.$, b);
        var d = c.cr;
        d.m = this.oz;
        _super.prototype.nv.call(this, a, b);
        var e = Math.min(this.m6 != null ? this.m6.count : 0, this.m8 != null ? this.m8.count : 0);
        if (e < 1) {
            return;
        }
        c.dc();
        c.dd();
    };
    BubbleSeries.prototype.pf = function () {
        if (this.bs == null) {
            return;
        }
        if (this.aw == null) {
            return;
        }
        if (this.aw.isItemwise) {
            var a = this.aw;
            this.aw.clearLegendItemsForSeries(this);
            this.pe();
            a.renderItemwiseContent(this);
        }
        if (this.aw.isScale) {
            var b = this.aw;
            b.restoreOriginalState();
            b.initializeLegend(this);
        }
    };
    BubbleSeries.o1 = 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.o2 = function (a, b, c, d, logBase_, e) {
        var f = logBase(e, logBase_);
        var g = logBase(a, logBase_);
        var h = logBase(b, logBase_);
        return BubbleSeries.o1(g, h, c, d, f);
    };
    BubbleSeries.prototype.dv = function (a, b, c) {
        var d = _super.prototype.dv.call(this, a, b, c);
        if (this.oz == null || this.bz == null || this.oz.count == 0 || this.bz.count != this.oz.count) {
            d = false;
        }
        return d;
    };
    BubbleSeries.prototype.nb = function (a, b) {
        if (b != 3) {
            return true;
        }
        if (a == null) {
            return true;
        }
        if (this.nk == a || this.no == a || this.pb == a) {
            return true;
        }
        return false;
    };
    BubbleSeries.prototype.pe = function () {
        var _this = this;
        var a = typeCast(IChartLegend_$type, this.aw);
        if (a == null || this.bz == null) {
            return;
        }
        this.o0 = new List$1(UIElement.$, 0);
        var b = this.bz;
        var _loop_2 = function (c) {
            var d = null;
            if (typeCast(ValueBrushScale.$, this_2.oq) !== null && this_2.ow != null) {
                d = this_2.oq.getBrushByIndex(c, this_2.ow);
            }
            else if (typeCast(CustomPaletteBrushScale.$, this_2.oq) !== null) {
                d = this_2.oq.getBrush1(c, b.count);
            }
            else if (this_2.oq != null) {
                d = this_2.oq.getBrush(c);
            }
            var e = new ContentControl();
            var f = this_2.oy != null && this_2.oy.item(c) != null ? this_2.oy.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.jr;
            if (g == null) {
                g = this_2.bi.bi(this_2.bk);
            }
            e.ah = g;
            this_2.o0.add(e);
        };
        var this_2 = this;
        for (var c = 0; c < b.count; c++) {
            _loop_2(c);
        }
    };
    BubbleSeries.prototype.hs = function (a, b, c, d) {
        _super.prototype.hs.call(this, a, b, c, d);
        this.pf();
    };
    BubbleSeries.prototype.h6 = function () {
        _super.prototype.h6.call(this);
        this.pf();
    };
    BubbleSeries.prototype.ib = function () {
        _super.prototype.ib.call(this);
        this.pf();
    };
    BubbleSeries.prototype.is = function (a, b, c, d) {
        var e_1, _a;
        _super.prototype.is.call(this, a, b, c, d);
        switch (b) {
            case "FastItemsSource":
                if (typeCast(IFastItemsSource_$type, c) != null) {
                    c.deregisterColumn(this.oz);
                    c.deregisterColumn(this.ow);
                    c.deregisterColumn(this.oy);
                    this.oz = null;
                    this.ow = null;
                    this.oy = null;
                }
                if (typeCast(IFastItemsSource_$type, d) != null) {
                    this.oz = this.bw(this.pb);
                    if (!stringIsNullOrEmpty(this.o4)) {
                        this.ow = this.bw(this.o4);
                    }
                    this.oy = this.by(this.o8);
                }
                this.i6(false);
                this.pf();
                break;
            case BubbleSeries.$$p[3]:
                if (this.bz != null) {
                    this.bz.deregisterColumn(this.oz);
                    this.oz = this.bw(this.pb);
                    this.pf();
                }
                break;
            case "RadiusColumn":
                this.my.ct.g$i();
                this.i6(false);
                this.id();
                break;
            case BubbleSeries.$$p[4]:
                this.i6(false);
                this.ic();
                break;
            case "DiscreteLegendItemTemplate":
                this.pf();
                break;
            case BubbleSeries.$$p[1]:
                this.i6(false);
                this.pf();
                this.ic();
                break;
            case "FillColumn":
                this.i6(false);
                this.ic();
                break;
            case BubbleSeries.$$p[0]:
                if (this.bz != null) {
                    this.bz.deregisterColumn(this.ow);
                    this.ow = this.bw(this.o4);
                    this.pf();
                }
                break;
            case BubbleSeries.$$p[2]:
                if (this.bz != null) {
                    this.bz.deregisterColumn(this.oy);
                    this.oy = this.by(this.o8);
                    this.pf();
                }
                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.pf();
                break;
            case "SeriesViewer":
                this.pf();
                break;
        }
    };
    BubbleSeries.prototype.ph = function (a, b, c, d) {
        var e = this.oz.minimum;
        var f = this.oz.maximum;
        var g = c.width;
        if (!this.bi.br.isEmpty) {
            g = this.bi.br.width;
        }
        else if (this.bs != null && !this.bs.m3.isEmpty) {
            g = this.bs.m3.width;
        }
        var h = DeviceUtils.c(g);
        var i = this.os || ((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.o2(e, f, j, k, l, b._inner[m]);
                }
            }
            else {
                for (var n = 0; n < b.count; n++) {
                    b._inner[n] = BubbleSeries.o1(e, f, j, k, b._inner[n]);
                }
            }
        }
        var o = 1;
        o = DeviceUtils.d();
        var p = this.os == 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.pg = function (a) {
        if (this.oq != null && !this.oq.o.contains(this)) {
            this.oq.o.add(this);
        }
        var b = typeCast(CustomPaletteBrushScale.$, this.oq);
        var c = typeCast(ValueBrushScale.$, this.oq);
        var d = this.oq;
        var e = this.oq == null || !this.oq.isReady || (c != null && this.o4 == null);
        if (e) {
            this.or.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.ow);
                }
                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.mo;
        },
        enumerable: true,
        configurable: true
    });
    BubbleSeries.$t = markType(BubbleSeries, 'BubbleSeries', ScatterBase.$, [IScaleLegendSeries_$type]);
    return BubbleSeries;
}(ScatterBase));
export { BubbleSeries };
Type.dep(DependencyProperty, PropertyMetadata, BubbleSeries, 'raisePropertyChanged', ['FillMemberPath:o4:pi', [2], 'FillScale:oq:pj', [BrushScale.$], 'LabelMemberPath:o8:pk', [2], 'RadiusMemberPath:pb:pl', [2, null], 'RadiusScale:os:pm', [SizeScale.$]]);
/**
 * @hidden
 */
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.o1(0, b - 1, 0, this.brushes.count - 1, a);
        return this.v(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, 'r', ['BrushSelectionMode', [BrushSelectionMode_$type, enumGetBox(BrushSelectionMode_$type, 0)]]);
/**
 * @hidden
 */
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.lu = 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.mx.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.ph(a.n, a.o, this.br, this == this.e.bh);
        this.an();
    };
    BubbleSeriesView.prototype.dd = function () {
        var a = this.cr;
        this.da.pg(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.fs + "</span><br/><span>" + "(${item." + this.da.nk + "}, ${item." + this.da.no + "})</span>";
        if (!stringIsNullOrEmpty(this.da.pb)) {
            a += "<span>, Radius: ${item." + this.da.pb + "}</span>";
        }
        a += "</div>";
        return a;
    };
    BubbleSeriesView.$t = markType(BubbleSeriesView, 'BubbleSeriesView', ScatterBaseView.$);
    return BubbleSeriesView;
}(ScatterBaseView));
export { BubbleSeriesView };
