/*
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 { CalloutAnnotationCollection } from "./CalloutAnnotationCollection";
import { Series } from "./Series";
import { DependencyProperty } from "igniteui-react-core/ES2015/DependencyProperty";
import { Brush } from "igniteui-react-core/ES2015/Brush";
import { CalloutCollisionMode_$type } from "./CalloutCollisionMode";
import { CalloutLayerFrame } from "./CalloutLayerFrame";
import { HashSet$1 } from "igniteui-react-core/ES2015/HashSet$1";
import { CalloutAnnotationWrapper } from "./CalloutAnnotationWrapper";
import { List$1 } from "igniteui-react-core/ES2015/List$1";
import { LinkedList$1 } from "igniteui-react-core/ES2015/LinkedList$1";
import { Dictionary$2 } from "igniteui-react-core/ES2015/Dictionary$2";
import { LinkedListNode$1 } from "igniteui-react-core/ES2015/LinkedListNode$1";
import { LabelCollisionManager } from "igniteui-react-core/ES2015/LabelCollisionManager";
import { Rect } from "igniteui-react-core/ES2015/Rect";
import { Type, Base, runOn, delegateCombine, fromEnum, enumGetBox, delegateRemove, Point_$type, markType, PointUtil } from "igniteui-react-core/ES2015/type";
import { CalloutAnnotation } from "./CalloutAnnotation";
import { QuadTreeBoundingBox } from "igniteui-react-core/ES2015/QuadTreeBoundingBox";
import { LabelCollisionInfo } from "igniteui-react-core/ES2015/LabelCollisionInfo";
import { Size } from "igniteui-react-core/ES2015/Size";
import { Pool$1 } from "igniteui-react-core/ES2015/Pool$1";
import { PropertyMetadata } from "igniteui-react-core/ES2015/PropertyMetadata";
import { AnnotationLayerView } from "./AnnotationLayerView";
import { BrushUtil } from "igniteui-react-core/ES2015/BrushUtil";
import { DOMExecutionContext } from "igniteui-react-core/ES2015/DOMExecutionContext";
import { isNaN_ } from "igniteui-react-core/ES2015/number";
import { CalloutViewHelper } from "./CalloutViewHelper";
import { TranslateTransform } from "igniteui-react-core/ES2015/TranslateTransform";
export class CalloutLayer extends AnnotationLayer {
    constructor() {
        super();
        this.m4 = new HashSet$1(CalloutAnnotationWrapper.$, 0);
        this.m6 = new List$1(CalloutAnnotationWrapper.$, 0);
        this.mq = new CalloutAnnotationCollection();
        this.m7 = new List$1(CalloutAnnotationWrapper.$, 0);
        this.m5 = new LinkedList$1(CalloutAnnotationWrapper.$);
        this.m3 = new Dictionary$2(CalloutAnnotationWrapper.$, LinkedListNode$1.$.specialize(CalloutAnnotationWrapper.$), 0);
        this.mv = null;
        this.oc = new Rect(0, 0, 0, 1, 1);
        this.my = false;
        this.m0 = false;
        this.m1 = false;
        this.m9 = 0;
        this.mz = false;
        let a = new CalloutLayerFrame();
        let b = new CalloutLayerFrame();
        let c = new CalloutLayerFrame();
        let d = this.fc / 1000;
        this.lq = a;
        this.lp = b;
        this.lr = c;
        let e = this.mq;
        e.collectionChanged = delegateCombine(e.collectionChanged, runOn(this, this.ns));
        let f = this.mq;
        f.collectionResetting = delegateCombine(f.collectionResetting, runOn(this, this.nt));
        this.ab = CalloutLayer.$;
    }
    nt(a, b) {
        this.m7.clear();
        this.i1(false);
    }
    ns(a, b) {
        switch (b.action) {
            case 0:
                if (b.newItems != null) {
                    for (let c = 0; c < b.newItems.count; c++) {
                        let d = b.newItems.item(c);
                        let e = new CalloutAnnotationWrapper();
                        e.b = d;
                        this.m7.insert(c + b.newStartingIndex, e);
                    }
                }
                break;
            case 1:
                if (b.oldItems != null) {
                    for (let f = 0; f < b.oldItems.count; f++) {
                        let g = this.m7._inner[b.oldStartingIndex].b;
                        this.m7.removeAt(b.oldStartingIndex);
                    }
                }
                break;
            case 2:
                if (b.oldItems != null) {
                    for (let h = 0; h < b.oldItems.count; h++) {
                        this.m7.removeAt(b.oldStartingIndex);
                    }
                }
                if (b.newItems != null) {
                    for (let i = 0; i < b.newItems.count; i++) {
                        let j = b.newItems.item(i);
                        let k = new CalloutAnnotationWrapper();
                        k.b = j;
                        this.m7.insert(i + b.newStartingIndex, k);
                    }
                }
                break;
            case 4:
                this.m7.clear();
                for (let l of fromEnum(this.mq)) {
                    let m = l;
                    let n = new CalloutAnnotationWrapper();
                    n.b = m;
                    this.m7.add(n);
                }
                break;
        }
        this.i1(false);
    }
    nv(a, b) {
        if (b.propertyName == CalloutAnnotation.$$p[8] || b.propertyName == CalloutAnnotation.$$p[9]) {
            let c = a.k;
            if (!this.m4.contains(c)) {
                this.m6.add(c);
                this.i1(false);
            }
        }
    }
    bf() {
        return new CalloutLayerView(this);
    }
    ii(a) {
        super.ii(a);
        this.mt = a;
    }
    get callouts() {
        return this.mq;
    }
    l7(a, b) {
        if (this.mu == null || this.mu == a) {
            if (!this.aq.c) {
                this.i1(b);
            }
        }
    }
    l9(a, b) {
        super.l9(a, b);
        let c = a;
        c.k.clear();
        c.f.clear();
        c.g.clear();
        c.h.clear();
        c.i.clear();
        c.j.clear();
        c.n.clear();
        c.o.clear();
        c.l.clear();
        c.m.clear();
        let d = this.m8();
        let e = new List$1(CalloutAnnotationWrapper.$, 0);
        if (this.m2) {
            if (this.lu(this.mu)) {
                this.nx(this.mu, c, e);
            }
            else {
                for (let f of fromEnum(this.bs.series)) {
                    if (this.lu(f)) {
                        this.nx(f, c, e);
                    }
                }
            }
        }
        this.nw(c, d, e);
    }
    jh(a, b) {
        super.jh(a, b);
        let c = b.width - this.oc.width;
        let d = b.height - this.oc.height;
        let e = 0;
        if (Math.abs(c) > Math.abs(d)) {
            e = c;
            this.m9 = c * this.lm.width;
        }
        else {
            e = d;
            this.m9 = d * this.lm.height;
        }
        if (e > 0 && this.m9 > 5) {
            this.m1 = true;
        }
        if (e < 0 && this.m9 > 5) {
            this.m0 = true;
        }
    }
    jg(a, b) {
        super.jg(a, b);
        this.mz = true;
    }
    nw(a, b, c) {
        if (this.bs != null) {
            this.oc = this.bs.mk;
        }
        if (this.lm.isEmpty) {
            return;
        }
        if (this.mv == null) {
            this.mv = new LabelCollisionManager(this.lm.width, this.lm.height, this.mt.b2(), this.nb, this.mw(), this.ng(), this.nf(), false);
            this.mv.g = runOn(this, this.nu);
        }
        if (this.m0) {
            this.m0 = false;
            if (this.mv.y > 2) {
                this.my = true;
            }
        }
        if (this.m1) {
            this.m1 = false;
            if (this.m9 > 5) {
                this.my = true;
            }
        }
        if (this.mz) {
            this.mz = false;
            this.mv.ak(this.lm.width, this.lm.height);
        }
        let d = new HashSet$1(CalloutAnnotationWrapper.$, 0);
        let e = new HashSet$1(CalloutAnnotationWrapper.$, 0);
        let f = new List$1(CalloutAnnotationWrapper.$, 0);
        let g = new List$1(CalloutAnnotationWrapper.$, 0);
        let h = new HashSet$1(CalloutAnnotationWrapper.$, 0);
        for (let i = 0; i < b.count; i++) {
            h.add_1(b._inner[i]);
        }
        for (let j = 0; j < c.count; j++) {
            h.add_1(c._inner[j]);
        }
        let k = this.m5.c;
        while (k != null) {
            let l = k.a;
            if (!h.contains(k.c)) {
                g.add(k.c);
                e.add_1(k.c);
                let m = k.c.b;
                m.propertyChanged = delegateRemove(m.propertyChanged, runOn(this, this.nv));
                this.m5.h(k);
                this.m3.removeItem(k.c);
                this.mv.aj(k.c.c);
            }
            k = l;
        }
        for (let n = 0; n < b.count; n++) {
            if (!this.m3.containsKey(b._inner[n])) {
                this.m5.f(b._inner[n]);
                let o = b._inner[n].b;
                o.propertyChanged = delegateCombine(o.propertyChanged, runOn(this, this.nv));
                this.m3.addItem(b._inner[n], this.m5.d);
                f.add(b._inner[n]);
                d.add_1(b._inner[n]);
                this.my = true;
            }
        }
        for (let p = 0; p < c.count; p++) {
            if (!this.m3.containsKey(c._inner[p])) {
                this.m5.f(c._inner[p]);
                this.m3.addItem(c._inner[p], this.m5.d);
                let q = c._inner[p].b;
                q.propertyChanged = delegateCombine(q.propertyChanged, runOn(this, this.nv));
                f.add(c._inner[p]);
                d.add_1(c._inner[p]);
                this.my = true;
            }
        }
        if (this.m6.count > 0) {
            this.my = true;
            this.m6.clear();
            this.m4.clear();
        }
        k = this.m5.c;
        while (k != null) {
            let r = this.mx(k.c);
            if (r == null) {
                return;
            }
            if (!Base.equalsStatic(r, k.c.c.c)) {
                let s = false;
                if (k.c.c.c == null) {
                    s = true;
                }
                let t = k.c.c;
                t.o = r.b;
                t.p = r.c;
                t.n = r.e;
                t.m = r.d;
                if (s) {
                    this.mv.aa(t);
                }
                else {
                    this.mv.ag(t);
                }
            }
            k = k.a;
        }
        if (this.my) {
            this.my = false;
            this.mv.al();
        }
        let u = this.m5.c;
        while (u != null) {
            let v = u.c;
            a.f.add(v.b.background);
            a.g.add(v.b.outline);
            a.h.add(v.b.backgroundPadding);
            a.i.add(v.b.strokeThickness);
            a.j.add(v.b.textColor);
            a.k.add1(v.b.text);
            a.n.add(v.c.o - (v.c.g - v.c.l));
            a.o.add(v.c.p - (v.c.h - v.c.k));
            a.l.add(v.c.g - v.c.l);
            a.m.add(v.c.h - v.c.k);
            u = u.a;
        }
    }
    nf() {
        return 10000;
    }
    ng() {
        return 1200;
    }
    mw() {
        switch (this.ms) {
            case 0: return 2;
            case 2: return 1;
            case 3: return 2;
            case 1: return 0;
        }
        return 2;
    }
    nu(a) {
        this.i1(true);
    }
    mx(a) {
        let b = this.mu;
        if (a.b.series != null) {
            b = a.b.series;
        }
        if (b == null) {
            for (let c = 0; c < this.bs.series.count; c++) {
                let d = this.bs.series._inner[c];
                if (!d.ci) {
                    b = d;
                    break;
                }
            }
        }
        if (b == null) {
            return null;
        }
        let e = b.ag();
        let f = null;
        let g = null;
        for (let h = 0; h < e.length; h++) {
            if (e[h].ce) {
                g = e[h];
            }
            else {
                f = e[h];
            }
        }
        if (f == null || g == null) {
            return null;
        }
        if (!f.cd() || !g.cd()) {
            return null;
        }
        let i = f.d8(this.nd(a.b.xValue));
        let j = g.d8(this.nd(a.b.yValue));
        let k = new QuadTreeBoundingBox();
        k.b = i;
        k.c = j;
        k.e = 3;
        k.d = 3;
        k.g = a.b;
        return k;
    }
    nd(a) {
        if (a == null) {
            return 0;
        }
        return a;
    }
    m8() {
        let a = new List$1(CalloutAnnotationWrapper.$, 0);
        for (let b = 0; b < this.m7.count; b++) {
            if (this.m7._inner[b].c == null) {
                let c = new LabelCollisionInfo();
                this.m7._inner[b].c = c;
                let d = this.mt.cb(this.nh(this.m7._inner[b]), this.m7._inner[b].b.backgroundPadding);
                c.k = d.height / 2;
                c.l = d.width / 2;
            }
            let e = this.mx(this.m7._inner[b]);
            if (e == null || e.b < 0 || e.b > this.lm.width || e.c < 0 || e.c > this.lm.height) {
                continue;
            }
            a.add(this.m7._inner[b]);
        }
        return a;
    }
    nh(a) {
        return a.b.text;
    }
    lu(a) {
        if (!super.lu(a)) {
            return false;
        }
        return true;
    }
    ib() {
        super.ib();
        if (!this.aq.c && !this.ca()) {
            return;
        }
        if (this.ca()) {
            this.ny(this.lr, false);
        }
        else {
            this.ny(this.lp, false);
        }
    }
    nx(a, b, c) {
    }
    ny(a, b) {
        if (a.k.count == 0) {
            this.mt.b3.count = 0;
            return;
        }
        let c = 0;
        for (c = 0; c < a.k.count; c++) {
            let d = this.mt.b3.item(c);
            if (d.g != a.k._inner[c]) {
                d.g = a.k._inner[c];
                let e = d.q();
            }
            d.j = a.f._inner[c];
            d.l = a.j._inner[c];
            d.k = a.g._inner[c];
            d.c = a.h._inner[c];
            d.d = a.i._inner[c];
            this.mt.b9(d, Math.min(a.l._inner[c], a.l._inner[c] + a.n._inner[c]), Math.min(a.m._inner[c], a.m._inner[c] + a.o._inner[c]));
            d.n = { $type: Point_$type, x: a.n._inner[c], y: a.o._inner[c] };
        }
        this.mt.b3.count = c;
    }
    mc(a, b) {
        super.mc(a, b);
        if (this.ca()) {
            this.ny(this.lr, true);
        }
        else {
            this.ny(this.lp, false);
        }
    }
    c5() {
        return true;
    }
    io(a, b, c, d) {
        super.io(a, b, c, d);
        switch (b) {
            case CalloutLayer.$$p[9]:
                this.i1(true);
                break;
            case CalloutLayer.$$p[8]:
            case CalloutLayer.$$p[0]:
            case CalloutLayer.$$p[3]:
            case CalloutLayer.$$p[4]:
            case CalloutLayer.$$p[6]:
            case CalloutLayer.$$p[7]:
                this.i1(true);
                break;
        }
    }
}
CalloutLayer.$t = markType(CalloutLayer, 'CalloutLayer', AnnotationLayer.$);
Type.dep(DependencyProperty, PropertyMetadata, CalloutLayer, 'raisePropertyChanged', ['CalloutBackground:n9:nz', [Brush.$, null], 'CalloutCollisionMode:ms:n0', [CalloutCollisionMode_$type, enumGetBox(CalloutCollisionMode_$type, 3)], 'CalloutInterpolatedValuePrecision:ne:n1', [1, -1], 'CalloutOutline:oa:n2', [Brush.$, null], 'CalloutPadding:na:n3', [1, NaN], 'CalloutPositionPadding:nb:n4', [1, 3], 'CalloutStrokeThickness:nc:n5', [1, NaN], 'CalloutTextColor:ob:n6', [Brush.$, null], 'IsAutoCalloutBehaviorEnabled:m2:n7', [0, false], 'TargetSeries:mu:n8', [Series.$, null]]);
export class CalloutLayerView extends AnnotationLayerView {
    constructor(a) {
        super(a);
        this.bz = null;
        this.b4 = null;
        this.b3 = null;
        this.b5 = NaN;
        this.b0 = null;
        this.bz = a;
        this.b4 = new List$1(CalloutView.$, 0);
    }
    ap() {
        super.ap();
        this.b3 = ((() => {
            let $ret = new Pool$1(CalloutView.$);
            $ret.create = runOn(this, this.b1);
            $ret.activate = runOn(this, this.b6);
            $ret.disactivate = runOn(this, this.b8);
            $ret.destroy = runOn(this, this.b7);
            return $ret;
        })());
    }
    b1() {
        let a = new CalloutView(this);
        this.b4.add(a);
        return a;
    }
    b6(a) {
        a.s = 0;
    }
    b8(a) {
        a.s = 1;
    }
    b7(a) {
        this.b4.remove(a);
    }
    ca(a) {
        if (a == null) {
            return a;
        }
        return BrushUtil.h(a, 0.1);
    }
    a3(a, b) {
        super.a3(a, b);
        if (a.d && !b) {
            for (let c = 0; c < this.b4.count; c++) {
                let d = this.b4._inner[c];
                if (d.s == 0) {
                    d.h(a);
                }
            }
        }
    }
    ai(a) {
        super.ai(a);
    }
    cc(a) {
        if (isNaN_(this.b5)) {
            this.b5 = this.e.bs.bk.a7;
        }
        return new Size(1, this.h.f(a.ak), this.b5);
    }
    cb(a, b) {
        if (this.b0 == null) {
            this.b0 = new CalloutView(this);
        }
        this.b0.g = a;
        this.b0.c = b;
        let c = this.b0.q();
        this.b0.g = null;
        return c;
    }
    b2() {
        return new DOMExecutionContext(this.e.bs.bk.i.af);
    }
    b9(a, b, c) {
        a.e = b;
        a.f = c;
    }
}
CalloutLayerView.$t = markType(CalloutLayerView, 'CalloutLayerView', AnnotationLayerView.$);
export class CalloutView extends Base {
    constructor(a) {
        super();
        this.a = null;
        this.s = 0;
        this.i = null;
        this.p = null;
        this.m = { $type: Point_$type, x: 0, y: 0 };
        this.o = null;
        this.b = 0;
        this.r = null;
        this.e = 0;
        this.f = 0;
        this.a = a;
        this.s = 0;
    }
    h(a) {
        a.z();
        a.af(this.e, this.f);
        let b = new TranslateTransform();
        b.j = this.r.c;
        b.k = this.r.e;
        this.p.j = b;
        a.r(this.o);
        a.z();
        a.j(this.p.j);
        a.v(this.p);
        this.i.o = this.b;
        this.i.n = this.b;
        a.w(this.i);
        a.y();
        a.y();
    }
    get n() {
        return this.m;
    }
    set n(a) {
        if (!PointUtil.equals(this.m, a)) {
            let b = this.q();
            let c = b.height;
            let d = b.width;
            this.m = a;
            let e = CalloutViewHelper.b(this.n, d, c, this.o);
            this.r = e;
        }
    }
    get l() {
        return this.i.am;
    }
    set l(a) {
        this.i.am = a;
    }
    get k() {
        return this.p._stroke;
    }
    set k(a) {
        this.p._stroke = a;
    }
    get j() {
        return this.p._fill;
    }
    set j(a) {
        this.p._fill = a;
    }
    get d() {
        return this.p.ac;
    }
    set d(a) {
        this.p.ac = a;
    }
    get c() {
        return this.b;
    }
    set c(a) {
        this.b = a;
    }
    get g() {
        return this.i.ak;
    }
    set g(a) {
        if (a == null) {
            this.i.ak = "";
        }
        else {
            this.i.ak = a.toString();
        }
    }
    q() {
        let a = this.a.cc(this.i);
        return new Size(1, a.width + this.b * 2, a.height + this.b * 2);
    }
}
CalloutView.$t = markType(CalloutView, 'CalloutView');
/*<BeginStatics Name="Infragistics.Controls.Charts.CalloutLayer" />*/
/*<EndStatics Name="Infragistics.Controls.Charts.CalloutLayer" />*/
