/*
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 { ScatterBase } from "./ScatterBase";
import { IScaleLegendSeries_$type } from "igniteui-react-core/ES2015/IScaleLegendSeries";
import { List$1 } from "igniteui-react-core/ES2015/List$1";
import { UIElement } from "igniteui-react-core/ES2015/UIElement";
import { Base, Type, typeCast, Point_$type, fromEnum, markType, enumGetBox, runOn } from "igniteui-react-core/ES2015/type";
import { DependencyProperty } from "igniteui-react-core/ES2015/DependencyProperty";
import { SizeScale } from "./SizeScale";
import { BrushScale } from "./BrushScale";
import { ScatterFrame } from "./ScatterFrame";
import { IChartLegend_$type } from "igniteui-react-core/ES2015/IChartLegend";
import { ValueBrushScale } from "./ValueBrushScale";
import { IFastItemsSource_$type } from "igniteui-react-core/ES2015/IFastItemsSource";
import { ScatterBaseView } from "./ScatterBaseView";
import { ScalerParams } from "./ScalerParams";
import { Dictionary$2 } from "igniteui-react-core/ES2015/Dictionary$2";
import { OwnedPoint } from "./OwnedPoint";
import { BubbleMarkerManager } from "./BubbleMarkerManager";
import { ContentControl } from "igniteui-react-core/ES2015/ContentControl";
import { DataContext } from "igniteui-react-core/ES2015/DataContext";
import { DeviceUtils } from "igniteui-react-core/ES2015/DeviceUtils";
import { PropertyMetadata } from "igniteui-react-core/ES2015/PropertyMetadata";
import { isNaN_, isInfinity, logBase } from "igniteui-react-core/ES2015/number";
import { stringIsNullOrEmpty } from "igniteui-react-core/ES2015/string";
import { BrushSelectionMode_$type } from "./BrushSelectionMode";
import { ScatterTrendLineManager } from "./ScatterTrendLineManager";
/**
 * @hidden
 */
export class BubbleSeries extends ScatterBase {
    constructor() {
        super();
        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();
    }
    bf() {
        return new BubbleSeriesView(this);
    }
    im(a) {
        super.im(a);
        this.or = a;
    }
    isAttachedTo(a) {
        return this.aw == a;
    }
    ci() {
        return true;
    }
    h0(a) {
        super.h0(a);
        if (this.oy != null && this.o0 != null && this.o0.count > 0) {
            for (let b = 0; b < this.o0.count; b++) {
                a(this.o0._inner[b]);
            }
        }
    }
    get ox() {
        return this.oz;
    }
    pn(a) {
        let b = typeCast(CustomPaletteBrushScale.$, this.oq);
        let c = typeCast(ValueBrushScale.$, this.oq);
        let 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;
    }
    get legendReady() {
        return this.oz != null && this.oz.count != 0;
    }
    get minScaleText() {
        return (Math.round(this.oz.minimum * 1000) / 1000).toString();
    }
    get maxScaleText() {
        return (Math.round(this.oz.maximum * 1000) / 1000).toString();
    }
    forScaleColors(a) {
        for (let b = 0; b < this.oz.count; b++) {
            if (this.oq == null || !this.oq.isReady) {
                return false;
            }
            let c = NaN;
            if (this.ow != null) {
                c = (this.ow.item(b) - this.ow.minimum) / (this.ow.maximum - this.ow.minimum);
            }
            let d = (this.oz.item(b) - this.oz.minimum) / (this.oz.maximum - this.oz.minimum);
            if (isNaN_(c)) {
                c = 0;
            }
            if (isNaN_(d)) {
                d = c;
            }
            let e = this.mo;
            let f = this.pn(b);
            let g = f != null ? f : e;
            a(g, d);
        }
        return true;
    }
    get oz() {
        return this.ov;
    }
    set oz(a) {
        if (this.ov != a) {
            let b = this.oz;
            this.ov = a;
            this.raisePropertyChanged("RadiusColumn", b, this.oz);
        }
    }
    get oy() {
        return this.ou;
    }
    set oy(a) {
        if (this.ou != a) {
            let b = this.oy;
            this.ou = a;
            this.raisePropertyChanged("LabelColumn", b, this.oy);
        }
    }
    get ow() {
        return this.ot;
    }
    set ow(a) {
        if (this.ot != a) {
            let b = this.ow;
            this.ot = a;
            this.raisePropertyChanged("FillColumn", b, this.ow);
        }
    }
    nr(a, b, c, d, e) {
        if (c <= this.nd) {
            b.g = new Dictionary$2(Base.$, OwnedPoint.$, 1, c);
        }
        let f = this.nd;
        let g = this.bz;
        let h;
        let i;
        let j = this.getEffectiveViewport1(a);
        let k = new ScalerParams(0, d, e, this.mx.d, j);
        let l = new ScalerParams(0, d, e, this.mx.e, j);
        for (let m = 0; m < c; m++) {
            h = this.m6.item(m);
            i = this.m8.item(m);
            let n = { $type: Point_$type, x: this.mx.a.dt(h, k), y: this.mx.b.dt(i, l) };
            if (!isInfinity(n.x) && !isInfinity(n.y)) {
                let o = g.item(m);
                if (c <= f) {
                    if (!b.g.containsKey(o)) {
                        let p = { $type: Point_$type, x: h, y: i };
                        b.g.addItem(o, ((() => {
                            let $ret = new OwnedPoint();
                            $ret.a = o;
                            $ret.b = p;
                            $ret.c = n;
                            return $ret;
                        })()));
                    }
                }
            }
        }
    }
    nv(a, b) {
        let c = typeCast(BubbleSeriesView.$, b);
        let d = c.cr;
        d.m = this.oz;
        super.nv(a, b);
        let 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();
    }
    pf() {
        if (this.bs == null) {
            return;
        }
        if (this.aw == null) {
            return;
        }
        if (this.aw.isItemwise) {
            let a = this.aw;
            this.aw.clearLegendItemsForSeries(this);
            this.pe();
            a.renderItemwiseContent(this);
        }
        if (this.aw.isScale) {
            let b = this.aw;
            b.restoreOriginalState();
            b.initializeLegend(this);
        }
    }
    static o1(a, b, c, d, e) {
        if (e <= a || isNaN_(e) || isInfinity(e)) {
            return c;
        }
        if (e >= b) {
            return d;
        }
        let f = c + ((d - c) / (b - a)) * (e - a);
        return f;
    }
    static o2(a, b, c, d, logBase_, e) {
        let f = logBase(e, logBase_);
        let g = logBase(a, logBase_);
        let h = logBase(b, logBase_);
        return BubbleSeries.o1(g, h, c, d, f);
    }
    dv(a, b, c) {
        let d = super.dv(a, b, c);
        if (this.oz == null || this.bz == null || this.oz.count == 0 || this.bz.count != this.oz.count) {
            d = false;
        }
        return d;
    }
    nb(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;
    }
    pe() {
        let a = typeCast(IChartLegend_$type, this.aw);
        if (a == null || this.bz == null) {
            return;
        }
        this.o0 = new List$1(UIElement.$, 0);
        let b = this.bz;
        for (let c = 0; c < b.count; c++) {
            let d = null;
            if (typeCast(ValueBrushScale.$, this.oq) !== null && this.ow != null) {
                d = this.oq.getBrushByIndex(c, this.ow);
            }
            else if (typeCast(CustomPaletteBrushScale.$, this.oq) !== null) {
                d = this.oq.getBrush1(c, b.count);
            }
            else if (this.oq != null) {
                d = this.oq.getBrush(c);
            }
            let e = new ContentControl();
            let f = this.oy != null && this.oy.item(c) != null ? this.oy.item(c).toString() : "";
            e.content = ((() => {
                let $ret = new DataContext();
                $ret.series = this;
                $ret.item = b.item(c);
                $ret.itemBrush = d;
                $ret.itemLabel = f;
                return $ret;
            })());
            let g = this.jr;
            if (g == null) {
                g = this.bi.bi(this.bk);
            }
            e.ah = g;
            this.o0.add(e);
        }
    }
    hs(a, b, c, d) {
        super.hs(a, b, c, d);
        this.pf();
    }
    h6() {
        super.h6();
        this.pf();
    }
    ib() {
        super.ib();
        this.pf();
    }
    is(a, b, c, d) {
        super.is(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":
                let e = typeCast(IChartLegend_$type, c);
                if (e != null) {
                    e.clearLegendItemsForSeries(this);
                }
                if (e != null && e.isScale) {
                    let f = e;
                    let g = true;
                    let h = null;
                    if (this.bs != null) {
                        for (let i of fromEnum(this.bs.series)) {
                            if (i.legend == f) {
                                h = i;
                                g = false;
                            }
                        }
                    }
                    if (g) {
                        f.restoreOriginalState();
                    }
                    else {
                        f.initializeLegend(typeCast(IScaleLegendSeries_$type, h));
                    }
                }
                this.pf();
                break;
            case "SeriesViewer":
                this.pf();
                break;
        }
    }
    ph(a, b, c, d) {
        let e = this.oz.minimum;
        let f = this.oz.maximum;
        let 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;
        }
        let h = DeviceUtils.c(g);
        let i = this.os || ((() => {
            let $ret = new SizeScale();
            $ret.minimumValue = h / 10;
            $ret.maximumValue = h / 5;
            return $ret;
        })());
        if (i != null) {
            let j = i.minimumValue;
            let k = i.maximumValue;
            let l = i.logarithmBase;
            if (!i.l.contains(this)) {
                i.l.add(this);
            }
            if (i.isLogarithmic) {
                for (let m = 0; m < b.count; m++) {
                    b._inner[m] = BubbleSeries.o2(e, f, j, k, l, b._inner[m]);
                }
            }
            else {
                for (let n = 0; n < b.count; n++) {
                    b._inner[n] = BubbleSeries.o1(e, f, j, k, b._inner[n]);
                }
            }
        }
        let o = 1;
        o = DeviceUtils.d();
        let p = this.os == null;
        if (d) {
            let q = c.width / g;
            for (let r = 0; r < b.count; r++) {
                if (p) {
                    b._inner[r] = b._inner[r] / o;
                }
                b._inner[r] = b._inner[r] * q;
            }
        }
        for (let s = 0; s < a.count; s++) {
            let 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]);
            }
        }
    }
    pg(a) {
        if (this.oq != null && !this.oq.o.contains(this)) {
            this.oq.o.add(this);
        }
        let b = typeCast(CustomPaletteBrushScale.$, this.oq);
        let c = typeCast(ValueBrushScale.$, this.oq);
        let d = this.oq;
        let e = this.oq == null || !this.oq.isReady || (c != null && this.o4 == null);
        if (e) {
            this.or.db();
            let f = typeCast(BubbleSeriesView.$, this.bh);
            if (f != null) {
                f.db();
            }
            return;
        }
        let g = a.count;
        let h = this.bz;
        for (let i = 0; i < g; i++) {
            let j = a._inner[i];
            let k = typeCast(DataContext.$, j.content);
            if (k != null) {
                let l = null;
                let 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;
            }
        }
    }
    get_cy() {
        return false;
    }
    get actualMarkerBrush() {
        return this.mo;
    }
}
BubbleSeries.$t = markType(BubbleSeries, 'BubbleSeries', ScatterBase.$, [IScaleLegendSeries_$type]);
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
 */
export class CustomPaletteBrushScale extends BrushScale {
    constructor() {
        super();
    }
    getBrush1(a, b) {
        if (this.brushes == null || this.brushes.count == 0) {
            return null;
        }
        if (this.brushSelectionMode == 0) {
            return super.getBrush(a % this.brushes.count);
        }
        let c = BubbleSeries.o1(0, b - 1, 0, this.brushes.count - 1, a);
        return this.v(c);
    }
    get_isReady() {
        return this.brushes != null && this.brushes.count > 0;
    }
}
CustomPaletteBrushScale.$t = markType(CustomPaletteBrushScale, 'CustomPaletteBrushScale', BrushScale.$);
Type.dep(DependencyProperty, PropertyMetadata, CustomPaletteBrushScale, 'r', ['BrushSelectionMode', [BrushSelectionMode_$type, enumGetBox(BrushSelectionMode_$type, 0)]]);
/**
 * @hidden
 */
export class BubbleSeriesView extends ScatterBaseView {
    constructor(a) {
        super(a);
        this.da = null;
        this.da = a;
        this.ct = new ScatterTrendLineManager();
    }
    ap() {
        super.ap();
        if (!this.n) {
            this.by.lu = 2;
            this.e.bj = 4;
        }
    }
    cq() {
        return new BubbleMarkerManager((a) => this.cu.item(a), (a) => this.cs.mx.c.item(a), runOn(this, this.c4), runOn(this, this.cn), runOn(this, this.cv));
    }
    dc() {
        let a = this.cr;
        this.da.ph(a.n, a.o, this.br, this == this.e.bh);
        this.an();
    }
    dd() {
        let a = this.cr;
        this.da.pg(a.n);
    }
    db() {
        let a = this.cr;
        for (let b of fromEnum(a.n)) {
            let c = typeCast(DataContext.$, b.content);
            if (c != null) {
                c.itemBrush = null;
            }
        }
    }
    getDefaultTooltipTemplate() {
        let a = "<div class='ui-chart-default-tooltip-content'><span";
        let 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.$);
