/*
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 * as tslib_1 from "tslib";
import { Base, Boolean_$type, fromEnum, markType } from "igniteui-react-core/ES5/type";
import { IHighlightingManager_$type } from "./IHighlightingManager";
import { List$1 } from "igniteui-react-core/ES5/List$1";
import { Series } from "./Series";
import { Dictionary$2 } from "igniteui-react-core/ES5/Dictionary$2";
import { HighlightingInfo } from "./HighlightingInfo";
import { HighlightingState } from "./HighlightingState";
/**
 * @hidden
 */
var HighlightingManager = /** @class */ (function (_super) {
    tslib_1.__extends(HighlightingManager, _super);
    function HighlightingManager() {
        var _this = _super.call(this) || this;
        _this.a = new Dictionary$2(HighlightingInfo.$, HighlightingInfo.$, 0);
        _this.c = new List$1(HighlightingInfo.$, 0);
        _this.d = 0;
        _this.e = 0;
        _this.f = 0;
        _this._dirtySeries = null;
        _this.b = new Dictionary$2(Series.$, Boolean_$type, 0);
        _this._inProgress = false;
        _this._maxHighlightingProgress = 0;
        _this._sumHighlightingProgress = 0;
        _this._maxMarkerHighlightingProgress = 0;
        _this._sumMarkerHighlightingProgress = 0;
        _this.dirtySeries = new List$1(Series.$, 0);
        return _this;
    }
    HighlightingManager.prototype.startHighlight = function (a) {
        if (a == null) {
            return;
        }
        this.inProgress = true;
        if (!this.a.containsKey(a)) {
            a.a = HighlightingState.inward;
            this.a.item(a, a);
            this.c.add(a);
        }
        if (a.a == HighlightingState.outward) {
            a.a = HighlightingState.inward;
        }
    };
    HighlightingManager.prototype.endHighlight = function (a) {
        if (a == null) {
            return;
        }
        if (!this.a.containsKey(a)) {
            return;
        }
        if (a.a == HighlightingState.inward) {
            if (a.d < 0.05) {
                if (a.c) {
                    this.e = this.e + a.d;
                }
                else {
                    this.d = this.d + a.d;
                }
                this.a.removeItem(a);
                this.c.remove(a);
                return;
            }
            a.a = HighlightingState.outward;
        }
        if (a.a == 2) {
            a.a = HighlightingState.outward;
            a.d = 1;
        }
    };
    HighlightingManager.prototype.clear = function () {
        this.c.clear();
        this.a.clear();
    };
    HighlightingManager.prototype.getHighlightingInfo = function (a, b, c, d, e, f) {
        for (var g = 0; g < this.c.count; g++) {
            var h = this.c._inner[g];
            var i = h.f;
            var j = h.e;
            if (d >= i && e <= j && h.c == f && a == h.b && (h.a == HighlightingState.inward || h.a == 2)) {
                return h;
            }
        }
        for (var k = 0; k < this.c.count; k++) {
            var l = this.c._inner[k];
            var m = l.f;
            var n = l.e;
            if (d >= m && e <= n && l.c == f && a == l.b && (l.a == HighlightingState.outward)) {
                return l;
            }
        }
        return null;
    };
    Object.defineProperty(HighlightingManager.prototype, "dirtySeries", {
        get: function () {
            return this._dirtySeries;
        },
        set: function (a) {
            this._dirtySeries = a;
        },
        enumerable: true,
        configurable: true
    });
    HighlightingManager.prototype.tick = function (a) {
        var e_1, _a;
        this.b.clear();
        this.dirtySeries.clear();
        var b = 0;
        if (this.f > a) {
            b = (1 - this.f) + a;
        }
        else {
            b = a - this.f;
        }
        this.f = a;
        var c = false;
        var d = new List$1(HighlightingInfo.$, 0);
        this.maxHighlightingProgress = 0;
        this.sumHighlightingProgress = 0;
        this.maxMarkerHighlightingProgress = 0;
        this.sumMarkerHighlightingProgress = 0;
        var e = false;
        var f = false;
        var g = 0;
        var h = 0;
        var i = 0;
        var j = 0;
        var k = 0;
        var l = 0;
        for (var m = 0; m < this.c.count; m++) {
            var n = this.c._inner[m];
            if (n.a == HighlightingState.inward) {
                if (n.c) {
                    e = true;
                    i = i + Math.min(1 - n.d, b);
                }
                else {
                    f = true;
                    g = g + Math.min(1 - n.d, b);
                }
            }
            if (n.a == HighlightingState.outward) {
                if (n.c) {
                    j = j + Math.min(n.d, b);
                    l++;
                }
                else {
                    h = h + Math.min(n.d, b);
                    k++;
                }
            }
        }
        h = h + this.d;
        this.d = 0;
        j = j + this.e;
        this.e = 0;
        var o = 1;
        var p = 1;
        if (f && h > g) {
            var q = h - g;
            o = g / h;
        }
        if (e && j > i) {
            var r = j - i;
            p = i / j;
        }
        var s;
        for (var t = 0; t < this.c.count; t++) {
            var u = this.c._inner[t];
            s = b;
            if (u.a == HighlightingState.outward) {
                s = Math.min(s, u.d);
                if (u.c) {
                    s = s * p;
                }
                else {
                    s = s * o;
                }
                u.d = u.d - s;
            }
            else {
                u.d = u.d + s;
            }
            if (u.a == 2) {
                u.d = 1;
            }
            if (u.d > 1) {
                u.d = 1;
            }
            if (u.d < 0) {
                u.d = 0;
            }
            if (u.c) {
                this.maxMarkerHighlightingProgress = Math.max(u.d, this.maxMarkerHighlightingProgress);
                this.sumMarkerHighlightingProgress = this.sumMarkerHighlightingProgress + u.d;
            }
            else {
                this.maxHighlightingProgress = Math.max(u.d, this.maxHighlightingProgress);
                this.sumHighlightingProgress = this.sumHighlightingProgress + u.d;
            }
            if (u.a != HighlightingState.outward && u.d != 1) {
                c = true;
            }
            if (u.a == HighlightingState.outward && u.d != 0) {
                c = true;
            }
            if (u.d == 1 && u.a == HighlightingState.inward) {
                u.a = 2;
            }
            if (u.d == 0 && u.a == HighlightingState.outward) {
                d.add(u);
            }
            if (!this.b.containsKey(u.b)) {
                this.b.addItem(u.b, true);
                this.dirtySeries.add(u.b);
            }
        }
        try {
            for (var _b = tslib_1.__values(fromEnum(d)), _c = _b.next(); !_c.done; _c = _b.next()) {
                var v = _c.value;
                this.a.removeItem(v);
                this.c.remove(v);
            }
        }
        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.a.count == 0) {
            this.inProgress = false;
            this.maxHighlightingProgress = 0;
            this.sumHighlightingProgress = 0;
            this.maxMarkerHighlightingProgress = 0;
            this.sumMarkerHighlightingProgress = 0;
        }
        if (!c) {
            this.f = 0;
        }
        return c;
    };
    Object.defineProperty(HighlightingManager.prototype, "inProgress", {
        get: function () {
            return this._inProgress;
        },
        set: function (a) {
            this._inProgress = a;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(HighlightingManager.prototype, "maxHighlightingProgress", {
        get: function () {
            return this._maxHighlightingProgress;
        },
        set: function (a) {
            this._maxHighlightingProgress = a;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(HighlightingManager.prototype, "sumHighlightingProgress", {
        get: function () {
            return this._sumHighlightingProgress;
        },
        set: function (a) {
            this._sumHighlightingProgress = a;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(HighlightingManager.prototype, "maxMarkerHighlightingProgress", {
        get: function () {
            return this._maxMarkerHighlightingProgress;
        },
        set: function (a) {
            this._maxMarkerHighlightingProgress = a;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(HighlightingManager.prototype, "sumMarkerHighlightingProgress", {
        get: function () {
            return this._sumMarkerHighlightingProgress;
        },
        set: function (a) {
            this._sumMarkerHighlightingProgress = a;
        },
        enumerable: true,
        configurable: true
    });
    HighlightingManager.$t = markType(HighlightingManager, 'HighlightingManager', Base.$, [IHighlightingManager_$type]);
    return HighlightingManager;
}(Base));
export { HighlightingManager };
