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