/*
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 { Base, runOn, Boolean_$type, fromEnum, markType } from "igniteui-react-core";
import { IHighlightingManager_$type } from "./IHighlightingManager";
import { List$1 } from "igniteui-react-core";
import { Series } from "./Series";
import { HighlightingInfoComparer } from "./HighlightingInfoComparer";
import { Dictionary$2 } from "igniteui-react-core";
import { HighlightingInfo } from "./HighlightingInfo";
import { HighlightingData } from "./HighlightingData";
import { HighlightingState } from "./HighlightingState";
/**
 * @hidden
 */
export let HighlightingManager = /*@__PURE__*/ (() => {
    class HighlightingManager extends Base {
        constructor() {
            super();
            this._highlightingBehavior = 0;
            this._executionContext = null;
            this._ensureAnimator = null;
            this.e = new Dictionary$2(HighlightingInfo.$, HighlightingInfo.$, 2, new HighlightingInfoComparer());
            this.i = new List$1(HighlightingInfo.$, 0);
            this.h = new Dictionary$2(HighlightingInfo.$, HighlightingInfo.$, 2, new HighlightingInfoComparer());
            this.j = new List$1(HighlightingInfo.$, 0);
            this.b = false;
            this.c = false;
            this.o = 0;
            this.n = 0;
            this.l = new List$1(HighlightingInfo.$, 0);
            this.m = 0;
            this._dirtySeries = null;
            this._allDirty = false;
            this.f = new Dictionary$2(Series.$, Boolean_$type, 0);
            this.g = new Dictionary$2(Series.$, HighlightingData.$, 0);
            this.k = new List$1(Series.$, 0);
            this._inProgress = false;
            this._maxHighlightingProgress = 0;
            this._sumHighlightingProgress = 0;
            this._maxMarkerHighlightingProgress = 0;
            this._sumMarkerHighlightingProgress = 0;
            this._totalHighlightingProgress = 0;
            this.highlightingBehavior = 1;
            this.dirtySeries = new List$1(Series.$, 0);
            this.n = 200;
        }
        get highlightingBehavior() {
            return this._highlightingBehavior;
        }
        set highlightingBehavior(a) {
            this._highlightingBehavior = a;
        }
        get d() {
            return this.highlightingBehavior == 1;
        }
        get executionContext() {
            return this._executionContext;
        }
        set executionContext(a) {
            this._executionContext = a;
        }
        get ensureAnimator() {
            return this._ensureAnimator;
        }
        set ensureAnimator(a) {
            this._ensureAnimator = a;
        }
        startHighlight(a) {
            if (a == null) {
                return;
            }
            this.a(a);
            this.inProgress = true;
            if (!this.e.containsKey(a)) {
                a.a = HighlightingState.inward;
                this.e.item(a, a);
                this.i.add(a);
            }
            else {
                a = this.e.item(a);
            }
            if (a.a == HighlightingState.outward) {
                a.a = HighlightingState.inward;
            }
        }
        a(a) {
            let b = null;
            if (this.h.containsKey(a)) {
                a = this.h.item(a);
                this.h.removeItem(a);
                this.j.remove(a);
                b = a;
            }
            let c = this.l;
            for (let d = 0; d < this.j.count; d++) {
                let e = this.j._inner[d];
                if (e.d && a.d && a.b == e.b) {
                    if (b == null) {
                        this.h.removeItem(e);
                        this.j.remove(e);
                        let f = this.e.item(e);
                        this.e.removeItem(f);
                        this.i.remove(f);
                        f.h = a.h;
                        f.g = a.g;
                        f.c = a.c;
                        this.e.item(f, f);
                        this.i.add(f);
                        b = f;
                    }
                    else {
                        c.add(e);
                    }
                }
                if (this.d && e.b != a.b) {
                    c.add(e);
                }
                if (e.b == a.b && e.e == a.e && !e.d && (e.h != a.h || e.g != a.g)) {
                    c.add(e);
                }
            }
            for (let g = 0; g < c.count; g++) {
                this.j.remove(c._inner[g]);
                this.h.removeItem(c._inner[g]);
                this.q(c._inner[g]);
            }
            c.clear();
            if (b != null) {
                return b;
            }
            return a;
        }
        endHighlight(a) {
            if (a == null) {
                return;
            }
            if (!this.e.containsKey(a)) {
                return;
            }
            else {
                a = this.e.item(a);
            }
            this.r(a);
        }
        r(a) {
            if (this.h.containsKey(a)) {
                let b = this.h.item(a);
            }
            else {
                this.h.addItem(a, a);
                this.j.add(a);
                a.i = this.executionContext.getCurrentRelativeTime();
            }
            this.b = true;
            if (!this.c) {
                this.c = true;
                this.executionContext.executeDelayed(runOn(this, this.p), this.n);
            }
        }
        p() {
            this.c = false;
            if (!this.b) {
                return;
            }
            let a = this.l;
            let b = 0;
            let c = this.executionContext.getCurrentRelativeTime();
            for (let d = 0; d < this.j.count; d++) {
                let e = this.j._inner[d];
                if (c - e.i >= this.n) {
                    a.add(e);
                }
                else {
                    b = Math.max(b, this.n - (c - e.i));
                }
            }
            for (let f = 0; f < a.count; f++) {
                this.j.remove(a._inner[f]);
                this.h.removeItem(a._inner[f]);
                this.q(a._inner[f]);
            }
            if (a.count > 0) {
                if (this.ensureAnimator != null) {
                    this.ensureAnimator();
                }
            }
            a.clear();
            if (b > 0) {
                this.b = true;
                this.c = true;
                this.executionContext.executeDelayed(runOn(this, this.p), b);
            }
        }
        q(a) {
            if (a.a == HighlightingState.inward) {
                a.a = HighlightingState.outward;
            }
            if (a.a == 2) {
                a.a = HighlightingState.outward;
                a.f = 1;
            }
        }
        clear() {
            this.j.clear();
            this.h.clear();
            this.b = false;
            this.i.clear();
            this.e.clear();
            this.maxHighlightingProgress = 0;
            this.sumHighlightingProgress = 0;
            this.maxMarkerHighlightingProgress = 0;
            this.sumMarkerHighlightingProgress = 0;
            this.totalHighlightingProgress = 0;
        }
        getHighlightingInfo(a, b, c, d, e) {
            for (let f = 0; f < this.i.count; f++) {
                let g = this.i._inner[f];
                let h = g.h;
                let i = g.g;
                if (((c >= h && d <= i) || g.d) && g.e == e && a == g.b && (g.a == HighlightingState.inward || g.a == 2)) {
                    return g;
                }
            }
            for (let j = 0; j < this.i.count; j++) {
                let k = this.i._inner[j];
                let l = k.h;
                let m = k.g;
                if (((c >= l && d <= m) || k.d) && k.e == e && a == k.b && (k.a == HighlightingState.outward)) {
                    return k;
                }
            }
            return null;
        }
        get dirtySeries() {
            return this._dirtySeries;
        }
        set dirtySeries(a) {
            this._dirtySeries = a;
        }
        get allDirty() {
            return this._allDirty;
        }
        set allDirty(a) {
            this._allDirty = a;
        }
        tick(a) {
            this.f.clear();
            this.dirtySeries.clear();
            this.g.clear();
            this.allDirty = false;
            let b = 0;
            if (this.m > a) {
                b = (1 - this.m) + a;
            }
            else {
                b = a - this.m;
            }
            this.m = a;
            let c = false;
            let d = new List$1(HighlightingInfo.$, 0);
            this.maxHighlightingProgress = 0;
            this.sumHighlightingProgress = 0;
            this.maxMarkerHighlightingProgress = 0;
            this.sumMarkerHighlightingProgress = 0;
            this.totalHighlightingProgress = 0;
            let e = false;
            let f = false;
            let g = false;
            let h = 0;
            let i = 0;
            let j = 0;
            let k = 0;
            let l = 0;
            let m = 0;
            for (let n = 0; n < this.i.count; n++) {
                let o = this.i._inner[n];
                if (o.a == HighlightingState.inward) {
                    if (o.e) {
                        e = true;
                        j = j + Math.min(1 - o.f, b);
                    }
                    else {
                        if (!this.d && !o.d) {
                            g = true;
                            l = l + Math.min(1 - o.f, b);
                        }
                        else {
                            f = true;
                            h = h + Math.min(1 - o.f, b);
                        }
                    }
                }
                if (o.a == HighlightingState.outward) {
                    if (o.e) {
                        k = k + Math.min(o.f, b);
                    }
                    else {
                        if (!this.d && !o.d) {
                            m = m + Math.min(o.f, b);
                        }
                        else {
                            i = i + Math.min(o.f, b);
                        }
                    }
                }
            }
            let p = 1;
            let q = 1;
            let r = 1;
            if (f && i > h) {
                let s = i - h;
                p = h / i;
            }
            if (e && k > j) {
                let t = k - j;
                q = j / k;
            }
            if (g && m > l) {
                let u = m - l;
                r = l / m;
            }
            else {
                r = p;
            }
            let v;
            for (let w = 0; w < this.i.count; w++) {
                let x = this.i._inner[w];
                v = b;
                if (x.a == HighlightingState.outward) {
                    v = Math.min(v, x.f);
                    if (x.e) {
                        v = v * q;
                    }
                    else {
                        if (x.d) {
                            v = v * p;
                        }
                        else {
                            v = v * r;
                        }
                    }
                    x.f = x.f - v;
                }
                else {
                    x.f = x.f + v;
                }
                if (x.a == 2) {
                    x.f = 1;
                }
                if (x.f > 1) {
                    x.f = 1;
                }
                if (x.f < 0) {
                    x.f = 0;
                }
                if (x.e) {
                    this.maxMarkerHighlightingProgress = Math.max(x.f, this.maxMarkerHighlightingProgress);
                    this.sumMarkerHighlightingProgress = this.sumMarkerHighlightingProgress + x.f;
                }
                else {
                    this.maxHighlightingProgress = Math.max(x.f, this.maxHighlightingProgress);
                    this.sumHighlightingProgress = this.sumHighlightingProgress + x.f;
                }
                if (x.a != HighlightingState.outward && x.f != 1) {
                    c = true;
                }
                if (x.a == HighlightingState.outward && x.f != 0) {
                    c = true;
                }
                if (x.f == 1 && x.a == HighlightingState.inward) {
                    x.a = 2;
                }
                if (x.f == 0 && x.a == HighlightingState.outward) {
                    d.add(x);
                }
                if (this.isCrossSeriesHighlight(x.b)) {
                    this.allDirty = true;
                }
                else {
                    if (!this.f.containsKey(x.b)) {
                        this.f.addItem(x.b, true);
                        this.dirtySeries.add(x.b);
                    }
                }
            }
            for (let y of fromEnum(d)) {
                this.e.removeItem(y);
                this.i.remove(y);
            }
            if (this.e.count == 0) {
                this.inProgress = false;
                this.maxHighlightingProgress = 0;
                this.sumHighlightingProgress = 0;
                this.maxMarkerHighlightingProgress = 0;
                this.sumMarkerHighlightingProgress = 0;
                this.totalHighlightingProgress = 0;
            }
            this.k.clear();
            for (let z = 0; z < this.i.count; z++) {
                let aa = this.i._inner[z];
                let ab = null;
                if (this.g.containsKey(aa.b)) {
                    ab = this.g.item(aa.b);
                }
                else {
                    ab = new HighlightingData();
                    ab.b = 0;
                    ab.e = 0;
                    ab.a = aa.b;
                    ab.c = 0;
                    ab.d = 0;
                    this.g.addItem(aa.b, ab);
                    this.k.add(aa.b);
                }
                if (aa.e) {
                    ab.c += aa.f;
                }
                else {
                    if (aa.d) {
                        if (aa.c) {
                            ab.d += aa.f;
                        }
                        else {
                            ab.e += aa.f;
                        }
                    }
                    else {
                        ab.b += aa.f;
                    }
                }
            }
            if (this.d) {
                this.totalHighlightingProgress = Math.max(this.sumHighlightingProgress, this.sumMarkerHighlightingProgress);
            }
            else {
                let ac = 0;
                let ad = 0;
                let ae = 0;
                let af = 0;
                let ag = 0;
                let ah = 0;
                let ai = 0;
                for (let aj = 0; aj < this.k.count; aj++) {
                    let ak = this.g.item(this.k._inner[aj]);
                    if (ak.b > 0) {
                        ah++;
                    }
                    if (ak.c > 0) {
                        ai++;
                    }
                    if (ak.e > 0) {
                        ae++;
                    }
                    ac += ak.b;
                    ad += ak.e;
                    ag += ak.d;
                    af += ak.c;
                }
                if (ah > 0) {
                    ac = ac / ah;
                }
                if (ai > 0) {
                    af = af / ai;
                }
                if (this.highlightingBehavior == 3) {
                    if (ae > 0) {
                        ad = ad / ae;
                    }
                }
                this.totalHighlightingProgress = Math.max(ag, Math.max(ac, Math.max(ad, af)));
            }
            if (!c) {
                this.m = 0;
            }
            return c;
        }
        isCrossSeriesHighlight(a) {
            if (a.be == 5 || a.be == 4) {
                return true;
            }
            return false;
        }
        get inProgress() {
            return this._inProgress;
        }
        set inProgress(a) {
            this._inProgress = a;
        }
        get maxHighlightingProgress() {
            return this._maxHighlightingProgress;
        }
        set maxHighlightingProgress(a) {
            this._maxHighlightingProgress = a;
        }
        get sumHighlightingProgress() {
            return this._sumHighlightingProgress;
        }
        set sumHighlightingProgress(a) {
            this._sumHighlightingProgress = a;
        }
        get maxMarkerHighlightingProgress() {
            return this._maxMarkerHighlightingProgress;
        }
        set maxMarkerHighlightingProgress(a) {
            this._maxMarkerHighlightingProgress = a;
        }
        get sumMarkerHighlightingProgress() {
            return this._sumMarkerHighlightingProgress;
        }
        set sumMarkerHighlightingProgress(a) {
            this._sumMarkerHighlightingProgress = a;
        }
        get totalHighlightingProgress() {
            return this._totalHighlightingProgress;
        }
        set totalHighlightingProgress(a) {
            this._totalHighlightingProgress = a;
        }
    }
    HighlightingManager.$t = /*@__PURE__*/ markType(HighlightingManager, 'HighlightingManager', Base.$, [IHighlightingManager_$type]);
    return HighlightingManager;
})();
//# sourceMappingURL=HighlightingManager.js.map
