/*
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";
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.lq = a;
        _this.lp = b;
        _this.lr = c;
        _this.ab = CategoryToolTipLayer.$;
        return _this;
    }
    CategoryToolTipLayer.prototype.bf = function () {
        return new CategoryToolTipLayerView(this);
    };
    CategoryToolTipLayer.prototype.ii = function (a) {
        _super.prototype.ii.call(this, a);
        this.ms = a;
    };
    CategoryToolTipLayer.prototype.get_cm = function () {
        return true;
    };
    Object.defineProperty(CategoryToolTipLayer.prototype, "cm", {
        get: function () {
            return this.get_cm();
        },
        enumerable: true,
        configurable: true
    });
    CategoryToolTipLayer.prototype.io = function (a, b, c, d) {
        _super.prototype.io.call(this, a, b, c, d);
        switch (b) {
            case CategoryToolTipLayer.$$p[0]:
                this.i1(true);
                break;
            case CategoryToolTipLayer.$$p[2]:
                this.i1(true);
                break;
            case CategoryToolTipLayer.$$p[1]:
                this.i1(true);
                break;
            case "SeriesViewer":
                this.ms.ca();
                break;
        }
    };
    CategoryToolTipLayer.prototype.l6 = function (a, b) {
        if (this.mq == a) {
            if (!this.aq.c) {
                this.i1(b);
            }
        }
    };
    CategoryToolTipLayer.prototype.l7 = function (a, b) {
        if (!this.aq.c) {
            this.i1(b);
        }
    };
    CategoryToolTipLayer.prototype.l9 = function (a, b) {
        _super.prototype.l9.call(this, a, b);
        var c = a;
        var d = this.mu;
        c.l = NaN;
        c.k = NaN;
        c.g = NaN;
        c.h = NaN;
        c.k = NaN;
        c.l = NaN;
        if (isNaN_(this.mn.x) && isNaN_(this.mn.y)) {
            this.ms.b8(this.ms.b5());
            return;
        }
        if (this.mq != null) {
            var e = this.mq;
            if (e == null || !e.b2 || e.b0) {
                return;
            }
            if (!e.cd()) {
                return;
            }
            var f = e;
            var g = this.mt(f.cu, d);
            this.mz(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 _a = tslib_1.__values(fromEnum(h.axes)), _b = _a.next(); !_b.done; _b = _a.next()) {
                        var l = _b.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 _c = tslib_1.__values(fromEnum(l.cu)), _d = _c.next(); !_d.done; _d = _c.next()) {
                                    var m = _d.value;
                                    k.add(m);
                                }
                            }
                            catch (e_1_1) { e_1 = { error: e_1_1 }; }
                            finally {
                                try {
                                    if (_d && !_d.done && (_e = _c.return)) _e.call(_c);
                                }
                                finally { if (e_1) throw e_1.error; }
                            }
                        }
                    }
                }
                catch (e_2_1) { e_2 = { error: e_2_1 }; }
                finally {
                    try {
                        if (_b && !_b.done && (_f = _a.return)) _f.call(_a);
                    }
                    finally { if (e_2) throw e_2.error; }
                }
                var n = this.mt(k, d);
                this.mz(n, c, d);
            }
        }
        var e_2, _f, e_1, _e;
    };
    CategoryToolTipLayer.prototype.mt = 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.cq && !j.ck) {
                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.l2(j);
            if (!l.a) {
                continue;
            }
            var m = j.k9(l.b, 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.mv(j);
            if (n == null) {
                continue;
            }
            var o = null;
            var p = j.fj(this.mn);
            if (j.cn && 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.lm.right;
            if (this.mr == 2 || this.mr == 1) {
                q = this.lm.left;
            }
            if (r < 0 || r > this.lm.bottom) {
                r = NaN;
            }
        }
        else {
            q = (c + d) / 2;
            r = this.lm.top;
            if (this.mr == 2 || this.mr == 1) {
                r = this.lm.bottom;
            }
            if (q < 0 || q > this.lm.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.mz = function (a, b, c) {
        var d = a.c;
        var e = a.a;
        var f = a.b;
        d = this.ms.cb(d);
        var g = 0;
        var h = this.bi.bp;
        var i = this.ms.b5();
        this.ms.b7();
        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.ms.b2(m, o, n)) {
                k = true;
            }
            g++;
        }
        if (!k) {
            this.ms.b8(i);
            return;
        }
        var p = this.ms.cc(i);
        var q = 0;
        var r = 0;
        var s = 0;
        var t = 0;
        var u = false;
        var v = false;
        var w = this.mr;
        if (w == 2 || w == 3) {
            v = true;
        }
        if (w == 2 || w == 1) {
            u = true;
        }
        var x = this.ms.cd();
        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.mv = function (a) {
        return a.toolTip;
    };
    CategoryToolTipLayer.prototype.mc = function (a, b) {
        _super.prototype.mc.call(this, a, b);
        var c = a;
        var d = b.bp.left;
        var e = b.bp.right;
        var f = b.bp.top;
        var g = b.bp.bottom;
        var h = this.ms.b5();
        if (isNaN_(c.k) || isNaN_(c.l) || isNaN_(c.g) || isNaN_(c.h)) {
            this.ms.b8(h);
            return;
        }
        var i = c.k;
        var j = c.l;
        var k = c.g;
        var l = c.h;
        this.ms.b9(h, i, j, k, l);
    };
    CategoryToolTipLayer.prototype.c5 = function () {
        return true;
    };
    CategoryToolTipLayer.$t = markType(CategoryToolTipLayer, 'CategoryToolTipLayer', AnnotationLayer.$);
    return CategoryToolTipLayer;
}(AnnotationLayer));
export { CategoryToolTipLayer };
Type.dep(DependencyProperty, PropertyMetadata, CategoryToolTipLayer, 'raisePropertyChanged', ['TargetAxis:mq:m0', [Axis.$, null], 'ToolTipPosition:mr:m1', [CategoryTooltipLayerPosition_$type, enumGetBox(CategoryTooltipLayerPosition_$type, 0)], 'UseInterpolation:mu:m2', [0, false]]);
var CategoryToolTipLayerView = /** @class */ (function (_super) {
    tslib_1.__extends(CategoryToolTipLayerView, _super);
    function CategoryToolTipLayerView(a) {
        var _this = _super.call(this, a) || this;
        _this.bz = null;
        _this.b0 = null;
        _this._tooltips = null;
        _this.b3 = null;
        _this.b1 = false;
        _this._htmlTest = null;
        _this.bz = a;
        _this.htmlTest = /^[^<]*(<[\w\W]+>)[^>]*$/;
        return _this;
    }
    CategoryToolTipLayerView.prototype.b5 = function () {
        if (this.b0 == null) {
            this.b0 = new PointerTooltip();
            this.b0.bs(this.e.ey);
            this.b0.ao = this.e.bs.bk.j;
            this._tooltips = this.e.ey.createElement("div");
            this.b3 = this.e.ey.createElement("div");
            this.b0._visibility = 0;
        }
        return this.b0;
    };
    CategoryToolTipLayerView.prototype.ap = function () {
        _super.prototype.ap.call(this);
        if (this.b0 != null) {
            this.b0._visibility = 1;
        }
    };
    CategoryToolTipLayerView.prototype.ag = function () {
        _super.prototype.ag.call(this);
        if (this.b0 != null) {
            this.b0.bo();
            this.b0 = null;
        }
    };
    Object.defineProperty(CategoryToolTipLayerView.prototype, "htmlTest", {
        get: function () {
            return this._htmlTest;
        },
        set: function (a) {
            this._htmlTest = a;
        },
        enumerable: true,
        configurable: true
    });
    CategoryToolTipLayerView.prototype.b2 = function (a, b, c) {
        if (a.fq == "default") {
            a.fq = a.bi.getDefaultTooltipTemplate();
        }
        var template_ = a.fq;
        var context_ = c;
        var d = false;
        if (!d) {
            return false;
        }
        if (a.ew == null) {
            return false;
        }
        var args_ = a.ew(c);
        var e = this.e.ey.expandTemplate(template_, args_);
        this._tooltips.append(e);
        return true;
    };
    CategoryToolTipLayerView.prototype.cc = 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.cb = function (a) {
        var b = 0;
        var c = 0;
        if (this.e.bs != null) {
            var d = this.e.bs.md();
            b += d.x;
            c += d.y;
        }
        return { $type: Point_$type, x: b + a.x, y: c + a.y };
    };
    CategoryToolTipLayerView.prototype.b9 = 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.cd = function () {
        var a = (window.innerWidth);
        var b = (window.innerHeight);
        return new Size(1, a, b);
    };
    CategoryToolTipLayerView.prototype.b8 = function (a) {
        this._tooltips.removeChildren();
        this.b3.removeChildren();
        this.b0._visibility = 1;
    };
    CategoryToolTipLayerView.prototype.b7 = function () {
        if (this._tooltips != null) {
            var a = this._tooltips;
            this._tooltips = this.b3;
            this.b3 = a;
            this._tooltips.removeChildren();
        }
    };
    CategoryToolTipLayerView.prototype.ca = 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.bk.i.i();
        }
        var d = 0;
        var e = 0;
        var f = this.e.bs;
        if (f != null) {
            var g = f.bk.i.b5();
            d += g.x;
            e += g.y;
        }
        if (this.b0 != null) {
            this.b0.ap = c;
            this.b0.bu(this.b0.n, this.b0.o);
        }
    };
    CategoryToolTipLayerView.prototype.ai = function (a) {
        _super.prototype.ai.call(this, a);
        var b = this.b0;
        var c = 0;
        var d = 0;
        if (this.e.bs != null) {
            var e = this.e.bs.md();
            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 };
/*<BeginStatics Name="Infragistics.Controls.Charts.CategoryToolTipLayer" />*/
/*<EndStatics Name="Infragistics.Controls.Charts.CategoryToolTipLayer" />*/
