/*
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, Boolean_$type, fromEnum, markType } from "igniteui-react-core/ES2015/type";
import { IHighlightingManager_$type } from "./IHighlightingManager";
import { List$1 } from "igniteui-react-core/ES2015/List$1";
import { Series } from "./Series";
import { Dictionary$2 } from "igniteui-react-core/ES2015/Dictionary$2";
import { HighlightingInfo } from "./HighlightingInfo";
import { HighlightingState } from "./HighlightingState";
/**
 * @hidden
 */
export class HighlightingManager extends Base {
    constructor() {
        super();
        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);
    }
    startHighlight(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;
        }
    }
    endHighlight(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;
        }
    }
    clear() {
        this.c.clear();
        this.a.clear();
    }
    getHighlightingInfo(a, b, c, d, e, f) {
        for (let g = 0; g < this.c.count; g++) {
            let h = this.c._inner[g];
            let i = h.f;
            let j = h.e;
            if (d >= i && e <= j && h.c == f && a == h.b && (h.a == HighlightingState.inward || h.a == 2)) {
                return h;
            }
        }
        for (let k = 0; k < this.c.count; k++) {
            let l = this.c._inner[k];
            let m = l.f;
            let n = l.e;
            if (d >= m && e <= n && l.c == f && a == l.b && (l.a == HighlightingState.outward)) {
                return l;
            }
        }
        return null;
    }
    get dirtySeries() {
        return this._dirtySeries;
    }
    set dirtySeries(a) {
        this._dirtySeries = a;
    }
    tick(a) {
        this.b.clear();
        this.dirtySeries.clear();
        let b = 0;
        if (this.f > a) {
            b = (1 - this.f) + a;
        }
        else {
            b = a - this.f;
        }
        this.f = a;
        let c = false;
        let d = new List$1(HighlightingInfo.$, 0);
        this.maxHighlightingProgress = 0;
        this.sumHighlightingProgress = 0;
        this.maxMarkerHighlightingProgress = 0;
        this.sumMarkerHighlightingProgress = 0;
        let e = false;
        let f = false;
        let g = 0;
        let h = 0;
        let i = 0;
        let j = 0;
        let k = 0;
        let l = 0;
        for (let m = 0; m < this.c.count; m++) {
            let 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;
        let o = 1;
        let p = 1;
        if (f && h > g) {
            let q = h - g;
            o = g / h;
        }
        if (e && j > i) {
            let r = j - i;
            p = i / j;
        }
        let s;
        for (let t = 0; t < this.c.count; t++) {
            let 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);
            }
        }
        for (let v of fromEnum(d)) {
            this.a.removeItem(v);
            this.c.remove(v);
        }
        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;
    }
    get inProgress() {
        return this._inProgress;
    }
    set inProgress(a) {
        this._inProgress = 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;
    }
}
HighlightingManager.$t = markType(HighlightingManager, 'HighlightingManager', Base.$, [IHighlightingManager_$type]);
