/*
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 { Axis } from "./Axis";
import { DependencyProperty } from "igniteui-react-core";
import { CategoryTooltipLayerPosition_$type } from "./CategoryTooltipLayerPosition";
import { Series } from "./Series";
import { CategoryToolTipLayerFrame } from "./CategoryToolTipLayerFrame";
import { Base, enumGetBox, typeCast, fromEnum, Point_$type, markType, markDep, String_$type } from "igniteui-react-core";
import { CategoryTooltipSeriesInfo } from "./CategoryTooltipSeriesInfo";
import { XamDataChart } from "./XamDataChart";
import { List$1 } 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";
/**
 * @hidden
 */
var CategoryToolTipLayer = /** @class */ /*@__PURE__*/ (function (_super) {
    __extends(CategoryToolTipLayer, _super);
    function CategoryToolTipLayer() {
        var _this = _super.call(this) || this;
        var a = new CategoryToolTipLayerFrame();
        var b = new CategoryToolTipLayerFrame();
        var c = new CategoryToolTipLayerFrame();
        _this.uh = a;
        _this.ug = b;
        _this.ui = c;
        _this.ab = CategoryToolTipLayer.$;
        return _this;
    }
    CategoryToolTipLayer.prototype.bc = function () {
        return new CategoryToolTipLayer();
    };
    CategoryToolTipLayer.prototype.get_d6 = function () {
        return true;
    };
    CategoryToolTipLayer.prototype.cd = function () {
        return new CategoryToolTipLayerView(this);
    };
    CategoryToolTipLayer.prototype.o2 = function (a) {
        _super.prototype.o2.call(this, a);
        this.w4 = a;
    };
    CategoryToolTipLayer.prototype.get_eh = function () {
        return true;
    };
    CategoryToolTipLayer.prototype.pa = function (a, b, c, d) {
        _super.prototype.pa.call(this, a, b, c, d);
        switch (b) {
            case CategoryToolTipLayer.$$p[0]:
                this.pr(true);
                break;
            case CategoryToolTipLayer.$$p[2]:
                this.pr(true);
                break;
            case CategoryToolTipLayer.$$p[1]:
                this.pr(true);
                break;
            case "SeriesViewer":
                this.w4.ci();
                break;
        }
    };
    CategoryToolTipLayer.prototype.vt = function (a, b) {
        if (this.w2 == a) {
            if (!this.at.c) {
                this.pr(b);
            }
        }
    };
    CategoryToolTipLayer.prototype.vu = function (a, b) {
        if (!this.at.c) {
            this.pr(b);
        }
    };
    CategoryToolTipLayer.prototype.vw = function (a, b) {
        var e_1, _a, e_2, _b;
        _super.prototype.vw.call(this, a, b);
        var c = a;
        var d = this.w6;
        c.l = NaN;
        c.k = NaN;
        c.g = NaN;
        c.h = NaN;
        c.k = NaN;
        c.l = NaN;
        if (isNaN_(this.wz.x) && isNaN_(this.wz.y)) {
            this.w4.cg(this.w4.cd());
            return;
        }
        if (this.w2 != null) {
            var e = this.w2;
            if (e == null || !e.cb || e.b9) {
                return;
            }
            if (!e.co()) {
                return;
            }
            var f = e;
            var g = this.w5(f.de, d);
            this.xb(g, c, d);
        }
        else {
            if (typeCast(XamDataChart.$, this.cv) !== null) {
                var h = this.cv;
                var i = true;
                var j = false;
                var k = new List$1(Series.$, 0);
                try {
                    for (var _c = __values(fromEnum(h.axes)), _d = _c.next(); !_d.done; _d = _c.next()) {
                        var l = _d.value;
                        if (l.cb && !l.b9) {
                            if (!l.co()) {
                                continue;
                            }
                            if (i) {
                                i = false;
                                j = l.cp;
                            }
                            else {
                                if (l.cp != j) {
                                    continue;
                                }
                            }
                            try {
                                for (var _e = (e_2 = void 0, __values(fromEnum(l.de))), _f = _e.next(); !_f.done; _f = _e.next()) {
                                    var m = _f.value;
                                    k.add(m);
                                }
                            }
                            catch (e_2_1) {
                                e_2 = { error: e_2_1 };
                            }
                            finally {
                                try {
                                    if (_f && !_f.done && (_b = _e.return))
                                        _b.call(_e);
                                }
                                finally {
                                    if (e_2)
                                        throw e_2.error;
                                }
                            }
                        }
                    }
                }
                catch (e_1_1) {
                    e_1 = { error: e_1_1 };
                }
                finally {
                    try {
                        if (_d && !_d.done && (_a = _c.return))
                            _a.call(_c);
                    }
                    finally {
                        if (e_1)
                            throw e_1.error;
                    }
                }
                var n = this.w5(k, d);
                this.xb(n, c, d);
            }
        }
    };
    CategoryToolTipLayer.prototype.w5 = function (a, b) {
        var c = 1.7976931348623157E+308;
        var d = -1.7976931348623157E+308;
        var e = new List$1(DataContext.$, 0);
        var f = new List$1(Base.$, 0);
        var g = false;
        var h = true;
        for (var i = 0; i < a.count; i++) {
            var j = a.item(i);
            if (!j.el && !j.ed) {
                continue;
            }
            if (j.isStacked) {
                continue;
            }
            var k = j;
            if (!k.categoryAxis.co()) {
                continue;
            }
            if (h) {
                g = j.isVertical;
            }
            else {
                if (g != j.isVertical) {
                    continue;
                }
            }
            var l = this.vp(j);
            if (!l.c) {
                continue;
            }
            var m = j.tj(l.d, b, false);
            if (g) {
                if (!isNaN_(m.y)) {
                    c = Math.min(c, m.y);
                    d = Math.max(d, m.y);
                }
            }
            else {
                if (!isNaN_(m.x)) {
                    c = Math.min(c, m.x);
                    d = Math.max(d, m.x);
                }
            }
            var n = this.w7(j);
            if (n == null) {
                continue;
            }
            var o = null;
            var p = j.js(this.wz);
            if (j.ei && p != null) {
                o = j.av(p);
            }
            else {
                o = new DataContext();
                o.item = p;
                o.series = j;
            }
            e.add(o);
            f.add1(n);
        }
        var q = 0;
        var r = 0;
        if (g) {
            r = (c + d) / 2;
            q = this.ty.right;
            if (this.w3 == 2 || this.w3 == 1) {
                q = this.ty.left;
            }
            if (r < 0 || r > this.ty.bottom) {
                r = NaN;
            }
        }
        else {
            q = (c + d) / 2;
            r = this.ty.top;
            if (this.w3 == 2 || this.w3 == 1) {
                r = this.ty.bottom;
            }
            if (q < 0 || q > this.ty.right) {
                q = NaN;
            }
        }
        var s = new CategoryTooltipSeriesInfo();
        s.c = { $type: Point_$type, x: q, y: r };
        s.a = e;
        s.b = f;
        return s;
    };
    CategoryToolTipLayer.prototype.n9 = function () {
        this.wz = { $type: Point_$type, x: NaN, y: NaN };
        this.cg.ar();
    };
    CategoryToolTipLayer.prototype.xb = function (a, b, c) {
        var d = a.c;
        var e = a.a;
        var f = a.b;
        d = this.w4.cj(d);
        var g = 0;
        var h = this.cg.bx;
        var i = this.w4.cd();
        this.w4.cf();
        var j = false;
        if (e.count == 0) {
            this.cg.ar();
            return;
        }
        var k = false;
        for (var l = 0; l < e.count; l++) {
            var m = e._inner[l].series;
            j = m.isVertical;
            var n = e._inner[l];
            var o = f._inner[l];
            if (this.w4.ca(m, o, n)) {
                k = true;
            }
            g++;
        }
        if (!k) {
            this.w4.cg(i);
            return;
        }
        var p = this.w4.ck(i);
        var q = 0;
        var r = 0;
        var s = 0;
        var t = 0;
        var u = false;
        var v = false;
        var w = this.w3;
        if (w == 2 || w == 3) {
            v = true;
        }
        if (w == 2 || w == 1) {
            u = true;
        }
        var x = this.w4.cl();
        if (j) {
            if (!u && !v && d.x + (p.width + DeviceUtils.g(10)) > x.width) {
                v = true;
            }
            if (u && !v && d.x - (p.width + DeviceUtils.g(10)) < 0) {
                v = true;
            }
        }
        else {
            if (!u && !v && d.y - (p.height + 10) < 0) {
                v = true;
            }
            if (u && !v && d.y + (p.height + 10) > x.height) {
                v = true;
            }
        }
        var y = DeviceUtils.g(10);
        var z = DeviceUtils.g(5);
        if (!u) {
            if (!j) {
                z = p.height + y;
            }
            if (v) {
                if (j) {
                    z = z + p.width + y;
                }
                else {
                    z = 0;
                }
            }
        }
        else {
            if (j) {
                z = p.width + y;
                if (v) {
                    z = 0;
                }
            }
            else {
                z = DeviceUtils.g(5);
                if (v) {
                    z = z + p.height + y;
                }
            }
        }
        if (j) {
            r = 0 - (y + DeviceUtils.g(5));
            if (u) {
                r = (r * -1) + p.width;
            }
            q = p.height / 2;
            t = d.y - p.height / 2;
            s = d.x - z;
        }
        else {
            q = p.height + DeviceUtils.g(10) + DeviceUtils.g(5);
            if (u) {
                q = 0 - (y + DeviceUtils.g(5));
            }
            r = p.width / 2;
            s = d.x - p.width / 2;
            t = d.y - z;
        }
        if (j) {
            if (t < 0) {
                q -= 0 - t;
                t = 0;
            }
            if (t + p.height > x.height) {
                q += (t + p.height) - x.height;
                t = x.height - p.height;
            }
        }
        else {
            if (s < 0) {
                r -= 0 - s;
                s = 0;
            }
            if (s + p.width > x.width) {
                r += (s + p.width) - x.width;
                s = x.width - p.width;
            }
        }
        b.k = s;
        b.l = t;
        b.g = r;
        b.h = q;
        b.j = p.width;
        b.i = p.height;
    };
    CategoryToolTipLayer.prototype.w7 = function (a) {
        return a.toolTip;
    };
    CategoryToolTipLayer.prototype.vz = function (a, b) {
        _super.prototype.vz.call(this, a, b);
        var c = a;
        var d = b.bx.left;
        var e = b.bx.right;
        var f = b.bx.top;
        var g = b.bx.bottom;
        var h = this.w4.cd();
        if (isNaN_(c.k) || isNaN_(c.l) || isNaN_(c.g) || isNaN_(c.h)) {
            this.w4.cg(h);
            return;
        }
        var i = c.k;
        var j = c.l;
        var k = c.g;
        var l = c.h;
        if (h != null) {
            this.w4.ch(h, i, j, k, l);
        }
    };
    CategoryToolTipLayer.prototype.fn = function () {
        return true;
    };
    CategoryToolTipLayer.$t = markType(CategoryToolTipLayer, 'CategoryToolTipLayer', AnnotationLayer.$);
    CategoryToolTipLayer.$$p = markDep(DependencyProperty, PropertyMetadata, CategoryToolTipLayer, 'raisePropertyChanged', ['TargetAxis:w2:xc', [Axis.$, null], 'ToolTipPosition:w3:xd', [CategoryTooltipLayerPosition_$type, enumGetBox(CategoryTooltipLayerPosition_$type, 0)], 'UseInterpolation:w6:xe', [0, false]]);
    return CategoryToolTipLayer;
}(AnnotationLayer));
export { CategoryToolTipLayer };
/**
 * @hidden
 */
var CategoryToolTipLayerView = /** @class */ /*@__PURE__*/ (function (_super) {
    __extends(CategoryToolTipLayerView, _super);
    function CategoryToolTipLayerView(a) {
        var _this = _super.call(this, a) || this;
        _this.b7 = null;
        _this.b8 = null;
        _this._tooltips = null;
        _this.cb = null;
        _this.b9 = false;
        _this._htmlTest = null;
        _this.b7 = a;
        _this.htmlTest = /^[^<]*(<[\w\W]+>)[^>]*$/;
        return _this;
    }
    CategoryToolTipLayerView.prototype.cd = function () {
        if (this.b8 == null) {
            this.b8 = new PointerTooltip();
            this.b8.bw(this.e.i5);
            this.b8.ap = this.e.cv.cu.k;
            this._tooltips = this.e.i5.createElement("div");
            this.cb = this.e.i5.createElement("div");
            this.b8._visibility = 0;
        }
        return this.b8;
    };
    CategoryToolTipLayerView.prototype.au = function () {
        _super.prototype.au.call(this);
        if (this.b8 != null) {
            this.b8._visibility = 1;
        }
    };
    CategoryToolTipLayerView.prototype.ak = function () {
        _super.prototype.ak.call(this);
        if (this.b8 != null) {
            this.b8.br();
            this.b8 = null;
        }
    };
    Object.defineProperty(CategoryToolTipLayerView.prototype, "htmlTest", {
        get: function () {
            return this._htmlTest;
        },
        set: function (a) {
            this._htmlTest = a;
        },
        enumerable: false,
        configurable: true
    });
    CategoryToolTipLayerView.prototype.ca = function (a, b, c) {
        if (a.j3 == "default") {
            a.j3 = a.cg.getDefaultTooltipTemplate();
        }
        var template_ = a.j3;
        var context_ = c;
        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', '');
            tooltip_.remove();
        }
        this._tooltips.append(a.toolTip);
        return true;
    };
    CategoryToolTipLayerView.prototype.ck = function (a) {
        var b = a;
        b.content = this._tooltips;
        var c = new Size(1, Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY);
        b.cy = 1;
        var d = b.cx(new Size(1, Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY));
        b.cy = 0;
        return d;
    };
    CategoryToolTipLayerView.prototype.cj = function (a) {
        var b = 0;
        var c = 0;
        if (this.e.cv != null) {
            var d = this.e.cv.tk();
            b += d.x;
            c += d.y;
        }
        return { $type: Point_$type, x: b + a.x, y: c + a.y };
    };
    CategoryToolTipLayerView.prototype.ch = function (a, b, c, d, e) {
        var f = a;
        var g = f.content;
        f.cs = { $type: Point_$type, x: d, y: e };
        f._visibility = 0;
        f.n = b;
        f.o = c;
    };
    CategoryToolTipLayerView.prototype.cl = function () {
        var a = (window.innerWidth);
        var b = (window.innerHeight);
        return new Size(1, a, b);
    };
    CategoryToolTipLayerView.prototype.cg = function (a) {
        this._tooltips.removeChildren();
        this.cb.removeChildren();
        this.b8._visibility = 1;
    };
    CategoryToolTipLayerView.prototype.cf = function () {
        if (this._tooltips != null) {
            this._tooltips.removeChildren();
        }
    };
    CategoryToolTipLayerView.prototype.ci = function () {
    };
    CategoryToolTipLayerView.prototype.a8 = function (a, b) {
        _super.prototype.a8.call(this, a, b);
        if (b) {
            return;
        }
        var c = null;
        if (this.e.cv != null) {
            c = this.e.cv.cu.j.i();
        }
        var d = 0;
        var e = 0;
        var f = this.e.cv;
        if (f != null) {
            var g = f.cu.j.b8();
            d += g.x;
            e += g.y;
        }
        if (this.b8 != null) {
            this.b8.aq = c;
            this.b8.by(this.b8.n, this.b8.o);
        }
    };
    CategoryToolTipLayerView.prototype.an = function (a) {
        _super.prototype.an.call(this, a);
        var b = this.b8;
        var c = 0;
        var d = 0;
        if (this.e.cv != null) {
            var e = this.e.cv.tk();
            c += e.x;
            d += e.y;
        }
        var f = b.a5();
        var g = b.n - c;
        var h = b.o - d;
        var i = new List$1(String_$type, 0);
        var j = this._tooltips.getChildCount();
        for (var k = 0; k < j; k++) {
            var indexliteral_ = k.toString();
            var l = this._tooltips.getChildAt(k).getText();
            var m = l.toString();
            i.add(m);
        }
        this.e.a5.addPointerTooltipData(a, f, g, h, i);
    };
    CategoryToolTipLayerView.$t = markType(CategoryToolTipLayerView, 'CategoryToolTipLayerView', AnnotationLayerView.$);
    return CategoryToolTipLayerView;
}(AnnotationLayerView));
export { CategoryToolTipLayerView };
