/*
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 { Base, EventArgs, runOn, markType } from "igniteui-react-core/ES5/type";
import { KDTreeNode2D } from "./KDTreeNode2D";
import { Random } from "igniteui-react-core/ES5/Random";
import { KDTreeThunk } from "./KDTreeThunk";
import { Stack$1 } from "igniteui-react-core/ES5/Stack$1";
import { List$1 } from "igniteui-react-core/ES5/List$1";
import { KNearestResult } from "./KNearestResult";
import { SearchData } from "./SearchData";
import { intDivide, isNaN_ } from "igniteui-react-core/ES5/number";
/**
 * @hidden
 */
var KDTree2D = /** @class */ (function (_super) {
    tslib_1.__extends(KDTree2D, _super);
    function KDTree2D(a) {
        var _rest = [];
        for (var _i = 1; _i < arguments.length; _i++) {
            _rest[_i - 1] = arguments[_i];
        }
        var _this = _super.call(this) || this;
        _this.a = null;
        _this.h = null;
        _this.g = null;
        _this.k = 0;
        _this.o = {};
        _this.progressiveThunkCompleted = null;
        _this.e = false;
        _this.q = new Random(0);
        _this.d = null;
        a = (a == void 0) ? 0 : a;
        switch (a) {
            case 0:
                {
                    var c = _rest[0];
                    var d = _rest[1];
                    _this.d = _this.c(c, 0, c.length - 1, 0, d);
                }
                break;
            case 1: break;
        }
        return _this;
    }
    Object.defineProperty(KDTree2D.prototype, "p", {
        get: function () {
            return this.o;
        },
        enumerable: true,
        configurable: true
    });
    KDTree2D.b = function (a, b) {
        var c = new KDTree2D(1);
        c.d = new KDTreeNode2D();
        c.d.g = true;
        c.a = a;
        c.h = new Stack$1(KDTreeThunk.$);
        c.g = new List$1(KDTreeThunk.$, 0);
        var d = new KDTreeThunk();
        d.e = 0;
        d.b = a.length - 1;
        d.c = 0;
        d.d = b;
        d.a = c.d;
        c.h.h(d);
        return c;
    };
    KDTree2D.prototype.f = function (a) {
        if (this.h.f == 0 && this.g.count == 0) {
            this.a = null;
            return false;
        }
        if (this.h.f == 0 && this.g.count > 0) {
            return true;
        }
        this.k = this.h.d().c;
        while (this.h.f > 0 && this.h.d().c == this.k) {
            this.g.add(this.h.e());
        }
        if (a != null) {
            a.setTimeout(runOn(this, this.y), 0);
        }
        else {
            window.setTimeout(runOn(this, this.y), 0);
        }
        return true;
    };
    KDTree2D.prototype.s = function () {
        this.e = true;
    };
    KDTree2D.prototype.y = function () {
        var a;
        for (var b = 0; b < this.g.count; b++) {
            if (this.e || this.a == null) {
                return;
            }
            if (this.a.length == 0) {
                continue;
            }
            a = this.g._inner[b];
            this.v(a.a, this.a, a.e, a.b, a.c, a.d);
        }
        this.g.clear();
        if (this.progressiveThunkCompleted != null) {
            this.progressiveThunkCompleted(this, new EventArgs());
        }
    };
    KDTree2D.prototype.v = function (a, b, c, d, e, f) {
        a.g = false;
        a.f = (e % 2) == 0;
        a.h = (d - c);
        if (c == d) {
            a.b = b[c];
            return;
        }
        if (c > d) {
            a.g = true;
            return;
        }
        if ((d - c) + 1 <= f) {
            a.b = b[c];
            a.a = new Array((d - c) + 1);
            var g = 0;
            for (var h = c; h <= d; h++) {
                a.a[g++] = b[h];
            }
            return;
        }
        var i = Math.max(intDivide((d - c), 2), 1);
        var j = this.m(b, c, d, a.f, i);
        a.b = b[j];
        if (c <= j - 1) {
            a.c = ((function () {
                var $ret = new KDTreeNode2D();
                $ret.g = true;
                return $ret;
            })());
            a.c.h = ((j - 1) - c) + 1;
            this.h.h(((function () {
                var $ret = new KDTreeThunk();
                $ret.e = c;
                $ret.b = j - 1;
                $ret.c = e + 1;
                $ret.d = f;
                $ret.a = a.c;
                return $ret;
            })()));
        }
        else {
            a.c = null;
        }
        if (j + 1 <= d) {
            a.d = ((function () {
                var $ret = new KDTreeNode2D();
                $ret.g = true;
                return $ret;
            })());
            a.d.h = (d - (j + 1)) + 1;
            this.h.h(((function () {
                var $ret = new KDTreeThunk();
                $ret.e = j + 1;
                $ret.b = d;
                $ret.c = e + 1;
                $ret.d = f;
                $ret.a = a.d;
                return $ret;
            })()));
        }
        else {
            a.d = null;
        }
    };
    KDTree2D.prototype.c = function (a, b, c, d, e) {
        var f = new KDTreeNode2D();
        f.f = (d % 2) == 0;
        f.h = (c - b);
        if (b == c) {
            f.b = a[b];
            return f;
        }
        if (b > c) {
            return null;
        }
        if ((c - b) + 1 <= e) {
            f.b = a[b];
            f.a = new Array(c - b + 1);
            var g = 0;
            for (var h = b; h <= c; h++) {
                f.a[g++] = a[h];
            }
            return f;
        }
        var i = Math.max(intDivide((c - b), 2), 1);
        var j = this.m(a, b, c, f.f, i);
        f.b = a[j];
        f.c = this.c(a, b, j - 1, d + 1, e);
        f.d = this.c(a, j + 1, c, d + 1, e);
        return f;
    };
    KDTree2D.prototype.l = function (a, b, c, d, e) {
        var f = b ? a[e].a : a[e].b;
        var g = a[e];
        a[e] = a[d];
        a[d] = g;
        var h = c;
        for (var i = c; i < d; i++) {
            var j = void 0;
            if (b) {
                j = a[i].a;
            }
            else {
                j = a[i].b;
            }
            if (j <= f) {
                g = a[h];
                a[h] = a[i];
                a[i] = g;
                h++;
            }
        }
        g = a[d];
        a[d] = a[h];
        a[h] = g;
        return h;
    };
    KDTree2D.prototype.m = function (a, b, c, d, e) {
        if (b == c) {
            return b;
        }
        var f = this.q.next2(b, c);
        var g = this.l(a, d, b, c, f);
        var h = g - b + 1;
        if (h == e) {
            return g;
        }
        else if (e < h) {
            return this.m(a, b, g - 1, d, e);
        }
        else {
            return this.m(a, g + 1, c, d, e - h);
        }
    };
    KDTree2D.prototype.w = function (a, b, c, d) {
        this.x(a, b, c, d, this.d);
    };
    KDTree2D.prototype.x = function (a, b, c, d, e) {
        if (e == null || e.g) {
            return;
        }
        if (e.c == null && e.d == null) {
            this.r(a, b, c, e, e.b, true, 0, d);
            if (a.a) {
                return;
            }
            if (e.a != null && e.a.length > 0) {
                for (var f = 0; f < e.a.length; f++) {
                    this.r(a, b, c, e, e.a[f], false, f, d);
                    if (a.a) {
                        return;
                    }
                }
            }
            return;
        }
        this.r(a, b, c, e, e.b, true, 0, d);
        if (a.a) {
            return;
        }
        if (e.f) {
            if (b <= e.b.a) {
                this.x(a, b, c, d, e.c);
                if (a.a) {
                    return;
                }
                if (this.i(b, c, e.b.a, c) < a.c) {
                    this.x(a, b, c, d, e.d);
                }
                if (a.a) {
                    return;
                }
            }
            else {
                this.x(a, b, c, d, e.d);
                if (a.a) {
                    return;
                }
                if (this.i(b, c, e.b.a, c) < a.c) {
                    this.x(a, b, c, d, e.c);
                }
                if (a.a) {
                    return;
                }
            }
        }
        else {
            if (c <= e.b.b) {
                this.x(a, b, c, d, e.c);
                if (a.a) {
                    return;
                }
                if (this.i(b, c, b, e.b.b) < a.c) {
                    this.x(a, b, c, d, e.d);
                }
                if (a.a) {
                    return;
                }
            }
            else {
                this.x(a, b, c, d, e.d);
                if (a.a) {
                    return;
                }
                if (this.i(b, c, b, e.b.b) < a.c) {
                    this.x(a, b, c, d, e.c);
                }
                if (a.a) {
                    return;
                }
            }
        }
    };
    KDTree2D.prototype.r = function (a, b, c, d, e, f, g, h) {
        if (a.a) {
            return;
        }
        if (a.e > a.f) {
            a.a = true;
            return;
        }
        if (a.b.count < h) {
            if (isNaN_(a.d)) {
                a.d = this.i(b, c, e.a, e.b);
                a.c = a.d;
                a.g = 0;
            }
            a.b.add(((function () {
                var $ret = new KNearestResult();
                $ret.b = f;
                $ret.e = g;
                $ret.a = d;
                $ret.c = e.a;
                $ret.d = e.b;
                return $ret;
            })()));
            a.e++;
            var i = this.i(b, c, e.a, e.b);
            if (i < a.d) {
                a.d = i;
            }
            if (i > a.c) {
                a.c = i;
                a.g = a.b.count - 1;
            }
            return;
        }
        var j = 0;
        if (j < a.c) {
            if (j < a.d) {
                a.d = j;
            }
            a.b._inner[a.g] = ((function () {
                var $ret = new KNearestResult();
                $ret.b = f;
                $ret.e = g;
                $ret.a = d;
                $ret.c = e.a;
                $ret.d = e.b;
                return $ret;
            })());
            var k = 0;
            var l = 0;
            for (var m = 0; m < a.b.count; m++) {
                var n = this.i(b, c, a.b._inner[m].c, a.b._inner[m].d);
                if (n > k) {
                    k = n;
                    l = m;
                }
            }
            a.e++;
        }
    };
    KDTree2D.prototype.i = function (a, b, c, d) {
        return (a - c) * (a - c) + (b - d) * (b - d);
    };
    KDTree2D.prototype.t = function (a, b, c, d, e, f) {
        this.u(a, this.d, b, c, d, e, f, false, 0);
    };
    KDTree2D.prototype.u = function (a, b, c, d, e, f, g, h, i) {
        if (b == null) {
            return;
        }
        var j = b.e;
        if (i > c.g || ((g - f) < c.f && (e - d) < c.e)) {
            if (b.e == null) {
                b.e = new SearchData();
            }
            j = b.e;
            j.a = true;
            j.d = d;
            j.b = e;
            j.e = f;
            j.c = g;
            a.add(b);
            return;
        }
        if (j != null) {
            j.a = false;
        }
        if (b.c == null && b.d == null) {
            a.add(b);
            return;
        }
        var k;
        var l;
        var m;
        var n;
        var o;
        var p;
        var q;
        var r;
        if (b.f) {
            k = d;
            l = b.b.a;
            m = f;
            n = g;
            o = b.b.a;
            p = e;
            q = f;
            r = g;
        }
        else {
            k = d;
            l = e;
            m = f;
            n = b.b.b;
            o = d;
            p = e;
            q = b.b.b;
            r = g;
        }
        if (h) {
            a.add(b);
            this.u(a, b.c, c, k, l, m, n, true, i + 1);
            this.u(a, b.d, c, o, p, q, r, true, i + 1);
        }
        else {
            var s = false;
            if (k >= c.c && l <= c.a && m >= c.d && n <= c.b) {
                s = true;
                a.add(b);
                this.u(a, b.c, c, k, l, m, n, true, i + 1);
            }
            else if (!(c.c > l || c.a < k || c.d > n || c.b < m)) {
                s = true;
                a.add(b);
                this.u(a, b.c, c, k, l, m, n, false, i + 1);
            }
            if (o >= c.c && p <= c.a && q >= c.d && r <= c.b) {
                if (!s) {
                    a.add(b);
                }
                this.u(a, b.d, c, o, p, q, r, true, i + 1);
            }
            else if (!(c.c > p || c.a < o || c.d > r || c.b < q)) {
                if (!s) {
                    a.add(b);
                }
                this.u(a, b.d, c, o, p, q, r, false, i + 1);
            }
        }
    };
    KDTree2D.prototype.z = function () {
        this.n(this.d);
    };
    KDTree2D.prototype.n = function (a) {
        if (a == null) {
            return 0;
        }
        if (this.n(a.c) + this.n(a.d) != a.h) {
            var b = 0;
        }
        return a.h + 1;
    };
    KDTree2D.$t = markType(KDTree2D, 'KDTree2D');
    return KDTree2D;
}(Base));
export { KDTree2D };
