/*
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, __read, __spreadArray, __values } from "tslib";
import { Base, String_$type, Number_$type, typeCast, fromEnum, markType } from "./type";
import { List$1 } from "./List$1";
import { Dictionary$2 } from "./Dictionary$2";
import { JsonDictionaryObject } from "./JsonDictionaryObject";
import { JsonDictionaryParser } from "./JsonDictionaryParser";
import { HashSet$1 } from "./HashSet$1";
import { Tuple$2 } from "./Tuple$2";
import { JsonDictionaryItem } from "./JsonDictionaryItem";
import { JsonDictionaryArray } from "./JsonDictionaryArray";
import { JsonDictionaryValue } from "./JsonDictionaryValue";
import { stringJoin, stringContains } from "./string";
/**
 * @hidden
 */
var DescriptionJsonValidator = /** @class */ /*@__PURE__*/ (function (_super) {
    __extends(DescriptionJsonValidator, _super);
    function DescriptionJsonValidator() {
        var _this = _super.call(this) || this;
        _this.i = new List$1(String_$type, 0);
        _this.h = new Dictionary$2(String_$type, List$1.$.specialize(String_$type), 0);
        _this.e = null;
        _this.g = null;
        return _this;
    }
    DescriptionJsonValidator.prototype.getContainingModules = function (a) {
        if (this.h.containsKey(a)) {
            return this.h.item(a).toArray();
        }
        return null;
    };
    DescriptionJsonValidator.prototype.validate = function (a) {
        var b = new JsonDictionaryParser();
        var c = b.parse(a);
        var d = this.b(c);
        var e = this.a(c);
        var f = new HashSet$1(String_$type, 0);
        for (var h = 0; h < e.length; h++) {
            var g = e[h];
            f.add_1(g);
        }
        var i = null;
        for (var k = 0; k < d.length; k++) {
            var j = d[k];
            var l = this.getContainingModules(j);
            var m = false;
            for (var o = 0; o < l.length; o++) {
                var n = l[o];
                if (f.contains(n)) {
                    m = true;
                }
            }
            if (!m) {
                var p = a.indexOf("\"" + j + "\"");
                var q = this.l(a, p);
                var r = q.c;
                var s = q.d;
                var t = s + ("\"" + j + "\"").length;
                i = this.f(i, r, s, r, t, "Type " + j + " appears not to have a containing module, consider adding one of: " + stringJoin.apply(void 0, __spreadArray([", "], __read(l))));
            }
        }
        for (var v = 0; v < d.length; v++) {
            var u = d[v];
            if (u == "PropertyEditor") {
                var w = a.indexOf("\"" + u + "\"");
                var x = this.l(a, w);
                var y = x.c;
                var z = x.d;
                var aa = z + ("\"" + u + "\"").length;
                if (!f.contains("withDescriptions")) {
                    i = this.f(i, y, z, y, aa, "Property editor is included, but withDescriptions is not in modules, did you forget it?");
                }
            }
        }
        return i == null ? null : i.b();
    };
    DescriptionJsonValidator.prototype.l = function (a, b) {
        var c = a.split('\n');
        var d = 0;
        var e = 0;
        while (e <= b && d < c.length) {
            var f = c[d];
            if ((f.length + 1) >= (b - e)) {
                return new Tuple$2(Number_$type, Number_$type, d + 1, (b - e) + 1);
            }
            e += (f.length + 1);
            d++;
        }
        return new Tuple$2(Number_$type, Number_$type, d + 1, (b - e) + 1);
    };
    DescriptionJsonValidator.prototype.f = function (a, b, c, d, e, f) {
        if (a == null) {
            a = new JsonDictionaryObject();
            a.item("warnings", new JsonDictionaryArray());
        }
        var g = a.item("warnings");
        var h = g.items;
        var i = new List$1(JsonDictionaryItem.$, 0);
        if (h != null) {
            for (var j = 0; j < h.length; j++) {
                i.add(h[j]);
            }
        }
        var k = new JsonDictionaryObject();
        k.item("line", ((function () {
            var $ret = new JsonDictionaryValue();
            $ret.e = 0;
            $ret.value = b;
            return $ret;
        })()));
        k.item("column", ((function () {
            var $ret = new JsonDictionaryValue();
            $ret.e = 0;
            $ret.value = c;
            return $ret;
        })()));
        k.item("endLine", ((function () {
            var $ret = new JsonDictionaryValue();
            $ret.e = 0;
            $ret.value = d;
            return $ret;
        })()));
        k.item("endColumn", ((function () {
            var $ret = new JsonDictionaryValue();
            $ret.e = 0;
            $ret.value = e;
            return $ret;
        })()));
        k.item("message", ((function () {
            var $ret = new JsonDictionaryValue();
            $ret.e = 2;
            $ret.value = f;
            return $ret;
        })()));
        i.add(k);
        g.items = i.toArray();
        return a;
    };
    DescriptionJsonValidator.prototype.a = function (a) {
        var b = new List$1(String_$type, 0);
        if (a != null && a.g("modules")) {
            if (typeCast(JsonDictionaryArray.$, a.item("modules")) !== null) {
                var c = a.item("modules");
                if (c.items != null) {
                    for (var d = 0; d < c.items.length; d++) {
                        var e = c.items[d];
                        var f = e.value.toString();
                        if (stringContains(f, "/")) {
                            var g = f.split('/');
                            b.add(g[1]);
                        }
                        else {
                            b.add(f);
                        }
                    }
                }
            }
        }
        return b.toArray();
    };
    DescriptionJsonValidator.prototype.b = function (a) {
        var e_1, _a;
        var b = new HashSet$1(String_$type, 0);
        this.m(b, null, a);
        var c = new List$1(String_$type, 0);
        try {
            for (var _b = __values(fromEnum(b)), _c = _b.next(); !_c.done; _c = _b.next()) {
                var d = _c.value;
                c.add(d);
            }
        }
        catch (e_1_1) {
            e_1 = { error: e_1_1 };
        }
        finally {
            try {
                if (_c && !_c.done && (_a = _b.return))
                    _a.call(_b);
            }
            finally {
                if (e_1)
                    throw e_1.error;
            }
        }
        return c.toArray();
    };
    DescriptionJsonValidator.prototype.m = function (a, b, c) {
        if (typeCast(JsonDictionaryObject.$, c) !== null) {
            var d = c;
            var e = d.e();
            for (var g = 0; g < e.length; g++) {
                var f = e[g];
                this.m(a, f, d.item(f));
            }
        }
        if (typeCast(JsonDictionaryArray.$, c) !== null) {
            var h = c;
            if (h.items != null) {
                for (var i = 0; i < h.items.length; i++) {
                    this.m(a, b + "[" + i + "]", h.items[i]);
                }
            }
        }
        if (typeCast(JsonDictionaryValue.$, c) !== null) {
            if (b != null && b == "type") {
                a.add_1(c.value.toString());
            }
        }
    };
    DescriptionJsonValidator.fromJson = function (a) {
        var b = new DescriptionJsonValidator();
        var c = new JsonDictionaryParser();
        var d = c.parse(a);
        var e = typeCast(JsonDictionaryObject.$, d);
        if (e != null) {
            if (e.g("types")) {
                var f = e.item("types");
                if (f.items != null) {
                    for (var g = 0; g < f.items.length; g++) {
                        b.i.add(f.items[g].value.toString());
                    }
                }
            }
            if (e.g("containingModules")) {
                var h = e.item("containingModules");
                var i = h.e();
                for (var j = 0; j < i.length; j++) {
                    var k = i[j];
                    var l = h.item(k);
                    b.h.item(k, new List$1(String_$type, 0));
                    for (var m = 0; m < l.items.length; m++) {
                        var n = l.items[m];
                        b.h.item(k).add(n.value.toString());
                    }
                }
            }
        }
        return b;
    };
    DescriptionJsonValidator.prototype.toJson = function () {
        var e_2, _a, e_3, _b;
        var a = new JsonDictionaryObject();
        var b = new JsonDictionaryArray();
        var c = new List$1(JsonDictionaryItem.$, 0);
        var _loop_1 = function (d) {
            c.add(((function () {
                var $ret = new JsonDictionaryValue();
                $ret.value = d;
                $ret.e = 2;
                return $ret;
            })()));
        };
        try {
            for (var _c = __values(fromEnum(this.i)), _d = _c.next(); !_d.done; _d = _c.next()) {
                var d = _d.value;
                _loop_1(d);
            }
        }
        catch (e_2_1) {
            e_2 = { error: e_2_1 };
        }
        finally {
            try {
                if (_d && !_d.done && (_a = _c.return))
                    _a.call(_c);
            }
            finally {
                if (e_2)
                    throw e_2.error;
            }
        }
        b.items = c.toArray();
        a.item("types", b);
        var e = new JsonDictionaryObject();
        var _loop_2 = function (f) {
            var e_4, _g;
            var g = new List$1(JsonDictionaryItem.$, 0);
            var _loop_3 = function (h) {
                g.add(((function () {
                    var $ret = new JsonDictionaryValue();
                    $ret.value = h;
                    $ret.e = 2;
                    return $ret;
                })()));
            };
            try {
                for (var _h = (e_4 = void 0, __values(fromEnum(this_1.h.item(f)))), _j = _h.next(); !_j.done; _j = _h.next()) {
                    var h = _j.value;
                    _loop_3(h);
                }
            }
            catch (e_4_1) {
                e_4 = { error: e_4_1 };
            }
            finally {
                try {
                    if (_j && !_j.done && (_g = _h.return))
                        _g.call(_h);
                }
                finally {
                    if (e_4)
                        throw e_4.error;
                }
            }
            e.item(f, ((function () {
                var $ret = new JsonDictionaryArray();
                $ret.items = g.toArray();
                return $ret;
            })()));
        };
        var this_1 = this;
        try {
            for (var _e = __values(fromEnum(this.h.keys)), _f = _e.next(); !_f.done; _f = _e.next()) {
                var f = _f.value;
                _loop_2(f);
            }
        }
        catch (e_3_1) {
            e_3 = { error: e_3_1 };
        }
        finally {
            try {
                if (_f && !_f.done && (_b = _e.return))
                    _b.call(_e);
            }
            finally {
                if (e_3)
                    throw e_3.error;
            }
        }
        a.item("containingModules", e);
        return a.b();
    };
    DescriptionJsonValidator.$t = markType(DescriptionJsonValidator, 'DescriptionJsonValidator');
    return DescriptionJsonValidator;
}(Base));
export { DescriptionJsonValidator };
