/*
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 { __extends, __values } from "tslib";
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
 */
var HighlightingManager = /** @class */ /*@__PURE__*/ (function (_super) {
    __extends(HighlightingManager, _super);
    function HighlightingManager() {
        var _this = _super.call(this) || this;
        _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;
        return _this;
    }
    Object.defineProperty(HighlightingManager.prototype, "executionContext", {
        get: function () {
            return this._executionContext;
        },
        set: function (a) {
            this._executionContext = a;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(HighlightingManager.prototype, "ensureAnimator", {
        get: function () {
            return this._ensureAnimator;
        },
        set: function (a) {
            this._ensureAnimator = a;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(HighlightingManager.prototype, "maxHighlightingProgress", {
        get: function () {
            return this._maxHighlightingProgress;
        },
        set: function (a) {
            this._maxHighlightingProgress = a;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(HighlightingManager.prototype, "sumHighlightingProgress", {
        get: function () {
            return this._sumHighlightingProgress;
        },
        set: function (a) {
            this._sumHighlightingProgress = a;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(HighlightingManager.prototype, "maxMarkerHighlightingProgress", {
        get: function () {
            return this._maxMarkerHighlightingProgress;
        },
        set: function (a) {
            this._maxMarkerHighlightingProgress = a;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(HighlightingManager.prototype, "sumMarkerHighlightingProgress", {
        get: function () {
            return this._sumMarkerHighlightingProgress;
        },
        set: function (a) {
            this._sumMarkerHighlightingProgress = a;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(HighlightingManager.prototype, "totalHighlightingProgress", {
        get: function () {
            return this._totalHighlightingProgress;
        },
        set: function (a) {
            this._totalHighlightingProgress = a;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(HighlightingManager.prototype, "inProgress", {
        get: function () {
            return this._inProgress;
        },
        set: function (a) {
            this._inProgress = a;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(HighlightingManager.prototype, "retainMainShapes", {
        get: function () {
            return this._retainMainShapes;
        },
        set: function (a) {
            this._retainMainShapes = a;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(HighlightingManager.prototype, "isExclusiveHighlight", {
        get: function () {
            return this._isExclusiveHighlight;
        },
        set: function (a) {
            this._isExclusiveHighlight = a;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(HighlightingManager.prototype, "l", {
        get: function () {
            return this.h;
        },
        enumerable: false,
        configurable: true
    });
    HighlightingManager.prototype.startHighlight = function (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;
        }
    };
    HighlightingManager.prototype.a = function (a) {
        var b = null;
        if (this.g.containsKey(a)) {
            a = this.g.item(a);
            this.g.removeItem(a);
            this.i.remove(a);
            b = a;
        }
        var c = this.k;
        for (var d = 0; d < this.i.count; d++) {
            var 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);
                    var 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 (var 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;
    };
    HighlightingManager.prototype.endHighlight = function (a) {
        if (a == null) {
            return;
        }
        if (!this.d.containsKey(a)) {
            return;
        }
        else {
            a = this.d.item(a);
        }
        this.r(a);
    };
    HighlightingManager.prototype.r = function (a) {
        if (this.g.containsKey(a)) {
            var 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);
        }
    };
    HighlightingManager.prototype.p = function () {
        this.c = false;
        if (!this.b) {
            return;
        }
        var a = this.k;
        var b = 0;
        var c = this.executionContext.getCurrentRelativeTime();
        for (var d = 0; d < this.i.count; d++) {
            var 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 (var 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);
        }
    };
    HighlightingManager.prototype.q = function (a) {
        if (a.a == HighlightingState.inward) {
            a.a = HighlightingState.outward;
        }
        if (a.a == 2) {
            a.a = HighlightingState.outward;
            a.e = 1;
        }
    };
    HighlightingManager.prototype.clear = function () {
        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;
    };
    HighlightingManager.prototype.getHighlightingInfo = function (a, b, c, d, e) {
        for (var f = 0; f < this.h.count; f++) {
            var g = this.h._inner[f];
            var h = g.g;
            var 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 (var j = 0; j < this.h.count; j++) {
            var k = this.h._inner[j];
            var l = k.g;
            var m = k.f;
            if (((c >= l && d <= m) || k.c) && k.d == e && a == k.i && (k.a == HighlightingState.outward)) {
                return k;
            }
        }
        return null;
    };
    Object.defineProperty(HighlightingManager.prototype, "dirtyContexts", {
        get: function () {
            return this._dirtyContexts;
        },
        set: function (a) {
            this._dirtyContexts = a;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(HighlightingManager.prototype, "allDirty", {
        get: function () {
            return this._allDirty;
        },
        set: function (a) {
            this._allDirty = a;
        },
        enumerable: false,
        configurable: true
    });
    HighlightingManager.prototype.tick = function (a) {
        var e_1, _a;
        this.e.clear();
        this.dirtyContexts.clear();
        this.f.clear();
        this.allDirty = false;
        var b = 0;
        if (this.m > a) {
            b = (1 - this.m) + a;
        }
        else {
            b = a - this.m;
        }
        this.m = a;
        var c = false;
        var d = new List$1(HighlightingInfo.$, 0);
        this.maxHighlightingProgress = 0;
        this.sumHighlightingProgress = 0;
        this.maxMarkerHighlightingProgress = 0;
        this.sumMarkerHighlightingProgress = 0;
        this.totalHighlightingProgress = 0;
        var e = false;
        var f = false;
        var g = false;
        var h = 0;
        var i = 0;
        var j = 0;
        var k = 0;
        var l = 0;
        var m = 0;
        for (var n = 0; n < this.h.count; n++) {
            var 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);
                    }
                }
            }
        }
        var p = 1;
        var q = 1;
        var r = 1;
        if (f && i > h) {
            var s = i - h;
            p = h / i;
        }
        if (e && k > j) {
            var t = k - j;
            q = j / k;
        }
        if (g && m > l) {
            var u = m - l;
            r = l / m;
        }
        else {
            r = p;
        }
        var v;
        for (var w = 0; w < this.h.count; w++) {
            var 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);
                }
            }
        }
        try {
            for (var _b = __values(fromEnum(d)), _c = _b.next(); !_c.done; _c = _b.next()) {
                var y = _c.value;
                this.d.removeItem(y);
                this.h.remove(y);
            }
        }
        catch (e_1_1) {
            e_1 = { error: e_1_1 };
        }
        finally {
            try {
                if (_c && !_c.done && (_a = _b.return))
                    _a.call(_b);
            }
            finally {
                if (e_1)
                    throw e_1.error;
            }
        }
        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 (var z = 0; z < this.h.count; z++) {
            var aa = this.h._inner[z];
            var 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 {
            var ac = 0;
            var ad = 0;
            var ae = 0;
            var af = 0;
            var ag = 0;
            var ah = 0;
            var ai = 0;
            for (var aj = 0; aj < this.j.count; aj++) {
                var 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;
    };
    HighlightingManager.prototype.isCrossContextHighlight = function (a) {
        return false;
    };
    HighlightingManager.prototype.isContextValid = function (a) {
        return a != null;
    };
    HighlightingManager.$t = markType(HighlightingManager, 'HighlightingManager', Base.$, [IHighlightingManager_$type]);
    return HighlightingManager;
}(Base));
export { HighlightingManager };
