/*
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 { DependencyProperty } from "igniteui-angular-core";
import { Series } from "./Series";
import { ItemTooltipLayerFrame } from "./ItemTooltipLayerFrame";
import { Base, String_$type, fromEnum, Point_$type, markType, markDep, runOn, typeCast } from "igniteui-angular-core";
import { Pool$1 } from "igniteui-angular-core";
import { List$1 } from "igniteui-angular-core";
import { ItemTooltipCollisionInfo } from "./ItemTooltipCollisionInfo";
import { Rect } 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 { DomWrapper_$type } from "igniteui-angular-core";
/**
 * @hidden
 */
export let ItemToolTipLayer = /*@__PURE__*/ (() => {
    class ItemToolTipLayer extends AnnotationLayer {
        constructor() {
            super();
            let a = new ItemTooltipLayerFrame();
            let b = new ItemTooltipLayerFrame();
            let c = new ItemTooltipLayerFrame();
            let d = this.ho / 1000;
            this.qa = a;
            this.p9 = b;
            this.qb = c;
            this.ab = ItemToolTipLayer.$;
        }
        get_db() {
            return true;
        }
        bx() {
            return new ItemToolTipLayerView(this);
        }
        l2(a) {
            super.l2(a);
            this.rc = a;
        }
        get_dl() {
            return true;
        }
        l7(a, b, c, d) {
            super.l7(a, b, c, d);
            switch (b) {
                case ItemToolTipLayer.$$p[1]:
                case ItemToolTipLayer.$$p[2]:
                    this.ml(true);
                    break;
                case ItemToolTipLayer.$$p[3]:
                    this.ml(true);
                    break;
                case ItemToolTipLayer.$$p[0]:
                    this.ml(true);
                    break;
                case "SeriesViewer":
                    this.rc.cg();
                    break;
            }
        }
        qt(a, b) {
            if (this.rd == null || this.rd == a) {
                if (!this.as.c) {
                    this.ml(b);
                }
            }
        }
        qg(a) {
            if (!super.qg(a)) {
                return false;
            }
            if (this.rj != null) {
                if (a.dv) {
                    let b = a.jr();
                    if (!Base.equalsStatic(this.rj, b)) {
                        return false;
                    }
                }
                else {
                    if (!Base.equalsStatic(this.rj, a.name)) {
                        return false;
                    }
                }
            }
            let c = this.qo(a);
            if (!c.c) {
                return false;
            }
            return true;
        }
        qv(a, b) {
            super.qv(a, b);
            let c = a;
            let d = this.rc.b5;
            let e = this.rg;
            let f = this.rf;
            c.o.clear();
            c.i.clear();
            c.n.clear();
            c.g.clear();
            c.f.clear();
            c.l.clear();
            c.j.clear();
            c.m.clear();
            c.k.clear();
            c.h.clear();
            if (isNaN_(this.q9.x) && isNaN_(this.q9.y)) {
                d.count = 0;
                return;
            }
            let g = 0;
            if (this.qg(this.rd)) {
                this.ro(this.rd, c, e, f, 0);
                g++;
            }
            else {
                for (let h of fromEnum(this.cd.series)) {
                    if (this.qg(h)) {
                        this.ro(h, c, e, f, g);
                        g++;
                    }
                    if (h.isStacked) {
                        h.ld((i) => {
                            if (this.qg(i)) {
                                this.ro(i, c, e, f, g);
                                g++;
                            }
                            return true;
                        });
                    }
                }
                this.rn(c);
            }
            d.count = g;
        }
        rn(a) {
            let b = new List$1(ItemTooltipCollisionInfo.$, 0);
            for (let c = 0; c < a.f.count; c++) {
                let d = new ItemTooltipCollisionInfo();
                d.d = c;
                d.e = { $type: Point_$type, x: a.f._inner[c], y: a.g._inner[c] };
                d.c = a.m._inner[c];
                d.a = a.k._inner[c];
                d.b = a.o._inner[c];
                if (isNaN_(d.e.x) || isNaN_(d.e.y)) {
                    continue;
                }
                b.add(d);
            }
            b.aa((e, f) => {
                if (e.e.y < f.e.y) {
                    return -1;
                }
                if (e.e.y > f.e.y) {
                    return 1;
                }
                if (e.b < f.b) {
                    return -1;
                }
                if (e.b > f.b) {
                    return 1;
                }
                return 0;
            });
            let e = this.re(b);
            if (!e) {
                return;
            }
            for (let f = 0; f < b.count - 1; f++) {
                let g = b._inner[f];
                let h = b._inner[f + 1];
                let i = new Rect(0, g.e.x, g.e.y, g.c, g.a);
                let j = new Rect(0, h.e.x, h.e.y, h.c, h.a);
                if (i.intersectsWith(j) || i.top > j.bottom) {
                    h.e = { $type: Point_$type, x: h.e.x, y: i.bottom + 1 };
                }
            }
            if (b._inner[b.count - 1].e.y + b._inner[b.count - 1].a > this.b0.bu.height) {
                b._inner[b.count - 1].e = { $type: Point_$type, x: b._inner[b.count - 1].e.x, y: b._inner[b.count - 1].e.y - ((b._inner[b.count - 1].e.y + b._inner[b.count - 1].a) - this.b0.bu.height) };
            }
            for (let k = b.count - 1; k >= 1; k--) {
                let l = b._inner[k];
                let m = b._inner[k - 1];
                let n = new Rect(0, l.e.x, l.e.y, l.c, l.a);
                let o = new Rect(0, m.e.x, m.e.y, m.c, m.a);
                if (n.intersectsWith(o) || n.top < o.bottom) {
                    m.e = { $type: Point_$type, x: m.e.x, y: n.top - (o.height + 1) };
                }
            }
            for (let p = 0; p < b.count; p++) {
                let q = b._inner[p].d;
                let r = b._inner[p];
                a.f._inner[q] = r.e.x;
                a.g._inner[q] = r.e.y;
            }
        }
        re(a) {
            if (a.count <= 1) {
                return false;
            }
            let b = this.b0.bu;
            for (let c = 0; c < a.count - 1; c++) {
                let d = a._inner[c];
                let e = a._inner[c + 1];
                let f = new Rect(0, d.e.x, d.e.y, d.c, d.a);
                let g = new Rect(0, e.e.x, e.e.y, e.c, e.a);
                if (f.intersectsWith(g)) {
                    return true;
                }
            }
            return false;
        }
        ro(a, b, c, d, e) {
            let f = this.qo(a);
            if (!f.c) {
                return;
            }
            let g = a.pq(f.d, c, d);
            let h = this.rc.b5;
            let i = this.rh(a);
            let j = this.q9;
            if (!c) {
                j = this.pt(g);
            }
            let k = a.hv(j);
            let l = h.item(e);
            let m = this.b0.bu;
            let n = null;
            if (a.dm && k != null) {
                n = a.au(k);
            }
            else {
                n = new DataContext();
                n.item = k;
                n.series = a;
            }
            if (!this.rc.b6(a, l, i, n)) {
                return;
            }
            let o = this.rc.ch(l, i, n);
            b.i.add1(l);
            b.j.add1(n);
            b.l.add1(i);
            let p = g.x;
            let q = g.x;
            q = Math.max(q, m.left);
            q = Math.min(q, m.right);
            b.n.add(q);
            let r = g.y;
            r = Math.max(r, m.top);
            r = Math.min(r, m.bottom);
            b.o.add(r);
            let s = DeviceUtils.g(10);
            let t = g.x + s;
            if (t + o.width > m.right) {
                t = (p - o.width) - s;
            }
            if (t < m.left) {
                t = m.left;
            }
            b.f.add(t);
            let u = (g.y - o.height) - s;
            if (u + o.height > m.bottom) {
                u = (m.bottom - o.height) - s;
            }
            if (u < m.top) {
                u = m.top;
            }
            b.g.add(u);
            b.m.add(o.width);
            b.k.add(o.height);
            let v = this.o3;
            if (v == null) {
                v = this.o7;
            }
            if (v == null) {
                v = a.o3;
            }
            b.h.add(v);
        }
        rh(a) {
            return a.toolTip;
        }
        lg() {
            this.q9 = { $type: Point_$type, x: NaN, y: NaN };
            this.ml(false);
            this.rc.ce(false);
        }
        lh() {
            this.q9 = { $type: Point_$type, x: NaN, y: NaN };
            this.ml(false);
            this.rc.ce(true);
        }
        qy(a, b) {
            super.qy(a, b);
            let c = a;
            let d = c.o.count;
            let e = b.bu.left;
            let f = b.bu.right;
            let g = b.bu.top;
            let h = b.bu.bottom;
            for (let i = 0; i < d; i++) {
                if (isNaN_(c.n._inner[i]) || isNaN_(c.o._inner[i]) || isNaN_(c.f._inner[i]) || isNaN_(c.g._inner[i]) || c.l._inner[i] == null || c.j._inner[i] == null) {
                    this.rc.cd(c.i._inner[i]);
                    continue;
                }
                let j = c.f._inner[i];
                let k = c.g._inner[i];
                let l = c.m._inner[i];
                let m = c.k._inner[i];
                let n = false;
                if (j < e && Math.abs(j - e) > 1) {
                    n = true;
                }
                if (k < g && Math.abs(k - g) > 1) {
                    n = true;
                }
                if ((j + l) > f && Math.abs((j + l) - f) > 1) {
                    n = true;
                }
                if ((k + m) > h && Math.abs((k + m) - h) > 1) {
                    n = true;
                }
                if (n) {
                    this.rc.cd(c.i._inner[i]);
                    continue;
                }
                let o = c.i._inner[i];
                let p = Math.min(c.f._inner[i], c.n._inner[i]);
                let q = Math.min(c.g._inner[i], c.o._inner[i]);
                let r = c.n._inner[i] - c.f._inner[i];
                let s = c.o._inner[i] - c.g._inner[i];
                if (o != null) {
                    this.rc.cf(o, p, q, r, s);
                }
            }
        }
        en() {
            return true;
        }
    }
    ItemToolTipLayer.$t = markType(ItemToolTipLayer, 'ItemToolTipLayer', AnnotationLayer.$);
    ItemToolTipLayer.$$p = markDep(DependencyProperty, PropertyMetadata, ItemToolTipLayer, 'raisePropertyChanged', ['SkipUnknownValues:rf:rp', [0, true], 'TargetSeriesName:rj:rq', [2, null], 'TargetSeries:rd:rr', [Series.$, null], 'UseInterpolation:rg:rs', [0, false]]);
    return ItemToolTipLayer;
})();
/**
 * @hidden
 */
export let ItemToolTipLayerView = /*@__PURE__*/ (() => {
    class ItemToolTipLayerView extends AnnotationLayerView {
        constructor(a) {
            super(a);
            this.b4 = null;
            this.b5 = null;
            this.b7 = null;
            this._htmlTest = null;
            this.b4 = a;
            this.b7 = new List$1(PointerTooltip.$, 0);
        }
        as() {
            super.as();
            this.b5 = ((() => {
                let $ret = new Pool$1(Base.$);
                $ret.create = runOn(this, this.b8);
                $ret.activate = runOn(this, this.ca);
                $ret.disactivate = runOn(this, this.cc);
                $ret.destroy = runOn(this, this.cb);
                return $ret;
            })());
            this.htmlTest = /^[^<]*(<[\w\W]+>)[^>]*$/;
        }
        aj() {
            super.aj();
            for (let a = 0; a < this.b5.a.count; a++) {
                this.b5.a._inner[a].br();
            }
            for (let b = 0; b < this.b5.b.count; b++) {
                this.b5.b._inner[b].br();
            }
            this.b5.clear();
        }
        b8() {
            let a = new PointerTooltip();
            a.bw(this.e.hb);
            a._visibility = 1;
            a.ap = this.e.cd.cu.k;
            this.b7.add(a);
            return a;
        }
        ca(a) {
            let b = a;
        }
        cc(a) {
            let b = a;
            b._visibility = 1;
        }
        cb(a) {
            let b = a;
            b.bz();
            b.content = null;
            this.b7.remove1(a);
        }
        get htmlTest() {
            return this._htmlTest;
        }
        set htmlTest(a) {
            this._htmlTest = a;
        }
        b6(a, b, c, d) {
            let e = b;
            if (a.h4 == "default") {
                a.h4 = a.b0.getDefaultTooltipTemplate();
            }
            let template_ = a.h4;
            let context_ = d;
            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', '');
            }
            e.content = a.toolTip;
            return true;
        }
        ch(a, b, c) {
            let d = a;
            let e = d.content;
            let f = new Size(1, Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY);
            d.cy = 1;
            let g = d.cx(new Size(1, Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY));
            d.cy = 0;
            return g;
        }
        cf(a, b, c, d, e) {
            let f = null;
            if (this.e.cd != null) {
                f = this.e.cd.cu.j.i();
            }
            let g = a;
            g.aq = f;
            let h = g.content;
            g.cs = { $type: Point_$type, x: d, y: e };
            g._visibility = 0;
            g.n = b;
            g.o = c;
        }
        cd(a) {
            if (a != null) {
                let b = a;
                b._visibility = 1;
            }
        }
        a6(a, b) {
            super.a6(a, b);
            if (b) {
                return;
            }
            let c = 0;
            let d = 0;
            let e = this.e.cd;
            if (e != null) {
                let f = e.cu.j.b7();
                c += f.x;
                d += f.y;
            }
            for (let g = 0; g < this.b7.count; g++) {
                this.b7._inner[g].by(this.b7._inner[g].n + c, this.b7._inner[g].o + d);
            }
        }
        al(a) {
            super.al(a);
            for (let tooltip_ of fromEnum(this.b5.a)) {
                if (tooltip_._visibility != 0) {
                    continue;
                }
                let b = tooltip_;
                let c = b.a5();
                let d = b.n;
                let e = b.o;
                let f = new List$1(String_$type, 0);
                let g = typeCast(DomWrapper_$type, b.content);
                if (g != null) {
                    let h = g.getText();
                    f.add(h);
                }
                this.e.a8.addPointerTooltipData(a, c, d, e, f);
            }
        }
        ce(a) {
            this.aq();
            for (let b = 0; b < this.b7.count; b++) {
                this.cd(this.b7._inner[b]);
                if (a) {
                    this.b7._inner[b].bs();
                }
            }
        }
        cg() {
            if (this.e.cd == null) {
                this.ce(true);
            }
        }
    }
    ItemToolTipLayerView.$t = markType(ItemToolTipLayerView, 'ItemToolTipLayerView', AnnotationLayerView.$);
    return ItemToolTipLayerView;
})();
