/*
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 {
        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 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;
        }
        constructor() {
            super();
            this._executionContext = null;
            this._ensureAnimator = null;
            this._maxHighlightingProgress = 0;
            this._sumHighlightingProgress = 0;
            this._maxMarkerHighlightingProgress = 0;
            this._sumMarkerHighlightingProgress = 0;
            this._totalHighlightingProgress = 0;
            this._inProgress = false;
            this._retainMainShapes = false;
            this._isExclusiveHighlight = false;
            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.o = 0;
            this.n = 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.n = 200;
        }
        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.q(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.r(a);
        }
        r(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.p), this.n);
            }
        }
        p() {
            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.n) {
                    a.add(e);
                }
                else {
                    b = Math.max(b, this.n - (c - e.h));
                }
            }
            for (let f = 0; f < a.count; f++) {
                this.i.remove(a._inner[f]);
                this.g.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.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 = 0;
            let i = 0;
            let j = 0;
            let k = 0;
            let l = 0;
            let m = 0;
            for (let n = 0; n < this.h.count; n++) {
                let o = this.h._inner[n];
                if (o.a == HighlightingState.inward) {
                    if (o.d) {
                        e = true;
                        j = j + Math.min(1 - o.e, b);
                    }
                    else {
                        if (!this.isExclusiveHighlight && !o.c) {
                            g = true;
                            l = l + Math.min(1 - o.e, b);
                        }
                        else {
                            f = true;
                            h = h + Math.min(1 - o.e, b);
                        }
                    }
                }
                if (o.a == HighlightingState.outward) {
                    if (o.d) {
                        k = k + Math.min(o.e, b);
                    }
                    else {
                        if (!this.isExclusiveHighlight && !o.c) {
                            m = m + Math.min(o.e, b);
                        }
                        else {
                            i = i + Math.min(o.e, 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.h.count; w++) {
                let x = this.h._inner[w];
                if (!this.isContextValid(x.i)) {
                    d.add(x);
                    continue;
                }
                v = b;
                if (x.a == HighlightingState.outward) {
                    v = Math.min(v, x.e);
                    if (x.d) {
                        v = v * q;
                    }
                    else {
                        if (x.c) {
                            v = v * p;
                        }
                        else {
                            v = v * r;
                        }
                    }
                    x.e = x.e - v;
                }
                else {
                    x.e = x.e + v;
                }
                if (x.a == 2) {
                    x.e = 1;
                }
                if (x.e > 1) {
                    x.e = 1;
                }
                if (x.e < 0) {
                    x.e = 0;
                }
                if (x.d) {
                    this.maxMarkerHighlightingProgress = Math.max(x.e, this.maxMarkerHighlightingProgress);
                    this.sumMarkerHighlightingProgress = this.sumMarkerHighlightingProgress + x.e;
                }
                else {
                    this.maxHighlightingProgress = Math.max(x.e, this.maxHighlightingProgress);
                    this.sumHighlightingProgress = this.sumHighlightingProgress + x.e;
                }
                if (x.a != HighlightingState.outward && x.e != 1) {
                    c = true;
                }
                if (x.a == HighlightingState.outward && x.e != 0) {
                    c = true;
                }
                if (x.e == 1 && x.a == HighlightingState.inward) {
                    x.a = 2;
                }
                if (x.e == 0 && x.a == HighlightingState.outward) {
                    d.add(x);
                }
                if (this.isCrossContextHighlight(x.i)) {
                    this.allDirty = true;
                }
                else {
                    if (!this.e.containsKey(x.i)) {
                        this.e.addItem(x.i, true);
                        this.dirtyContexts.add1(x.i);
                    }
                }
            }
            for (let y of fromEnum(d)) {
                this.d.removeItem(y);
                this.h.remove(y);
            }
            if (this.d.count == 0) {
                this.inProgress = false;
                this.maxHighlightingProgress = 0;
                this.sumHighlightingProgress = 0;
                this.maxMarkerHighlightingProgress = 0;
                this.sumMarkerHighlightingProgress = 0;
                this.totalHighlightingProgress = 0;
            }
            this.j.clear();
            for (let z = 0; z < this.h.count; z++) {
                let aa = this.h._inner[z];
                let ab = null;
                if (this.f.containsKey(aa.i)) {
                    ab = this.f.item(aa.i);
                }
                else {
                    ab = new HighlightingData();
                    ab.a = 0;
                    ab.d = 0;
                    ab.e = aa.i;
                    ab.b = 0;
                    ab.c = 0;
                    this.f.addItem(aa.i, ab);
                    this.j.add1(aa.i);
                }
                if (aa.d) {
                    ab.b += aa.e;
                }
                else {
                    if (aa.c) {
                        if (aa.b) {
                            ab.c += aa.e;
                        }
                        else {
                            ab.d += aa.e;
                        }
                    }
                    else {
                        ab.a += aa.e;
                    }
                }
            }
            if (this.isExclusiveHighlight) {
                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.j.count; aj++) {
                    let ak = this.f.item(this.j._inner[aj]);
                    if (ak.a > 0) {
                        ah++;
                    }
                    if (ak.b > 0) {
                        ai++;
                    }
                    if (ak.d > 0) {
                        ae++;
                    }
                    ac += ak.a;
                    ad += ak.d;
                    ag += ak.c;
                    af += ak.b;
                }
                if (ah > 0) {
                    ac = ac / ah;
                }
                if (ai > 0) {
                    af = af / ai;
                }
                if (this.retainMainShapes) {
                    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;
        }
        isCrossContextHighlight(a) {
            return false;
        }
        isContextValid(a) {
            return a != null;
        }
    }
    HighlightingManager.$t = /*@__PURE__*/ markType(HighlightingManager, 'HighlightingManager', Base.$, [IHighlightingManager_$type]);
    return HighlightingManager;
})();
