/*
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, markType } from "./type";
import { List$1 } from "./List$1";
import { SectionData } from "./SectionData";
import { SectionDataStartIndexComparer } from "./SectionDataStartIndexComparer";
import { SectionDataUndisplacedStartIndexComparer } from "./SectionDataUndisplacedStartIndexComparer";
import { SectionSearchData } from "./SectionSearchData";
/**
 * @hidden
 */
export class SectionMap extends Base {
    constructor() {
        super();
        this.a = new SectionData();
        this.h = new SectionDataStartIndexComparer();
        this.i = new SectionDataUndisplacedStartIndexComparer();
        this.o = new List$1(SectionData.$, 0);
        this.k = new SectionSearchData();
        this.j = new SectionSearchData();
    }
    get p() {
        return this.o;
    }
    get q() {
        return this.o._inner[this.o.count - 1].l + 1;
    }
    get n() {
        return this.p.count > 0;
    }
    l(a, b, c) {
        if (a >= 0 && a < this.o.count) {
            let d = this.o._inner[a];
            if (b >= d.n && b <= d.l) {
                c.c++;
                if (c.c >= c.a.length) {
                    c.c = 0;
                }
                c.b[c.c] = a;
                c.a[c.c] = d;
                return true;
            }
        }
        return false;
    }
    m(a, b, c) {
        if (a >= 0 && a < this.o.count) {
            let d = this.o._inner[a];
            if (b >= d.j && b <= d.i) {
                c.c++;
                if (c.c >= c.a.length) {
                    c.c = 0;
                }
                c.b[c.c] = a;
                c.a[c.c] = d;
                return true;
            }
        }
        return false;
    }
    d(a) {
        return this.e(a, this.h, this.k);
    }
    e(a, b, c) {
        if (c.a == null) {
            c.a = new Array(5);
            c.b = new Array(5);
            for (let d = 0; d < c.a.length; d++) {
                c.a[d] = null;
                c.b[d] = -1;
            }
        }
        if (c.a[c.c] != null) {
            if (a >= c.a[c.c].n && a <= c.a[c.c].l) {
                return c.a[c.c];
            }
            for (let e = 0; e < c.a.length; e++) {
                if (c.a[e] != null) {
                    if (a >= c.a[e].n && a <= c.a[e].l) {
                        c.c = e;
                        return c.a[c.c];
                    }
                }
            }
            let f = c.b[c.c] + 1;
            if (this.l(f, a, c)) {
                return c.a[c.c];
            }
            f = c.b[c.c] - 1;
            if (this.l(f, a, c)) {
                return c.a[c.c];
            }
        }
        this.a.n = a;
        let g = this.o.g(this.a, b);
        if (g < 0) {
            g = ~g;
            g--;
        }
        if (this.l(g, a, c)) {
            return c.a[c.c];
        }
        if (this.l(g + 1, a, c)) {
            return c.a[c.c];
        }
        if (this.l(g - 1, a, c)) {
            return c.a[c.c];
        }
        return null;
    }
    g(a, b, c) {
        if (c.a == null) {
            c.a = new Array(5);
            c.b = new Array(5);
            for (let d = 0; d < c.a.length; d++) {
                c.a[d] = null;
                c.b[d] = -1;
            }
        }
        if (c.a[c.c] != null) {
            let e = c.a[c.c];
            if (a >= e.j && a <= e.i) {
                return e;
            }
            for (let f = 0; f < c.a.length; f++) {
                if (c.a[f] != null) {
                    if (a >= c.a[f].j && a <= c.a[f].i) {
                        c.c = f;
                        return c.a[c.c];
                    }
                }
            }
            let g = c.b[c.c] + 1;
            if (this.m(g, a, c)) {
                return c.a[c.c];
            }
            g = c.b[c.c] - 1;
            if (this.m(g, a, c)) {
                return c.a[c.c];
            }
        }
        this.a.n = a;
        let h = this.o.g(this.a, b);
        if (h < 0) {
            h = ~h;
            h--;
        }
        if (this.m(h, a, c)) {
            return c.a[c.c];
        }
        if (this.m(h + 1, a, c)) {
            return c.a[c.c];
        }
        if (this.m(h - 1, a, c)) {
            return c.a[c.c];
        }
        return null;
    }
    r() {
        this.o.clear();
        this.k.a = null;
        this.j.a = null;
    }
    x(a, b, c) {
        for (let d = a.m + 1; d < this.o.count; d++) {
            let e = this.o._inner[d];
            e.g += c;
            e.n += b;
            e.l += b;
        }
    }
    w(a, b, c) {
        for (let d = a.m; d < this.o.count; d++) {
            let e = this.o._inner[d];
            e.g += c;
            e.n += b;
            e.l += b;
            e.m = d;
        }
    }
    t(a) {
        if (this.o.count == 0) {
            return;
        }
        let b = this.o._inner[this.o.count - 1];
        b.l = a;
    }
    aa(a, b, c, d, e, f) {
        let g = new SectionData();
        g.n = a;
        g.c = d;
        g.m = this.o.count;
        if (this.o.count > 0) {
            g.g = this.o._inner[this.o.count - 1].g;
        }
        let h = new Array(d.length);
        for (let i = 0; i < h.length; i++) {
            h[i] = b.getItemProperty(c, d[i]);
        }
        g.d = h;
        g.o(e, f);
        g.e = new Array(b.summaryDescriptions.k.count);
        this.o.add(g);
    }
    y(a, b, c) {
        for (let d = a.m + 1; d < this.o.count; d++) {
            this.o._inner[d].m = d;
            this.o._inner[d].g += c;
            this.o._inner[d].n += b;
            this.o._inner[d].l += b;
        }
    }
    v(a, b) {
        this.o.insert(a, b);
    }
    z(a) {
        if (this.k != null) {
            let b = this.k;
            SectionMap.s(a, b);
        }
        if (this.j != null) {
            let c = this.j;
            SectionMap.s(a, c);
        }
        this.o.removeAt(a);
    }
    static s(a, b) {
        if (b.a != null) {
            for (let c = 0; c < b.a.length; c++) {
                if (b.a[c] != null) {
                    if (b.a[c].m == a) {
                        b.a[c] = null;
                    }
                }
            }
        }
    }
    c() {
        return this.o._inner[this.o.count - 1];
    }
    b() {
        return this.o._inner[0];
    }
    u() {
        this.o._inner[this.o.count - 1].g++;
    }
    f(a) {
        return this.g(a, this.i, this.j);
    }
}
SectionMap.$t = markType(SectionMap, 'SectionMap');
