/*
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 { DependencyProperty } from "igniteui-react-core/ES2015/DependencyProperty";
import { Type, typeCast, Number_$type, Point_$type, fromEnum, runOn, delegateRemove, delegateCombine, markType } from "igniteui-react-core/ES2015/type";
import { ValueBrushScale } from "./ValueBrushScale";
import { FastItemIntColumn } from "igniteui-react-core/ES2015/FastItemIntColumn";
import { LinearContourValueResolver } from "./LinearContourValueResolver";
import { SeriesView } from "./SeriesView";
import { IFastItemsSource_$type } from "igniteui-react-core/ES2015/IFastItemsSource";
import { ScalerParams } from "./ScalerParams";
import { ContourBuilder } from "./ContourBuilder";
import { Clipper } from "igniteui-react-core/ES2015/Clipper";
import { List$1 } from "igniteui-react-core/ES2015/List$1";
import { RectUtil } from "igniteui-react-core/ES2015/RectUtil";
import { DataContext } from "igniteui-react-core/ES2015/DataContext";
import { Path } from "igniteui-react-core/ES2015/Path";
import { PropertyMetadata } from "igniteui-react-core/ES2015/PropertyMetadata";
import { boxArray$1 } from "igniteui-react-core/ES2015/array";
import { isNaN_ } from "igniteui-react-core/ES2015/number";
import { Pool$1 } from "igniteui-react-core/ES2015/Pool$1";
import { Brush } from "igniteui-react-core/ES2015/Brush";
import { PathGeometry } from "igniteui-react-core/ES2015/PathGeometry";
import { PathFigure } from "igniteui-react-core/ES2015/PathFigure";
import { BrushUtil } from "igniteui-react-core/ES2015/BrushUtil";
import { BrushCollection } from "igniteui-react-core/ES2015/BrushCollection";
import { PointCollectionUtil } from "igniteui-react-core/ES2015/PointCollectionUtil";
import { PolyLineSegment } from "igniteui-react-core/ES2015/PolyLineSegment";
import { Tuple$2 } from "igniteui-react-core/ES2015/Tuple$2";
import { GeometryUtil } from "igniteui-react-core/ES2015/GeometryUtil";
import { stringIsNullOrEmpty } from "igniteui-react-core/ES2015/string";
/**
 * @hidden
 */
export class ScatterContourSeries extends ScatterTriangulationSeries {
    constructor() {
        super();
        this.ne = null;
        this.na = null;
        FastItemIntColumn.k();
        this.ab = ScatterContourSeries.$;
        this.nb = new LinearContourValueResolver();
    }
    bf() {
        this.nc = new ScatterContourSeriesView(this);
        return this.nc;
    }
    get nf() {
        return this.ne;
    }
    set nf(a) {
        let b = this.nf != a;
        if (b) {
            let c = this.nf;
            this.ne = a;
            this.raisePropertyChanged("ValueColumn", c, this.nf);
        }
    }
    is(a, b, c, d) {
        super.is(a, b, c, d);
        switch (b) {
            case "FastItemsSource":
                let e = typeCast(IFastItemsSource_$type, c);
                if (e != null) {
                    e.deregisterColumn(this.nf);
                    this.nf = null;
                }
                let f = typeCast(IFastItemsSource_$type, d);
                if (f != null) {
                    this.nf = this.bw(this.ni);
                }
                this.i6(false);
                break;
            case ScatterContourSeries.$$p[1]:
                if (this.bz != null) {
                    this.bz.deregisterColumn(this.nf);
                    this.nf = this.bw(this.ni);
                }
                this.i6(false);
                this.ic();
                break;
            case "ValueResolver":
                this.i6(false);
                this.ic();
                break;
            case ScatterContourSeries.$$p[0]:
                let g = c;
                let h = d;
                if (g != null) {
                    g.unregisterSeries(this);
                }
                if (h != null) {
                    h.registerSeries(this);
                }
                this.i6(false);
                this.ic();
                break;
            case "ActualBrush":
                this.i6(false);
                this.ic();
                break;
        }
    }
    hp(a, b) {
        super.hp(a, b);
        if (a) {
            b.b0(a);
        }
    }
    i7(a) {
        super.i7(a);
        this.nl(this.bi);
    }
    i5(a, b, c, d, e) {
        super.i5(a, b, c, d, e);
        let f = this.a0.a$h.item(d);
        f.ay(c);
        if (this.cc(f)) {
            return;
        }
        this.nl(f);
    }
    nl(a) {
        if (this.cc(a)) {
            return;
        }
        let b;
        let c;
        let d = a;
        let e = a.getViewInfo(c, b);
        c = e.p0;
        b = e.p1;
        let f = new ScalerParams(1, b, c, this.lt.b8);
        f.c = this.getEffectiveViewport1(a);
        let g = new ScalerParams(1, b, c, this.lu.b8);
        g.c = this.getEffectiveViewport1(a);
        let h = this.lt;
        let i = this.lu;
        let j = this.l3.count;
        let k;
        let xArr_ = this.l6.asArray();
        k = (xArr_.slice(0));
        let l;
        let yArr_ = this.l7.asArray();
        l = (yArr_.slice(0));
        let m = this.nf.asArray();
        let n = this.l3.asArray();
        let o = this.l4.asArray();
        let p = this.l5.asArray();
        let q = this.nb.j(this.nf);
        let r = new List$1(Number_$type, 1, q).toArray();
        h.gh(boxArray$1(k), 0, k.length, f);
        i.gh(boxArray$1(l), 0, l.length, g);
        let s = r.length;
        let t = new Array(s);
        for (let u = 0; u < s; ++u) {
            t[u] = new ContourBuilder();
        }
        let v = c.top;
        let w = c.left;
        let x = c.right;
        let y = c.bottom;
        let z;
        let aa;
        let ab;
        let ac;
        let ad = k.length;
        for (let ae = 0; ae < j; ae++) {
            let af = n[ae];
            let ag = o[ae];
            let ah = p[ae];
            if ((af >= ad) || (ag >= ad) || (ah >= ad) || (af < 0) || (ag < 0) || (ah < 0)) {
                continue;
            }
            let ai = k[af];
            let aj = l[af];
            let ak = { $type: Point_$type, x: ai, y: aj };
            let al = k[ag];
            let am = l[ag];
            let an = { $type: Point_$type, x: al, y: am };
            let ao = k[ah];
            let ap = l[ah];
            let aq = { $type: Point_$type, x: ao, y: ap };
            z = an.y < aq.y ? an.y : aq.y;
            z = ak.y < z ? ak.y : z;
            aa = an.y > aq.y ? an.y : aq.y;
            aa = ak.y > aa ? ak.y : aa;
            ab = an.x < aq.x ? an.x : aq.x;
            ab = ak.x < ab ? ak.x : ab;
            ac = an.x > aq.x ? an.x : aq.x;
            ac = ak.x > ac ? ak.x : ac;
            if (z < y && aa > v && ab < x && ac > w) {
                let ar = m[af];
                let as = m[ag];
                let at = m[ah];
                if (isNaN_(ar) || isNaN_(as) || isNaN_(at)) {
                    continue;
                }
                for (let au = 0; au < s; au++) {
                    let av = r[au];
                    let aw = t[au];
                    switch ((ar < av ? 1 : 0) | (as < av ? 2 : 0) | (at < av ? 4 : 0)) {
                        case 0: break;
                        case 1:
                            aw.d(aw.k(af, ak.x, ak.y, ar, ag, an.x, an.y, as, av), aw.k(ah, aq.x, aq.y, at, af, ak.x, ak.y, ar, av));
                            break;
                        case 2:
                            aw.d(aw.k(ag, an.x, an.y, as, ah, aq.x, aq.y, at, av), aw.k(af, ak.x, ak.y, ar, ag, an.x, an.y, as, av));
                            break;
                        case 3:
                            aw.d(aw.k(ag, an.x, an.y, as, ah, aq.x, aq.y, at, av), aw.k(ah, aq.x, aq.y, at, af, ak.x, ak.y, ar, av));
                            break;
                        case 4:
                            aw.d(aw.k(ah, aq.x, aq.y, at, af, ak.x, ak.y, ar, av), aw.k(ag, an.x, an.y, as, ah, aq.x, aq.y, at, av));
                            break;
                        case 5:
                            aw.d(aw.k(af, ak.x, ak.y, ar, ag, an.x, an.y, as, av), aw.k(ag, an.x, an.y, as, ah, aq.x, aq.y, at, av));
                            break;
                        case 6:
                            aw.d(aw.k(ah, aq.x, aq.y, at, af, ak.x, ak.y, ar, av), aw.k(af, ak.x, ak.y, ar, ag, an.x, an.y, as, av));
                            break;
                        case 7: break;
                    }
                }
            }
        }
        let ax = new Clipper(0, RectUtil.r(c, 2), false);
        let ay = new List$1(List$1.$.specialize(List$1.$.specialize(Point_$type)), 0);
        for (let az = 0; az < s; ++az) {
            let a0 = t[az];
            let a1 = new List$1(List$1.$.specialize(Point_$type), 0);
            let a2 = a0.c();
            for (let a3 = 0; a3 < a2.count; a3++) {
                let a4 = a2._inner[a3];
                let a5 = new List$1(Point_$type, 0);
                ax.i = a5;
                for (let a6 of fromEnum(a4)) {
                    ax.j({ $type: Point_$type, x: a0.g.item(a6), y: a0.h.item(a6) });
                }
                ax.i = null;
                a1.add(a5);
            }
            ay.add(a1);
        }
        d.bz(ay, r);
    }
    get nb() {
        return this.na;
    }
    set nb(a) {
        let b = a != this.nb;
        if (b) {
            if (this.na != null) {
                let c = this.na;
                c.updated = delegateRemove(c.updated, runOn(this, this.nm));
            }
            let d = this.na;
            this.na = a;
            if (this.na != null) {
                let e = this.na;
                e.updated = delegateCombine(e.updated, runOn(this, this.nm));
            }
            this.raisePropertyChanged("ValueResolver", d, a);
            this.i6(false);
        }
    }
    nm(a, b) {
        this.i6(false);
    }
    dv(a, b, c) {
        return super.dv(a, b, c) && this.nb != null && this.l6 != null && this.l7 != null && this.l3 != null && this.l4 != null && this.l5 != null && this.l6.count > 0 && this.l7.count > 0 && this.l3.count > 0 && this.l4.count > 0 && this.l5.count > 0;
    }
    ar(a, b) {
        let c = this.nc.b4(a, b);
        if (c != null) {
            return c.dataContext;
        }
        return null;
    }
}
ScatterContourSeries.$t = markType(ScatterContourSeries, 'ScatterContourSeries', ScatterTriangulationSeries.$);
Type.dep(DependencyProperty, PropertyMetadata, ScatterContourSeries, 'raisePropertyChanged', ['FillScale:nd:nn', [ValueBrushScale.$, null], 'ValueMemberPath:ni:no', [2, null]]);
/**
 * @hidden
 */
export class ScatterContourSeriesView extends SeriesView {
    constructor(a) {
        super(a);
        this.bw = null;
        this.bv = null;
        this.by = null;
        this.bx = new List$1(Brush.$, 0);
        this.bu = null;
        this.by = new List$1(Path.$, 0);
        this.bv = a;
        this.bw = ((() => {
            let $ret = new Pool$1(Path.$);
            $ret.create = runOn(this, this.b5);
            $ret.destroy = runOn(this, this.b3);
            $ret.activate = runOn(this, this.b1);
            $ret.disactivate = runOn(this, this.b2);
            return $ret;
        })());
    }
    b5() {
        let a = new Path();
        a.dataContext = ((() => {
            let $ret = new DataContext();
            $ret.series = this.e;
            return $ret;
        })());
        this.by.add(a);
        return a;
    }
    b1(a) {
        a._visibility = 0;
    }
    b2(a) {
        a._visibility = 1;
    }
    b3(a) {
        this.by.remove(a);
    }
    bz(a, b) {
        let c = 0;
        this.bu = b;
        this.bx.clear();
        let d = this.bv.nd;
        if (d == null) {
            d = new ValueBrushScale();
            d.brushes.add(BrushUtil.h(this.e.kw, -0.5));
            d.brushes.add(this.e.kw);
        }
        else if (d.brushes == null) {
            d.brushes = new BrushCollection();
            d.brushes.add(BrushUtil.h(this.e.kw, -0.5));
            d.brushes.add(this.e.kw);
        }
        else if (d.brushes.count == 0) {
            d.brushes.add(BrushUtil.h(this.e.kw, -0.5));
            d.brushes.add(this.e.kw);
        }
        for (let e = 0; e < b.length; e++) {
            let f = b[e];
            let g = a._inner[c];
            let h = new PathGeometry();
            for (let i = 0; i < g.count; i++) {
                let j = g._inner[i];
                if (j.count == 0) {
                    continue;
                }
                let k = PointCollectionUtil.f(j);
                let l = ((() => {
                    let $ret = new PathFigure();
                    $ret.isClosed = false;
                    $ret.isFilled = false;
                    $ret.startPoint = k._inner[0];
                    return $ret;
                })());
                l._segments.add(((() => {
                    let $ret = new PolyLineSegment();
                    $ret.points = k;
                    return $ret;
                })()));
                h.b.add(l);
            }
            this.bw.item(c).aj = h;
            if (this.bw.item(c).dataContext != null) {
                this.bw.item(c).dataContext.item = f;
            }
            if (d != null) {
                let m = d.getBrushByValue(f, this.bv.nf);
                if (m == null) {
                    m = this.e.kw;
                }
                this.bx.add(m);
            }
            else {
                this.bx.add(this.e.kw);
            }
            c++;
        }
        this.bw.count = c;
        this.an();
    }
    bb(a, b) {
        super.bb(a, b);
        let c = a;
        c._stroke = this.bx._inner[b];
        c.ac = this.e.ev;
        if (c.y != null) {
            this.h.i(c, c.y);
        }
    }
    bc(a, b) {
        super.bc(a, b);
        let c = a;
        let d = this.bm(b);
        c._stroke = d;
        c.ac = this.e.ev + 3;
    }
    a3(a, b) {
        super.a3(a, b);
        for (let c = 0; c < this.by.count; c++) {
            let d = this.by._inner[c];
            if (d._visibility != 1) {
                this.ba(d, c, b);
                a.s(d);
            }
        }
    }
    b0(a) {
        this.bw.count = 0;
        this.an();
    }
    ai(a) {
        super.ai(a);
        let b = new List$1(Tuple$2.$.specialize(Number_$type, Path.$), 0);
        if (this.bu != null) {
            for (let c = 0; c < this.bw.count; c++) {
                b.add(new Tuple$2(Number_$type, Path.$, this.bu[c], this.bw.item(c)));
            }
        }
        b.aa((d, e) => {
            if (d.c < e.c) {
                return -1;
            }
            else if (d.c > e.c) {
                return 1;
            }
            return 0;
        });
        for (let d = 0; d < b.count; d++) {
            let e = typeCast(Path.$, b._inner[d].d);
            if (e._visibility != 1) {
                this.e.a2.exportPathData(a, e, "contour" + d, ["Main "]);
            }
        }
    }
    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.bv.fs + "</span><br/>";
        if (!stringIsNullOrEmpty(this.bv.ni)) {
            a += "<span>Value: ${item}</span>";
        }
        a += "</div>";
        return a;
    }
    b4(a, b) {
        for (let c = 0; c < this.by.count; c++) {
            let d = this.by._inner[c];
            let e = typeCast(PathGeometry.$, d.aj);
            if (e != null) {
                for (let f = 0; f < e.b.count; f++) {
                    let g = e.b._inner[f];
                    let h = e.b._inner[f]._startPoint;
                    for (let i = 0; i < g._segments.count; i++) {
                        let j = typeCast(PolyLineSegment.$, g._segments._inner[i]);
                        if (j != null) {
                            let k = this.bv.ev / 2 + this.bv.eb(b);
                            if (j._points.count > 0 && GeometryUtil.b(a, h, j._points, k)) {
                                return d;
                            }
                        }
                    }
                }
            }
        }
        return null;
    }
}
ScatterContourSeriesView.$t = markType(ScatterContourSeriesView, 'ScatterContourSeriesView', SeriesView.$);
