/*
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 { AnnotationLayer } from "./AnnotationLayer";
import { Axis } from "./Axis";
import { DependencyProperty } from "igniteui-angular-core";
import { CategoryTooltipLayerPosition_$type } from "./CategoryTooltipLayerPosition";
import { Brush } from "igniteui-angular-core";
import { Series } from "./Series";
import { CategoryToolTipLayerFrame } from "./CategoryToolTipLayerFrame";
import { Base, enumGetBox, typeCast, fromEnum, Point_$type, markType, markDep, String_$type } from "igniteui-angular-core";
import { CategoryTooltipSeriesInfo } from "./CategoryTooltipSeriesInfo";
import { XamDataChart } from "./XamDataChart";
import { List$1 } from "igniteui-angular-core";
import { DataContext } from "igniteui-angular-core";
import { Size } from "igniteui-angular-core";
import { DeviceUtils } from "igniteui-angular-core";
import { PropertyMetadata } from "igniteui-angular-core";
import { isNaN_ } from "igniteui-angular-core";
import { AnnotationLayerView } from "./AnnotationLayerView";
import { PointerTooltip } from "./PointerTooltip";
import { PointerTooltipStyle } from "./PointerTooltipStyle";
import { PointerTooltipUtils } from "./PointerTooltipUtils";
/**
 * @hidden
 */
export let CategoryToolTipLayer = /*@__PURE__*/ (() => {
    class CategoryToolTipLayer extends AnnotationLayer {
        constructor() {
            super();
            let a = new CategoryToolTipLayerFrame();
            let b = new CategoryToolTipLayerFrame();
            let c = new CategoryToolTipLayerFrame();
            this.xm = a;
            this.xl = b;
            this.xn = c;
            this.ab = CategoryToolTipLayer.$;
        }
        bk() {
            return new CategoryToolTipLayer();
        }
        get_et() {
            return true;
        }
        ct() {
            return new CategoryToolTipLayerView(this);
        }
        q7(a) {
            super.q7(a);
            this.aah = a;
        }
        get_e4() {
            return true;
        }
        rg(a, b, c, d) {
            super.rg(a, b, c, d);
            switch (b) {
                case CategoryToolTipLayer.$$p[0]:
                    this.ry(true);
                    break;
                case CategoryToolTipLayer.$$p[5]:
                    this.ry(true);
                    break;
                case CategoryToolTipLayer.$$p[4]:
                    this.ry(true);
                    break;
                case "SeriesViewer":
                    this.aah.cy();
                    break;
                case CategoryToolTipLayer.$$p[1]:
                case CategoryToolTipLayer.$$p[2]:
                case CategoryToolTipLayer.$$p[3]:
                    this.ry(true);
                    break;
            }
        }
        y0(a, b) {
            if (this.aaf == a) {
                if (!this.a1.c) {
                    this.ry(b);
                }
            }
        }
        y1(a, b) {
            if (!this.a1.c) {
                this.ry(b);
            }
        }
        y3(a, b) {
            super.y3(a, b);
            let c = a;
            let d = this.aaj;
            c.l = NaN;
            c.k = NaN;
            c.g = NaN;
            c.h = NaN;
            c.k = NaN;
            c.l = NaN;
            if (isNaN_(this.aac.x) && isNaN_(this.aac.y)) {
                this.aah.cw(this.aah.ct());
                return;
            }
            if (this.aaf != null) {
                let e = this.aaf;
                if (e == null || !e.c8 || e.c5) {
                    return;
                }
                if (!e.dp()) {
                    return;
                }
                let f = e;
                let g = this.aai(f.eh, d);
                this.aas(g, c, d);
            }
            else {
                if (typeCast(XamDataChart.$, this.dc) !== null) {
                    let h = this.dc;
                    let i = true;
                    let j = false;
                    let k = new List$1(Series.$, 0);
                    for (let l of fromEnum(h.axes)) {
                        if (l.c8 && !l.c5) {
                            if (!l.dp()) {
                                continue;
                            }
                            if (i) {
                                i = false;
                                j = l.dq;
                            }
                            else {
                                if (l.dq != j) {
                                    continue;
                                }
                            }
                            for (let m of fromEnum(l.eh)) {
                                k.add(m);
                            }
                        }
                    }
                    let n = this.aai(k, d);
                    this.aas(n, c, d);
                }
            }
        }
        aai(a, b) {
            let c = 1.7976931348623157E+308;
            let d = -1.7976931348623157E+308;
            let e = new List$1(DataContext.$, 0);
            let f = new List$1(Base.$, 0);
            let g = false;
            let h = true;
            for (let i = 0; i < a.count; i++) {
                let j = a.item(i);
                if (!j.e8 && !j.e0) {
                    continue;
                }
                if (j.isStacked) {
                    continue;
                }
                let k = j;
                if (!k.categoryAxis.dp()) {
                    continue;
                }
                if (h) {
                    g = j.isVertical;
                }
                else {
                    if (g != j.isVertical) {
                        continue;
                    }
                }
                let l = this.yw(j);
                if (!l.c) {
                    continue;
                }
                let m = j.wm(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);
                    }
                }
                let n = this.aal(j);
                if (n == null) {
                    continue;
                }
                let o = null;
                let p = j.kr(this.aac);
                if (j.e5 && p != null) {
                    o = j.a3(p);
                }
                else {
                    o = new DataContext();
                    o.item = p;
                    o.series = j;
                }
                e.add(o);
                f.add1(n);
            }
            let q = 0;
            let r = 0;
            if (g) {
                r = (c + d) / 2;
                q = this.w2.right;
                if (this.aag == 2 || this.aag == 1) {
                    q = this.w2.left;
                }
                if (r < 0 || r > this.w2.bottom) {
                    r = NaN;
                }
            }
            else {
                q = (c + d) / 2;
                r = this.w2.top;
                if (this.aag == 2 || this.aag == 1) {
                    r = this.w2.bottom;
                }
                if (q < 0 || q > this.w2.right) {
                    q = NaN;
                }
            }
            let s = new CategoryTooltipSeriesInfo();
            s.c = { $type: Point_$type, x: q, y: r };
            s.a = e;
            s.b = f;
            return s;
        }
        qd() {
            this.aac = { $type: Point_$type, x: NaN, y: NaN };
            this.cw.a1();
        }
        aas(a, b, c) {
            let d = a.c;
            let e = a.a;
            let f = a.b;
            d = this.aah.cz(d);
            let g = 0;
            let h = this.cw.b9;
            let i = this.aah.ct();
            this.aah.cv();
            let j = false;
            if (e.count == 0) {
                this.cw.a1();
                return;
            }
            let k = false;
            for (let l = 0; l < e.count; l++) {
                let m = e._inner[l].series;
                j = m.isVertical;
                let n = e._inner[l];
                let o = f._inner[l];
                if (this.aah.cq(m, o, n)) {
                    k = true;
                }
                g++;
            }
            if (!k) {
                this.aah.cw(i);
                return;
            }
            let p = this.aah.c0(i);
            let q = 0;
            let r = 0;
            let s = 0;
            let t = 0;
            let u = false;
            let v = false;
            let w = this.aag;
            if (w == 2 || w == 3) {
                v = true;
            }
            if (w == 2 || w == 1) {
                u = true;
            }
            let x = this.aah.c1();
            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;
                }
            }
            let y = DeviceUtils.g(10);
            let 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;
        }
        aal(a) {
            return a.toolTip;
        }
        y6(a, b) {
            super.y6(a, b);
            let c = a;
            let d = b.b9.left;
            let e = b.b9.right;
            let f = b.b9.top;
            let g = b.b9.bottom;
            let h = this.aah.ct();
            if (isNaN_(c.k) || isNaN_(c.l) || isNaN_(c.g) || isNaN_(c.h)) {
                this.aah.cw(h);
                return;
            }
            let i = c.k;
            let j = c.l;
            let k = c.g;
            let l = c.h;
            if (h != null) {
                this.aah.cx(h, i, j, k, l);
            }
        }
        gb() {
            return true;
        }
    }
    CategoryToolTipLayer.$t = markType(CategoryToolTipLayer, 'CategoryToolTipLayer', AnnotationLayer.$);
    CategoryToolTipLayer.$$p = markDep(DependencyProperty, PropertyMetadata, CategoryToolTipLayer, 'raisePropertyChanged', ['TargetAxis:aaf:aat', [Axis.$, null], 'ToolTipBackground:aaz:aau', [Brush.$, null], 'ToolTipBorderBrush:aa0:aav', [Brush.$, null], 'ToolTipBorderThickness:aak:aaw', [1, NaN], 'ToolTipPosition:aag:aax', [CategoryTooltipLayerPosition_$type, enumGetBox(CategoryTooltipLayerPosition_$type, 0)], 'UseInterpolation:aaj:aay', [0, false]]);
    return CategoryToolTipLayer;
})();
/**
 * @hidden
 */
export let CategoryToolTipLayerView = /*@__PURE__*/ (() => {
    class CategoryToolTipLayerView extends AnnotationLayerView {
        constructor(a) {
            super(a);
            this.cn = null;
            this.co = null;
            this._tooltips = null;
            this.cr = null;
            this.cp = false;
            this._htmlTest = null;
            this.cn = a;
            this.htmlTest = /^[^<]*(<[\w\W]+>)[^>]*$/;
        }
        ct() {
            if (this.co == null) {
                this.co = new PointerTooltip();
                this.co.bw(this.f.j3);
                this.co.ap = this.f.dc.dp.k;
                this._tooltips = this.f.j3.createElement("div");
                this.cr = this.f.j3.createElement("div");
                this.co._visibility = 0;
            }
            return this.co;
        }
        a4() {
            super.a4();
            if (this.co != null) {
                this.co._visibility = 1;
            }
        }
        as() {
            super.as();
            if (this.co != null) {
                this.co.br();
                this.co = null;
            }
        }
        get htmlTest() {
            return this._htmlTest;
        }
        set htmlTest(a) {
            this._htmlTest = a;
        }
        cq(a, b, c) {
            if (a.k6 == "default") {
                a.k6 = a.cw.getDefaultTooltipTemplate();
            }
            let template_ = a.k6;
            let context_ = c;
            if (a.toolTip == null) {
                return false;
            }
            let 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;
        }
        c0(a) {
            let b = a;
            b.content = this._tooltips;
            let c = new Size(1, Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY);
            b.cy = 1;
            let d = b.cx(new Size(1, Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY));
            b.cy = 0;
            return d;
        }
        cz(a) {
            let b = 0;
            let c = 0;
            if (this.f.dc != null) {
                let d = this.f.dc.xf();
                b += d.x;
                c += d.y;
            }
            return { $type: Point_$type, x: b + a.x, y: c + a.y };
        }
        cx(a, b, c, d, e) {
            let f = a;
            let g = f.content;
            f.cs = { $type: Point_$type, x: d, y: e };
            f._visibility = 0;
            f.n = b;
            f.o = c;
        }
        c1() {
            let a = (window.innerWidth);
            let b = (window.innerHeight);
            return new Size(1, a, b);
        }
        cw(a) {
            this._tooltips.removeChildren();
            this.cr.removeChildren();
            this.co._visibility = 1;
        }
        cv() {
            if (this._tooltips != null) {
                this._tooltips.removeChildren();
            }
        }
        cy() {
        }
        bj(a, b) {
            super.bj(a, b);
            if (b) {
                return;
            }
            let c = new PointerTooltipStyle();
            if (this.f.dc != null && this.f.dc.dp != null && this.f.dc.dp.j != null) {
                let d = this.f.dc.dp.j.i();
                c = new PointerTooltipStyle();
                PointerTooltipUtils.a(d, c, this.cn.aaz, this.cn.aa0, this.cn.aak);
            }
            let e = 0;
            let f = 0;
            let g = this.f.dc;
            if (g != null) {
                let h = g.dp.j.b8();
                e += h.x;
                f += h.y;
            }
            if (this.co != null) {
                this.co.aq = c;
                this.co.by(this.co.n, this.co.o);
            }
        }
        ax(a) {
            super.ax(a);
            let b = this.co;
            let c = 0;
            let d = 0;
            if (this.f.dc != null) {
                let e = this.f.dc.xf();
                c += e.x;
                d += e.y;
            }
            let f = b.a5();
            let g = b.n - c;
            let h = b.o - d;
            let i = new List$1(String_$type, 0);
            let j = this._tooltips.getChildCount();
            for (let k = 0; k < j; k++) {
                let indexliteral_ = k.toString();
                let l = this._tooltips.getChildAt(k).getText();
                let m = l.toString();
                i.add(m);
            }
            this.f.bc.addPointerTooltipData(a, f, g, h, i);
        }
    }
    CategoryToolTipLayerView.$t = markType(CategoryToolTipLayerView, 'CategoryToolTipLayerView', AnnotationLayerView.$);
    return CategoryToolTipLayerView;
})();
