/*
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 { Series } from "./Series";
import { DependencyProperty } from "igniteui-react-core/ES2015/DependencyProperty";
import { ItemTooltipLayerFrame } from "./ItemTooltipLayerFrame";
import { Type, Base, fromEnum, Point_$type, markType, DomWrapper_$type, runOn, String_$type, typeCast } from "igniteui-react-core/ES2015/type";
import { Pool$1 } from "igniteui-react-core/ES2015/Pool$1";
import { List$1 } from "igniteui-react-core/ES2015/List$1";
import { ItemTooltipCollisionInfo } from "./ItemTooltipCollisionInfo";
import { Rect } from "igniteui-react-core/ES2015/Rect";
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 ItemToolTipLayer extends AnnotationLayer {
    constructor() {
        super();
        let a = new ItemTooltipLayerFrame();
        let b = new ItemTooltipLayerFrame();
        let c = new ItemTooltipLayerFrame();
        let d = this.fc / 1000;
        this.lq = a;
        this.lp = b;
        this.lr = c;
        this.ab = ItemToolTipLayer.$;
    }
    bf() {
        return new ItemToolTipLayerView(this);
    }
    ii(a) {
        super.ii(a);
        this.mq = 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 ItemToolTipLayer.$$p[1]:
                this.i1(true);
                break;
            case ItemToolTipLayer.$$p[2]:
                this.i1(true);
                break;
            case ItemToolTipLayer.$$p[0]:
                this.i1(true);
                break;
        }
    }
    l7(a, b) {
        if (this.mr == null || this.mr == a) {
            if (!this.aq.c) {
                this.i1(b);
            }
        }
    }
    lu(a) {
        if (!super.lu(a)) {
            return false;
        }
        let b = this.l2(a);
        if (!b.a) {
            return false;
        }
        return true;
    }
    l9(a, b) {
        super.l9(a, b);
        let c = a;
        let d = this.mq.b0;
        let e = this.mu;
        let f = this.mt;
        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.mn.x) && isNaN_(this.mn.y)) {
            d.count = 0;
            return;
        }
        let g = 0;
        if (this.lu(this.mr)) {
            this.m0(this.mr, c, e, f, 0);
            g++;
        }
        else {
            for (let h of fromEnum(this.bs.series)) {
                if (this.lu(h)) {
                    this.m0(h, c, e, f, g);
                    g++;
                }
            }
            this.mz(c);
        }
        d.count = g;
    }
    mz(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.ms(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.bi.bp.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.bi.bp.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;
        }
    }
    ms(a) {
        if (a.count <= 1) {
            return false;
        }
        let b = this.bi.bp;
        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;
    }
    m0(a, b, c, d, e) {
        let f = this.l2(a);
        if (!f.a) {
            return;
        }
        let g = a.k9(f.b, c, d);
        let h = this.mq.b0;
        let i = this.mv(a);
        let j = this.mn;
        if (!c) {
            j = this.lc(g);
        }
        let k = a.fj(j);
        let l = h.item(e);
        let m = this.bi.bp;
        let n = null;
        if (a.cn && k != null) {
            n = a.as(k);
        }
        else {
            n = new DataContext();
            n.item = k;
            n.series = a;
        }
        if (!this.mq.b1(a, l, i, n)) {
            return;
        }
        let o = this.mq.ca(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.kr;
        if (v == null) {
            v = this.kt;
        }
        if (v == null) {
            v = a.kr;
        }
        b.h.add(v);
    }
    mv(a) {
        return a.toolTip;
    }
    mc(a, b) {
        super.mc(a, b);
        let c = a;
        let d = c.o.count;
        let e = b.bp.left;
        let f = b.bp.right;
        let g = b.bp.top;
        let h = b.bp.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.mq.b8(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.mq.b8(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];
            this.mq.b9(o, p, q, r, s);
        }
    }
    c5() {
        return true;
    }
}
ItemToolTipLayer.$t = markType(ItemToolTipLayer, 'ItemToolTipLayer', AnnotationLayer.$);
Type.dep(DependencyProperty, PropertyMetadata, ItemToolTipLayer, 'raisePropertyChanged', ['SkipUnknownValues:mt:m1', [0, true], 'TargetSeries:mr:m2', [Series.$, null], 'UseInterpolation:mu:m3', [0, false]]);
export class ItemToolTipLayerView extends AnnotationLayerView {
    constructor(a) {
        super(a);
        this.bz = null;
        this.b0 = null;
        this.b2 = null;
        this._htmlTest = null;
        this.bz = a;
        this.b2 = new List$1(PointerTooltip.$, 0);
    }
    ap() {
        super.ap();
        this.b0 = ((() => {
            let $ret = new Pool$1(Base.$);
            $ret.create = runOn(this, this.b3);
            $ret.activate = runOn(this, this.b5);
            $ret.disactivate = runOn(this, this.b7);
            $ret.destroy = runOn(this, this.b6);
            return $ret;
        })());
        this.htmlTest = /^[^<]*(<[\w\W]+>)[^>]*$/;
    }
    ag() {
        super.ag();
        for (let a = 0; a < this.b0.a.count; a++) {
            this.b0.a._inner[a].bo();
        }
        for (let b = 0; b < this.b0.b.count; b++) {
            this.b0.b._inner[b].bo();
        }
        this.b0.clear();
    }
    b3() {
        let a = new PointerTooltip();
        a.bs(this.e.ey);
        a._visibility = 1;
        a.ao = this.e.bs.bk.j;
        this.b2.add(a);
        return a;
    }
    b5(a) {
        let b = a;
    }
    b7(a) {
        let b = a;
        b._visibility = 1;
    }
    b6(a) {
        let b = a;
        b.bv();
        b.content = null;
        this.b2.remove1(a);
    }
    get htmlTest() {
        return this._htmlTest;
    }
    set htmlTest(a) {
        this._htmlTest = a;
    }
    b1(a, b, c, d) {
        let e = b;
        if (a.fq == "default") {
            a.fq = a.bi.getDefaultTooltipTemplate();
        }
        let template_ = a.fq;
        let context_ = d;
        let f = false;
        if (!f) {
            return false;
        }
        if (a.ew == null) {
            return false;
        }
        let args_ = a.ew(d);
        let g = this.e.bs.bk.i.af.expandTemplate(template_, args_);
        e.content = g;
        return true;
    }
    ca(a, b, c) {
        let d = a;
        let e = d.content;
        let f = new Size(1, Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY);
        d.ct = 1;
        let g = d.cs(new Size(1, Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY));
        d.ct = 0;
        return g;
    }
    b9(a, b, c, d, e) {
        let f = null;
        if (this.e.bs != null) {
            f = this.e.bs.bk.i.i();
        }
        let g = a;
        g.ap = f;
        let h = g.content;
        g.cn = { $type: Point_$type, x: d, y: e };
        g._visibility = 0;
        g.n = b;
        g.o = c;
    }
    b8(a) {
        if (a != null) {
            let b = a;
            b._visibility = 1;
        }
    }
    a3(a, b) {
        super.a3(a, b);
        if (b) {
            return;
        }
        let c = 0;
        let d = 0;
        let e = this.e.bs;
        if (e != null) {
            let f = e.bk.i.b5();
            c += f.x;
            d += f.y;
        }
        for (let g = 0; g < this.b2.count; g++) {
            this.b2._inner[g].bu(this.b2._inner[g].n + c, this.b2._inner[g].o + d);
        }
    }
    ai(a) {
        super.ai(a);
        for (let tooltip_ of fromEnum(this.b0.a)) {
            if (tooltip_._visibility != 0) {
                continue;
            }
            let b = tooltip_;
            let c = b.a3();
            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.a2.addPointerTooltipData(a, c, d, e, f);
        }
    }
}
ItemToolTipLayerView.$t = markType(ItemToolTipLayerView, 'ItemToolTipLayerView', AnnotationLayerView.$);
/*<BeginStatics Name="Infragistics.Controls.Charts.ItemToolTipLayer" />*/
/*<EndStatics Name="Infragistics.Controls.Charts.ItemToolTipLayer" />*/
