/*
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 { ScatterTriangulationSeries } from "./ScatterTriangulationSeries";
import { Type, runOn, delegateRemove, delegateCombine, typeCast, Point_$type, markType } from "igniteui-react-core/ES2015/type";
import { DependencyProperty } from "igniteui-react-core/ES2015/DependencyProperty";
import { Color } from "igniteui-react-core/ES2015/Color";
import { FastItemIntColumn } from "igniteui-react-core/ES2015/FastItemIntColumn";
import { IFastItemsSource_$type } from "igniteui-react-core/ES2015/IFastItemsSource";
import { Rect } from "igniteui-react-core/ES2015/Rect";
import { SeriesView } from "./SeriesView";
import { TriangleRasterizer } from "./TriangleRasterizer";
import { ScalerParams } from "./ScalerParams";
import { ObservableColorCollection } from "igniteui-react-core/ES2015/ObservableColorCollection";
import { ColorUtil } from "igniteui-react-core/ES2015/ColorUtil";
import { CustomPaletteColorScale } from "./CustomPaletteColorScale";
import { PropertyMetadata } from "igniteui-react-core/ES2015/PropertyMetadata";
import { truncate, intDivide } from "igniteui-react-core/ES2015/number";
import { boxArray$1 } from "igniteui-react-core/ES2015/array";
import { RenderingContext } from "igniteui-react-core/ES2015/RenderingContext";
import { CanvasViewRenderer } from "igniteui-react-core/ES2015/CanvasViewRenderer";
import { Rectangle } from "igniteui-react-core/ES2015/Rectangle";
import { stringIsNullOrEmpty } from "igniteui-react-core/ES2015/string";
export class ScatterAreaSeries extends ScatterTriangulationSeries {
    constructor() {
        super();
        this.nb = null;
        this.ng = null;
        this.m9 = null;
        this.na = null;
        this.m8 = [Color.u(255, 0, 0, 255), Color.u(255, 0, 255, 0), Color.u(255, 255, 0, 0)];
        FastItemIntColumn.k();
        this.ab = ScatterAreaSeries.$;
    }
    get nc() {
        return this.nb;
    }
    set nc(a) {
        let b = this.nb != a;
        if (b) {
            let c = this.nb;
            if (this.nb != null) {
                let d = this.nb;
                d.propertyChanged = delegateRemove(d.propertyChanged, runOn(this, this.np));
            }
            this.nb = a;
            if (this.nb != null) {
                let e = this.nb;
                e.propertyChanged = delegateCombine(e.propertyChanged, runOn(this, this.np));
            }
            this.raisePropertyChanged("ColorScale", c, this.nb);
        }
    }
    np(a, b) {
        this.i4(false);
    }
    get nh() {
        return this.ng;
    }
    set nh(a) {
        let b = this.nh != a;
        if (b) {
            let c = this.nh;
            this.ng = a;
            this.raisePropertyChanged("ColorColumn", c, this.nh);
        }
    }
    get_dn() {
        return true;
    }
    set_dn(a) {
        super.set_dn(a);
    }
    ir(a, b, c, d) {
        super.ir(a, b, c, d);
        switch (b) {
            case "FastItemsSource":
                let e = typeCast(IFastItemsSource_$type, c);
                if (e != null) {
                    e.deregisterColumn(this.nh);
                    this.nh = null;
                }
                let f = typeCast(IFastItemsSource_$type, d);
                if (f != null) {
                    this.nh = this.bw(this.nk);
                }
                this.i4(false);
                break;
            case ScatterAreaSeries.$$p[0]:
                if (this.bz != null) {
                    this.bz.deregisterColumn(this.nh);
                    this.nh = this.bw(this.nk);
                }
                this.i4(false);
                this.ib();
                break;
            case "ColorScale":
                this.i4(false);
                this.ib();
                break;
        }
    }
    i5(a) {
        super.i5(a);
        this.nq(this.bi);
    }
    i3(a, b, c, d, e) {
        super.i3(a, b, c, d, e);
        let f = this.a0.a$h.item(d);
        f.ay(c);
        if (this.cc(f)) {
            return;
        }
        this.nq(f);
    }
    nq(a) {
        if (this.cc(a)) {
            return;
        }
        let b;
        let c;
        let d = a.getViewInfo(c, b);
        c = d.p0;
        b = d.p1;
        let e = a;
        let f = null;
        let g = null;
        let h = this.lr;
        let i = this.ls;
        let j = this.getEffectiveViewport1(a);
        let k = new ScalerParams(1, b, c, h.b8);
        k.c = j;
        let l = new ScalerParams(1, b, c, i.b8);
        l.c = j;
        g = this.nf(a, k, l);
        if (g != null) {
            e.b5(g);
        }
        else {
            e.b8(f);
        }
    }
    nn(a) {
        let b;
        let c;
        let d = a.getViewInfo(b, c);
        b = d.p0;
        c = d.p1;
        let e = truncate(Math.round(b.width)) * truncate(Math.round(b.height));
        let f = a.bx;
        if (f == null || f.length != e) {
            f = new Array(e);
            for (let g = 0; g < e; g++) {
                f[g] = 0;
            }
        }
        else {
            for (let h = 0; h < e; h++) {
                f[h] = 0;
            }
        }
        a.bx = f;
    }
    fl(a) {
        let b = truncate(Math.round(this.lp.width));
        let c = truncate(Math.round(this.lp.height));
        let d = this.bi.bx;
        if (d == null || d.length != (b * c) || this.l1 == null || this.l1.count < 1 || this.l2 == null || this.l2.count < 1 || this.l3 == null || this.l3.count < 1) {
            return null;
        }
        let e = this.bs.m1;
        let f = (a.x - e.left) / e.width;
        let g = (a.y - e.top) / e.height;
        let h = truncate(Math.round(b * f));
        let i = truncate(Math.round(c * g));
        let j = (b * i) + h;
        if (j < 0 || j > d.length - 1) {
            return null;
        }
        let k = d[j] - 1;
        if (k < 0 || k > this.l1.count) {
            return null;
        }
        let l = this.m9.length;
        let m = this.l1.item(k);
        let n = this.l2.item(k);
        let o = this.l3.item(k);
        if ((m >= l) || (n >= l) || (o >= l)) {
            return null;
        }
        let p = (this.m9[m] - this.lp.left) / this.lp.width;
        let q = (this.na[m] - this.lp.top) / this.lp.height;
        let r = (this.m9[n] - this.lp.left) / this.lp.width;
        let s = (this.na[n] - this.lp.top) / this.lp.height;
        let t = (this.m9[o] - this.lp.left) / this.lp.width;
        let u = (this.na[o] - this.lp.top) / this.lp.height;
        let v = Math.pow(p - f, 2) + Math.pow(q - g, 2);
        let w = Math.pow(r - f, 2) + Math.pow(s - g, 2);
        let x = Math.pow(t - f, 2) + Math.pow(u - g, 2);
        if (v < w && v < x) {
            return this.bz.item(m);
        }
        if (w < v && w < x) {
            return this.bz.item(n);
        }
        if (x < v && x < w) {
            return this.bz.item(o);
        }
        return this.bz.item(m);
    }
    nf(a, b, c) {
        let d;
        let e;
        let f = a.getViewInfo(e, d);
        e = f.p0;
        d = f.p1;
        e = new Rect(0, 0, 0, e.width, e.height);
        b = new ScalerParams(0, b.e, e, b.a, b.c);
        c = new ScalerParams(0, c.e, e, c.a, c.c);
        c.b = this.ed();
        let g = a;
        let h = this.lr;
        let i = this.ls;
        let j = this.l1.count;
        let k;
        let xArr_ = this.l4.asArray();
        k = (xArr_.slice(0));
        let l;
        let yArr_ = this.l5.asArray();
        l = (yArr_.slice(0));
        let m = this.nh.asArray();
        let n = this.l1.asArray();
        let o = this.l2.asArray();
        let p = this.l3.asArray();
        this.m9 = k;
        this.na = l;
        h.gh(boxArray$1(k), 0, k.length, b);
        i.gh(boxArray$1(l), 0, l.length, c);
        let q = this.nd();
        let r = g.bz(b.d, q, this.nh);
        let s = e.top;
        let t = e.left;
        let u = e.right;
        let v = e.bottom;
        let w;
        let x;
        let y;
        let z;
        this.nn(g);
        let aa = g.bx;
        let ab = k.length;
        for (let ac = 0; ac < j; ac++) {
            let ad = n[ac];
            let ae = o[ac];
            let af = p[ac];
            if ((ad >= ab) || (ae >= ab) || (af >= ab) || (ad < 0) || (ae < 0) || (af < 0)) {
                continue;
            }
            let ag = k[ad];
            let ah = l[ad];
            let ai = { $type: Point_$type, x: ag, y: ah };
            let aj = k[ae];
            let ak = l[ae];
            let al = { $type: Point_$type, x: aj, y: ak };
            let am = k[af];
            let an = l[af];
            let ao = { $type: Point_$type, x: am, y: an };
            w = al.y < ao.y ? al.y : ao.y;
            w = ai.y < w ? ai.y : w;
            x = al.y > ao.y ? al.y : ao.y;
            x = ai.y > x ? ai.y : x;
            y = al.x < ao.x ? al.x : ao.x;
            y = ai.x < y ? ai.x : y;
            z = al.x > ao.x ? al.x : ao.x;
            z = ai.x > z ? ai.x : z;
            if (w < v && x > s && y < u && z > t) {
                let ap = m[ad];
                let aq = m[ae];
                let ar = m[af];
                r.p(ac, aa, ai, al, ao, ap, aq, ar);
            }
        }
        return r;
    }
    ni() {
        let a = new Color();
        let b = this.ku;
        if (b == null) {
            a = this.m8[this.fd % this.m8.length];
        }
        else {
            a = ColorUtil.j(b);
        }
        let c = new ObservableColorCollection();
        c.add(ColorUtil.o(a, -0.5));
        c.add(a);
        return c;
    }
    nd() {
        let a = this.nc;
        if (a == null) {
            a = ((() => {
                let $ret = new CustomPaletteColorScale();
                $ret.palette = this.ni();
                $ret.interpolationMode = 2;
                return $ret;
            })());
        }
        else if (typeCast(CustomPaletteColorScale.$, a) !== null) {
            let b = a;
            if (b.palette == null || b.palette.count == 0) {
                b.palette = this.ni();
            }
            a = b;
        }
        return a;
    }
    ho(a, b) {
        super.ho(a, b);
        let c = b;
        c.b4();
    }
    bf() {
        return new ScatterAreaSeriesView(this);
    }
    il(a) {
        super.il(a);
        this.ne = typeCast(ScatterAreaSeriesView.$, a);
    }
    no(a) {
        if (this.ne != null) {
            this.ne.b3(a);
        }
    }
}
ScatterAreaSeries.$t = markType(ScatterAreaSeries, 'ScatterAreaSeries', ScatterTriangulationSeries.$);
Type.dep(DependencyProperty, PropertyMetadata, ScatterAreaSeries, 'raisePropertyChanged', ['ColorMemberPath:nk:nr', [2, null]]);
export class ScatterAreaSeriesView extends SeriesView {
    constructor(a) {
        super(a);
        this.by = null;
        this.bu = null;
        this.b2 = null;
        this.b1 = null;
        this.b0 = null;
        this.by = a;
    }
    get bx() {
        return this.bu;
    }
    set bx(a) {
        this.bu = a;
    }
    b7(a, b) {
    }
    b3(a) {
    }
    bv() {
        return this.bw(true);
    }
    bw(a) {
        this.b6();
        let b = truncate(Math.round(this.br.left));
        let c = truncate(Math.round(this.br.left));
        let d = truncate(Math.round(this.br.width));
        let e = truncate(Math.round(this.br.height));
        let f = d.toString();
        let g = e.toString();
        if (this.b1.getAttribute("width") != f) {
            this.b1.setAttribute("width", d.toString());
        }
        if (this.b1.getAttribute("height") != g) {
            this.b1.setAttribute("height", e.toString());
        }
        let h = this.b0.g();
        if (a) {
            h.clearRect(0, 0, d, e);
        }
        this.b2 = h.getImageData(0, 0, d, e);
        let imageDataArray_ = this.b2.data;
        return imageDataArray_;
    }
    b6() {
        if (this.b1 == null) {
            this.b1 = this.e.e0.createElement("canvas");
            let a = this.e.e0.get2DCanvasContext(this.b1);
            this.b0 = new RenderingContext(new CanvasViewRenderer(), a);
        }
    }
    bz(a, b, c) {
        let d = truncate(Math.round(this.br.left));
        let e = truncate(Math.round(this.br.left));
        let f = truncate(Math.round(this.br.width));
        let g = truncate(Math.round(this.br.height));
        return new TriangleRasterizer(this.bv(), b, c.minimum, c.maximum, c, f, g);
    }
    b5(a) {
        this.b8(null);
    }
    b4() {
        if (this.b0 != null) {
            this.b0.k(this.br.left, this.br.top, this.br.width, this.br.height);
        }
        this.an();
    }
    a3(a, b) {
        super.a3(a, b);
        if (this.b1 == null) {
            return;
        }
        if (b) {
            let c = new Rectangle();
            c.n = this.br.left;
            c.o = this.br.top;
            c.width = this.br.width;
            c.height = this.br.height;
            c._fill = this.bl();
            a.v(c);
        }
        else {
            let d = truncate(Math.round(this.br.left));
            let e = truncate(Math.round(this.br.top));
            let f = truncate(Math.round(this.br.width));
            let g = truncate(Math.round(this.br.height));
            a.m(this.b1.getNativeElement(), 1, d, e, f, g);
        }
    }
    b8(a) {
        let b = truncate(Math.round(this.br.left));
        let c = truncate(Math.round(this.br.left));
        let d = truncate(Math.round(this.br.width));
        let e = truncate(Math.round(this.br.height));
        this.b6();
        this.b0.g().putImageData(this.b2, 0, 0);
        this.an();
    }
    ai(a) {
        super.ai(a);
        let b = truncate(Math.round(this.br.width));
        let c = truncate(Math.round(this.br.height));
        let d = this.bw(false);
        if (d == null || d.length == 0) {
            this.e.a2.exportSeriesPixelsData(a, null, b, c);
            return;
        }
        let e = new Array(intDivide(d.length, 4));
        let f;
        for (let g = 0; g < intDivide(d.length, 4); g++) {
            f = g * 4;
            e[g] = d[f + 3] << 24 | d[f] << 16 | d[f + 1] << 8 | d[f + 2];
        }
        this.e.a2.exportSeriesPixelsData(a, e, b, c);
    }
    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.by.fr + "</span><br/><span>" + "(${item." + this.by.mp + "}, ${item." + this.by.mt + "})</span>";
        if (!stringIsNullOrEmpty(this.by.nk)) {
            a += "<span>, Value: ${item." + this.by.nk + "}</span>";
        }
        a += "</div>";
        return a;
    }
}
ScatterAreaSeriesView.$t = markType(ScatterAreaSeriesView, 'ScatterAreaSeriesView', SeriesView.$);
