/*
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 { __extends, __values } from "tslib";
import { AnnotationLayer } from "./AnnotationLayer";
import { DependencyProperty } from "igniteui-react-core";
import { Series } from "./Series";
import { ItemTooltipLayerFrame } from "./ItemTooltipLayerFrame";
import { Base, String_$type, fromEnum, Point_$type, markType, markDep, runOn, typeCast } from "igniteui-react-core";
import { Pool$1 } from "igniteui-react-core";
import { List$1 } from "igniteui-react-core";
import { ItemTooltipCollisionInfo } from "./ItemTooltipCollisionInfo";
import { Rect } from "igniteui-react-core";
import { DataContext } from "igniteui-react-core";
import { Size } from "igniteui-react-core";
import { DeviceUtils } from "igniteui-react-core";
import { PropertyMetadata } from "igniteui-react-core";
import { isNaN_ } from "igniteui-react-core";
import { AnnotationLayerView } from "./AnnotationLayerView";
import { PointerTooltip } from "./PointerTooltip";
import { DomWrapper_$type } from "igniteui-react-core";
/**
 * @hidden
 */
var ItemToolTipLayer = /** @class */ /*@__PURE__*/ (function (_super) {
    __extends(ItemToolTipLayer, _super);
    function ItemToolTipLayer() {
        var _this = _super.call(this) || this;
        var a = new ItemTooltipLayerFrame();
        var b = new ItemTooltipLayerFrame();
        var c = new ItemTooltipLayerFrame();
        var d = _this.ji / 1000;
        _this.uf = a;
        _this.ue = b;
        _this.ug = c;
        _this.ab = ItemToolTipLayer.$;
        return _this;
    }
    ItemToolTipLayer.prototype.get_d5 = function () {
        return true;
    };
    ItemToolTipLayer.prototype.cg = function () {
        return new ItemToolTipLayerView(this);
    };
    ItemToolTipLayer.prototype.o0 = function (a) {
        _super.prototype.o0.call(this, a);
        this.w0 = a;
    };
    ItemToolTipLayer.prototype.get_eg = function () {
        return true;
    };
    ItemToolTipLayer.prototype.o8 = function (a, b, c, d) {
        _super.prototype.o8.call(this, a, b, c, d);
        switch (b) {
            case ItemToolTipLayer.$$p[1]:
            case ItemToolTipLayer.$$p[2]:
                this.pp(true);
                break;
            case ItemToolTipLayer.$$p[3]:
                this.pp(true);
                break;
            case ItemToolTipLayer.$$p[0]:
                this.pp(true);
                break;
            case "SeriesViewer":
                this.w0.cj();
                break;
        }
    };
    ItemToolTipLayer.prototype.vs = function (a, b) {
        if (this.w1 == null || this.w1 == a) {
            if (!this.at.c) {
                this.pp(b);
            }
        }
    };
    ItemToolTipLayer.prototype.ul = function (a) {
        if (!_super.prototype.ul.call(this, a)) {
            return false;
        }
        if (this.w7 != null) {
            if (a.ep) {
                var b = a.lu();
                if (!Base.equalsStatic(this.w7, b)) {
                    return false;
                }
            }
            else {
                if (!Base.equalsStatic(this.w7, a.name)) {
                    return false;
                }
            }
        }
        var c = this.vn(a);
        if (!c.c) {
            return false;
        }
        return true;
    };
    ItemToolTipLayer.prototype.vu = function (a, b) {
        var e_1, _a;
        var _this = this;
        _super.prototype.vu.call(this, a, b);
        var c = a;
        var d = this.w0.b8;
        var e = this.w4;
        var f = this.w3;
        c.o.clear();
        c.i.clear();
        c.n.clear();
        c.g.clear();
        c.f.clear();
        c.l.clear();
        c.j.clear();
        c.m.clear();
        c.k.clear();
        c.h.clear();
        if (isNaN_(this.wx.x) && isNaN_(this.wx.y)) {
            d.count = 0;
            return;
        }
        var g = 0;
        if (this.ul(this.w1)) {
            this.xc(this.w1, c, e, f, 0);
            g++;
        }
        else {
            try {
                for (var _b = __values(fromEnum(this.cy.series)), _c = _b.next(); !_c.done; _c = _b.next()) {
                    var h = _c.value;
                    if (this.ul(h)) {
                        this.xc(h, c, e, f, g);
                        g++;
                    }
                    if (h.isStacked) {
                        h.n5(function (i) {
                            if (_this.ul(i)) {
                                _this.xc(i, c, e, f, g);
                                g++;
                            }
                            if (i.dv) {
                                i.n3(function (j) {
                                    if (_this.ul(j)) {
                                        _this.xc(j, c, e, f, g);
                                        g++;
                                    }
                                    return true;
                                });
                            }
                            return true;
                        });
                    }
                    if (h.dv) {
                        h.n3(function (i) {
                            if (_this.ul(i)) {
                                _this.xc(i, c, e, f, g);
                                g++;
                            }
                            return true;
                        });
                    }
                }
            }
            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;
                }
            }
            this.xb(c);
        }
        d.count = g;
    };
    ItemToolTipLayer.prototype.xb = function (a) {
        var b = new List$1(ItemTooltipCollisionInfo.$, 0);
        for (var c = 0; c < a.f.count; c++) {
            var d = new ItemTooltipCollisionInfo();
            d.d = c;
            d.e = { $type: Point_$type, x: a.f._inner[c], y: a.g._inner[c] };
            d.c = a.m._inner[c];
            d.a = a.k._inner[c];
            d.b = a.o._inner[c];
            if (isNaN_(d.e.x) || isNaN_(d.e.y)) {
                continue;
            }
            b.add(d);
        }
        b.aa(function (e, f) {
            if (e.e.y < f.e.y) {
                return -1;
            }
            if (e.e.y > f.e.y) {
                return 1;
            }
            if (e.b < f.b) {
                return -1;
            }
            if (e.b > f.b) {
                return 1;
            }
            return 0;
        });
        var e = this.w2(b);
        if (!e) {
            return;
        }
        for (var f = 0; f < b.count - 1; f++) {
            var g = b._inner[f];
            var h = b._inner[f + 1];
            var i = new Rect(0, g.e.x, g.e.y, g.c, g.a);
            var j = new Rect(0, h.e.x, h.e.y, h.c, h.a);
            if (i.intersectsWith(j) || i.top > j.bottom) {
                h.e = { $type: Point_$type, x: h.e.x, y: i.bottom + 1 };
            }
        }
        if (b._inner[b.count - 1].e.y + b._inner[b.count - 1].a > this.cj.bx.height) {
            b._inner[b.count - 1].e = { $type: Point_$type, x: b._inner[b.count - 1].e.x, y: b._inner[b.count - 1].e.y - ((b._inner[b.count - 1].e.y + b._inner[b.count - 1].a) - this.cj.bx.height) };
        }
        for (var k = b.count - 1; k >= 1; k--) {
            var l = b._inner[k];
            var m = b._inner[k - 1];
            var n = new Rect(0, l.e.x, l.e.y, l.c, l.a);
            var o = new Rect(0, m.e.x, m.e.y, m.c, m.a);
            if (n.intersectsWith(o) || n.top < o.bottom) {
                m.e = { $type: Point_$type, x: m.e.x, y: n.top - (o.height + 1) };
            }
        }
        for (var p = 0; p < b.count; p++) {
            var q = b._inner[p].d;
            var r = b._inner[p];
            a.f._inner[q] = r.e.x;
            a.g._inner[q] = r.e.y;
        }
    };
    ItemToolTipLayer.prototype.w2 = function (a) {
        if (a.count <= 1) {
            return false;
        }
        var b = this.cj.bx;
        for (var c = 0; c < a.count - 1; c++) {
            var d = a._inner[c];
            var e = a._inner[c + 1];
            var f = new Rect(0, d.e.x, d.e.y, d.c, d.a);
            var g = new Rect(0, e.e.x, e.e.y, e.c, e.a);
            if (f.intersectsWith(g)) {
                return true;
            }
        }
        return false;
    };
    ItemToolTipLayer.prototype.xc = function (a, b, c, d, e) {
        var f = this.vn(a);
        if (!f.c) {
            return;
        }
        var g = a.th(f.d, c, d);
        var h = this.w0.b8;
        var i = this.w5(a);
        var j = this.wx;
        if (!c) {
            j = this.tm(g);
        }
        var k = a.jr(j);
        var l = h.item(e);
        var m = this.cj.bx;
        var n = null;
        if (a.eh && k != null) {
            n = a.av(k);
        }
        else {
            n = new DataContext();
            n.item = k;
            n.series = a;
        }
        if (!this.w0.b9(a, l, i, n)) {
            return;
        }
        var o = this.w0.ck(l, i, n);
        b.i.add1(l);
        b.j.add1(n);
        b.l.add1(i);
        var p = g.x;
        var q = g.x;
        q = Math.max(q, m.left);
        q = Math.min(q, m.right);
        b.n.add(q);
        var r = g.y;
        r = Math.max(r, m.top);
        r = Math.min(r, m.bottom);
        b.o.add(r);
        var s = DeviceUtils.g(10);
        var t = g.x + s;
        if (t + o.width > m.right) {
            t = (p - o.width) - s;
        }
        if (t < m.left) {
            t = m.left;
        }
        b.f.add(t);
        var u = (g.y - o.height) - s;
        if (u + o.height > m.bottom) {
            u = (m.bottom - o.height) - s;
        }
        if (u < m.top) {
            u = m.top;
        }
        b.g.add(u);
        b.m.add(o.width);
        b.k.add(o.height);
        var v = this.ss;
        if (v == null) {
            v = this.sw;
        }
        if (v == null) {
            v = a.ss;
        }
        b.h.add(v);
    };
    ItemToolTipLayer.prototype.w5 = function (a) {
        return a.toolTip;
    };
    ItemToolTipLayer.prototype.n8 = function () {
        this.wx = { $type: Point_$type, x: NaN, y: NaN };
        this.pp(false);
        this.w0.ch(false);
    };
    ItemToolTipLayer.prototype.n9 = function () {
        this.wx = { $type: Point_$type, x: NaN, y: NaN };
        this.pp(false);
        this.w0.ch(true);
    };
    ItemToolTipLayer.prototype.vx = function (a, b) {
        _super.prototype.vx.call(this, a, b);
        var c = a;
        var d = c.o.count;
        var e = b.bx.left;
        var f = b.bx.right;
        var g = b.bx.top;
        var h = b.bx.bottom;
        for (var i = 0; i < d; i++) {
            if (isNaN_(c.n._inner[i]) || isNaN_(c.o._inner[i]) || isNaN_(c.f._inner[i]) || isNaN_(c.g._inner[i]) || c.l._inner[i] == null || c.j._inner[i] == null) {
                this.w0.cg(c.i._inner[i]);
                continue;
            }
            var j = c.f._inner[i];
            var k = c.g._inner[i];
            var l = c.m._inner[i];
            var m = c.k._inner[i];
            var n = false;
            if (j < e && Math.abs(j - e) > 1) {
                n = true;
            }
            if (k < g && Math.abs(k - g) > 1) {
                n = true;
            }
            if ((j + l) > f && Math.abs((j + l) - f) > 1) {
                n = true;
            }
            if ((k + m) > h && Math.abs((k + m) - h) > 1) {
                n = true;
            }
            if (n) {
                this.w0.cg(c.i._inner[i]);
                continue;
            }
            var o = c.i._inner[i];
            var p = Math.min(c.f._inner[i], c.n._inner[i]);
            var q = Math.min(c.g._inner[i], c.o._inner[i]);
            var r = c.n._inner[i] - c.f._inner[i];
            var s = c.o._inner[i] - c.g._inner[i];
            if (o != null) {
                this.w0.ci(o, p, q, r, s);
            }
        }
    };
    ItemToolTipLayer.prototype.fm = function () {
        return true;
    };
    ItemToolTipLayer.$t = markType(ItemToolTipLayer, 'ItemToolTipLayer', AnnotationLayer.$);
    ItemToolTipLayer.$$p = markDep(DependencyProperty, PropertyMetadata, ItemToolTipLayer, 'raisePropertyChanged', ['SkipUnknownValues:w3:xd', [0, true], 'TargetSeriesName:w7:xe', [2, null], 'TargetSeries:w1:xf', [Series.$, null], 'UseInterpolation:w4:xg', [0, false]]);
    return ItemToolTipLayer;
}(AnnotationLayer));
export { ItemToolTipLayer };
/**
 * @hidden
 */
var ItemToolTipLayerView = /** @class */ /*@__PURE__*/ (function (_super) {
    __extends(ItemToolTipLayerView, _super);
    function ItemToolTipLayerView(a) {
        var _this = _super.call(this, a) || this;
        _this.b7 = null;
        _this.b8 = null;
        _this.ca = null;
        _this._htmlTest = null;
        _this.b7 = a;
        _this.ca = new List$1(PointerTooltip.$, 0);
        return _this;
    }
    ItemToolTipLayerView.prototype.au = function () {
        var _this = this;
        _super.prototype.au.call(this);
        this.b8 = ((function () {
            var $ret = new Pool$1(Base.$);
            $ret.create = runOn(_this, _this.cb);
            $ret.activate = runOn(_this, _this.cd);
            $ret.disactivate = runOn(_this, _this.cf);
            $ret.destroy = runOn(_this, _this.ce);
            return $ret;
        })());
        this.htmlTest = /^[^<]*(<[\w\W]+>)[^>]*$/;
    };
    ItemToolTipLayerView.prototype.ak = function () {
        _super.prototype.ak.call(this);
        for (var a = 0; a < this.b8.a.count; a++) {
            this.b8.a._inner[a].br();
        }
        for (var b = 0; b < this.b8.b.count; b++) {
            this.b8.b._inner[b].br();
        }
        this.b8.clear();
    };
    ItemToolTipLayerView.prototype.cb = function () {
        var a = new PointerTooltip();
        a.bw(this.e.i4);
        a._visibility = 1;
        a.ap = this.e.cy.cv.k;
        this.ca.add(a);
        return a;
    };
    ItemToolTipLayerView.prototype.cd = function (a) {
        var b = a;
    };
    ItemToolTipLayerView.prototype.cf = function (a) {
        var b = a;
        b._visibility = 1;
    };
    ItemToolTipLayerView.prototype.ce = function (a) {
        var b = a;
        b.bz();
        b.content = null;
        this.ca.remove1(a);
    };
    Object.defineProperty(ItemToolTipLayerView.prototype, "htmlTest", {
        get: function () {
            return this._htmlTest;
        },
        set: function (a) {
            this._htmlTest = a;
        },
        enumerable: false,
        configurable: true
    });
    ItemToolTipLayerView.prototype.b9 = function (a, b, c, d) {
        var e = b;
        if (a.j2 == "default") {
            a.j2 = a.cj.getDefaultTooltipTemplate();
        }
        var template_ = a.j2;
        var context_ = d;
        if (a.toolTip == null) {
            return false;
        }
        var tooltip_ = a.toolTip;
        if ((tooltip_.updateToolTip)) {
            tooltip_._inPointer = true;
            tooltip_.updateToolTip(context_, true);
            tooltip_.setStyleProperty('position', 'relative');
            tooltip_.setStyleProperty('left', '');
            tooltip_.setStyleProperty('top', '');
        }
        e.content = a.toolTip;
        return true;
    };
    ItemToolTipLayerView.prototype.ck = function (a, b, c) {
        var d = a;
        var e = d.content;
        var f = new Size(1, Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY);
        d.cy = 1;
        var g = d.cx(new Size(1, Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY));
        d.cy = 0;
        return g;
    };
    ItemToolTipLayerView.prototype.ci = function (a, b, c, d, e) {
        var f = null;
        if (this.e.cy != null) {
            f = this.e.cy.cv.j.i();
        }
        var g = a;
        g.aq = f;
        var h = g.content;
        g.cs = { $type: Point_$type, x: d, y: e };
        g._visibility = 0;
        g.n = b;
        g.o = c;
    };
    ItemToolTipLayerView.prototype.cg = function (a) {
        if (a != null) {
            var b = a;
            b._visibility = 1;
        }
    };
    ItemToolTipLayerView.prototype.a8 = function (a, b) {
        _super.prototype.a8.call(this, a, b);
        if (b) {
            return;
        }
        var c = 0;
        var d = 0;
        var e = this.e.cy;
        if (e != null) {
            var f = e.cv.j.b8();
            c += f.x;
            d += f.y;
        }
        for (var g = 0; g < this.ca.count; g++) {
            this.ca._inner[g].by(this.ca._inner[g].n + c, this.ca._inner[g].o + d);
        }
    };
    ItemToolTipLayerView.prototype.an = function (a) {
        var e_2, _a;
        _super.prototype.an.call(this, a);
        try {
            for (var _b = __values(fromEnum(this.b8.a)), _c = _b.next(); !_c.done; _c = _b.next()) {
                var tooltip_ = _c.value;
                if (tooltip_._visibility != 0) {
                    continue;
                }
                var b = tooltip_;
                var c = b.a5();
                var d = b.n;
                var e = b.o;
                var f = new List$1(String_$type, 0);
                var g = typeCast(DomWrapper_$type, b.content);
                if (g != null) {
                    var h = g.getText();
                    f.add(h);
                }
                this.e.a9.addPointerTooltipData(a, c, d, e, f);
            }
        }
        catch (e_2_1) {
            e_2 = { error: e_2_1 };
        }
        finally {
            try {
                if (_c && !_c.done && (_a = _b.return))
                    _a.call(_b);
            }
            finally {
                if (e_2)
                    throw e_2.error;
            }
        }
    };
    ItemToolTipLayerView.prototype.ch = function (a) {
        this.as();
        for (var b = 0; b < this.ca.count; b++) {
            this.cg(this.ca._inner[b]);
            if (a) {
                this.ca._inner[b].bs();
            }
        }
    };
    ItemToolTipLayerView.prototype.cj = function () {
        if (this.e.cy == null) {
            this.ch(true);
        }
    };
    ItemToolTipLayerView.$t = markType(ItemToolTipLayerView, 'ItemToolTipLayerView', AnnotationLayerView.$);
    return ItemToolTipLayerView;
}(AnnotationLayerView));
export { ItemToolTipLayerView };
