/*
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 { AnnotationLayer } from "./AnnotationLayer";
import { CalloutAnnotationCollection } from "./CalloutAnnotationCollection";
import { Series } from "./Series";
import { DependencyProperty } from "igniteui-react-core/ES5/DependencyProperty";
import { Brush } from "igniteui-react-core/ES5/Brush";
import { CalloutCollisionMode_$type } from "./CalloutCollisionMode";
import { CalloutLayerFrame } from "./CalloutLayerFrame";
import { HashSet$1 } from "igniteui-react-core/ES5/HashSet$1";
import { CalloutAnnotationWrapper } from "./CalloutAnnotationWrapper";
import { List$1 } from "igniteui-react-core/ES5/List$1";
import { LinkedList$1 } from "igniteui-react-core/ES5/LinkedList$1";
import { Dictionary$2 } from "igniteui-react-core/ES5/Dictionary$2";
import { LinkedListNode$1 } from "igniteui-react-core/ES5/LinkedListNode$1";
import { LabelCollisionManager } from "igniteui-react-core/ES5/LabelCollisionManager";
import { Rect } from "igniteui-react-core/ES5/Rect";
import { Type, Base, runOn, delegateCombine, fromEnum, enumGetBox, delegateRemove, Point_$type, markType, PointUtil } from "igniteui-react-core/ES5/type";
import { CalloutAnnotation } from "./CalloutAnnotation";
import { QuadTreeBoundingBox } from "igniteui-react-core/ES5/QuadTreeBoundingBox";
import { LabelCollisionInfo } from "igniteui-react-core/ES5/LabelCollisionInfo";
import { Size } from "igniteui-react-core/ES5/Size";
import { Pool$1 } from "igniteui-react-core/ES5/Pool$1";
import { PropertyMetadata } from "igniteui-react-core/ES5/PropertyMetadata";
import { AnnotationLayerView } from "./AnnotationLayerView";
import { BrushUtil } from "igniteui-react-core/ES5/BrushUtil";
import { DOMExecutionContext } from "igniteui-react-core/ES5/DOMExecutionContext";
import { isNaN_ } from "igniteui-react-core/ES5/number";
import { CalloutViewHelper } from "./CalloutViewHelper";
import { TranslateTransform } from "igniteui-react-core/ES5/TranslateTransform";
var CalloutLayer = /** @class */ (function (_super) {
    tslib_1.__extends(CalloutLayer, _super);
    function CalloutLayer() {
        var _this = _super.call(this) || this;
        _this.m4 = new HashSet$1(CalloutAnnotationWrapper.$, 0);
        _this.m6 = new List$1(CalloutAnnotationWrapper.$, 0);
        _this.mq = new CalloutAnnotationCollection();
        _this.m7 = new List$1(CalloutAnnotationWrapper.$, 0);
        _this.m5 = new LinkedList$1(CalloutAnnotationWrapper.$);
        _this.m3 = new Dictionary$2(CalloutAnnotationWrapper.$, LinkedListNode$1.$.specialize(CalloutAnnotationWrapper.$), 0);
        _this.mv = null;
        _this.oc = new Rect(0, 0, 0, 1, 1);
        _this.my = false;
        _this.m0 = false;
        _this.m1 = false;
        _this.m9 = 0;
        _this.mz = false;
        var a = new CalloutLayerFrame();
        var b = new CalloutLayerFrame();
        var c = new CalloutLayerFrame();
        var d = _this.fc / 1000;
        _this.lq = a;
        _this.lp = b;
        _this.lr = c;
        var e = _this.mq;
        e.collectionChanged = delegateCombine(e.collectionChanged, runOn(_this, _this.ns));
        var f = _this.mq;
        f.collectionResetting = delegateCombine(f.collectionResetting, runOn(_this, _this.nt));
        _this.ab = CalloutLayer.$;
        return _this;
    }
    CalloutLayer.prototype.nt = function (a, b) {
        this.m7.clear();
        this.i1(false);
    };
    CalloutLayer.prototype.ns = function (a, b) {
        switch (b.action) {
            case 0:
                if (b.newItems != null) {
                    for (var c = 0; c < b.newItems.count; c++) {
                        var d = b.newItems.item(c);
                        var e = new CalloutAnnotationWrapper();
                        e.b = d;
                        this.m7.insert(c + b.newStartingIndex, e);
                    }
                }
                break;
            case 1:
                if (b.oldItems != null) {
                    for (var f = 0; f < b.oldItems.count; f++) {
                        var g = this.m7._inner[b.oldStartingIndex].b;
                        this.m7.removeAt(b.oldStartingIndex);
                    }
                }
                break;
            case 2:
                if (b.oldItems != null) {
                    for (var h = 0; h < b.oldItems.count; h++) {
                        this.m7.removeAt(b.oldStartingIndex);
                    }
                }
                if (b.newItems != null) {
                    for (var i = 0; i < b.newItems.count; i++) {
                        var j = b.newItems.item(i);
                        var k = new CalloutAnnotationWrapper();
                        k.b = j;
                        this.m7.insert(i + b.newStartingIndex, k);
                    }
                }
                break;
            case 4:
                this.m7.clear();
                try {
                    for (var _a = tslib_1.__values(fromEnum(this.mq)), _b = _a.next(); !_b.done; _b = _a.next()) {
                        var l = _b.value;
                        var m = l;
                        var n = new CalloutAnnotationWrapper();
                        n.b = m;
                        this.m7.add(n);
                    }
                }
                catch (e_1_1) { e_1 = { error: e_1_1 }; }
                finally {
                    try {
                        if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
                    }
                    finally { if (e_1) throw e_1.error; }
                }
                break;
        }
        this.i1(false);
        var e_1, _c;
    };
    CalloutLayer.prototype.nv = function (a, b) {
        if (b.propertyName == CalloutAnnotation.$$p[8] || b.propertyName == CalloutAnnotation.$$p[9]) {
            var c = a.k;
            if (!this.m4.contains(c)) {
                this.m6.add(c);
                this.i1(false);
            }
        }
    };
    CalloutLayer.prototype.bf = function () {
        return new CalloutLayerView(this);
    };
    CalloutLayer.prototype.ii = function (a) {
        _super.prototype.ii.call(this, a);
        this.mt = a;
    };
    Object.defineProperty(CalloutLayer.prototype, "callouts", {
        get: function () {
            return this.mq;
        },
        enumerable: true,
        configurable: true
    });
    CalloutLayer.prototype.l7 = function (a, b) {
        if (this.mu == null || this.mu == a) {
            if (!this.aq.c) {
                this.i1(b);
            }
        }
    };
    CalloutLayer.prototype.l9 = function (a, b) {
        _super.prototype.l9.call(this, a, b);
        var c = a;
        c.k.clear();
        c.f.clear();
        c.g.clear();
        c.h.clear();
        c.i.clear();
        c.j.clear();
        c.n.clear();
        c.o.clear();
        c.l.clear();
        c.m.clear();
        var d = this.m8();
        var e = new List$1(CalloutAnnotationWrapper.$, 0);
        if (this.m2) {
            if (this.lu(this.mu)) {
                this.nx(this.mu, c, e);
            }
            else {
                try {
                    for (var _a = tslib_1.__values(fromEnum(this.bs.series)), _b = _a.next(); !_b.done; _b = _a.next()) {
                        var f = _b.value;
                        if (this.lu(f)) {
                            this.nx(f, c, e);
                        }
                    }
                }
                catch (e_2_1) { e_2 = { error: e_2_1 }; }
                finally {
                    try {
                        if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
                    }
                    finally { if (e_2) throw e_2.error; }
                }
            }
        }
        this.nw(c, d, e);
        var e_2, _c;
    };
    CalloutLayer.prototype.jh = function (a, b) {
        _super.prototype.jh.call(this, a, b);
        var c = b.width - this.oc.width;
        var d = b.height - this.oc.height;
        var e = 0;
        if (Math.abs(c) > Math.abs(d)) {
            e = c;
            this.m9 = c * this.lm.width;
        }
        else {
            e = d;
            this.m9 = d * this.lm.height;
        }
        if (e > 0 && this.m9 > 5) {
            this.m1 = true;
        }
        if (e < 0 && this.m9 > 5) {
            this.m0 = true;
        }
    };
    CalloutLayer.prototype.jg = function (a, b) {
        _super.prototype.jg.call(this, a, b);
        this.mz = true;
    };
    CalloutLayer.prototype.nw = function (a, b, c) {
        if (this.bs != null) {
            this.oc = this.bs.mk;
        }
        if (this.lm.isEmpty) {
            return;
        }
        if (this.mv == null) {
            this.mv = new LabelCollisionManager(this.lm.width, this.lm.height, this.mt.b2(), this.nb, this.mw(), this.ng(), this.nf(), false);
            this.mv.g = runOn(this, this.nu);
        }
        if (this.m0) {
            this.m0 = false;
            if (this.mv.y > 2) {
                this.my = true;
            }
        }
        if (this.m1) {
            this.m1 = false;
            if (this.m9 > 5) {
                this.my = true;
            }
        }
        if (this.mz) {
            this.mz = false;
            this.mv.ak(this.lm.width, this.lm.height);
        }
        var d = new HashSet$1(CalloutAnnotationWrapper.$, 0);
        var e = new HashSet$1(CalloutAnnotationWrapper.$, 0);
        var f = new List$1(CalloutAnnotationWrapper.$, 0);
        var g = new List$1(CalloutAnnotationWrapper.$, 0);
        var h = new HashSet$1(CalloutAnnotationWrapper.$, 0);
        for (var i = 0; i < b.count; i++) {
            h.add_1(b._inner[i]);
        }
        for (var j = 0; j < c.count; j++) {
            h.add_1(c._inner[j]);
        }
        var k = this.m5.c;
        while (k != null) {
            var l = k.a;
            if (!h.contains(k.c)) {
                g.add(k.c);
                e.add_1(k.c);
                var m = k.c.b;
                m.propertyChanged = delegateRemove(m.propertyChanged, runOn(this, this.nv));
                this.m5.h(k);
                this.m3.removeItem(k.c);
                this.mv.aj(k.c.c);
            }
            k = l;
        }
        for (var n = 0; n < b.count; n++) {
            if (!this.m3.containsKey(b._inner[n])) {
                this.m5.f(b._inner[n]);
                var o = b._inner[n].b;
                o.propertyChanged = delegateCombine(o.propertyChanged, runOn(this, this.nv));
                this.m3.addItem(b._inner[n], this.m5.d);
                f.add(b._inner[n]);
                d.add_1(b._inner[n]);
                this.my = true;
            }
        }
        for (var p = 0; p < c.count; p++) {
            if (!this.m3.containsKey(c._inner[p])) {
                this.m5.f(c._inner[p]);
                this.m3.addItem(c._inner[p], this.m5.d);
                var q = c._inner[p].b;
                q.propertyChanged = delegateCombine(q.propertyChanged, runOn(this, this.nv));
                f.add(c._inner[p]);
                d.add_1(c._inner[p]);
                this.my = true;
            }
        }
        if (this.m6.count > 0) {
            this.my = true;
            this.m6.clear();
            this.m4.clear();
        }
        k = this.m5.c;
        while (k != null) {
            var r = this.mx(k.c);
            if (r == null) {
                return;
            }
            if (!Base.equalsStatic(r, k.c.c.c)) {
                var s = false;
                if (k.c.c.c == null) {
                    s = true;
                }
                var t = k.c.c;
                t.o = r.b;
                t.p = r.c;
                t.n = r.e;
                t.m = r.d;
                if (s) {
                    this.mv.aa(t);
                }
                else {
                    this.mv.ag(t);
                }
            }
            k = k.a;
        }
        if (this.my) {
            this.my = false;
            this.mv.al();
        }
        var u = this.m5.c;
        while (u != null) {
            var v = u.c;
            a.f.add(v.b.background);
            a.g.add(v.b.outline);
            a.h.add(v.b.backgroundPadding);
            a.i.add(v.b.strokeThickness);
            a.j.add(v.b.textColor);
            a.k.add1(v.b.text);
            a.n.add(v.c.o - (v.c.g - v.c.l));
            a.o.add(v.c.p - (v.c.h - v.c.k));
            a.l.add(v.c.g - v.c.l);
            a.m.add(v.c.h - v.c.k);
            u = u.a;
        }
    };
    CalloutLayer.prototype.nf = function () {
        return 10000;
    };
    CalloutLayer.prototype.ng = function () {
        return 1200;
    };
    CalloutLayer.prototype.mw = function () {
        switch (this.ms) {
            case 0: return 2;
            case 2: return 1;
            case 3: return 2;
            case 1: return 0;
        }
        return 2;
    };
    CalloutLayer.prototype.nu = function (a) {
        this.i1(true);
    };
    CalloutLayer.prototype.mx = function (a) {
        var b = this.mu;
        if (a.b.series != null) {
            b = a.b.series;
        }
        if (b == null) {
            for (var c = 0; c < this.bs.series.count; c++) {
                var d = this.bs.series._inner[c];
                if (!d.ci) {
                    b = d;
                    break;
                }
            }
        }
        if (b == null) {
            return null;
        }
        var e = b.ag();
        var f = null;
        var g = null;
        for (var h = 0; h < e.length; h++) {
            if (e[h].ce) {
                g = e[h];
            }
            else {
                f = e[h];
            }
        }
        if (f == null || g == null) {
            return null;
        }
        if (!f.cd() || !g.cd()) {
            return null;
        }
        var i = f.d8(this.nd(a.b.xValue));
        var j = g.d8(this.nd(a.b.yValue));
        var k = new QuadTreeBoundingBox();
        k.b = i;
        k.c = j;
        k.e = 3;
        k.d = 3;
        k.g = a.b;
        return k;
    };
    CalloutLayer.prototype.nd = function (a) {
        if (a == null) {
            return 0;
        }
        return a;
    };
    CalloutLayer.prototype.m8 = function () {
        var a = new List$1(CalloutAnnotationWrapper.$, 0);
        for (var b = 0; b < this.m7.count; b++) {
            if (this.m7._inner[b].c == null) {
                var c = new LabelCollisionInfo();
                this.m7._inner[b].c = c;
                var d = this.mt.cb(this.nh(this.m7._inner[b]), this.m7._inner[b].b.backgroundPadding);
                c.k = d.height / 2;
                c.l = d.width / 2;
            }
            var e = this.mx(this.m7._inner[b]);
            if (e == null || e.b < 0 || e.b > this.lm.width || e.c < 0 || e.c > this.lm.height) {
                continue;
            }
            a.add(this.m7._inner[b]);
        }
        return a;
    };
    CalloutLayer.prototype.nh = function (a) {
        return a.b.text;
    };
    CalloutLayer.prototype.lu = function (a) {
        if (!_super.prototype.lu.call(this, a)) {
            return false;
        }
        return true;
    };
    CalloutLayer.prototype.ib = function () {
        _super.prototype.ib.call(this);
        if (!this.aq.c && !this.ca()) {
            return;
        }
        if (this.ca()) {
            this.ny(this.lr, false);
        }
        else {
            this.ny(this.lp, false);
        }
    };
    CalloutLayer.prototype.nx = function (a, b, c) {
    };
    CalloutLayer.prototype.ny = function (a, b) {
        if (a.k.count == 0) {
            this.mt.b3.count = 0;
            return;
        }
        var c = 0;
        for (c = 0; c < a.k.count; c++) {
            var d = this.mt.b3.item(c);
            if (d.g != a.k._inner[c]) {
                d.g = a.k._inner[c];
                var e = d.q();
            }
            d.j = a.f._inner[c];
            d.l = a.j._inner[c];
            d.k = a.g._inner[c];
            d.c = a.h._inner[c];
            d.d = a.i._inner[c];
            this.mt.b9(d, Math.min(a.l._inner[c], a.l._inner[c] + a.n._inner[c]), Math.min(a.m._inner[c], a.m._inner[c] + a.o._inner[c]));
            d.n = { $type: Point_$type, x: a.n._inner[c], y: a.o._inner[c] };
        }
        this.mt.b3.count = c;
    };
    CalloutLayer.prototype.mc = function (a, b) {
        _super.prototype.mc.call(this, a, b);
        if (this.ca()) {
            this.ny(this.lr, true);
        }
        else {
            this.ny(this.lp, false);
        }
    };
    CalloutLayer.prototype.c5 = function () {
        return true;
    };
    CalloutLayer.prototype.io = function (a, b, c, d) {
        _super.prototype.io.call(this, a, b, c, d);
        switch (b) {
            case CalloutLayer.$$p[9]:
                this.i1(true);
                break;
            case CalloutLayer.$$p[8]:
            case CalloutLayer.$$p[0]:
            case CalloutLayer.$$p[3]:
            case CalloutLayer.$$p[4]:
            case CalloutLayer.$$p[6]:
            case CalloutLayer.$$p[7]:
                this.i1(true);
                break;
        }
    };
    CalloutLayer.$t = markType(CalloutLayer, 'CalloutLayer', AnnotationLayer.$);
    return CalloutLayer;
}(AnnotationLayer));
export { CalloutLayer };
Type.dep(DependencyProperty, PropertyMetadata, CalloutLayer, 'raisePropertyChanged', ['CalloutBackground:n9:nz', [Brush.$, null], 'CalloutCollisionMode:ms:n0', [CalloutCollisionMode_$type, enumGetBox(CalloutCollisionMode_$type, 3)], 'CalloutInterpolatedValuePrecision:ne:n1', [1, -1], 'CalloutOutline:oa:n2', [Brush.$, null], 'CalloutPadding:na:n3', [1, NaN], 'CalloutPositionPadding:nb:n4', [1, 3], 'CalloutStrokeThickness:nc:n5', [1, NaN], 'CalloutTextColor:ob:n6', [Brush.$, null], 'IsAutoCalloutBehaviorEnabled:m2:n7', [0, false], 'TargetSeries:mu:n8', [Series.$, null]]);
var CalloutLayerView = /** @class */ (function (_super) {
    tslib_1.__extends(CalloutLayerView, _super);
    function CalloutLayerView(a) {
        var _this = _super.call(this, a) || this;
        _this.bz = null;
        _this.b4 = null;
        _this.b3 = null;
        _this.b5 = NaN;
        _this.b0 = null;
        _this.bz = a;
        _this.b4 = new List$1(CalloutView.$, 0);
        return _this;
    }
    CalloutLayerView.prototype.ap = function () {
        var _this = this;
        _super.prototype.ap.call(this);
        this.b3 = ((function () {
            var $ret = new Pool$1(CalloutView.$);
            $ret.create = runOn(_this, _this.b1);
            $ret.activate = runOn(_this, _this.b6);
            $ret.disactivate = runOn(_this, _this.b8);
            $ret.destroy = runOn(_this, _this.b7);
            return $ret;
        })());
    };
    CalloutLayerView.prototype.b1 = function () {
        var a = new CalloutView(this);
        this.b4.add(a);
        return a;
    };
    CalloutLayerView.prototype.b6 = function (a) {
        a.s = 0;
    };
    CalloutLayerView.prototype.b8 = function (a) {
        a.s = 1;
    };
    CalloutLayerView.prototype.b7 = function (a) {
        this.b4.remove(a);
    };
    CalloutLayerView.prototype.ca = function (a) {
        if (a == null) {
            return a;
        }
        return BrushUtil.h(a, 0.1);
    };
    CalloutLayerView.prototype.a3 = function (a, b) {
        _super.prototype.a3.call(this, a, b);
        if (a.d && !b) {
            for (var c = 0; c < this.b4.count; c++) {
                var d = this.b4._inner[c];
                if (d.s == 0) {
                    d.h(a);
                }
            }
        }
    };
    CalloutLayerView.prototype.ai = function (a) {
        _super.prototype.ai.call(this, a);
    };
    CalloutLayerView.prototype.cc = function (a) {
        if (isNaN_(this.b5)) {
            this.b5 = this.e.bs.bk.a7;
        }
        return new Size(1, this.h.f(a.ak), this.b5);
    };
    CalloutLayerView.prototype.cb = function (a, b) {
        if (this.b0 == null) {
            this.b0 = new CalloutView(this);
        }
        this.b0.g = a;
        this.b0.c = b;
        var c = this.b0.q();
        this.b0.g = null;
        return c;
    };
    CalloutLayerView.prototype.b2 = function () {
        return new DOMExecutionContext(this.e.bs.bk.i.af);
    };
    CalloutLayerView.prototype.b9 = function (a, b, c) {
        a.e = b;
        a.f = c;
    };
    CalloutLayerView.$t = markType(CalloutLayerView, 'CalloutLayerView', AnnotationLayerView.$);
    return CalloutLayerView;
}(AnnotationLayerView));
export { CalloutLayerView };
var CalloutView = /** @class */ (function (_super) {
    tslib_1.__extends(CalloutView, _super);
    function CalloutView(a) {
        var _this = _super.call(this) || this;
        _this.a = null;
        _this.s = 0;
        _this.i = null;
        _this.p = null;
        _this.m = { $type: Point_$type, x: 0, y: 0 };
        _this.o = null;
        _this.b = 0;
        _this.r = null;
        _this.e = 0;
        _this.f = 0;
        _this.a = a;
        _this.s = 0;
        return _this;
    }
    CalloutView.prototype.h = function (a) {
        a.z();
        a.af(this.e, this.f);
        var b = new TranslateTransform();
        b.j = this.r.c;
        b.k = this.r.e;
        this.p.j = b;
        a.r(this.o);
        a.z();
        a.j(this.p.j);
        a.v(this.p);
        this.i.o = this.b;
        this.i.n = this.b;
        a.w(this.i);
        a.y();
        a.y();
    };
    Object.defineProperty(CalloutView.prototype, "n", {
        get: function () {
            return this.m;
        },
        set: function (a) {
            if (!PointUtil.equals(this.m, a)) {
                var b = this.q();
                var c = b.height;
                var d = b.width;
                this.m = a;
                var e = CalloutViewHelper.b(this.n, d, c, this.o);
                this.r = e;
            }
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(CalloutView.prototype, "l", {
        get: function () {
            return this.i.am;
        },
        set: function (a) {
            this.i.am = a;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(CalloutView.prototype, "k", {
        get: function () {
            return this.p._stroke;
        },
        set: function (a) {
            this.p._stroke = a;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(CalloutView.prototype, "j", {
        get: function () {
            return this.p._fill;
        },
        set: function (a) {
            this.p._fill = a;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(CalloutView.prototype, "d", {
        get: function () {
            return this.p.ac;
        },
        set: function (a) {
            this.p.ac = a;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(CalloutView.prototype, "c", {
        get: function () {
            return this.b;
        },
        set: function (a) {
            this.b = a;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(CalloutView.prototype, "g", {
        get: function () {
            return this.i.ak;
        },
        set: function (a) {
            if (a == null) {
                this.i.ak = "";
            }
            else {
                this.i.ak = a.toString();
            }
        },
        enumerable: true,
        configurable: true
    });
    CalloutView.prototype.q = function () {
        var a = this.a.cc(this.i);
        return new Size(1, a.width + this.b * 2, a.height + this.b * 2);
    };
    CalloutView.$t = markType(CalloutView, 'CalloutView');
    return CalloutView;
}(Base));
export { CalloutView };
/*<BeginStatics Name="Infragistics.Controls.Charts.CalloutLayer" />*/
/*<EndStatics Name="Infragistics.Controls.Charts.CalloutLayer" />*/
