/*
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 } from "tslib";
import { Base, typeCast, Array_$type, String_$type, Boolean_$type, markType } from "./type";
import { ISupportsDataChangeNotifications_$type } from "./ISupportsDataChangeNotifications";
import { List$1 } from "./List$1";
import { SectionData } from "./SectionData";
import { SectionMap } from "./SectionMap";
import { Dictionary$2 } from "./Dictionary$2";
import { DataSourceSpecialRow } from "./DataSourceSpecialRow";
import { RemovedShiftedRowInfo } from "./RemovedShiftedRowInfo";
/**
 * @hidden
 */
var RearrangedListView = /** @class */ (function (_super) {
    __extends(RearrangedListView, _super);
    function RearrangedListView(a) {
        var _this = _super.call(this) || this;
        _this.h = null;
        _this.an = null;
        _this.p = new SectionMap();
        _this.a = null;
        _this.i = null;
        _this.aj = null;
        _this.ai = null;
        _this.k = null;
        _this.q = null;
        _this.g = null;
        _this.s = null;
        _this.t = null;
        _this.u = null;
        _this.aa = false;
        _this.e = 0;
        _this.ae = false;
        _this.ad = false;
        _this.af = false;
        _this.v = false;
        _this.am = new List$1(Array_$type, 0);
        _this.ah = new Dictionary$2(String_$type, Boolean_$type, 0);
        _this.h = a;
        return _this;
    }
    Object.defineProperty(RearrangedListView.prototype, "ap", {
        get: function () {
            return this.an;
        },
        enumerable: true,
        configurable: true
    });
    RearrangedListView.prototype.n = function (a) {
        return this.p.d(a);
    };
    Object.defineProperty(RearrangedListView.prototype, "j", {
        get: function () {
            return this.i;
        },
        set: function (a) {
            this.i = a;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(RearrangedListView.prototype, "al", {
        get: function () {
            return this.aj;
        },
        set: function (a) {
            this.aj = a;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(RearrangedListView.prototype, "ak", {
        get: function () {
            return this.ai;
        },
        set: function (a) {
            this.ai = a;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(RearrangedListView.prototype, "l", {
        get: function () {
            return this.k;
        },
        set: function (a) {
            this.k = a;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(RearrangedListView.prototype, "r", {
        get: function () {
            return this.q;
        },
        set: function (a) {
            this.q = a;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(RearrangedListView.prototype, "f", {
        get: function () {
            return this.e;
        },
        set: function (a) {
            this.e = a;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(RearrangedListView.prototype, "ab", {
        get: function () {
            if (this.g.isSectionCollapsable) {
                return this.g.isSectionExpandedDefault;
            }
            return true;
        },
        enumerable: true,
        configurable: true
    });
    RearrangedListView.prototype.be = function () {
        var _this = this;
        if (this.an == this.h || this.an == null) {
            this.an = new List$1(Base.$, 0);
        }
        this.an.clear();
        if (!this.g.isSectionCollapsable) {
            this.ah.clear();
        }
        if (this.h == null) {
            return;
        }
        var a = this.h.actualCount;
        this.a3(null);
        var b = this.u.k.count > 0 && this.f != 2 && this.f != 3;
        if (this.k == null) {
            for (var c = 0; c < a; c++) {
                var d = this.h.getItemAtIndex(c);
                this.an.add1(d);
                if (b) {
                    this.a2(null, d);
                }
            }
        }
        else {
            for (var e = 0; e < a; e++) {
                var f = this.h.getItemAtIndex(e);
                if (this.k(f)) {
                    this.an.add1(f);
                    if (b) {
                        this.a2(null, f);
                    }
                }
            }
        }
        this.a5(null);
        if (this.aj != null) {
            this.an.z(this.aj);
        }
        this.v = false;
        this.p.r();
        if (this.s != null && this.s.k.count > 0) {
            this.v = true;
            this.a9();
        }
        else {
            this.p.p.add(((function () {
                var $ret = new SectionData();
                $ret.m = 0;
                $ret.c = null;
                $ret.d = null;
                $ret.n = 0;
                $ret.l = _this.an.count;
                $ret.b = new Array(0);
                return $ret;
            })()));
        }
        var g = this.j;
        var h = new List$1(Base.$, 0);
        if (this.am.count > 0 && this.af) {
            if (!this.v) {
                var i = this.p.p._inner[0];
                for (var j = 0; j < this.am.count; j++) {
                    var k = this.g.indexOfKey(this.am._inner[j]);
                    if (k != -1) {
                        this.ax(i, this.an._inner[k], j);
                    }
                }
            }
            else {
                for (var l = 0; l < this.am.count; l++) {
                    var m = this.g.indexOfKey(this.am._inner[l]);
                    if (m != -1) {
                        var n = this.p.d(m);
                        this.ax(n, this.an._inner[m], l);
                    }
                }
            }
        }
        if (this.r != null) {
            this.r.notifyClearItems();
        }
    };
    RearrangedListView.prototype.at = function (a, b, c) {
        var d = a.n + a.h;
        if (this.ae) {
            d += a.b.length;
        }
        this.av(d, b);
        a.h++;
        a.l++;
        a.g++;
        if (c) {
            this.p.x(a, 1, 1);
        }
        return d;
    };
    RearrangedListView.prototype.au = function (a, b, c) {
        if (this.ae) {
            b += a.b.length;
        }
        this.av(b, c);
        a.h++;
        a.l++;
        a.g++;
        this.p.x(a, 1, 1);
        return b;
    };
    RearrangedListView.prototype.av = function (a, b) {
        var c = new DataSourceSpecialRow();
        c.rowType = 3;
        c.targetRow = b;
        this.an.insert1(a, c);
        return a + 1;
    };
    RearrangedListView.prototype.aw = function (a, b) {
        var c = -1;
        for (var d = 0; d < this.am.count; d++) {
            if (this.j.i(b, this.am._inner[d]) == 0) {
                c = d;
                break;
            }
        }
        return this.ax(a, b, c);
    };
    RearrangedListView.prototype.ax = function (a, b, c) {
        var d = -1;
        if (c != -1) {
            var e = a.n;
            var f = e + a.h;
            var g = false;
            for (var h = e; h < f; h++) {
                var i = this.ae ? h + 1 : h;
                var j = typeCast(DataSourceSpecialRow.$, this.an._inner[i]);
                if (this.aj != null) {
                    var k = this.aj.compare(b, j.targetRow);
                    if (k == 0 || k < 0) {
                        d = this.au(a, h, b);
                        g = true;
                        break;
                    }
                }
                else {
                    var l = -1;
                    for (var m = 0; m < this.am.count; m++) {
                        if (this.j.i(j.targetRow, this.am._inner[m]) == 0) {
                            l = m;
                            break;
                        }
                    }
                    if (l > c) {
                        d = this.au(a, h, b);
                        g = true;
                        break;
                    }
                }
            }
            if (!g) {
                d = this.at(a, b, true);
                g = true;
            }
        }
        return d;
    };
    Object.defineProperty(RearrangedListView.prototype, "ao", {
        get: function () {
            return this.p.p;
        },
        enumerable: true,
        configurable: true
    });
    RearrangedListView.prototype.a9 = function () {
        if (this.an.count == 0) {
            return;
        }
        var a = this.b();
        var b = this.c();
        var c = null;
        var d = null;
        for (var e = 0; e < this.an.count; e++) {
            var f = this.an._inner[e];
            if (f == null) {
                continue;
            }
            if (c == null) {
                this.bj(e, f, b);
                var g = this.p.p._inner[this.p.p.count - 1];
                var h = this.as(g, e, a, b);
                e += h;
                if (this.ae && !g.f) {
                    this.an.removeAt(e);
                    e--;
                }
                g.k++;
                d = f;
                this.a3(g);
                this.a2(g, f);
            }
            else {
                if (this.ak.compare(c, f) != 0) {
                    var i = this.p.p._inner[this.p.p.count - 1];
                    e += this.ar(e, d, a, b);
                    this.a4(e - 1, d, a);
                    this.a5(i);
                    this.bj(e, f, b);
                    var j = this.p.p._inner[this.p.p.count - 1];
                    e += this.as(j, e, a, b);
                    if (this.ae && !j.f) {
                        this.an.removeAt(e);
                        e--;
                    }
                    j.k++;
                    d = f;
                    this.a3(j);
                    this.a2(j, f);
                }
                else {
                    var k = this.p.p._inner[this.p.p.count - 1];
                    if (this.ae && !k.f) {
                        this.an.removeAt(e);
                        e--;
                    }
                    k.k++;
                    this.a2(k, f);
                }
            }
            if (e == this.an.count - 1) {
                var l = this.p.p._inner[this.p.p.count - 1];
                e += this.ar(e + 1, d, a, b);
                this.a4(e, d, a);
                this.a5(l);
                l.k++;
            }
            c = f;
        }
    };
    RearrangedListView.prototype.z = function (a) {
        if (!this.g.isSectionCollapsable) {
            return true;
        }
        return this.ah.containsKey(a) ? this.ah.item(a) : this.ab;
    };
    RearrangedListView.prototype.as = function (a, b, c, d) {
        var e = 0;
        if (this.ae) {
            var f = this.an._inner[b];
            if (this.g.sectionHeaderDisplayMode == 0) {
                this.a7(b, 0, f, c, d);
                this.p.u();
                e++;
                if (a != null) {
                    var g = this.ay(a);
                    a.a[0] = g;
                    a.b[0] = this.z(g);
                }
            }
            else if (this.g.sectionHeaderDisplayMode == 1) {
                for (var h = 0; h < d.length; h++) {
                    e++;
                    this.a7(b + h, h, f, c, [d[h]]);
                    this.p.u();
                    if (a != null) {
                        var i = this.az(a, h);
                        a.a[h] = i;
                        a.b[h] = this.z(i);
                        if (!a.b[h]) {
                            break;
                        }
                    }
                    else if (!this.ab) {
                        break;
                    }
                }
            }
        }
        return e;
    };
    RearrangedListView.prototype.ar = function (a, b, c, d) {
        var e = 0;
        if (this.ad) {
            if (this.g.sectionHeaderDisplayMode == 0) {
                this.a6(a, 0, b, c, d);
                e++;
            }
            else if (this.g.sectionHeaderDisplayMode == 1) {
                for (var f = d.length - 1; f >= 0; f--) {
                    if (this.p.p._inner[this.p.p.count - 1].b[f]) {
                        this.a6(a, f, b, c, [d[f]]);
                        a++;
                        e++;
                    }
                }
            }
        }
        return e;
    };
    RearrangedListView.prototype.b = function () {
        var a = new Array(this.s.k.count + this.t.k.count);
        var b = 0;
        for (var c = 0; c < this.s.k.count; c++) {
            a[b] = this.s.k.item(c).propertyName;
            b++;
        }
        for (var d = 0; d < this.t.k.count; d++) {
            a[b] = this.t.k.item(d).propertyName;
            b++;
        }
        return a;
    };
    RearrangedListView.prototype.c = function () {
        var a = new Array(this.s.k.count);
        var b = 0;
        for (var c = 0; c < this.s.k.count; c++) {
            a[b] = this.s.k.item(c).propertyName;
            b++;
        }
        return a;
    };
    RearrangedListView.prototype.a4 = function (a, b, c) {
        this.p.t(a);
    };
    RearrangedListView.prototype.bi = function (a) {
        this.am = new List$1(Array_$type, 0);
        for (var b = 0; b < a.count; b++) {
            this.am.add(a._inner[b]);
        }
    };
    RearrangedListView.prototype.bj = function (a, b, c) {
        this.p.aa(a, this.g, b, c, this.g.sectionHeaderDisplayMode, this.ab);
    };
    RearrangedListView.prototype.a6 = function (a, b, c, d, e) {
        var f = new DataSourceSpecialRow();
        f.level = b;
        for (var g = 0; g < d.length; g++) {
            f.setValue(d[g], this.g.getItemProperty(c, d[g]));
            if (g < e.length) {
                f.setSectionValue(e[g], this.g.getItemProperty(c, e[g]));
            }
        }
        f.rowType = 2;
        this.an.insert1(a, f);
    };
    RearrangedListView.prototype.a7 = function (a, b, c, d, e) {
        var f = new DataSourceSpecialRow();
        f.level = b;
        for (var g = 0; g < d.length; g++) {
            f.setValue(d[g], this.g.getItemProperty(c, d[g]));
            if (g < e.length) {
                f.setSectionValue(e[g], this.g.getItemProperty(c, e[g]));
            }
        }
        f.rowType = 1;
        f.summaryResults = new Array(this.u.k.count);
        this.an.insert1(a, f);
    };
    RearrangedListView.prototype.ba = function () {
        this.an.clear();
        if (this.r != null) {
            this.r.notifyClearItems();
        }
    };
    RearrangedListView.prototype.notifySetItem = function (a, b, c) {
        if (this.aj != null) {
            this.bf(b);
            this.a0(c);
        }
        else {
            this.bg(a, b);
            this.a1(a, c);
        }
        this.bo(a, b, c);
    };
    RearrangedListView.prototype.a1 = function (a, b) {
        if (!this.ag(b)) {
            return;
        }
        var c = this.aq(a);
        this.bb(c, b);
    };
    RearrangedListView.prototype.bb = function (a, b) {
        this.a8(a, b);
        this.bl(a, b);
        this.bm(a, b);
    };
    RearrangedListView.prototype.a8 = function (a, b) {
        if (this.v) {
            var c = this.p.d(a);
            if (c == null) {
                c = this.p.c();
            }
            if (c.n == a) {
                if (!this.w(c, b, this.an._inner[a])) {
                    c = this.p.d(a - 1);
                    if (c == null) {
                        c = this.p.b();
                        c.n++;
                    }
                }
                else {
                    var d = this.ae ? 1 : 0;
                    a += d + c.h;
                }
            }
            if (!c.f) {
                return;
            }
            c.l++;
            this.p.x(c, 1, 0);
        }
        this.an.insert1(a, b);
        if (this.r != null) {
            this.r.notifyInsertItem(a, b);
        }
    };
    RearrangedListView.prototype.bl = function (a, b) {
        var c = null;
        if (this.v) {
            c = this.p.d(a);
        }
        else {
            c = this.p.p._inner[0];
        }
        if (c == null) {
            if (this.v && a == 0) {
                c = this.p.b();
            }
            else {
                c = this.p.c();
            }
        }
        if (this.w(c, b, this.an._inner[c.n])) {
            c.k++;
            if (this.af && c.f) {
                var d = this.aw(c, b);
                if (d != -1 && this.r != null) {
                    this.r.notifyInsertItem(d, this.an._inner[d]);
                }
            }
        }
        else {
            var e = this.p.d(a - 1);
            if (e != null && this.w(e, b, this.an._inner[e.n])) {
                e.k++;
                if (this.af && e.f) {
                    var f = this.aw(e, b);
                    if (f != -1 && this.r != null) {
                        this.r.notifyInsertItem(f, this.an._inner[f]);
                    }
                }
            }
            else {
                var g = false;
                if (a <= c.n) {
                    g = true;
                }
                var h = 0;
                var i = null;
                var j = null;
                var k = this.b();
                var l = this.c();
                var m = new SectionData();
                m.n = a;
                m.l = a;
                m.c = l;
                m.d = this.d(l, b);
                m.m = g ? c.m : c.m + 1;
                m.o(this.g.sectionHeaderDisplayMode, this.ab);
                this.p.v(m.m, m);
                if (this.ae) {
                    var n = this.as(m, a, k, l);
                    m.l += n;
                    h += n;
                    i = this.an._inner[a];
                    c.l--;
                }
                if (m.f) {
                    if (this.af) {
                        for (var o = 0; o < this.am.count; o++) {
                            if (this.j.i(b, this.am._inner[o]) == 0) {
                                this.at(m, b, false);
                                h++;
                                break;
                            }
                        }
                    }
                    m.k++;
                }
                else {
                    this.an.removeAt(a + h);
                    m.l--;
                    h--;
                }
                if (this.ad) {
                    var p = this.ar(a + (h - 1), b, k, l);
                    if (p > 1) {
                        m.l += p;
                    }
                    h += p;
                    j = this.an._inner[a + (h - 1)];
                }
                this.p.y(m, h, h);
                if (this.r != null) {
                    if (i != null) {
                        this.r.notifyInsertItem(a, i);
                    }
                    if (j != null) {
                        this.r.notifyInsertItem(a + (h - 1), j);
                    }
                }
            }
        }
    };
    RearrangedListView.prototype.w = function (a, b, c) {
        if (this.s.k.count == 0) {
            return true;
        }
        if (this.ak.compare(b, c) == 0) {
            return true;
        }
        return false;
    };
    RearrangedListView.prototype.d = function (a, b) {
        var c = new Array(a.length);
        for (var d = 0; d < c.length; d++) {
            c[d] = this.g.getItemProperty(b, a[d]);
        }
        return c;
    };
    RearrangedListView.prototype.ac = function (a, b) {
        if (a == null && b == null) {
            return true;
        }
        if (a == null && b != null) {
            return false;
        }
        if (a != null && b == null) {
            return false;
        }
        return Base.equalsStatic(a, b);
    };
    RearrangedListView.prototype.aq = function (a) {
        if (this.s.k.count == 0 && this.am.count == 0 && this.k == null && this.h.actualCount == this.an.count) {
            return a;
        }
        var b = 0;
        var c = 0;
        var d = this.an.count;
        while (b < a && c < d) {
            while (c < d) {
                var e = this.an._inner[c];
                var f = false;
                var viewItem_ = e;
                if ((viewItem_.$$isSpecialRow !== undefined)) {
                    f = true;
                }
                if (!f) {
                    break;
                }
                c++;
            }
            var g = this.h.getItemAtIndex(b);
            if (this.ac(g, this.an._inner[c])) {
                c++;
                b++;
            }
            else {
                b++;
            }
        }
        return c;
    };
    RearrangedListView.prototype.bg = function (a, b) {
        var c = this.aq(a);
        if (c < this.an.count) {
            if (this.ac(b, this.an._inner[c])) {
                this.bc(c, b);
            }
        }
    };
    RearrangedListView.prototype.bc = function (a, b) {
        this.an.removeAt(a);
        if (this.r != null) {
            this.r.notifyRemoveItem(a, b);
        }
        var c = this.o(a, b);
        this.bn(a, b, c);
    };
    RearrangedListView.prototype.o = function (a, b) {
        var c = null;
        if (this.v) {
            c = this.p.d(a);
        }
        else {
            c = this.p.p._inner[0];
        }
        if (c != null) {
            c.k--;
            if (c.f) {
                c.l--;
                this.p.x(c, -1, 0);
                if (this.af) {
                    for (var d = 0; d < c.h; d++) {
                        var e = 0;
                        if (this.ae) {
                            e = 1;
                        }
                        var f = this.an._inner[c.n + d + e];
                        var g = null;
                        var item_ = f;
                        if ((item_ != null && item_.$$isSpecialRow !== undefined)) {
                            g = f;
                        }
                        if (g != null) {
                            if (g.targetRow == b) {
                                this.an.removeAt(c.n + d + e);
                                c.l--;
                                c.h--;
                                c.g--;
                                this.p.x(c, -1, -1);
                                if (this.r != null) {
                                    this.r.notifyRemoveItem(c.n + d + e, g);
                                }
                            }
                        }
                    }
                }
            }
            if (this.v && c.k <= 0 && this.x(c)) {
                var h = 0;
                var i = null;
                var j = null;
                if (this.ae) {
                    i = this.an._inner[c.n];
                    for (var k = 0; k < c.b.length; k++) {
                        this.an.removeAt(c.n);
                        h++;
                        if (!c.b[k]) {
                            break;
                        }
                    }
                }
                if (this.ad) {
                    j = this.an._inner[c.l];
                    for (var l = 0; l < c.b.length; l++) {
                        if (!c.b[l]) {
                            break;
                        }
                        this.an.removeAt(c.l);
                        h++;
                        c.l--;
                    }
                }
                this.p.z(c.m);
                this.p.w(c, -h, -h);
                if (this.r != null) {
                    if (i != null) {
                        this.r.notifyRemoveItem(c.n, i);
                    }
                    if (j != null) {
                        this.r.notifyRemoveItem(c.l, j);
                    }
                }
                c = null;
            }
        }
        return c;
    };
    RearrangedListView.prototype.x = function (a) {
        var b = (a.l - a.n) + 1;
        if (b > a.b.length + 1) {
            return false;
        }
        for (var c = a.n; c <= a.l; c++) {
            if (this.g.getRowType(c) == 0) {
                return false;
            }
        }
        return true;
    };
    RearrangedListView.prototype.a0 = function (a) {
        if (this.ag(a)) {
            var b = this.an.g(a, this.aj);
            if (b < 0) {
                b = ~b;
            }
            this.bb(b, a);
        }
    };
    RearrangedListView.prototype.bd = function (a) {
        this.am.add(a);
        var b = this.g.indexOfKey(a);
        var c = this.g.getItemAtIndex(b);
        if (b == -1) {
            return;
        }
        if (this.af) {
            var d = this.p.d(b);
            var e = this.aw(d, this.an._inner[b]);
            if (this.r != null) {
                this.r.notifyInsertItem(e, this.an._inner[e]);
            }
        }
    };
    RearrangedListView.prototype.bk = function (a) {
        if (this.am.remove(a)) {
            var b = this.g.indexOfKey(a);
            var c = this.g.getItemAtIndex(b);
            if (b == -1) {
                return;
            }
            var d = this.p.d(b);
            var e = d.n;
            if (this.ae) {
                e += d.b.length;
            }
            if (this.af) {
                var f = this.m(e, this.an._inner[b], d.h, this.b());
                if (f != null) {
                    d.l--;
                    d.h--;
                    d.g--;
                    this.p.x(d, -1, -1);
                    if (this.r != null) {
                        this.r.notifyRemoveItem(f.a, f.item);
                    }
                }
            }
        }
    };
    RearrangedListView.prototype.m = function (a, b, c, d) {
        var _loop_1 = function (e) {
            if (typeCast(DataSourceSpecialRow.$, this_1.an._inner[e]) !== null) {
                var f_1 = this_1.an._inner[e];
                if (f_1.rowType == 3) {
                    var g = f_1.targetRow;
                    if (g == b) {
                        this_1.an.removeAt(e);
                        return { value: ((function () {
                                var $ret = new RemovedShiftedRowInfo();
                                $ret.item = f_1;
                                $ret.a = e;
                                return $ret;
                            })()) };
                    }
                }
            }
        };
        var this_1 = this;
        for (var e = a + (c - 1); e >= a; e--) {
            var state_1 = _loop_1(e);
            if (typeof state_1 === "object")
                return state_1.value;
        }
        return null;
    };
    RearrangedListView.prototype.bf = function (a) {
        var b = this.an.g(a, this.aj);
        if (b < 0) {
            b = ~b;
        }
        if (b >= 0) {
            var c = this.an.count;
            var d = 0;
            while (d < c) {
                var e = b - d;
                var f = b + d;
                if (e < 0 && f > c - 1) {
                    break;
                }
                if (e >= 0 && e < c) {
                    if (this.an._inner[e] != null && Base.equalsStatic(this.an._inner[e], a)) {
                        b = e;
                        break;
                    }
                }
                if (f < c) {
                    if (this.an._inner[f] != null && Base.equalsStatic(this.an._inner[f], a)) {
                        b = f;
                        break;
                    }
                }
                d++;
            }
            if (b < 0 || b > c - 1) {
                return;
            }
            var g = this.an._inner[b];
            if (g == null && a == null) {
                this.an.removeAt(b);
                this.q.notifyRemoveItem(b, a);
                var h = this.o(b, a);
                this.bn(b, a, h);
                return;
            }
            if (g == null) {
                return;
            }
            if (Base.equalsStatic(g, a)) {
                this.an.removeAt(b);
                this.q.notifyRemoveItem(b, a);
                var i = this.o(b, a);
                this.bn(b, a, i);
            }
            else {
                this.o(b, a);
            }
        }
        else {
            var j = this.an.count;
            for (var k = 0; k < j; k++) {
                var l = this.an._inner[k];
                if (l == null && a == null) {
                    this.an.removeAt(k);
                    break;
                }
                if (l == null) {
                    continue;
                }
                if (Base.equalsStatic(l, a)) {
                    this.an.removeAt(k);
                    this.q.notifyRemoveItem(k, a);
                    var m = this.o(k, a);
                    this.bn(k, a, m);
                    break;
                }
            }
        }
    };
    RearrangedListView.prototype.ag = function (a) {
        if (this.k == null) {
            return true;
        }
        return this.k(a);
    };
    RearrangedListView.prototype.notifyClearItems = function () {
    };
    RearrangedListView.prototype.notifyInsertItem = function (a, b) {
        if (this.aj != null) {
            this.a0(b);
        }
        else {
            this.a1(a, b);
        }
    };
    RearrangedListView.prototype.notifyRemoveItem = function (a, b) {
        if (this.aj != null) {
            this.bf(b);
        }
        else {
            this.bg(a, b);
        }
    };
    RearrangedListView.prototype.bh = function (a, b) {
        var c = this.p.d(a);
        if (c == null) {
            c = this.p.c();
        }
        for (var d = 0; d < c.b.length; d++) {
            if (a == c.n + d) {
                var e = c.a[d];
                if (this.ah.containsKey(e) && this.ah.item(e) != b) {
                    if (b == this.ab) {
                        this.ah.removeItem(e);
                    }
                    else {
                        this.ah.item(e, b);
                    }
                    this.g.queueAutoRefresh();
                }
                else if (b != this.ab) {
                    this.ah.addItem(e, b);
                    this.g.queueAutoRefresh();
                }
            }
        }
    };
    RearrangedListView.prototype.y = function (a) {
        var b = this.p.d(a);
        if (b == null) {
            b = this.p.c();
        }
        for (var c = 0; c < b.b.length; c++) {
            if (a == b.n + c) {
                return b.b[c];
            }
        }
        return true;
    };
    RearrangedListView.prototype.ay = function (a) {
        var b = "";
        for (var c = 0; c < a.c.length; c++) {
            b += a.c[c] + ":" + a.d[c];
        }
        return b;
    };
    RearrangedListView.prototype.az = function (a, b) {
        if (this.g.sectionHeaderDisplayMode == 0) {
            return this.ay(a);
        }
        var c = "";
        for (var d = 0; d < a.c.length; d++) {
            c += a.c[d] + ":" + a.d[d] + ":";
        }
        c += b;
        return c;
    };
    RearrangedListView.prototype.a3 = function (a) {
        if (this.f == 3 || (a != null && this.f == 1) || (a == null && this.f == 2)) {
            return;
        }
        if (a == null && (this.a == null || this.a.length != this.u.k.count)) {
            this.a = new Array(this.u.k.count);
        }
        for (var b = 0; b < this.u.k.count; b++) {
            var c = this.u.k.item(b);
            c.calculator.beginCalculation(this.g, c.propertyName);
        }
    };
    RearrangedListView.prototype.a5 = function (a) {
        if (this.f == 3 || (a != null && this.f == 1) || (a == null && this.f == 2)) {
            return;
        }
        for (var b = 0; b < this.u.k.count; b++) {
            var c = this.u.k.item(b);
            var d = c.calculator.endCalculation();
            if (a != null) {
                a.e[b] = d;
                if (this.g.sectionHeaderDisplayMode == 1) {
                    for (var e = 0; e < a.b.length; e++) {
                        var f = this.an._inner[a.n + e];
                        if (f != null) {
                            f.summaryResults[b] = d;
                        }
                        if (!a.b[e]) {
                            break;
                        }
                    }
                }
                else {
                    var g = this.an._inner[a.n];
                    if (g != null) {
                        g.summaryResults[b] = d;
                    }
                }
            }
            else {
                this.a[b] = d;
            }
        }
    };
    RearrangedListView.prototype.a2 = function (a, b) {
        if (this.f == 3 || (a != null && this.f == 1) || (a == null && this.f == 2)) {
            return;
        }
        if (b != null) {
            for (var c = 0; c < this.u.k.count; c++) {
                var d = this.u.k.item(c);
                d.calculator.aggregate(b);
            }
        }
    };
    RearrangedListView.prototype.bm = function (a, b) {
        var c = this.n(a);
        if (c == null) {
            c = this.p.c();
        }
        var d = typeCast(DataSourceSpecialRow.$, this.an._inner[c.n]);
        var e = false;
        for (var f = 0; f < this.u.k.count; f++) {
            var g = this.u.k.item(f);
            this.a[f] = g.calculator.itemAdded(b, this.a[f], this.g.actualCount);
            c.e[f] = g.calculator.itemAdded(b, c.e[f], c.k);
            if (d != null) {
                d.summaryResults[f] = c.e[f];
            }
            if (g.calculator.isSummaryDirty) {
                e = true;
            }
        }
        if (e) {
            this.g.queueAutoRefresh();
        }
    };
    RearrangedListView.prototype.bn = function (a, b, c) {
        var d = null;
        if (c != null) {
            d = typeCast(DataSourceSpecialRow.$, this.an._inner[c.n]);
        }
        var e = false;
        for (var f = 0; f < this.u.k.count; f++) {
            var g = this.u.k.item(f);
            this.a[f] = g.calculator.itemRemoved(b, this.a[f], this.g.actualCount);
            if (c != null) {
                c.e[f] = g.calculator.itemRemoved(b, c.e[f], c.k);
                if (d != null) {
                    d.summaryResults[f] = c.e[f];
                }
            }
            if (g.calculator.isSummaryDirty) {
                e = true;
            }
        }
        if (e) {
            this.g.queueAutoRefresh();
        }
    };
    RearrangedListView.prototype.bo = function (a, b, c) {
        var d = this.n(a);
        if (d == null) {
            d = this.p.c();
        }
        var e = typeCast(DataSourceSpecialRow.$, this.an._inner[d.n]);
        var f = false;
        for (var g = 0; g < this.u.k.count; g++) {
            var h = this.u.k.item(g);
            this.a[g] = h.calculator.itemChanged(c, b, this.a[g], this.g.actualCount);
            d.e[g] = h.calculator.itemChanged(c, b, d.e[g], d.k);
            if (e != null) {
                e.summaryResults[g] = d.e[g];
            }
            if (h.calculator.isSummaryDirty) {
                f = true;
            }
        }
        if (f) {
            this.g.queueAutoRefresh();
        }
    };
    RearrangedListView.$t = markType(RearrangedListView, 'RearrangedListView', Base.$, [ISupportsDataChangeNotifications_$type]);
    return RearrangedListView;
}(Base));
export { RearrangedListView };
