/*
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 "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
 */
var HighlightingManager = /** @class */ /*@__PURE__*/ (function (_super) {
    __extends(HighlightingManager, _super);
    function HighlightingManager() {
        var _this = _super.call(this) || this;
        _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;
        return _this;
    }
    Object.defineProperty(HighlightingManager.prototype, "highlightingBehavior", {
        get: function () {
            return this._highlightingBehavior;
        },
        set: function (a) {
            this._highlightingBehavior = a;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(HighlightingManager.prototype, "d", {
        get: function () {
            return this.highlightingBehavior == 1;
        },
        enumerable: false,
        configurable: true
    });
    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
    });
    HighlightingManager.prototype.startHighlight = function (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;
        }
    };
    HighlightingManager.prototype.a = function (a) {
        var b = null;
        if (this.h.containsKey(a)) {
            a = this.h.item(a);
            this.h.removeItem(a);
            this.j.remove(a);
            b = a;
        }
        var c = this.l;
        for (var d = 0; d < this.j.count; d++) {
            var 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);
                    var 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 (var 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;
    };
    HighlightingManager.prototype.endHighlight = function (a) {
        if (a == null) {
            return;
        }
        if (!this.e.containsKey(a)) {
            return;
        }
        else {
            a = this.e.item(a);
        }
        this.r(a);
    };
    HighlightingManager.prototype.r = function (a) {
        if (this.h.containsKey(a)) {
            var 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);
        }
    };
    HighlightingManager.prototype.p = function () {
        this.c = false;
        if (!this.b) {
            return;
        }
        var a = this.l;
        var b = 0;
        var c = this.executionContext.getCurrentRelativeTime();
        for (var d = 0; d < this.j.count; d++) {
            var 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 (var 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);
        }
    };
    HighlightingManager.prototype.q = function (a) {
        if (a.a == HighlightingState.inward) {
            a.a = HighlightingState.outward;
        }
        if (a.a == 2) {
            a.a = HighlightingState.outward;
            a.f = 1;
        }
    };
    HighlightingManager.prototype.clear = function () {
        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;
    };
    HighlightingManager.prototype.getHighlightingInfo = function (a, b, c, d, e) {
        for (var f = 0; f < this.i.count; f++) {
            var g = this.i._inner[f];
            var h = g.h;
            var 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 (var j = 0; j < this.i.count; j++) {
            var k = this.i._inner[j];
            var l = k.h;
            var m = k.g;
            if (((c >= l && d <= m) || k.d) && k.e == e && a == k.b && (k.a == HighlightingState.outward)) {
                return k;
            }
        }
        return null;
    };
    Object.defineProperty(HighlightingManager.prototype, "dirtySeries", {
        get: function () {
            return this._dirtySeries;
        },
        set: function (a) {
            this._dirtySeries = 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.f.clear();
        this.dirtySeries.clear();
        this.g.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.i.count; n++) {
            var 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);
                    }
                }
            }
        }
        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.i.count; w++) {
            var x = this.i._inner[w];
            if (x.b == null || x.b.cy == null) {
                d.add(x);
                continue;
            }
            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);
                }
            }
        }
        try {
            for (var _b = __values(fromEnum(d)), _c = _b.next(); !_c.done; _c = _b.next()) {
                var y = _c.value;
                this.e.removeItem(y);
                this.i.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.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 (var z = 0; z < this.i.count; z++) {
            var aa = this.i._inner[z];
            var 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 {
            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.k.count; aj++) {
                var 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;
    };
    HighlightingManager.prototype.isCrossSeriesHighlight = function (a) {
        if (a.bs == 5 || a.bs == 4) {
            return true;
        }
        return false;
    };
    Object.defineProperty(HighlightingManager.prototype, "inProgress", {
        get: function () {
            return this._inProgress;
        },
        set: function (a) {
            this._inProgress = 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
    });
    HighlightingManager.$t = markType(HighlightingManager, 'HighlightingManager', Base.$, [IHighlightingManager_$type]);
    return HighlightingManager;
}(Base));
export { HighlightingManager };
