/*
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 { Type, Base, ICollection$1_$type, NotSupportedException, fromEnum, toNullable, Number_$type, toEnum, markType, getEnumerator, typeCast } from "igniteui-angular-core";
import { List$1 } from "igniteui-angular-core";
import { LoadOnDemandTree_ChangeHistoryItem$1 } from "./LoadOnDemandTree_ChangeHistoryItem$1";
import { LoadOnDemandTree_ChangeHistoryItemAdd$1 } from "./LoadOnDemandTree_ChangeHistoryItemAdd$1";
import { LoadOnDemandTree_ChangeHistoryItemRemove$1 } from "./LoadOnDemandTree_ChangeHistoryItemRemove$1";
import { Stack$1 } from "igniteui-angular-core";
import { nullableLessThan, nullableGreaterThan, nullableEquals, nullableGreaterThanOrEqual } from "igniteui-angular-core";
/**
 * @hidden
 */
export var IBinaryTreeNodeOwner$1_$type = new Type(null, 'IBinaryTreeNodeOwner$1');
/**
 * @hidden
 */
var LoadOnDemandTree$1 = /** @class */ (function (_super) {
    tslib_1.__extends(LoadOnDemandTree$1, _super);
    function LoadOnDemandTree$1($t, a, b) {
        var _this = _super.call(this) || this;
        _this.$t = null;
        _this._a = null;
        _this._aj = 0;
        _this._h = null;
        _this._i = null;
        _this._ae = 0;
        _this._n = false;
        _this._q = 0;
        _this._r = 0;
        _this._s = 0;
        _this._ab = null;
        _this._aa = null;
        _this.$t = $t;
        _this.$type = _this.$type.specialize(_this.$t);
        _this._a = a;
        _this._ae = b;
        return _this;
    }
    LoadOnDemandTree$1.prototype.recalculateHeight = function () {
        return false;
    };
    LoadOnDemandTree$1.prototype.verifyBalanceOfChild = function (a) {
        var _this = this;
        return ((function () { var b = LoadOnDemandTree_BinaryTreeNode$1.j(_this.$t, _this._h); _this._h = b.p0; return b.ret; })());
    };
    LoadOnDemandTree$1.prototype.add = function (a) {
    };
    LoadOnDemandTree$1.prototype.clear = function () {
        this._h = null;
        this._aj = 0;
    };
    LoadOnDemandTree$1.prototype.contains = function (a) {
        var e_1, _c;
        try {
            for (var _t = tslib_1.__values(fromEnum(this)), _v = _t.next(); !_v.done; _v = _t.next()) {
                var b = _v.value;
                if (Base.equalsStatic(b, a)) {
                    return true;
                }
            }
        }
        catch (e_1_1) { e_1 = { error: e_1_1 }; }
        finally {
            try {
                if (_v && !_v.done && (_c = _t.return)) _c.call(_t);
            }
            finally { if (e_1) throw e_1.error; }
        }
        return false;
    };
    LoadOnDemandTree$1.prototype.copyTo = function (a, b) {
        var e_2, _c;
        try {
            for (var _t = tslib_1.__values(fromEnum(this)), _v = _t.next(); !_v.done; _v = _t.next()) {
                var c = _v.value;
                a[b++] = c;
            }
        }
        catch (e_2_1) { e_2 = { error: e_2_1 }; }
        finally {
            try {
                if (_v && !_v.done && (_c = _t.return)) _c.call(_t);
            }
            finally { if (e_2) throw e_2.error; }
        }
    };
    Object.defineProperty(LoadOnDemandTree$1.prototype, "isReadOnly", {
        get: function () {
            return false;
        },
        enumerable: true,
        configurable: true
    });
    LoadOnDemandTree$1.prototype.remove = function (a) {
        return false;
    };
    LoadOnDemandTree$1.prototype.getEnumerator = function () {
        return getEnumerator(this._x(0, 0x7FFFFFFF, true));
    };
    LoadOnDemandTree$1.prototype.__x = function (a, b, c) {
        var d, e, f, g, h, i, j, _c, _t, k, l, m, n, o, p, q, r, s, t, u, e_3_1;
        var e_3, _v;
        return tslib_1.__generator(this, function (_0) {
            switch (_0.label) {
                case 0:
                    b = Math.min(b, this._ae - 1);
                    d = null;
                    e = null;
                    f = -1;
                    g = a;
                    h = b;
                    if (this._aa != null && this._aa.count > 0) {
                        g = this._ah(a);
                        h = this._ah(b);
                        i = this._w(a, b, c);
                        d = getEnumerator(i);
                        if (d.moveNext()) {
                            e = d.current;
                            f = this._a._getItemIndex$i(e);
                        }
                    }
                    j = c ? function (k, l) { return k < l; } : function (k, l) { return k > l; };
                    _0.label = 1;
                case 1:
                    _0.trys.push([1, 15, 16, 17]);
                    _c = tslib_1.__values(fromEnum(this._z(g, h, c))), _t = _c.next();
                    _0.label = 2;
                case 2:
                    if (!!_t.done) return [3 /*break*/, 14];
                    k = _t.value;
                    l = k.b;
                    m = 0;
                    if (k.l < g) {
                        m = g - k.l;
                    }
                    n = Math.min(h - k.l, 32 - 1);
                    o = void 0;
                    p = void 0;
                    q = void 0;
                    if (c) {
                        o = m;
                        p = n + 1;
                        q = 1;
                    }
                    else {
                        o = n;
                        p = m - 1;
                        q = -1;
                    }
                    r = o;
                    _0.label = 3;
                case 3:
                    if (!(r != p)) return [3 /*break*/, 13];
                    s = l[r];
                    if (!(s != null)) return [3 /*break*/, 12];
                    t = toNullable(Number_$type, null);
                    if (!(this._ab != null)) return [3 /*break*/, 6];
                    t = toNullable(Number_$type, this._a._getItemIndex$i(s));
                    if (t.value < 0) {
                        l[r] = null;
                        return [3 /*break*/, 12];
                    }
                    _0.label = 4;
                case 4:
                    if (!(e != null && j(f, t.value))) return [3 /*break*/, 6];
                    if (f < a || f > b) {
                        return [3 /*break*/, 6];
                    }
                    return [4 /*yield*/, e];
                case 5:
                    _0.sent();
                    if (!d.moveNext()) {
                        e = null;
                        return [3 /*break*/, 6];
                    }
                    e = d.current;
                    f = this._a._getItemIndex$i(e);
                    return [3 /*break*/, 4];
                case 6:
                    u = !(nullableLessThan(t, a) || nullableGreaterThan(t, b));
                    if (!nullableEquals(t, null)) return [3 /*break*/, 9];
                    if (!u) return [3 /*break*/, 8];
                    return [4 /*yield*/, s];
                case 7:
                    _0.sent();
                    _0.label = 8;
                case 8: return [3 /*break*/, 12];
                case 9:
                    if (!u) return [3 /*break*/, 11];
                    return [4 /*yield*/, s];
                case 10:
                    _0.sent();
                    return [3 /*break*/, 12];
                case 11:
                    if (nullableLessThan(t, 0) || nullableGreaterThanOrEqual(t, this._ae)) {
                        l[r] = null;
                        this._a._initializeItemIndex$i(s, -1, this._n);
                    }
                    _0.label = 12;
                case 12:
                    r += q;
                    return [3 /*break*/, 3];
                case 13:
                    _t = _c.next();
                    return [3 /*break*/, 2];
                case 14: return [3 /*break*/, 17];
                case 15:
                    e_3_1 = _0.sent();
                    e_3 = { error: e_3_1 };
                    return [3 /*break*/, 17];
                case 16:
                    try {
                        if (_t && !_t.done && (_v = _c.return)) _v.call(_c);
                    }
                    finally { if (e_3) throw e_3.error; }
                    return [7 /*endfinally*/];
                case 17:
                    if (!(e != null)) return [3 /*break*/, 20];
                    f = this._a._getItemIndex$i(e);
                    if (!(f >= 0)) return [3 /*break*/, 19];
                    if (f < a || f > b) {
                        return [3 /*break*/, 20];
                    }
                    return [4 /*yield*/, e];
                case 18:
                    _0.sent();
                    _0.label = 19;
                case 19:
                    if (!d.moveNext()) {
                        return [3 /*break*/, 20];
                    }
                    e = d.current;
                    return [3 /*break*/, 17];
                case 20: return [2 /*return*/];
            }
        });
    };
    LoadOnDemandTree$1.prototype._x = function (a, b, c) {
        var _this = this;
        return toEnum(function () { return _this.__x(a, b, c); });
    };
    LoadOnDemandTree$1.prototype.getEnumeratorObject = function () {
        return getEnumerator(this);
    };
    LoadOnDemandTree$1.prototype._af = function (a, b) {
        if (b == this._s) {
            b = 0;
        }
        if (b == this._q || a < 0) {
            return a;
        }
        var c;
        if (this._r > b) {
            c = b;
        }
        else {
            c = b - this._r;
        }
        var d = this._ad;
        var e = d.count;
        var f = a;
        for (var g = c; g < e; g++) {
            var h = d._inner[g];
            if (h.a) {
                if (h.f <= f) {
                    f += h.c;
                }
            }
            else {
                var i = h.f + h.c - 1;
                if (i < f) {
                    f -= h.c;
                }
                else {
                    if (h.f <= f && i >= f) {
                        return -1;
                    }
                }
            }
        }
        return f;
    };
    LoadOnDemandTree$1.prototype._ah = function (a) {
        var b = 0;
        var c = this._aa != null ? this._aa.count : 0;
        for (var d = 0; d < c; d++) {
            var e = this._aa._inner[d];
            if (e.n > a) {
                break;
            }
            if (e.e) {
                if (e.l >= a) {
                    b += e.n - a;
                    continue;
                }
                b -= e.k;
            }
            else {
                b += e.k;
            }
        }
        return a + b;
    };
    LoadOnDemandTree$1.prototype._ai = function (a) {
        var b = a;
        var c = this._aa != null ? this._aa.count : 0;
        for (var d = 0; d < c; d++) {
            var e = this._aa._inner[d];
            if (e.n > b) {
                break;
            }
            if (e.e) {
                b += e.k;
            }
            else {
                b -= e.k;
            }
        }
        return b;
    };
    LoadOnDemandTree$1.prototype._j = function (a) {
        return this._k(a, 0, a.c);
    };
    LoadOnDemandTree$1.prototype._k = function (a, b, c) {
        return new LoadOnDemandTree_ChangeAdjustmentPoint$1(this.$t, this, a, b, c);
    };
    LoadOnDemandTree$1.prototype._l = function (a, b) {
        return new LoadOnDemandTree_ChangeHistoryItemAdd$1(this.$t, a, b);
    };
    LoadOnDemandTree$1.prototype._m = function (a, b) {
        return new LoadOnDemandTree_ChangeHistoryItemRemove$1(this.$t, a, b);
    };
    LoadOnDemandTree$1.prototype._f = function (a, b) {
        return new LoadOnDemandTree_BinaryTreeNode$1(this.$t, a, b, this);
    };
    LoadOnDemandTree$1.prototype.__w = function (a, b, c) {
        var d, e, f, g, h, i, j, k;
        return tslib_1.__generator(this, function (_c) {
            switch (_c.label) {
                case 0:
                    if (!(this._aa != null && this._aa.count > 0)) return [3 /*break*/, 6];
                    d = void 0;
                    e = void 0;
                    f = void 0;
                    g = void 0;
                    if (c) {
                        d = 0;
                        f = this._aa.count;
                        e = 1;
                    }
                    else {
                        d = this._aa.count - 1;
                        f = -1;
                        e = -1;
                    }
                    g = this._aa._inner[d];
                    _c.label = 1;
                case 1:
                    if (!(g != null)) return [3 /*break*/, 6];
                    if (!(g.d && g.f(a, b))) return [3 /*break*/, 5];
                    h = void 0;
                    i = void 0;
                    if (c) {
                        h = Math.max(0, a);
                        i = Math.min(g.l, b) + 1;
                    }
                    else {
                        h = Math.min(g.l, b);
                        i = Math.max(0, a) - 1;
                    }
                    j = h;
                    _c.label = 2;
                case 2:
                    if (!(j != i)) return [3 /*break*/, 5];
                    if (!g.f(j, j)) return [3 /*break*/, 4];
                    k = g.item(j - g.n);
                    if (!(k != null && this._a._getItemIndex$i(k) >= 0)) return [3 /*break*/, 4];
                    return [4 /*yield*/, k];
                case 3:
                    _c.sent();
                    _c.label = 4;
                case 4:
                    j += e;
                    return [3 /*break*/, 2];
                case 5:
                    d += e;
                    if (d == f) {
                        g = null;
                    }
                    else {
                        g = this._aa._inner[d];
                    }
                    return [3 /*break*/, 1];
                case 6: return [2 /*return*/];
            }
        });
    };
    LoadOnDemandTree$1.prototype._w = function (a, b, c) {
        var _this = this;
        return toEnum(function () { return _this.__w(a, b, c); });
    };
    LoadOnDemandTree$1.prototype._y = function () {
        return this._z(0, 0x7FFFFFFF, true);
    };
    LoadOnDemandTree$1.prototype.__z = function (a, b, c) {
        var d, e, f;
        return tslib_1.__generator(this, function (_c) {
            switch (_c.label) {
                case 0:
                    d = new Stack$1(LoadOnDemandTree_BinaryTreeNode$1.$.specialize(this.$t));
                    e = this._h;
                    _c.label = 1;
                case 1:
                    if (!true) return [3 /*break*/, 4];
                    LoadOnDemandTree$1._aq(this.$t, e, d, c);
                    if (d.f == 0) {
                        return [2 /*return*/, undefined];
                    }
                    f = d.e();
                    if (!(a < (f.l + 32) && f.l <= b)) return [3 /*break*/, 3];
                    return [4 /*yield*/, f];
                case 2:
                    _c.sent();
                    _c.label = 3;
                case 3:
                    if (c) {
                        e = f.i;
                    }
                    else {
                        e = f.g;
                    }
                    return [3 /*break*/, 1];
                case 4: return [2 /*return*/];
            }
        });
    };
    LoadOnDemandTree$1.prototype._z = function (a, b, c) {
        var _this = this;
        return toEnum(function () { return _this.__z(a, b, c); });
    };
    LoadOnDemandTree$1.prototype._b = function (a, b, c) {
        var _this = this;
        var d = this._aa != null ? this._aa.count : 0;
        var e = a;
        for (var f = 0; f < d; f++) {
            var g = this._aa._inner[f];
            if (g.n > a) {
                break;
            }
            if (g.e) {
                if (g.l >= a) {
                    c = 0;
                    if (false == b && false == g.d) {
                        return {
                            ret: null,
                            p2: c
                        };
                    }
                    var h = a - g.n;
                    var i = g.item(h);
                    if (i == null) {
                        if (b) {
                            i = this._a._createValue$i(a);
                            g.item(h, i);
                            c = 2;
                            this._aj++;
                        }
                    }
                    else {
                        c = 1;
                    }
                    return {
                        ret: i,
                        p2: c
                    };
                }
                e -= g.k;
            }
            else {
                e += g.k;
            }
        }
        var j = e % 32;
        var k = e - j;
        var l = null;
        if (this._h == null) {
            if (b == false) {
                c = 0;
                return {
                    ret: null,
                    p2: c
                };
            }
            c = 2;
            l = this._h = this._f(k, this);
            l.b[j] = this._a._createValue$i(a);
        }
        else {
            if (this._i != null) {
                if (k == this._i.l) {
                    l = this._i;
                }
                else if (k == this._i.l + 32) {
                    l = this._i.i;
                    if (l != null) {
                        while (true) {
                            var m = l.g;
                            if (m == null) {
                                break;
                            }
                            l = m;
                        }
                    }
                    else {
                    }
                }
                else if (k == this._i.l - 32) {
                    l = this._i.g;
                    if (l != null) {
                        while (true) {
                            var n = l.i;
                            if (n == null) {
                                break;
                            }
                            l = n;
                        }
                    }
                    else {
                    }
                }
            }
            if (l == null) {
                l = this._h;
            }
            l = ((function () { var o = l.e(a, e, b, b, _this, c); c = o.p5; return o.ret; })());
        }
        if (l == null) {
            return {
                ret: null,
                p2: c
            };
        }
        this._i = l;
        if (c == 2) {
            this._aj++;
        }
        var o = l.b[j];
        if (o != null) {
            if (!this._a._isItemStillValid$i(o)) {
                if (b) {
                    o = this._a._createValue$i(a);
                }
                else {
                    o = null;
                }
                l.b[j] = o;
            }
        }
        return {
            ret: o,
            p2: c
        };
    };
    LoadOnDemandTree$1.prototype._ao = function (a, b) {
        var e_4, _c;
        var c = Math.max(this._ae - b, a + b);
        var d = this._ae - 1;
        if (c <= d) {
            try {
                for (var _t = tslib_1.__values(fromEnum(this._x(c, d, true))), _v = _t.next(); !_v.done; _v = _t.next()) {
                    var e = _v.value;
                    this._aj--;
                }
            }
            catch (e_4_1) { e_4 = { error: e_4_1 }; }
            finally {
                try {
                    if (_v && !_v.done && (_c = _t.return)) _c.call(_t);
                }
                finally { if (e_4) throw e_4.error; }
            }
        }
        this._am(this._l(a, b));
    };
    LoadOnDemandTree$1._aq = function ($t, a, b, c) {
        while (true) {
            if (a == null) {
                break;
            }
            b.h(a);
            if (c) {
                a = a.g;
            }
            else {
                a = a.i;
            }
        }
    };
    LoadOnDemandTree$1.prototype._ar = function (a, b) {
        var e_5, _c;
        try {
            for (var _t = tslib_1.__values(fromEnum(this._x(a, a + b - 1, true))), _v = _t.next(); !_v.done; _v = _t.next()) {
                var c = _v.value;
                this._aj--;
            }
        }
        catch (e_5_1) { e_5 = { error: e_5_1 }; }
        finally {
            try {
                if (_v && !_v.done && (_c = _t.return)) _c.call(_t);
            }
            finally { if (e_5) throw e_5.error; }
        }
        this._am(this._m(a, b));
    };
    LoadOnDemandTree$1.prototype._e = function (a, b) {
        var _this = this;
        var c = this._aa != null ? this._aa.count : 0;
        var d = b;
        for (var e = 0; e < c; e++) {
            var f = this._aa._inner[e];
            if (f.n > b) {
                break;
            }
            if (f.e) {
                if (f.l >= b) {
                    var g = b - f.n;
                    var h = f.item(g);
                    f.item(g, a);
                    return h;
                }
                d -= f.k;
            }
            else {
                d += f.k;
            }
        }
        var i = null;
        var j;
        return ((function () { var k = _this._d(i, a, b, j); i = k.p0; j = k.p3; return k.ret; })());
    };
    LoadOnDemandTree$1.prototype._p = function (a, b) {
        var _this = this;
        var c;
        b = ((function () { var d = _this._b(a, false, c); c = d.p2; return d.ret; })());
        return {
            ret: c == 1,
            p1: b
        };
    };
    LoadOnDemandTree$1.prototype._am = function (a) {
        this._i = null;
        var b = this._ad;
        b.add(a);
        this._q++;
        this._ap(a);
        this._as();
        if (this._aa == null) {
            return;
        }
        var c = this._aa.count - 1;
        for (; c >= 0; c--) {
            var d = this._aa._inner[c];
            if (d.l < this._ae) {
                break;
            }
            if (d.n < this._ae) {
                d.u(this._ae - d.n);
            }
            else {
                this._aa.removeAt(c);
            }
        }
        var e = this._aa.count;
        var f = 0;
        for (c = 0; c < e; c++) {
            var g = this._aa._inner[c];
            if (g.e) {
                f += g.k;
            }
            else {
                f -= g.k;
            }
        }
        if (f < 0) {
            var h = this._ae + f;
            var i = this._l(h, -f);
            b.add(i);
            this._q++;
            this._aa.add(this._j(i));
        }
    };
    LoadOnDemandTree$1.prototype._an = function () {
        var e_6, _c;
        var _this = this;
        if (this._ab == null) {
            return;
        }
        this._n = true;
        try {
            var a = new List$1(this.$t, 1, this._w(0, this._ae, true));
            var b = new List$1(LoadOnDemandTree_BinaryTreeNode$1.$.specialize(this.$t), 1, this._y());
            var c_1 = null;
            var d = b.count;
            for (var e = 0; e < d; e++) {
                c_1 = b._inner[e];
                var _loop_1 = function (f) {
                    var g = c_1.b[f];
                    if (g != null) {
                        var h = c_1.l + f;
                        var i_1 = this_1._a._getItemIndex$i(g);
                        this_1._a._initializeItemIndex$i(g, i_1, true);
                        if (i_1 < 0) {
                            c_1.b[f] = null;
                            return "continue";
                        }
                        if (h != i_1) {
                            c_1.b[f] = null;
                            var j_1;
                            var k_1 = c_1;
                            var l_1 = ((function () { var m = _this._d(k_1, g, i_1, j_1); k_1 = m.p0; j_1 = m.p3; return m.ret; })());
                            while (l_1 != null) {
                                var m = i_1;
                                if (j_1 < 0) {
                                    this_1._a._initializeItemIndex$i(l_1, i_1, true);
                                    break;
                                }
                                l_1 = ((function () { var n = _this._d(k_1, l_1, j_1, j_1); k_1 = n.p0; j_1 = n.p3; return n.ret; })());
                            }
                        }
                    }
                };
                var this_1 = this;
                for (var f = 0; f < 32; f++) {
                    _loop_1(f);
                }
            }
            var _loop_2 = function (n) {
                var o = this_2._a._getItemIndex$i(n);
                if (o < 0) {
                    this_2._a._initializeItemIndex$i(n, o, true);
                }
                else {
                    var p_1;
                    var q = ((function () { var r = _this._d(c_1, n, o, p_1); c_1 = r.p0; p_1 = r.p3; return r.ret; })());
                }
            };
            var this_2 = this;
            try {
                for (var _t = tslib_1.__values(fromEnum(a)), _v = _t.next(); !_v.done; _v = _t.next()) {
                    var n = _v.value;
                    _loop_2(n);
                }
            }
            catch (e_6_1) { e_6 = { error: e_6_1 }; }
            finally {
                try {
                    if (_v && !_v.done && (_c = _t.return)) _c.call(_t);
                }
                finally { if (e_6) throw e_6.error; }
            }
        }
        finally {
            this._n = false;
            this._aa = null;
            this._ab = null;
            if (this._q >= 150) {
                this._s = this._q;
                this._q = 0;
            }
            this._r = this._q;
        }
    };
    LoadOnDemandTree$1.prototype._o = function (a, b) {
        if (this._aa == null) {
            return false;
        }
        var c = this._aa.count;
        for (var d = 0; d < c; d++) {
            var e = this._aa._inner[d];
            if (e.e) {
                if (e.n + e.k <= b) {
                    continue;
                }
            }
            else if (e.m <= b) {
                continue;
            }
            if (e.e && a > 0 && b + a > e.n && b != e.m + 1 && b != e.n) {
                this._an();
                return true;
            }
        }
        return false;
    };
    LoadOnDemandTree$1.prototype._ap = function (a) {
        var b = this._j(a);
        if (this._aa == null) {
            this._aa = new List$1(LoadOnDemandTree_ChangeAdjustmentPoint$1.$.specialize(this.$t), 0);
            this._aa.add(b);
        }
        else {
            var c = false;
            var d = new List$1(LoadOnDemandTree_ChangeAdjustmentPoint$1.$.specialize(this.$t), 2, 3);
            var e = this._aa.count;
            for (var f = 0; f < e; f++) {
                var g = this._aa._inner[f];
                if (g.e) {
                    if (g.n + g.k <= a.f) {
                        continue;
                    }
                }
                else if (g.m <= a.f) {
                    continue;
                }
                var h = g.q(b, d, c);
                c = h.p2;
                if (d.count == 1) {
                    this._aa._inner[f] = d._inner[0];
                }
                else {
                    this._aa.removeAt(f);
                    this._aa.s(f, d);
                    e += d.count - 1;
                    f += d.count - 1;
                }
            }
            if (!c) {
                this._aa.add(b);
            }
        }
    };
    LoadOnDemandTree$1.prototype._d = function (a, b, c, d) {
        d = -1;
        var e = !(c < 0 || c >= this._ae);
        var f = this._n ? c : this._ah(c);
        if (a == null || a.l > f || a.l + 32 <= f) {
            a = this._g(a, c, f);
        }
        if (a == null) {
            this._a._initializeItemIndex$i(b, -1, this._n);
            return {
                ret: null,
                p0: a,
                p3: d
            };
        }
        var g = f - a.l;
        var h = a.b[g];
        a.b[g] = b;
        this._a._initializeItemIndex$i(b, c, this._n);
        if (h == null || Base.equalsStatic(h, b)) {
            return {
                ret: null,
                p0: a,
                p3: d
            };
        }
        d = this._a._getItemIndex$i(h);
        if (d == c) {
            return {
                ret: null,
                p0: a,
                p3: d
            };
        }
        return {
            ret: h,
            p0: a,
            p3: d
        };
    };
    LoadOnDemandTree$1.prototype._g = function (a, b, c) {
        var _this = this;
        if (b < 0) {
            return null;
        }
        var d = a == null ? false : c >= a.l && c < a.l + 32;
        var e;
        if (!d) {
            if (this._h == null) {
                var f = c % 32;
                var g = c - f;
                a = this._h = this._f(g, this);
            }
            else {
                a = ((function () { var h = _this._h.e(b, c, true, false, _this, e); e = h.p5; return h.ret; })());
            }
        }
        return a;
    };
    LoadOnDemandTree$1.prototype._as = function () {
        var a = this._ad;
        if (a.count == 0) {
            return;
        }
        if (a.count > 60 || a._inner[a.count - 1].c > 100) {
            this._an();
        }
    };
    Object.defineProperty(LoadOnDemandTree$1.prototype, "_ac", {
        get: function () {
            return this._aa;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(LoadOnDemandTree$1.prototype, "_u", {
        get: function () {
            return this._q;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(LoadOnDemandTree$1.prototype, "count", {
        get: function () {
            return this._aj;
        },
        enumerable: true,
        configurable: true
    });
    LoadOnDemandTree$1.prototype._item = function (a) {
        var _this = this;
        var b;
        return ((function () { var c = _this._b(a, true, b); b = c.p2; return c.ret; })());
    };
    Object.defineProperty(LoadOnDemandTree$1.prototype, "_al", {
        get: function () {
            return this._ae;
        },
        set: function (a) {
            if (a != this._ae) {
                this._an();
                this._ae = a;
            }
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(LoadOnDemandTree$1.prototype, "_ad", {
        get: function () {
            if (this._ab == null) {
                this._ab = new List$1(LoadOnDemandTree_ChangeHistoryItem$1.$.specialize(this.$t), 0);
            }
            return this._ab;
        },
        enumerable: true,
        configurable: true
    });
    LoadOnDemandTree$1.$t = markType(LoadOnDemandTree$1, 'LoadOnDemandTree$1', Base.$, [ICollection$1_$type.specialize(0), IBinaryTreeNodeOwner$1_$type.specialize(0)]);
    return LoadOnDemandTree$1;
}(Base));
export { LoadOnDemandTree$1 };
/**
 * @hidden
 */
var LoadOnDemandTree_ChangeAdjustmentPoint$1 = /** @class */ (function (_super) {
    tslib_1.__extends(LoadOnDemandTree_ChangeAdjustmentPoint$1, _super);
    function LoadOnDemandTree_ChangeAdjustmentPoint$1($t, a, b, c, d) {
        var _this = _super.call(this) || this;
        _this.$t = null;
        _this.a = null;
        _this.i = 0;
        _this.h = 0;
        _this.j = 0;
        _this.c = null;
        _this.$t = $t;
        _this.$type = _this.$type.specialize(_this.$t);
        _this.a = a;
        _this.c = b;
        _this.j = c;
        _this.i = b.f + c;
        _this.h = d;
        return _this;
    }
    Object.defineProperty(LoadOnDemandTree_ChangeAdjustmentPoint$1.prototype, "n", {
        get: function () {
            return this.i;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(LoadOnDemandTree_ChangeAdjustmentPoint$1.prototype, "l", {
        get: function () {
            return this.i + this.h - 1;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(LoadOnDemandTree_ChangeAdjustmentPoint$1.prototype, "m", {
        get: function () {
            if (!this.e) {
                return this.i;
            }
            return this.l;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(LoadOnDemandTree_ChangeAdjustmentPoint$1.prototype, "k", {
        get: function () {
            return this.h;
        },
        set: function (a) {
            if (this.h != a) {
                this.h = a;
                this.t();
            }
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(LoadOnDemandTree_ChangeAdjustmentPoint$1.prototype, "p", {
        get: function () {
            return this.c.c;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(LoadOnDemandTree_ChangeAdjustmentPoint$1.prototype, "o", {
        get: function () {
            return this.j;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(LoadOnDemandTree_ChangeAdjustmentPoint$1.prototype, "d", {
        get: function () {
            if (!this.e) {
                return false;
            }
            return (this.c).j;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(LoadOnDemandTree_ChangeAdjustmentPoint$1.prototype, "e", {
        get: function () {
            return this.c.a;
        },
        enumerable: true,
        configurable: true
    });
    LoadOnDemandTree_ChangeAdjustmentPoint$1.prototype.item = function (a, b) {
        if (arguments.length === 2) {
            if (!this.e) {
                throw new NotSupportedException(1, "Indexer is only supported on a add operation");
            }
            var c = typeCast(LoadOnDemandTree_ChangeHistoryItemAdd$1.$.specialize(this.$t), this.c);
            c.i[a + this.o] = b;
            return b;
        }
        else {
            if (!this.e) {
                throw new NotSupportedException(1, "Indexer is only supported on a add operation");
            }
            return this.c.i[a + this.o];
        }
    };
    LoadOnDemandTree_ChangeAdjustmentPoint$1.prototype.q = function (a, b, c) {
        b.clear();
        var d = this.f(a.n, a.l);
        if (d) {
            if (a.c.a) {
                var e = this.r(a, b, c);
                c = e.p2;
                return {
                    p2: c
                };
            }
            if (this.c.a) {
                var f = this.s(a, b, c);
                c = f.p2;
            }
            else {
                if (c) {
                    this.i -= a.k;
                }
                else {
                    var g = Math.min(this.i, a.n);
                    this.i = g;
                    this.k += a.h;
                    c = true;
                }
                b.add(this);
            }
            return {
                p2: c
            };
        }
        if (a.n < this.i) {
            if (!c) {
                c = true;
                b.add(a);
            }
            b.add(this);
            if (a.c.a) {
                this.i += a.p;
            }
            else {
                this.i -= a.p;
            }
        }
        else {
            b.add(this);
            if (!c) {
                c = true;
                b.add(a);
            }
        }
        return {
            p2: c
        };
    };
    LoadOnDemandTree_ChangeAdjustmentPoint$1.prototype.r = function (a, b, c) {
        if (a.n <= this.n) {
            if (!c) {
                c = true;
                if (false == this.e && a.i == this.i && a.h == this.h) {
                    return {
                        p2: c
                    };
                }
                b.add(a);
            }
            this.i += a.k;
            b.add(this);
            return {
                p2: c
            };
        }
        if (c) {
            this.i += a.k;
            b.add(this);
            return {
                p2: c
            };
        }
        var d = a.n - this.n;
        b.o(this.g(d, 0, a));
        c = true;
        return {
            p2: c
        };
    };
    LoadOnDemandTree_ChangeAdjustmentPoint$1.prototype.s = function (a, b, c) {
        var d = a.n - this.n;
        if (d == 0) {
            c = true;
            if (this.h == a.h) {
            }
            else if (this.h < a.h) {
                a.k -= this.h;
                b.add(a);
            }
            else {
                this.k -= a.h;
                this.j += a.h;
                b.add(this);
            }
            return {
                p2: c
            };
        }
        if (d > 0) {
            c = true;
            if (a.l <= this.l) {
                b.o(this.g(d, a.k, null));
            }
            else {
                var e = this.h;
                this.k = d;
                a.k -= e - this.h;
                b.add(this);
                b.add(a);
            }
            return {
                p2: c
            };
        }
        if (this.l <= a.l) {
            a.k -= this.h;
            if (!c) {
                b.add(a);
                c = true;
            }
        }
        else {
            d *= -1;
            var f = this.h;
            this.i -= d;
            this.k -= a.h - d;
            this.j += f - this.h;
            a.k = d;
            if (!c) {
                b.add(a);
                c = true;
            }
            b.add(this);
        }
        return {
            p2: c
        };
    };
    LoadOnDemandTree_ChangeAdjustmentPoint$1.prototype.t = function () {
    };
    LoadOnDemandTree_ChangeAdjustmentPoint$1.prototype.f = function (a, b) {
        var c = Math.max(a, this.i);
        var d = Math.min(b, this.m);
        return (c <= d);
    };
    LoadOnDemandTree_ChangeAdjustmentPoint$1.prototype.g = function (a, b, c) {
        var d = new List$1(LoadOnDemandTree_ChangeAdjustmentPoint$1.$.specialize(this.$t), 2, 3);
        var e = this.a._k(this.c, this.o, a);
        var f = this.a._k(this.c, this.o + a + b, this.k - (a + b));
        if (e.k > 0) {
            d.add(e);
        }
        if (c != null) {
            c.i = e.i + a;
            d.add(c);
            f.i = c.l + 1;
        }
        if (f.k > 0) {
            f.i -= b;
            d.add(f);
        }
        return d;
    };
    LoadOnDemandTree_ChangeAdjustmentPoint$1.prototype.u = function (a) {
        this.c.g(a + this.o);
        this.k = a;
    };
    LoadOnDemandTree_ChangeAdjustmentPoint$1.$t = markType(LoadOnDemandTree_ChangeAdjustmentPoint$1, 'LoadOnDemandTree_ChangeAdjustmentPoint$1');
    return LoadOnDemandTree_ChangeAdjustmentPoint$1;
}(Base));
export { LoadOnDemandTree_ChangeAdjustmentPoint$1 };
/**
 * @hidden
 */
var LoadOnDemandTree_BinaryTreeNode$1 = /** @class */ (function (_super) {
    tslib_1.__extends(LoadOnDemandTree_BinaryTreeNode$1, _super);
    function LoadOnDemandTree_BinaryTreeNode$1($t, a, b, c) {
        var _this = _super.call(this) || this;
        _this.$t = null;
        _this.n = 0;
        _this.k = 0;
        _this.c = null;
        _this.f = null;
        _this.h = null;
        _this.a = null;
        _this.$t = $t;
        _this.$type = _this.$type.specialize(_this.$t);
        _this.k = a;
        _this.a = new Array(32);
        _this.c = b;
        return _this;
    }
    LoadOnDemandTree_BinaryTreeNode$1.prototype.verifyBalanceOfChild = function (a) {
        var _this = this;
        if (a == this.f) {
            return ((function () { var b = LoadOnDemandTree_BinaryTreeNode$1.j(_this.$t, _this.f); _this.f = b.p0; return b.ret; })());
        }
        else if (a == this.h) {
            return ((function () { var b = LoadOnDemandTree_BinaryTreeNode$1.j(_this.$t, _this.h); _this.h = b.p0; return b.ret; })());
        }
        else {
            return false;
        }
    };
    LoadOnDemandTree_BinaryTreeNode$1.j = function ($t, a) {
        var b = a.f;
        var c = a.h;
        var d = LoadOnDemandTree_BinaryTreeNode$1.m($t, b);
        var e = LoadOnDemandTree_BinaryTreeNode$1.m($t, c);
        if (d < e - 1) {
            var f = c.f;
            var g = c.h;
            var h = LoadOnDemandTree_BinaryTreeNode$1.m($t, f);
            var i = LoadOnDemandTree_BinaryTreeNode$1.m($t, g);
            if (h < i) {
                var j = c.f;
                c.c = a.c;
                a.i = j;
                c.g = a;
                a.recalculateHeight();
                a = c;
            }
            else {
                var k = f.f;
                var l = f.h;
                f.c = a.c;
                f.g = a;
                f.i = c;
                a.i = k;
                c.g = l;
                a.recalculateHeight();
                c.recalculateHeight();
                f.recalculateHeight();
                a = f;
            }
            return {
                ret: true,
                p0: a
            };
        }
        if (e < d - 1) {
            var m = b.f;
            var n = b.h;
            var o = LoadOnDemandTree_BinaryTreeNode$1.m($t, m);
            var p = LoadOnDemandTree_BinaryTreeNode$1.m($t, n);
            if (p < o) {
                var q = b.h;
                b.c = a.c;
                a.g = q;
                b.i = a;
                a.recalculateHeight();
                a = b;
            }
            else {
                var r = n.f;
                var s = n.h;
                n.c = a.c;
                n.g = b;
                n.i = a;
                a.g = s;
                b.i = r;
                a.recalculateHeight();
                b.recalculateHeight();
                n.recalculateHeight();
                a = n;
            }
            return {
                ret: true,
                p0: a
            };
        }
        return {
            ret: false,
            p0: a
        };
    };
    LoadOnDemandTree_BinaryTreeNode$1.prototype.e = function (a, b, c, d, e, f) {
        if (d) {
            c = true;
        }
        var g = b % 32;
        var h = b - g;
        f = 0;
        var i = this;
        var j = null;
        while (true) {
            if (h == i.k) {
                if (i.b[g] == null) {
                    if (d) {
                        i.b[g] = e._a._createValue$i(a);
                        f = 2;
                    }
                    else {
                        f = 0;
                    }
                }
                else {
                    f = 1;
                }
                return {
                    ret: i,
                    p5: f
                };
            }
            var k = i;
            if (h < i.k) {
                i = i.f;
                if (i == null) {
                    if (c) {
                        j = e._f(h, this);
                        if (d) {
                            j.b[g] = e._a._createValue$i(a);
                            f = 2;
                        }
                        else {
                            f = 0;
                        }
                        i = j;
                        k.g = i;
                    }
                    break;
                }
            }
            else {
                i = i.h;
                if (i == null) {
                    if (c) {
                        j = e._f(h, this);
                        if (d) {
                            j.b[g] = e._a._createValue$i(a);
                            f = 2;
                        }
                        else {
                            f = 0;
                        }
                        i = j;
                        k.i = i;
                    }
                    break;
                }
            }
        }
        if (f == 2) {
            var l = i.c;
            while (i != null) {
                l = i.c;
                if (l.verifyBalanceOfChild(i)) {
                    break;
                }
                if (l.recalculateHeight() == false) {
                    break;
                }
                i = typeCast(LoadOnDemandTree_BinaryTreeNode$1.$.specialize(this.$t), l);
            }
        }
        return {
            ret: j,
            p5: f
        };
    };
    LoadOnDemandTree_BinaryTreeNode$1.m = function ($t, a) {
        return a == null ? -1 : a.n;
    };
    LoadOnDemandTree_BinaryTreeNode$1.prototype.recalculateHeight = function () {
        var a = LoadOnDemandTree_BinaryTreeNode$1.m(this.$t, this.f);
        var b = LoadOnDemandTree_BinaryTreeNode$1.m(this.$t, this.h);
        var c = this.n;
        if (a < b) {
            this.n = b + 1;
        }
        else {
            this.n = a + 1;
        }
        return this.n != c;
    };
    Object.defineProperty(LoadOnDemandTree_BinaryTreeNode$1.prototype, "l", {
        get: function () {
            return this.k;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(LoadOnDemandTree_BinaryTreeNode$1.prototype, "g", {
        get: function () {
            return this.f;
        },
        set: function (a) {
            if (this.f != a) {
                this.f = a;
                if (this.f != null) {
                    this.f.c = this;
                }
            }
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(LoadOnDemandTree_BinaryTreeNode$1.prototype, "d", {
        get: function () {
            return this.c;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(LoadOnDemandTree_BinaryTreeNode$1.prototype, "i", {
        get: function () {
            return this.h;
        },
        set: function (a) {
            if (this.h != a) {
                this.h = a;
                if (this.h != null) {
                    this.h.c = this;
                }
            }
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(LoadOnDemandTree_BinaryTreeNode$1.prototype, "b", {
        get: function () {
            return this.a;
        },
        enumerable: true,
        configurable: true
    });
    LoadOnDemandTree_BinaryTreeNode$1.$t = markType(LoadOnDemandTree_BinaryTreeNode$1, 'LoadOnDemandTree_BinaryTreeNode$1', Base.$, [IBinaryTreeNodeOwner$1_$type.specialize(0)]);
    return LoadOnDemandTree_BinaryTreeNode$1;
}(Base));
export { LoadOnDemandTree_BinaryTreeNode$1 };
//# sourceMappingURL=IBinaryTreeNodeOwner$1_combined.js.map