/*
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, toNullable, Number_$type, fromEnum, toEnum, markType } from "igniteui-angular-core";
import { SortedIntRangeList_IntRange } from "./SortedIntRangeList_IntRange";
import { List$1 } from "igniteui-angular-core";
import { intDivide } from "igniteui-angular-core";
/**
 * @hidden
 */
export let SortedIntRangeList = /*@__PURE__*/ (() => {
    class SortedIntRangeList extends Base {
        constructor() {
            super();
            this.e = 0;
            this.d = null;
            this.d = new List$1(SortedIntRangeList_IntRange.$, 0);
            this.e = -1;
        }
        get j() {
            if (this.e == -1) {
                this.e = 0;
                for (let a = 0; a < this.d.count; a++) {
                    this.e += this.d._inner[a].a;
                }
            }
            return this.e;
        }
        get k() {
            return this.d.count;
        }
        m(a) {
            this.n(a, a);
        }
        n(a, b) {
            let c = this.g(a - 1);
            let d = this.g(b + 1);
            if (c == d) {
                if (c < 0) {
                    let e = ~c;
                    this.d.insert(e, new SortedIntRangeList_IntRange(1, a, b));
                }
            }
            else {
                if (c < 0) {
                    c = ~c;
                }
                else {
                    a = this.d._inner[c].c;
                }
                if (d < 0) {
                    d = ~d - 1;
                }
                else {
                    b = this.d._inner[d].b;
                }
                if (c == this.d.count) {
                    this.d.add(new SortedIntRangeList_IntRange(1, a, b));
                }
                else {
                    let f = this.d._inner[c];
                    f.c = a;
                    f.b = b;
                    this.d._inner[c] = f;
                    for (let g = d; g > c; g--) {
                        this.d.removeAt(g);
                    }
                }
            }
            this.e = -1;
        }
        o() {
            this.d.clear();
            this.e = 0;
        }
        a(a) {
            return this.g(a) >= 0;
        }
        g(a) {
            let b = this.d;
            let c = 0, d = b.count - 1;
            let e = 0;
            while (c <= d) {
                e = intDivide((c + d), 2);
                let f = b._inner[e];
                if (f.c > a) {
                    d = e - 1;
                }
                else if (f.b < a) {
                    c = e + 1;
                }
                else {
                    return e;
                }
            }
            return ~c;
        }
        l(a) {
            for (let b = 0, c = this.d.count; b < c; b++) {
                let d = this.d._inner[b];
                if (d.a > a) {
                    return toNullable(Number_$type, d.c + a);
                }
                a -= d.a;
            }
            return toNullable(Number_$type, null);
        }
        i(a) {
            return this.d._inner[a].c;
        }
        h(a) {
            return this.d._inner[a].b;
        }
        *_c() {
            for (let a of fromEnum(this.d)) {
                for (let b = a.c; b <= a.b; b++) {
                    yield b;
                }
            }
        }
        c() {
            return toEnum(() => this._c());
        }
        b(a) {
            let b = this.g(a);
            if (b < 0) {
                return false;
            }
            this.e = -1;
            let c = this.d._inner[b];
            if (c.a == 1) {
                this.d.removeAt(b);
            }
            else {
                if (c.c == a) {
                    c.c++;
                }
                else if (c.b == a) {
                    c.b--;
                }
                else {
                    let d = c.b;
                    c.b = a - 1;
                    this.d.insert(b + 1, new SortedIntRangeList_IntRange(1, a + 1, d));
                }
                this.d._inner[b] = c;
            }
            return true;
        }
        p(a) {
            let b = this.l(a);
            if (b.hasValue) {
                this.b(b.value);
            }
        }
        q(a, b) {
            if (b < a) {
                let c = b;
                b = a;
                a = c;
            }
            let d = this.g(a);
            let e = this.g(b);
            if (d == e) {
                if (d < 0) {
                    return;
                }
                let f = this.d._inner[d];
                if (f.c < a) {
                    if (b < f.b) {
                        this.d.insert(d + 1, new SortedIntRangeList_IntRange(1, b + 1, f.b));
                    }
                    f.b = a - 1;
                    this.d._inner[d] = f;
                }
                else if (b == f.b) {
                    this.d.removeAt(d);
                }
                else {
                    f.c = b + 1;
                    this.d._inner[d] = f;
                }
            }
            else {
                if (d >= 0 && this.d._inner[d].c < a) {
                    let g = this.d._inner[d];
                    g.b = a - 1;
                    this.d._inner[d] = g;
                    d++;
                }
                else if (d < 0) {
                    d = ~d;
                }
                if (e >= 0 && this.d._inner[e].b > b) {
                    let h = this.d._inner[e];
                    h.c = b + 1;
                    this.d._inner[e] = h;
                    e--;
                }
                else if (e < 0) {
                    e = ~e - 1;
                }
                if (d <= e) {
                    this.d.v(d, (e - d) + 1);
                }
            }
            this.e = -1;
        }
    }
    SortedIntRangeList.$t = markType(SortedIntRangeList, 'SortedIntRangeList');
    return SortedIntRangeList;
})();
