/*
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 { Axis } from "./Axis";
import { DependencyProperty } from "igniteui-react-core/ES5/DependencyProperty";
import { CategoryTooltipLayerPosition_$type } from "./CategoryTooltipLayerPosition";
import { CategoryToolTipLayerFrame } from "./CategoryToolTipLayerFrame";
import { Type, Base, enumGetBox, typeCast, fromEnum, Point_$type, markType, String_$type } from "igniteui-react-core/ES5/type";
import { Series } from "./Series";
import { CategoryTooltipSeriesInfo } from "./CategoryTooltipSeriesInfo";
import { XamDataChart } from "./XamDataChart";
import { List$1 } from "igniteui-react-core/ES5/List$1";
import { DataContext } from "igniteui-react-core/ES5/DataContext";
import { Size } from "igniteui-react-core/ES5/Size";
import { DeviceUtils } from "igniteui-react-core/ES5/DeviceUtils";
import { PropertyMetadata } from "igniteui-react-core/ES5/PropertyMetadata";
import { isNaN_ } from "igniteui-react-core/ES5/number";
import { AnnotationLayerView } from "./AnnotationLayerView";
import { PointerTooltip } from "./PointerTooltip";
/**
 * @hidden
 */
var CategoryToolTipLayer = /** @class */ (function (_super) {
    tslib_1.__extends(CategoryToolTipLayer, _super);
    function CategoryToolTipLayer() {
        var _this = _super.call(this) || this;
        var a = new CategoryToolTipLayerFrame();
        var b = new CategoryToolTipLayerFrame();
        var c = new CategoryToolTipLayerFrame();
        _this.lv = a;
        _this.lu = b;
        _this.lw = c;
        _this.ab = CategoryToolTipLayer.$;
        return _this;
    }
    CategoryToolTipLayer.prototype.bf = function () {
        return new CategoryToolTipLayerView(this);
    };
    CategoryToolTipLayer.prototype.im = function (a) {
        _super.prototype.im.call(this, a);
        this.mx = a;
    };
    CategoryToolTipLayer.prototype.get_cn = function () {
        return true;
    };
    CategoryToolTipLayer.prototype.is = function (a, b, c, d) {
        _super.prototype.is.call(this, a, b, c, d);
        switch (b) {
            case CategoryToolTipLayer.$$p[0]:
                this.i6(true);
                break;
            case CategoryToolTipLayer.$$p[2]:
                this.i6(true);
                break;
            case CategoryToolTipLayer.$$p[1]:
                this.i6(true);
                break;
            case "SeriesViewer":
                this.mx.cc();
                break;
        }
    };
    CategoryToolTipLayer.prototype.mb = function (a, b) {
        if (this.mv == a) {
            if (!this.aq.c) {
                this.i6(b);
            }
        }
    };
    CategoryToolTipLayer.prototype.mc = function (a, b) {
        if (!this.aq.c) {
            this.i6(b);
        }
    };
    CategoryToolTipLayer.prototype.me = function (a, b) {
        var e_1, _a, e_2, _b;
        _super.prototype.me.call(this, a, b);
        var c = a;
        var d = this.mz;
        c.l = NaN;
        c.k = NaN;
        c.g = NaN;
        c.h = NaN;
        c.k = NaN;
        c.l = NaN;
        if (isNaN_(this.ms.x) && isNaN_(this.ms.y)) {
            this.mx.ca(this.mx.b7());
            return;
        }
        if (this.mv != null) {
            var e = this.mv;
            if (e == null || !e.b2 || e.b0) {
                return;
            }
            if (!e.cd()) {
                return;
            }
            var f = e;
            var g = this.my(f.cu, d);
            this.m4(g, c, d);
        }
        else {
            if (typeCast(XamDataChart.$, this.bs) !== null) {
                var h = this.bs;
                var i = true;
                var j = false;
                var k = new List$1(Series.$, 0);
                try {
                    for (var _c = tslib_1.__values(fromEnum(h.axes)), _d = _c.next(); !_d.done; _d = _c.next()) {
                        var l = _d.value;
                        if (l.b2 && !l.b0) {
                            if (!l.cd()) {
                                continue;
                            }
                            if (i) {
                                i = false;
                                j = l.ce;
                            }
                            else {
                                if (l.ce != j) {
                                    continue;
                                }
                            }
                            try {
                                for (var _e = tslib_1.__values(fromEnum(l.cu)), _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.my(k, d);
                this.m4(n, c, d);
            }
        }
    };
    CategoryToolTipLayer.prototype.my = 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.cr && !j.cl) {
                continue;
            }
            if (j.isStacked) {
                continue;
            }
            var k = j;
            if (!k.categoryAxis.cd()) {
                continue;
            }
            if (h) {
                g = j.isVertical;
            }
            else {
                if (g != j.isVertical) {
                    continue;
                }
            }
            var l = this.l7(j);
            if (!l.c) {
                continue;
            }
            var m = j.le(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.m0(j);
            if (n == null) {
                continue;
            }
            var o = null;
            var p = j.fm(this.ms);
            if (j.co && p != null) {
                o = j.as(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.lr.right;
            if (this.mw == 2 || this.mw == 1) {
                q = this.lr.left;
            }
            if (r < 0 || r > this.lr.bottom) {
                r = NaN;
            }
        }
        else {
            q = (c + d) / 2;
            r = this.lr.top;
            if (this.mw == 2 || this.mw == 1) {
                r = this.lr.bottom;
            }
            if (q < 0 || q > this.lr.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.m4 = function (a, b, c) {
        var d = a.c;
        var e = a.a;
        var f = a.b;
        d = this.mx.cd(d);
        var g = 0;
        var h = this.bi.br;
        var i = this.mx.b7();
        this.mx.b9();
        var j = false;
        if (e.count == 0) {
            this.bi.am();
            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.mx.b4(m, o, n)) {
                k = true;
            }
            g++;
        }
        if (!k) {
            this.mx.ca(i);
            return;
        }
        var p = this.mx.ce(i);
        var q = 0;
        var r = 0;
        var s = 0;
        var t = 0;
        var u = false;
        var v = false;
        var w = this.mw;
        if (w == 2 || w == 3) {
            v = true;
        }
        if (w == 2 || w == 1) {
            u = true;
        }
        var x = this.mx.cf();
        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.m0 = function (a) {
        return a.toolTip;
    };
    CategoryToolTipLayer.prototype.mh = function (a, b) {
        _super.prototype.mh.call(this, a, b);
        var c = a;
        var d = b.br.left;
        var e = b.br.right;
        var f = b.br.top;
        var g = b.br.bottom;
        var h = this.mx.b7();
        if (isNaN_(c.k) || isNaN_(c.l) || isNaN_(c.g) || isNaN_(c.h)) {
            this.mx.ca(h);
            return;
        }
        var i = c.k;
        var j = c.l;
        var k = c.g;
        var l = c.h;
        this.mx.cb(h, i, j, k, l);
    };
    CategoryToolTipLayer.prototype.c7 = function () {
        return true;
    };
    CategoryToolTipLayer.$t = markType(CategoryToolTipLayer, 'CategoryToolTipLayer', AnnotationLayer.$);
    return CategoryToolTipLayer;
}(AnnotationLayer));
export { CategoryToolTipLayer };
Type.dep(DependencyProperty, PropertyMetadata, CategoryToolTipLayer, 'raisePropertyChanged', ['TargetAxis:mv:m5', [Axis.$, null], 'ToolTipPosition:mw:m6', [CategoryTooltipLayerPosition_$type, enumGetBox(CategoryTooltipLayerPosition_$type, 0)], 'UseInterpolation:mz:m7', [0, false]]);
/**
 * @hidden
 */
var CategoryToolTipLayerView = /** @class */ (function (_super) {
    tslib_1.__extends(CategoryToolTipLayerView, _super);
    function CategoryToolTipLayerView(a) {
        var _this = _super.call(this, a) || this;
        _this.b1 = null;
        _this.b2 = null;
        _this._tooltips = null;
        _this.b5 = null;
        _this.b3 = false;
        _this._htmlTest = null;
        _this.b1 = a;
        _this.htmlTest = /^[^<]*(<[\w\W]+>)[^>]*$/;
        return _this;
    }
    CategoryToolTipLayerView.prototype.b7 = function () {
        if (this.b2 == null) {
            this.b2 = new PointerTooltip();
            this.b2.bs(this.e.e1);
            this.b2.ao = this.e.bs.bn.j;
            this._tooltips = this.e.e1.createElement("div");
            this.b5 = this.e.e1.createElement("div");
            this.b2._visibility = 0;
        }
        return this.b2;
    };
    CategoryToolTipLayerView.prototype.ap = function () {
        _super.prototype.ap.call(this);
        if (this.b2 != null) {
            this.b2._visibility = 1;
        }
    };
    CategoryToolTipLayerView.prototype.ag = function () {
        _super.prototype.ag.call(this);
        if (this.b2 != null) {
            this.b2.bo();
            this.b2 = null;
        }
    };
    Object.defineProperty(CategoryToolTipLayerView.prototype, "htmlTest", {
        get: function () {
            return this._htmlTest;
        },
        set: function (a) {
            this._htmlTest = a;
        },
        enumerable: true,
        configurable: true
    });
    CategoryToolTipLayerView.prototype.b4 = function (a, b, c) {
        if (a.fu == "default") {
            a.fu = a.bi.getDefaultTooltipTemplate();
        }
        var template_ = a.fu;
        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.ce = function (a) {
        var b = a;
        b.content = this._tooltips;
        var c = new Size(1, Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY);
        b.ct = 1;
        var d = b.cs(new Size(1, Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY));
        b.ct = 0;
        return d;
    };
    CategoryToolTipLayerView.prototype.cd = function (a) {
        var b = 0;
        var c = 0;
        if (this.e.bs != null) {
            var d = this.e.bs.mj();
            b += d.x;
            c += d.y;
        }
        return { $type: Point_$type, x: b + a.x, y: c + a.y };
    };
    CategoryToolTipLayerView.prototype.cb = function (a, b, c, d, e) {
        var f = a;
        var g = f.content;
        f.cn = { $type: Point_$type, x: d, y: e };
        f._visibility = 0;
        f.n = b;
        f.o = c;
    };
    CategoryToolTipLayerView.prototype.cf = function () {
        var a = (window.innerWidth);
        var b = (window.innerHeight);
        return new Size(1, a, b);
    };
    CategoryToolTipLayerView.prototype.ca = function (a) {
        this._tooltips.removeChildren();
        this.b5.removeChildren();
        this.b2._visibility = 1;
    };
    CategoryToolTipLayerView.prototype.b9 = function () {
        if (this._tooltips != null) {
            this._tooltips.removeChildren();
        }
    };
    CategoryToolTipLayerView.prototype.cc = function () {
    };
    CategoryToolTipLayerView.prototype.a3 = function (a, b) {
        _super.prototype.a3.call(this, a, b);
        if (b) {
            return;
        }
        var c = null;
        if (this.e.bs != null) {
            c = this.e.bs.bn.i.i();
        }
        var d = 0;
        var e = 0;
        var f = this.e.bs;
        if (f != null) {
            var g = f.bn.i.b5();
            d += g.x;
            e += g.y;
        }
        if (this.b2 != null) {
            this.b2.ap = c;
            this.b2.bu(this.b2.n, this.b2.o);
        }
    };
    CategoryToolTipLayerView.prototype.ai = function (a) {
        _super.prototype.ai.call(this, a);
        var b = this.b2;
        var c = 0;
        var d = 0;
        if (this.e.bs != null) {
            var e = this.e.bs.mj();
            c += e.x;
            d += e.y;
        }
        var f = b.a3();
        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.a2.addPointerTooltipData(a, f, g, h, i);
    };
    CategoryToolTipLayerView.$t = markType(CategoryToolTipLayerView, 'CategoryToolTipLayerView', AnnotationLayerView.$);
    return CategoryToolTipLayerView;
}(AnnotationLayerView));
export { CategoryToolTipLayerView };
