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