/*
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";
export class BubbleSeries extends ScatterBase {
    constructor() {
        super();
        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();
    }
    bf() {
        return new BubbleSeriesView(this);
    }
    il(a) {
        super.il(a);
        this.op = a;
    }
    isAttachedTo(a) {
        return this.aw == a;
    }
    ci() {
        return true;
    }
    hz(a) {
        super.hz(a);
        if (this.ow != null && this.oy != null && this.oy.count > 0) {
            for (let b = 0; b < this.oy.count; b++) {
                a(this.oy._inner[b]);
            }
        }
    }
    get ov() {
        return this.ox;
    }
    pl(a) {
        let b = typeCast(CustomPaletteBrushScale.$, this.oo);
        let c = typeCast(ValueBrushScale.$, this.oo);
        let 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;
    }
    get legendReady() {
        return this.ox != null && this.ox.count != 0;
    }
    get minScaleText() {
        return (Math.round(this.ox.minimum * 1000) / 1000).toString();
    }
    get maxScaleText() {
        return (Math.round(this.ox.maximum * 1000) / 1000).toString();
    }
    forScaleColors(a) {
        for (let b = 0; b < this.ox.count; b++) {
            if (this.oo == null || !this.oo.isReady) {
                return false;
            }
            let c = NaN;
            if (this.ou != null) {
                c = (this.ou.item(b) - this.ou.minimum) / (this.ou.maximum - this.ou.minimum);
            }
            let d = (this.ox.item(b) - this.ox.minimum) / (this.ox.maximum - this.ox.minimum);
            if (isNaN_(c)) {
                c = 0;
            }
            if (isNaN_(d)) {
                d = c;
            }
            let e = this.mm;
            let f = this.pl(b);
            let g = f != null ? f : e;
            a(g, d);
        }
        return true;
    }
    get ox() {
        return this.ot;
    }
    set ox(a) {
        if (this.ot != a) {
            let b = this.ox;
            this.ot = a;
            this.raisePropertyChanged("RadiusColumn", b, this.ox);
        }
    }
    get ow() {
        return this.os;
    }
    set ow(a) {
        if (this.os != a) {
            let b = this.ow;
            this.os = a;
            this.raisePropertyChanged("LabelColumn", b, this.ow);
        }
    }
    get ou() {
        return this.or;
    }
    set ou(a) {
        if (this.or != a) {
            let b = this.ou;
            this.or = a;
            this.raisePropertyChanged("FillColumn", b, this.ou);
        }
    }
    np(a, b, c, d, e) {
        if (c <= this.nb) {
            b.g = new Dictionary$2(Base.$, OwnedPoint.$, 1, c);
        }
        let f = this.nb;
        let g = this.bz;
        let h;
        let i;
        let j = this.getEffectiveViewport1(a);
        let k = new ScalerParams(0, d, e, this.mv.d, j);
        let l = new ScalerParams(0, d, e, this.mv.e, j);
        for (let m = 0; m < c; m++) {
            h = this.m4.item(m);
            i = this.m6.item(m);
            let n = { $type: Point_$type, x: this.mv.a.dt(h, k), y: this.mv.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;
                        })()));
                    }
                }
            }
        }
    }
    nt(a, b) {
        let c = typeCast(BubbleSeriesView.$, b);
        let d = c.cr;
        d.m = this.ox;
        super.nt(a, b);
        let 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();
    }
    pd() {
        if (this.bs == null) {
            return;
        }
        if (this.aw == null) {
            return;
        }
        if (this.aw.isItemwise) {
            let a = this.aw;
            this.aw.clearLegendItemsForSeries(this);
            this.pc();
            a.renderItemwiseContent(this);
        }
        if (this.aw.isScale) {
            let b = this.aw;
            b.restoreOriginalState();
            b.initializeLegend(this);
        }
    }
    static oz(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 o0(a, b, c, d, logBase_, e) {
        let f = logBase(e, logBase_);
        let g = logBase(a, logBase_);
        let h = logBase(b, logBase_);
        return BubbleSeries.oz(g, h, c, d, f);
    }
    du(a, b, c) {
        let d = super.du(a, b, c);
        if (this.ox == null || this.bz == null || this.ox.count == 0 || this.bz.count != this.ox.count) {
            d = false;
        }
        return d;
    }
    m9(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;
    }
    pc() {
        let a = typeCast(IChartLegend_$type, this.aw);
        if (a == null || this.bz == null) {
            return;
        }
        this.oy = new List$1(UIElement.$, 0);
        let b = this.bz;
        for (let c = 0; c < b.count; c++) {
            let d = null;
            if (typeCast(ValueBrushScale.$, this.oo) !== null && this.ou != null) {
                d = this.oo.getBrushByIndex(c, this.ou);
            }
            else if (typeCast(CustomPaletteBrushScale.$, this.oo) !== null) {
                d = this.oo.getBrush1(c, b.count);
            }
            else if (this.oo != null) {
                d = this.oo.getBrush(c);
            }
            let e = new ContentControl();
            let f = this.ow != null && this.ow.item(c) != null ? this.ow.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.jp;
            if (g == null) {
                g = this.bi.bi(this.bk);
            }
            e.ah = g;
            this.oy.add(e);
        }
    }
    hr(a, b, c, d) {
        super.hr(a, b, c, d);
        this.pd();
    }
    h5() {
        super.h5();
        this.pd();
    }
    ia() {
        super.ia();
        this.pd();
    }
    ir(a, b, c, d) {
        super.ir(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":
                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.pd();
                break;
            case "SeriesViewer":
                this.pd();
                break;
        }
    }
    pf(a, b, c, d) {
        let e = this.ox.minimum;
        let f = this.ox.maximum;
        let 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;
        }
        let h = DeviceUtils.c(g);
        let i = this.oq || ((() => {
            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.o0(e, f, j, k, l, b._inner[m]);
                }
            }
            else {
                for (let n = 0; n < b.count; n++) {
                    b._inner[n] = BubbleSeries.oz(e, f, j, k, b._inner[n]);
                }
            }
        }
        let o = 1;
        o = DeviceUtils.d();
        let p = this.oq == 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]);
            }
        }
    }
    pe(a) {
        if (this.oo != null && !this.oo.n.contains(this)) {
            this.oo.n.add(this);
        }
        let b = typeCast(CustomPaletteBrushScale.$, this.oo);
        let c = typeCast(ValueBrushScale.$, this.oo);
        let d = this.oo;
        let e = this.oo == null || !this.oo.isReady || (c != null && this.o2 == null);
        if (e) {
            this.op.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.ou);
                }
                else if (d != null) {
                    l = d.getBrush(m);
                }
                k.itemBrush = l;
            }
        }
    }
    get_cy() {
        return false;
    }
    get actualMarkerBrush() {
        return this.mm;
    }
}
BubbleSeries.$t = markType(BubbleSeries, 'BubbleSeries', ScatterBase.$, [IScaleLegendSeries_$type]);
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.$]]);
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.oz(0, b - 1, 0, this.brushes.count - 1, a);
        return this.u(c);
    }
    get_isReady() {
        return this.brushes != null && this.brushes.count > 0;
    }
}
CustomPaletteBrushScale.$t = markType(CustomPaletteBrushScale, 'CustomPaletteBrushScale', BrushScale.$);
Type.dep(DependencyProperty, PropertyMetadata, CustomPaletteBrushScale, 'q', ['BrushSelectionMode', [BrushSelectionMode_$type, enumGetBox(BrushSelectionMode_$type, 0)]]);
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.ls = 2;
            this.e.bj = 4;
        }
    }
    cq() {
        return new BubbleMarkerManager((a) => this.cu.item(a), (a) => this.cs.mv.c.item(a), runOn(this, this.c4), runOn(this, this.cn), runOn(this, this.cv));
    }
    dc() {
        let a = this.cr;
        this.da.pf(a.n, a.o, this.br, this == this.e.bh);
        this.an();
    }
    dd() {
        let a = this.cr;
        this.da.pe(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.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.$);
