/*
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 { List$1 } from "./List$1";
import { LabelCollisionPlacementPositions_$type } from "./LabelCollisionPlacementPositions";
import { LabelInfo } from "./LabelInfo";
import { QuadTree } from "./QuadTree";
import { LinkedList$1 } from "./LinkedList$1";
import { QuadTreeBoundingBox } from "./QuadTreeBoundingBox";
import { Random } from "./Random";
import { HashSet$1 } from "./HashSet$1";
import { Base, runOn, Point_$type, markType } from "./type";
import { GeometryUtil } from "./GeometryUtil";
import { truncate } from "./number";
var LabelCollisionManager = /** @class */ (function (_super) {
    tslib_1.__extends(LabelCollisionManager, _super);
    function LabelCollisionManager(a, b, c, d, e, f, g, h) {
        var _this = _super.call(this) || this;
        _this.k = null;
        _this.g = null;
        _this.f = null;
        _this.d = null;
        _this.c = 0;
        _this.b = null;
        _this.o = 0;
        _this.u = 0;
        _this.t = 0;
        _this.i = new LinkedList$1(LabelInfo.$);
        _this.q = 0;
        _this.j = new List$1(LabelInfo.$, 0);
        _this.s = 0;
        _this.p = 0;
        _this.l = 0;
        _this.m = 0;
        _this.r = 0;
        _this.h = false;
        _this.d = new QuadTree(a, b, 8, 8);
        _this.c = e;
        _this.b = c;
        _this.k = new List$1(LabelCollisionPlacementPositions_$type, 0);
        _this.k.add(0);
        _this.k.add(5);
        _this.k.add(7);
        _this.k.add(2);
        _this.k.add(3);
        _this.k.add(6);
        _this.k.add(1);
        _this.k.add(4);
        _this.o = d;
        _this.u = f;
        _this.t = g;
        _this.h = h;
        return _this;
    }
    LabelCollisionManager.prototype.e = function (a, b, c, d) {
        var e = 0;
        var f = 0;
        switch (b) {
            case 0:
                e = a.o;
                f = ((a.p - a.m) - a.k) - this.o;
                break;
            case 4:
                e = ((a.o - a.n) - a.l) - this.o;
                f = ((a.p - a.m) - a.k) - this.o;
                break;
            case 5:
                e = ((a.o + a.n) + a.l) + this.o;
                f = ((a.p - a.m) - a.k) - this.o;
                break;
            case 1:
                e = ((a.o - a.n) - a.l) - this.o;
                f = a.p;
                break;
            case 2:
                e = ((a.o + a.n) + a.l) + this.o;
                f = a.p;
                break;
            case 3:
                e = a.o;
                f = ((a.p + a.m) + a.k) + this.o;
                break;
            case 6:
                e = ((a.o - a.n) - a.l) - this.o;
                f = ((a.p + a.m) + a.k) + this.o;
                break;
            case 7:
                e = ((a.o + a.n) + a.l) + this.o;
                f = ((a.p + a.m) + a.k) + this.o;
                break;
        }
        var g = new QuadTreeBoundingBox();
        g.b = e + c;
        g.c = f + d;
        g.e = a.l;
        g.d = a.k;
        g.g = a;
        return g;
    };
    LabelCollisionManager.prototype.aa = function (a, b, c, d, e) {
        this.z(a, b, c, d, e);
    };
    LabelCollisionManager.prototype.z = function (a, b, c, d, e) {
        this.d.d(a);
        a.b = b;
        a.c = c;
        a.e = d;
        a.d = e;
        this.d.c(a);
    };
    LabelCollisionManager.prototype.ae = function (a) {
        this.d.d(a);
    };
    LabelCollisionManager.prototype.w = function (a) {
        this.q++;
        this.i.f(a);
        a.e = this.i.d;
        a.c = new QuadTreeBoundingBox();
        a.c.b = a.o;
        a.c.c = a.p;
        a.c.e = a.n;
        a.c.d = a.m;
        a.c.g = a;
        this.d.c(a.c);
    };
    LabelCollisionManager.prototype.af = function (a) {
        if (a.c != null) {
            this.d.d(a.c);
            a.c = null;
        }
        if (a.b != null) {
            this.d.d(a.b);
            a.b = null;
        }
        if (a.e != null) {
            this.i.h(a.e);
            a.e = null;
        }
        this.q--;
    };
    LabelCollisionManager.prototype.ac = function (a) {
        this.d.d(a.c);
        a.c.b = a.o;
        a.c.c = a.p;
        a.c.e = a.n;
        a.c.d = a.m;
        this.d.c(a.c);
    };
    LabelCollisionManager.prototype.ab = function (a) {
        this.d.d(a.b);
        a.b.b = a.g;
        a.b.c = a.h;
        a.b.e = a.l;
        a.b.d = a.k;
        this.d.c(a.b);
    };
    LabelCollisionManager.prototype.ag = function () {
        this.p = 0;
        this.l = 4;
        this.m = 20;
        var a = truncate(Math.ceil((this.t / 33) * this.u));
        this.s = a;
        this.x();
    };
    LabelCollisionManager.prototype.x = function () {
        this.j.clear();
        this.ad(false);
        if (this.c == 0) {
            for (var a = 0; a < this.u; a++) {
                this.m = this.l * (this.s - this.p) / this.s;
                this.y();
                this.p++;
                if (this.p > this.s) {
                    if (this.f != null) {
                        this.f();
                    }
                    return;
                }
            }
            if (this.g != null) {
                this.g(this.j);
            }
            this.b.executeDelayed(runOn(this, this.x), 33);
        }
        else {
            if (this.c == 2) {
                var b = this.i.c;
                this.d.i();
                while (b != null) {
                    var c = b.c;
                    if (c.b != null) {
                        this.d.d(c.b);
                        c.b = null;
                        c.d = false;
                        c.f = null;
                    }
                    b = b.a;
                }
                this.r = 0;
                this.ad(true);
            }
            if (this.g != null) {
                this.g(this.j);
            }
        }
    };
    LabelCollisionManager.prototype.y = function () {
        var a = truncate(Math.round((this.q - 1) * LabelCollisionManager.v.nextDouble()));
        var b = 0;
        var c = this.i.c;
        var d = this.n(null);
        while (c != null) {
            var e = c.c;
            if (b == a) {
                var f = truncate(Math.round((this.k.count - 1) * LabelCollisionManager.v.nextDouble()));
                while (this.k._inner[f] == e.a) {
                    f = truncate(Math.round((this.k.count - 1) * LabelCollisionManager.v.nextDouble()));
                }
                var g = e.b;
                var h = this.e(e, this.k._inner[f], 0, 0);
                e.b = h;
                this.d.d(g);
                this.d.c(h);
                var i = this.n(e);
                var j = d > i;
                if (!j) {
                    var k = Math.exp(-(i - d) / this.m);
                    if (LabelCollisionManager.v.nextDouble() < k) {
                        j = true;
                    }
                }
                if (!j) {
                    this.d.d(h);
                    this.d.c(g);
                    e.b = g;
                    if (!this.h) {
                        this.n(e);
                    }
                }
                else {
                    e.a = this.k._inner[f];
                    e.g = e.b.b;
                    e.h = e.b.c;
                    this.j.add(e);
                }
                break;
            }
            b++;
            c = c.a;
        }
    };
    LabelCollisionManager.prototype.n = function (a) {
        if (this.h) {
            var b = 0;
            var c = this.i.c;
            while (c != null) {
                var d = c.c;
                var e = d.d;
                d.d = false;
                if (d.b != null) {
                    var f = this.d.e(d.b);
                    for (var g = 0; g < f.count; g++) {
                        var h = f._inner[g].l.c;
                        while (h != null) {
                            var i = h.c;
                            if (i.g == d) {
                                h = h.a;
                                continue;
                            }
                            if (d.b.a(i)) {
                                d.d = true;
                                b += d.b.f(i);
                            }
                            h = h.a;
                        }
                    }
                    if (d.d != e) {
                        this.j.add(d);
                    }
                }
                c = c.a;
            }
            return b;
        }
        else {
            if (a != null) {
                var j = new HashSet$1(LabelInfo.$, 0);
                var k = new HashSet$1(LabelInfo.$, 0);
                var l = new List$1(LabelInfo.$, 0);
                if (a.f != null) {
                    for (var m = 0; m < a.f.count; m++) {
                        j.add_1(a.f._inner[m]);
                    }
                }
                var n = a.d;
                a.d = false;
                if (a.b != null) {
                    var o = this.d.e(a.b);
                    for (var p = 0; p < o.count; p++) {
                        var q = o._inner[p].l.c;
                        while (q != null) {
                            var r = q.c;
                            if (r.g == a) {
                                q = q.a;
                                continue;
                            }
                            if (a.b.a(r)) {
                                a.d = true;
                                k.add_1(r.g);
                                l.add(r.g);
                            }
                            q = q.a;
                        }
                    }
                    if (a.d != n) {
                        this.j.add(a);
                    }
                    if (a.f != null) {
                        for (var s = a.f.count - 1; s >= 0; s--) {
                            if (!k.contains(a.f._inner[s])) {
                                var t = a.f._inner[s];
                                t.f.remove(a);
                                a.f.removeAt(s);
                                if (t.f.count == 0) {
                                    this.r--;
                                }
                                j.remove(t);
                                if (j.count == 0) {
                                    this.r--;
                                }
                            }
                        }
                    }
                    for (var u = 0; u < l.count; u++) {
                        var v = l._inner[u];
                        if (!j.contains(v)) {
                            if (a.f == null) {
                                a.f = new List$1(LabelInfo.$, 0);
                            }
                            a.f.add(v);
                            if (j.count == 0) {
                                this.r++;
                            }
                            j.add_1(v);
                            if (v.f == null) {
                                v.f = new List$1(LabelInfo.$, 0);
                            }
                            if (v.f.count == 0) {
                                this.r++;
                            }
                            v.f.add(a);
                        }
                    }
                }
            }
            return this.r;
        }
    };
    LabelCollisionManager.prototype.ad = function (a) {
        var b = this.i.c;
        while (b != null) {
            var c = b.c;
            if (c.b == null || a) {
                var d = false;
                if (a) {
                    var e = this.d.a(c.o, c.p, 1.5);
                    var f = this.a(e);
                    for (var g = 0; g < f.length; g++) {
                        var h = f[g];
                        var i = this.e(c, h, 0, 0);
                        var j = this.d.e(i);
                        d = true;
                        for (var k = 0; k < j.count; k++) {
                            var l = j._inner[k].l.c;
                            while (l != null) {
                                var m = l.c;
                                if (i.a(m)) {
                                    d = false;
                                    break;
                                }
                                l = l.a;
                            }
                        }
                        if (d) {
                            c.b = i;
                            c.a = h;
                            c.g = c.b.b;
                            c.h = c.b.c;
                            this.d.c(c.b);
                            this.j.add(c);
                            break;
                        }
                    }
                }
                if (!d) {
                    for (var n = 0; n < this.k.count; n++) {
                        var o = this.e(c, this.k._inner[n], 0, 0);
                        var p = this.d.e(o);
                        d = true;
                        for (var q = 0; q < p.count; q++) {
                            var r = p._inner[q].l.c;
                            while (r != null) {
                                var s = r.c;
                                if (o.a(s)) {
                                    d = false;
                                    break;
                                }
                                r = r.a;
                            }
                        }
                        if (d) {
                            c.b = o;
                            c.a = this.k._inner[n];
                            c.g = c.b.b;
                            c.h = c.b.c;
                            this.d.c(c.b);
                            this.j.add(c);
                            break;
                        }
                    }
                }
                if (!d) {
                    this.r++;
                    var t = Number.POSITIVE_INFINITY;
                    var u = 0;
                    var v = null;
                    for (var w = 0; w < this.k.count; w++) {
                        var x = 0;
                        var y = null;
                        if (!this.h) {
                            y = new List$1(LabelInfo.$, 0);
                        }
                        var z = this.e(c, this.k._inner[w], 0, 0);
                        var aa = this.d.e(z);
                        for (var ab = 0; ab < aa.count; ab++) {
                            var ac = aa._inner[ab].l.c;
                            while (ac != null) {
                                var ad = ac.c;
                                if (z.a(ad)) {
                                    x += z.f(ad);
                                    if (!this.h) {
                                        y.add(ad.g);
                                    }
                                }
                                ac = ac.a;
                            }
                        }
                        if (x < t) {
                            t = x;
                            u = w;
                            if (!this.h) {
                                v = y;
                            }
                        }
                    }
                    var ae = this.e(c, this.k._inner[u], 0, 0);
                    c.d = true;
                    if (!this.h) {
                        c.f = v;
                        for (var af = 0; af < v.count; af++) {
                            var ag = v._inner[af];
                            if (ag.f == null) {
                                ag.f = new List$1(LabelInfo.$, 0);
                            }
                            if (ag.f.count == 0) {
                                this.r++;
                            }
                            ag.f.add(c);
                        }
                    }
                    c.b = ae;
                    c.a = this.k._inner[u];
                    c.g = c.b.b;
                    c.h = c.b.c;
                    this.d.c(c.b);
                    this.j.add(c);
                }
            }
            b = b.a;
        }
    };
    LabelCollisionManager.prototype.a = function (a) {
        var b = new Array(3);
        var c = GeometryUtil.e({ $type: Point_$type, x: 0, y: 0 }, { $type: Point_$type, x: a.a, y: a.b });
        c = c * 180 / Math.PI;
        c = GeometryUtil.g(c);
        if ((c >= 0 && c <= 22.5) || (c >= 337.5 && c <= 360)) {
            b[0] = 2;
            b[1] = 5;
            b[2] = 7;
        }
        if (c >= 22.5 && c <= 67.5) {
            b[0] = 2;
            b[1] = 7;
            b[2] = 3;
        }
        if (c >= 67.5 && c <= 112.5) {
            b[0] = 7;
            b[1] = 3;
            b[2] = 6;
        }
        if (c >= 112.5 && c <= 157.5) {
            b[0] = 3;
            b[1] = 6;
            b[2] = 1;
        }
        if (c >= 157.5 && c <= 202.5) {
            b[0] = 6;
            b[1] = 1;
            b[2] = 4;
        }
        if (c >= 202.5 && c <= 247.5) {
            b[0] = 1;
            b[1] = 4;
            b[2] = 0;
        }
        if (c >= 247.5 && c <= 292.5) {
            b[0] = 4;
            b[1] = 0;
            b[2] = 5;
        }
        if (c >= 292.5 && c <= 337.5) {
            b[0] = 0;
            b[1] = 5;
            b[2] = 2;
        }
        return b;
    };
    LabelCollisionManager.v = new Random(0);
    LabelCollisionManager.$t = markType(LabelCollisionManager, 'LabelCollisionManager');
    return LabelCollisionManager;
}(Base));
export { LabelCollisionManager };
/*<BeginStatics Name="Infragistics.LabelCollisionManager" />*/
/*<EndStatics Name="Infragistics.LabelCollisionManager" />*/
//# sourceMappingURL=LabelCollisionManager.js.map