/*
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-react-core/ES2015/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/ES2015/type";
import { Series } from "./Series";
import { CategoryTooltipSeriesInfo } from "./CategoryTooltipSeriesInfo";
import { XamDataChart } from "./XamDataChart";
import { List$1 } from "igniteui-react-core/ES2015/List$1";
import { DataContext } from "igniteui-react-core/ES2015/DataContext";
import { Size } from "igniteui-react-core/ES2015/Size";
import { DeviceUtils } from "igniteui-react-core/ES2015/DeviceUtils";
import { PropertyMetadata } from "igniteui-react-core/ES2015/PropertyMetadata";
import { isNaN_ } from "igniteui-react-core/ES2015/number";
import { AnnotationLayerView } from "./AnnotationLayerView";
import { PointerTooltip } from "./PointerTooltip";
export class CategoryToolTipLayer extends AnnotationLayer {
    constructor() {
        super();
        let a = new CategoryToolTipLayerFrame();
        let b = new CategoryToolTipLayerFrame();
        let c = new CategoryToolTipLayerFrame();
        this.lq = a;
        this.lp = b;
        this.lr = c;
        this.ab = CategoryToolTipLayer.$;
    }
    bf() {
        return new CategoryToolTipLayerView(this);
    }
    ii(a) {
        super.ii(a);
        this.ms = a;
    }
    get_cm() {
        return true;
    }
    get cm() {
        return this.get_cm();
    }
    io(a, b, c, d) {
        super.io(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;
        }
    }
    l6(a, b) {
        if (this.mq == a) {
            if (!this.aq.c) {
                this.i1(b);
            }
        }
    }
    l7(a, b) {
        if (!this.aq.c) {
            this.i1(b);
        }
    }
    l9(a, b) {
        super.l9(a, b);
        let c = a;
        let 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) {
            let e = this.mq;
            if (e == null || !e.b2 || e.b0) {
                return;
            }
            if (!e.cd()) {
                return;
            }
            let f = e;
            let g = this.mt(f.cu, d);
            this.mz(g, c, d);
        }
        else {
            if (typeCast(XamDataChart.$, this.bs) !== null) {
                let h = this.bs;
                let i = true;
                let j = false;
                let k = new List$1(Series.$, 0);
                for (let l of fromEnum(h.axes)) {
                    if (l.b2 && !l.b0) {
                        if (!l.cd()) {
                            continue;
                        }
                        if (i) {
                            i = false;
                            j = l.ce;
                        }
                        else {
                            if (l.ce != j) {
                                continue;
                            }
                        }
                        for (let m of fromEnum(l.cu)) {
                            k.add(m);
                        }
                    }
                }
                let n = this.mt(k, d);
                this.mz(n, c, d);
            }
        }
    }
    mt(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.cq && !j.ck) {
                continue;
            }
            if (j.isStacked) {
                continue;
            }
            let k = j;
            if (!k.categoryAxis.cd()) {
                continue;
            }
            if (h) {
                g = j.isVertical;
            }
            else {
                if (g != j.isVertical) {
                    continue;
                }
            }
            let l = this.l2(j);
            if (!l.a) {
                continue;
            }
            let 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);
                }
            }
            let n = this.mv(j);
            if (n == null) {
                continue;
            }
            let o = null;
            let 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);
        }
        let q = 0;
        let 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;
            }
        }
        let s = new CategoryTooltipSeriesInfo();
        s.c = { $type: Point_$type, x: q, y: r };
        s.a = e;
        s.b = f;
        return s;
    }
    mz(a, b, c) {
        let d = a.c;
        let e = a.a;
        let f = a.b;
        d = this.ms.cb(d);
        let g = 0;
        let h = this.bi.bp;
        let i = this.ms.b5();
        this.ms.b7();
        let j = false;
        if (e.count == 0) {
            this.bi.am();
            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.ms.b2(m, o, n)) {
                k = true;
            }
            g++;
        }
        if (!k) {
            this.ms.b8(i);
            return;
        }
        let p = this.ms.cc(i);
        let q = 0;
        let r = 0;
        let s = 0;
        let t = 0;
        let u = false;
        let v = false;
        let w = this.mr;
        if (w == 2 || w == 3) {
            v = true;
        }
        if (w == 2 || w == 1) {
            u = true;
        }
        let 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;
            }
        }
        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;
    }
    mv(a) {
        return a.toolTip;
    }
    mc(a, b) {
        super.mc(a, b);
        let c = a;
        let d = b.bp.left;
        let e = b.bp.right;
        let f = b.bp.top;
        let g = b.bp.bottom;
        let h = this.ms.b5();
        if (isNaN_(c.k) || isNaN_(c.l) || isNaN_(c.g) || isNaN_(c.h)) {
            this.ms.b8(h);
            return;
        }
        let i = c.k;
        let j = c.l;
        let k = c.g;
        let l = c.h;
        this.ms.b9(h, i, j, k, l);
    }
    c5() {
        return true;
    }
}
CategoryToolTipLayer.$t = markType(CategoryToolTipLayer, 'CategoryToolTipLayer', AnnotationLayer.$);
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]]);
export class CategoryToolTipLayerView extends AnnotationLayerView {
    constructor(a) {
        super(a);
        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]+>)[^>]*$/;
    }
    b5() {
        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;
    }
    ap() {
        super.ap();
        if (this.b0 != null) {
            this.b0._visibility = 1;
        }
    }
    ag() {
        super.ag();
        if (this.b0 != null) {
            this.b0.bo();
            this.b0 = null;
        }
    }
    get htmlTest() {
        return this._htmlTest;
    }
    set htmlTest(a) {
        this._htmlTest = a;
    }
    b2(a, b, c) {
        if (a.fq == "default") {
            a.fq = a.bi.getDefaultTooltipTemplate();
        }
        let template_ = a.fq;
        let context_ = c;
        let d = false;
        if (!d) {
            return false;
        }
        if (a.ew == null) {
            return false;
        }
        let args_ = a.ew(c);
        let e = this.e.ey.expandTemplate(template_, args_);
        this._tooltips.append(e);
        return true;
    }
    cc(a) {
        let b = a;
        b.content = this._tooltips;
        let c = new Size(1, Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY);
        b.ct = 1;
        let d = b.cs(new Size(1, Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY));
        b.ct = 0;
        return d;
    }
    cb(a) {
        let b = 0;
        let c = 0;
        if (this.e.bs != null) {
            let d = this.e.bs.md();
            b += d.x;
            c += d.y;
        }
        return { $type: Point_$type, x: b + a.x, y: c + a.y };
    }
    b9(a, b, c, d, e) {
        let f = a;
        let g = f.content;
        f.cn = { $type: Point_$type, x: d, y: e };
        f._visibility = 0;
        f.n = b;
        f.o = c;
    }
    cd() {
        let a = (window.innerWidth);
        let b = (window.innerHeight);
        return new Size(1, a, b);
    }
    b8(a) {
        this._tooltips.removeChildren();
        this.b3.removeChildren();
        this.b0._visibility = 1;
    }
    b7() {
        if (this._tooltips != null) {
            let a = this._tooltips;
            this._tooltips = this.b3;
            this.b3 = a;
            this._tooltips.removeChildren();
        }
    }
    ca() {
    }
    a3(a, b) {
        super.a3(a, b);
        if (b) {
            return;
        }
        let c = null;
        if (this.e.bs != null) {
            c = this.e.bs.bk.i.i();
        }
        let d = 0;
        let e = 0;
        let f = this.e.bs;
        if (f != null) {
            let 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);
        }
    }
    ai(a) {
        super.ai(a);
        let b = this.b0;
        let c = 0;
        let d = 0;
        if (this.e.bs != null) {
            let e = this.e.bs.md();
            c += e.x;
            d += e.y;
        }
        let f = b.a3();
        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.e.a2.addPointerTooltipData(a, f, g, h, i);
    }
}
CategoryToolTipLayerView.$t = markType(CategoryToolTipLayerView, 'CategoryToolTipLayerView', AnnotationLayerView.$);
/*<BeginStatics Name="Infragistics.Controls.Charts.CategoryToolTipLayer" />*/
/*<EndStatics Name="Infragistics.Controls.Charts.CategoryToolTipLayer" />*/
