/*
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 { Base, typeCast, String_$type, markType } from "./type";
import { DescriptionTreeNode } from "./DescriptionTreeNode";
import { List$1 } from "./List$1";
import { DescriptionTreeAction } from "./DescriptionTreeAction";
import { DescriptionPropertyValue } from "./DescriptionPropertyValue";
import { Dictionary$2 } from "./Dictionary$2";
import { DescriptionTreeReconciler_TreeModeData } from "./DescriptionTreeReconciler_TreeModeData";
/**
 * @hidden
 */
var DescriptionTreeReconciler = /** @class */ /*@__PURE__*/ (function (_super) {
    tslib_1.__extends(DescriptionTreeReconciler, _super);
    function DescriptionTreeReconciler() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    DescriptionTreeReconciler.f = function (a, b) {
        for (var c = 0; c < b.count; c++) {
            var d = b._inner[c];
            switch (d.a) {
                case 0:
                    d.b.l(d.i, d.f);
                    break;
                case 1:
                    d.b.k(d.i);
                    break;
                case 3:
                    {
                        var e = d.b.a(d.i).b;
                        e[d.e] = d.f;
                    }
                    break;
                case 4:
                    {
                        var f = d.b.a(d.i).b;
                        var g = new Array(f.length - 1);
                        var h = 0;
                        for (var i = 0; i < f.length; i++) {
                            if (i != d.e) {
                                g[h] = f[i];
                                h++;
                            }
                        }
                        d.b.l(d.i, g);
                    }
                    break;
                case 2:
                    {
                        var j = d.b.a(d.i).b;
                        var k = new Array(j.length + 1);
                        var l = 0;
                        for (var m = 0; m < k.length; m++) {
                            if (m == d.d) {
                                k[m] = d.f;
                            }
                            else {
                                k[m] = j[l];
                                l++;
                            }
                        }
                        d.b.l(d.i, k);
                    }
                    break;
                case 5:
                    d.b.l(d.i, []);
                    break;
            }
        }
    };
    DescriptionTreeReconciler.b = function (a, b, c) {
        var d = new List$1(DescriptionTreeAction.$, 0);
        if (a == null && b == null) {
            return d;
        }
        if (a != null && b == null) {
            var e = new DescriptionTreeAction();
            e.a = 4;
            e.e = 0;
            e.g = a;
            e.c = null;
            e.i = null;
            d.add(e);
            return d;
        }
        if (a == null && b != null) {
            var f = new DescriptionTreeAction();
            f.a = 2;
            f.e = 0;
            f.f = b;
            f.c = null;
            f.i = null;
            d.add(f);
            return d;
        }
        if (b.h != a.h) {
            var g = new DescriptionTreeAction();
            g.a = 3;
            g.e = 0;
            g.g = a;
            g.f = b;
            g.c = null;
            g.i = null;
            d.add(g);
            return d;
        }
        DescriptionTreeReconciler.g(d, a, b, c);
        return d;
    };
    DescriptionTreeReconciler.g = function (a, b, c, d) {
        c.f = b.f;
        var e = DescriptionTreeReconciler.d(b);
        var f = DescriptionTreeReconciler.d(c);
        var g = new List$1(DescriptionPropertyValue.$, 0);
        var h = new List$1(DescriptionPropertyValue.$, 0);
        var i = new List$1(DescriptionPropertyValue.$, 0);
        for (var j = 0; j < e.count; j++) {
            var k = e._inner[j];
            if (c.b(k.c)) {
                if (!Base.equalsStatic(k, c.a(k.c))) {
                    var l = c.a(k.c);
                    if (typeCast(DescriptionTreeNode.$, k.b) !== null && typeCast(DescriptionTreeNode.$, l.b) !== null) {
                        var m = k.b;
                        var n = l.b;
                        if (m.h == n.h) {
                            DescriptionTreeReconciler.g(a, m, n, d);
                        }
                        else {
                            i.add(c.a(k.c));
                        }
                    }
                    else {
                        i.add(c.a(k.c));
                    }
                }
            }
            else {
                if (!d) {
                    g.add(k);
                }
            }
        }
        for (var o = 0; o < f.count; o++) {
            var p = f._inner[o];
            if (!b.b(p.c)) {
                h.add(p);
            }
        }
        for (var q = 0; q < g.count; q++) {
            var r = g._inner[q];
            var s = new DescriptionTreeAction();
            s.b = b;
            s.a = 1;
            s.i = r.c;
            s.c = r.a;
            s.g = r.b;
            a.add(s);
        }
        for (var t = 0; t < h.count; t++) {
            var u = h._inner[t];
            var v = new DescriptionTreeAction();
            v.b = b;
            v.a = 0;
            v.i = u.c;
            v.c = u.a;
            v.f = u.b;
            v.g = null;
            a.add(v);
        }
        for (var w = 0; w < i.count; w++) {
            var x = i._inner[w];
            var y = b.a(x.c).b;
            var z = x.b;
            if (!Base.equalsStatic(z, y)) {
                var aa = new DescriptionTreeAction();
                aa.b = b;
                aa.a = 0;
                aa.i = x.c;
                aa.c = x.a;
                aa.f = x.b;
                aa.g = b.a(x.c).b;
                a.add(aa);
            }
        }
        var ab = DescriptionTreeReconciler.c(b);
        var ac = DescriptionTreeReconciler.c(c);
        var ad = new List$1(DescriptionPropertyValue.$, 0);
        var ae = new List$1(DescriptionPropertyValue.$, 0);
        var af = new List$1(DescriptionPropertyValue.$, 0);
        for (var ag = 0; ag < ab.count; ag++) {
            var ah = ab._inner[ag];
            if (c.b(ah.c)) {
                if (!DescriptionTreeReconciler.a(ah.b, c.a(ah.c).b)) {
                    af.add(c.a(ah.c));
                }
                else {
                    var ai = ah.b;
                    var aj = c.a(ah.c).b;
                    for (var ak = 0; ak < ai.length; ak++) {
                        var al = ai[ak];
                        var am = aj[ak];
                        if (typeCast(DescriptionTreeNode.$, al) !== null && typeCast(DescriptionTreeNode.$, am) !== null) {
                            var an = al;
                            var ao = am;
                            if (an.h == ao.h) {
                                DescriptionTreeReconciler.g(a, an, ao, d);
                            }
                            else {
                                var ap = new DescriptionTreeAction();
                                ap.i = ah.c;
                                ap.c = ah.a;
                                ap.b = b;
                                ap.a = 3;
                                ap.e = ak;
                                ap.d = ak;
                                ap.g = an;
                                ap.f = ao;
                                a.add(ap);
                            }
                        }
                        else {
                            if (!Base.equalsStatic(al, am)) {
                                var aq = new DescriptionTreeAction();
                                aq.i = ah.c;
                                aq.c = ah.a;
                                aq.b = b;
                                aq.a = 3;
                                aq.e = ak;
                                aq.d = ak;
                                aq.g = al;
                                aq.f = am;
                                a.add(aq);
                            }
                        }
                    }
                }
            }
            else {
                if (!d) {
                    ad.add(ah);
                }
            }
        }
        for (var ar = 0; ar < ac.count; ar++) {
            var as = ac._inner[ar];
            if (!b.b(as.c)) {
                ae.add(as);
            }
        }
        for (var at = 0; at < ad.count; at++) {
            var au = ad._inner[at];
            var av = new DescriptionTreeAction();
            av.b = b;
            av.i = au.c;
            av.c = au.a;
            av.a = 5;
            av.g = au.b;
            a.add(av);
        }
        for (var aw = 0; aw < ae.count; aw++) {
            var ax = ae._inner[aw];
            var ay = new DescriptionTreeAction();
            ay.b = b;
            ay.i = ax.c;
            ay.c = ax.a;
            ay.a = 0;
            ay.g = null;
            ay.f = ax.b;
            a.add(ay);
        }
        for (var az = 0; az < af.count; az++) {
            DescriptionTreeReconciler.h(a, b, c, b.a(af._inner[az].c), af._inner[az], d);
        }
    };
    DescriptionTreeReconciler.e = function (a) {
        if (a == null) {
            return "UNKOWN";
        }
        if (!a.b("Type")) {
            return "UNKOWN";
        }
        return a.a("Type").b;
    };
    DescriptionTreeReconciler.h = function (a, b, c, d, e, f) {
        var g = false;
        var h = d.b;
        var i = e.b;
        if (h.length > 0) {
            if (typeCast(DescriptionTreeNode.$, h[0]) !== null) {
                var j = h[0];
                if (j.b("Name") && j.a("Name") != null) {
                    g = true;
                }
            }
        }
        if (i.length > 0) {
            if (typeCast(DescriptionTreeNode.$, i[0]) !== null) {
                var k = i[0];
                if (k.b("Name") && k.a("Name") != null) {
                    g = true;
                }
            }
        }
        if (g) {
            var l = new Dictionary$2(String_$type, DescriptionTreeReconciler_TreeModeData.$, 0);
            var m = new List$1(DescriptionTreeReconciler_TreeModeData.$, 0);
            var n = new Dictionary$2(String_$type, DescriptionTreeReconciler_TreeModeData.$, 0);
            var o = new List$1(DescriptionTreeReconciler_TreeModeData.$, 0);
            var p = 0;
            for (var q = 0; q < h.length; q++) {
                var r = new DescriptionTreeReconciler_TreeModeData();
                r.a = h[q];
                r.d = q;
                r.f = r.a.b("Name") ? r.a.a("Name").b : null;
                if (r.f == null) {
                    r.f = "BADKEY_" + p.toString();
                    p++;
                }
                m.add(r);
                l.addItem(r.f, r);
            }
            for (var s = 0; s < i.length; s++) {
                var t = new DescriptionTreeReconciler_TreeModeData();
                t.a = i[s];
                t.d = s;
                t.f = t.a.b("Name") ? t.a.a("Name").b : null;
                if (t.f == null) {
                    t.f = "BADKEY_" + p.toString();
                    p++;
                }
                o.add(t);
                n.addItem(t.f, t);
            }
            for (var u = 0; u < m.count; u++) {
                var v = m._inner[u];
                if (!n.containsKey(v.f) || (n.containsKey(v.f) && DescriptionTreeReconciler.e(v.a) != DescriptionTreeReconciler.e(n.item(v.f).a))) {
                    v.c = true;
                }
            }
            for (var w = 0; w < o.count; w++) {
                var x = o._inner[w];
                if (!l.containsKey(x.f) || (l.containsKey(x.f) && DescriptionTreeReconciler.e(x.a) != DescriptionTreeReconciler.e(l.item(x.f).a))) {
                    x.b = true;
                }
                else {
                    l.item(x.f).e = w;
                }
            }
            for (var y = 0; y < m.count; y++) {
                var z = m._inner[y];
                if (z.c) {
                    var aa = new DescriptionTreeAction();
                    aa.i = e.c;
                    aa.b = b;
                    aa.c = e.a;
                    aa.e = z.d;
                    aa.a = 4;
                    aa.g = z.a;
                    a.add(aa);
                    for (var ab = y + 1; ab < m.count; ab++) {
                        var ac = m._inner[ab];
                        ac.d--;
                    }
                }
            }
            for (var ad = 0; ad < o.count; ad++) {
                var ae = o._inner[ad];
                if (ae.b) {
                    var af = new DescriptionTreeAction();
                    af.i = e.c;
                    af.b = b;
                    af.c = e.a;
                    af.d = ad;
                    af.a = 2;
                    af.f = i[ad];
                    a.add(af);
                    for (var ag = 0; ag < m.count; ag++) {
                        var ah = m._inner[ag];
                        if (!ah.c && ah.d >= ad) {
                            ah.d++;
                        }
                    }
                }
                else {
                    var ai = l.item(ae.f);
                    if (ai.d == ai.e) {
                        DescriptionTreeReconciler.g(a, ai.a, ae.a, f);
                        continue;
                    }
                    var aj = new DescriptionTreeAction();
                    aj.i = e.c;
                    aj.b = b;
                    aj.c = e.a;
                    aj.e = ai.d;
                    aj.a = 4;
                    aj.g = ai.a;
                    a.add(aj);
                    for (var ak = 0; ak < m.count; ak++) {
                        var al = m._inner[ak];
                        if (!al.c && al.a != ai.a && al.d >= ai.d) {
                            al.d--;
                        }
                    }
                    aj = new DescriptionTreeAction();
                    aj.i = e.c;
                    aj.b = b;
                    aj.c = e.a;
                    aj.d = ai.e;
                    aj.a = 2;
                    aj.f = ai.a;
                    a.add(aj);
                    ai.d = ai.e;
                    for (var am = 0; am < m.count; am++) {
                        var an = m._inner[am];
                        if (!an.c && an.a != ai.a && an.d >= ai.d) {
                            an.d++;
                        }
                    }
                    DescriptionTreeReconciler.g(a, ai.a, ae.a, f);
                }
            }
        }
        else {
            var ao = Math.max(h.length, i.length);
            for (var ap = 0; ap < ao; ap++) {
                if (ap >= h.length) {
                    var aq = new DescriptionTreeAction();
                    aq.i = e.c;
                    aq.b = b;
                    aq.c = e.a;
                    aq.d = ap;
                    aq.a = 2;
                    aq.f = i[ap];
                    a.add(aq);
                }
                else if (ap >= i.length) {
                    var ar = new DescriptionTreeAction();
                    ar.i = e.c;
                    ar.b = b;
                    ar.c = e.a;
                    ar.e = i.length;
                    ar.a = 4;
                    ar.g = h[ap];
                    a.add(ar);
                }
                else {
                    var as = h[ap];
                    var at = i[ap];
                    if (typeCast(DescriptionTreeNode.$, as) !== null && typeCast(DescriptionTreeNode.$, at) !== null) {
                        var au = as;
                        var av = at;
                        if (au.h == av.h) {
                            DescriptionTreeReconciler.g(a, au, av, f);
                        }
                        else {
                            var aw = new DescriptionTreeAction();
                            aw.i = e.c;
                            aw.b = b;
                            aw.c = e.a;
                            aw.e = ap;
                            aw.a = 3;
                            aw.g = h[ap];
                            aw.f = i[ap];
                            a.add(aw);
                        }
                    }
                    else {
                        if (!Base.equalsStatic(as, at)) {
                            var ax = new DescriptionTreeAction();
                            ax.i = e.c;
                            ax.b = b;
                            ax.c = e.a;
                            ax.e = ap;
                            ax.a = 3;
                            ax.g = h[ap];
                            ax.f = i[ap];
                            a.add(ax);
                        }
                    }
                }
            }
        }
    };
    DescriptionTreeReconciler.a = function (a, b) {
        var c = a;
        var d = b;
        if (c.length != d.length) {
            return false;
        }
        for (var e = 0; e < c.length; e++) {
            var f = c[e];
            var g = d[e];
            if (typeCast(DescriptionTreeNode.$, f) !== null && !(typeCast(DescriptionTreeNode.$, g) !== null)) {
                return false;
            }
            if (!(typeCast(DescriptionTreeNode.$, f) !== null) && typeCast(DescriptionTreeNode.$, g) !== null) {
                return false;
            }
            if (typeCast(DescriptionTreeNode.$, f) !== null) {
                var h = f;
                var i = g;
                if (h.h != i.h) {
                    return false;
                }
                var j = h.b("Name") ? h.a("Name").b : null;
                var k = i.b("Name") ? i.a("Name").b : null;
                if (!Base.equalsStatic(j, k)) {
                    return false;
                }
            }
            else {
                if (!Base.equalsStatic(f, g)) {
                    return false;
                }
            }
        }
        return true;
    };
    DescriptionTreeReconciler.d = function (a) {
        var b = new List$1(DescriptionPropertyValue.$, 0);
        var c = a.e();
        for (var d = 0; d < c.count; d++) {
            var e = c._inner[d];
            if (e.a != null && e.a.knownType != 10 && e.a.knownType != 9) {
                b.add(e);
            }
        }
        return b;
    };
    DescriptionTreeReconciler.c = function (a) {
        var b = new List$1(DescriptionPropertyValue.$, 0);
        var c = a.e();
        for (var d = 0; d < c.count; d++) {
            var e = c._inner[d];
            if (e.a != null && (e.a.knownType == 10 || e.a.knownType == 9)) {
                b.add(e);
            }
        }
        return b;
    };
    DescriptionTreeReconciler.$t = markType(DescriptionTreeReconciler, 'DescriptionTreeReconciler');
    return DescriptionTreeReconciler;
}(Base));
export { DescriptionTreeReconciler };
//# sourceMappingURL=DescriptionTreeReconciler.js.map
