/*
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, __values } from "tslib";
import { Base, String_$type, typeCast, fromEnum, Number_$type, markType } from "./type";
import { ICodeGenerationRendererTemplate_$type } from "./ICodeGenerationRendererTemplate";
import { List$1 } from "./List$1";
import { JsonDictionaryObject } from "./JsonDictionaryObject";
import { JsonDictionaryArray } from "./JsonDictionaryArray";
import { JsonDictionaryItem } from "./JsonDictionaryItem";
import { JsonDictionaryValue } from "./JsonDictionaryValue";
import { JsonDictionaryParser } from "./JsonDictionaryParser";
import { HashSet$1 } from "./HashSet$1";
import { CodeGeneratingImportManager } from "./CodeGeneratingImportManager";
import { CodeGeneratingRegexHelper } from "./CodeGeneratingRegexHelper";
import { Tuple$3 } from "./Tuple$3";
import { StringBuilder } from "./StringBuilder";
import { stringReplace, stringStartsWith, stringEndsWith } from "./string";
/**
 * @hidden
 */
var CodeGenerationFolderTemplate = /** @class */ /*@__PURE__*/ (function (_super) {
    __extends(CodeGenerationFolderTemplate, _super);
    function CodeGenerationFolderTemplate() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.d = new List$1(String_$type, 0);
        _this.b = new List$1(String_$type, 0);
        _this.c = new List$1(String_$type, 0);
        return _this;
    }
    CodeGenerationFolderTemplate.prototype.addFile = function (a, b) {
        this.d.add(a);
        this.b.add(b);
    };
    CodeGenerationFolderTemplate.prototype.getFilePaths = function () {
        return this.d.toArray();
    };
    CodeGenerationFolderTemplate.prototype.getFileOuutput = function (a) {
        for (var b = 0; b < this.d.count; b++) {
            if (this.d._inner[b] == a) {
                return this.c._inner[b];
            }
        }
        return null;
    };
    CodeGenerationFolderTemplate.prototype.saveTemplateToJson = function () {
        var _this = this;
        var a = new JsonDictionaryObject();
        var b = new JsonDictionaryArray();
        var c = new List$1(JsonDictionaryItem.$, 0);
        var _loop_1 = function (d) {
            var e = new JsonDictionaryObject();
            var f = ((function () {
                var $ret = new JsonDictionaryValue();
                $ret.e = 2;
                $ret.value = _this.d._inner[d];
                return $ret;
            })());
            var g = ((function () {
                var $ret = new JsonDictionaryValue();
                $ret.e = 2;
                $ret.value = _this.b._inner[d];
                return $ret;
            })());
            e.item("path", f);
            e.item("content", g);
            c.add(e);
        };
        for (var d = 0; d < this.d.count; d++) {
            _loop_1(d);
        }
        b.items = c.toArray();
        a.item("files", b);
        return a.b();
    };
    CodeGenerationFolderTemplate.prototype.loadTemplateFromJson = function (a) {
        var b = new JsonDictionaryParser();
        var c = b.parse(a);
        if (typeCast(JsonDictionaryObject.$, c) !== null) {
            var d = c;
            if (d.g("files")) {
                var e = d.item("Files");
                if (typeCast(JsonDictionaryArray.$, e) !== null) {
                    var f = e;
                    for (var g = 0; g < f.items.length; g++) {
                        var h = f.items[g];
                        if (typeCast(JsonDictionaryObject.$, h) !== null) {
                            var i = h;
                            if (i.g("path") && i.g("content")) {
                                this.d.add(i.item("path").value.toString());
                                this.b.add(i.item("content").value.toString());
                            }
                        }
                    }
                }
            }
        }
    };
    CodeGenerationFolderTemplate.prototype.execute = function (a) {
        var e_1, _a, e_2, _b;
        this.c.clear();
        for (var b = 0; b < this.b.count; b++) {
            this.c.add(this.b._inner[b]);
        }
        var c = a.getKeys();
        var d = new HashSet$1(String_$type, 0);
        for (var e = 0; e < c.length; e++) {
            d.add_1(c[e]);
        }
        var f = a.getImportsKeys();
        for (var g = 0; g < f.length; g++) {
            d.add_1(f[g]);
        }
        var h = a.getStringNames();
        for (var i = 0; i < h.length; i++) {
            d.add_1(h[i]);
        }
        var j = a.getPackages();
        for (var k = 0; k < j.length; k++) {
            d.add_1(j[k]);
        }
        for (var l = 0; l < this.b.count; l++) {
            var m = this.c._inner[l];
            m = this.h(m, d);
            this.c._inner[l] = m;
        }
        for (var n = 0; n < this.b.count; n++) {
            var o = new List$1(String_$type, 0);
            var r = a.getImportsKeys();
            for (var q = 0; q < r.length; q++) {
                var p = r[q];
                o.add(p);
            }
            var s = this.c._inner[n];
            var t = this.d._inner[n];
            s = this.j(t, s, o.toArray(), a);
            this.c._inner[n] = s;
        }
        var w = a.getKeys();
        for (var v = 0; v < w.length; v++) {
            var u = w[v];
            for (var x = 0; x < this.b.count; x++) {
                var y = this.c._inner[x];
                var z = a.getContentForKey(u);
                y = this.i(y, u, z, a);
                this.c._inner[x] = y;
            }
        }
        var ac = a.getStringNames();
        for (var ab = 0; ab < ac.length; ab++) {
            var aa = ac[ab];
            for (var ad = 0; ad < this.b.count; ad++) {
                var ae = this.c._inner[ad];
                ae = stringReplace(ae, "$$" + aa, a.getString(aa));
                this.c._inner[ad] = ae;
            }
        }
        try {
            for (var _c = __values(fromEnum(a.getNewFiles())), _d = _c.next(); !_d.done; _d = _c.next()) {
                var af = _d.value;
                var ag = a.getNewFileContent(af);
                var ah = "";
                try {
                    for (var _e = (e_2 = void 0, __values(fromEnum(this.d))), _f = _e.next(); !_f.done; _f = _e.next()) {
                        var ai = _f.value;
                        if (stringStartsWith(ai, "src")) {
                            ah = "src/";
                        }
                    }
                }
                catch (e_2_1) {
                    e_2 = { error: e_2_1 };
                }
                finally {
                    try {
                        if (_f && !_f.done && (_b = _e.return))
                            _b.call(_e);
                    }
                    finally {
                        if (e_2)
                            throw e_2.error;
                    }
                }
                this.d.add(ah + af);
                this.c.add(ag);
            }
        }
        catch (e_1_1) {
            e_1 = { error: e_1_1 };
        }
        finally {
            try {
                if (_d && !_d.done && (_a = _c.return))
                    _a.call(_c);
            }
            finally {
                if (e_1)
                    throw e_1.error;
            }
        }
    };
    CodeGenerationFolderTemplate.prototype.h = function (a, b) {
        return a;
    };
    CodeGenerationFolderTemplate.prototype.j = function (a, b, c, d) {
        var e_3, _a;
        var _this = this;
        var e = new List$1(Tuple$3.$.specialize(Number_$type, String_$type, CodeGeneratingImportManager.$), 0);
        var _loop_2 = function (g) {
            var f = c[g];
            var h = new CodeGeneratingRegexHelper("(?:(<!--)|(\\/\\/))\\s*insert " + f + "(?:(\\s*-->)?)[\\w\\W\\r\\n]*?(?:(<!--)|(\\/\\/))\\s*end " + f + "(?:(\\s*-->)?)((\\r\\n)|(\\n))");
            b = h.execute(b, function (i, j, k) {
                var e_4, _a;
                var l = d.r(f);
                var m = new CodeGeneratingImportManager();
                try {
                    for (var _b = (e_4 = void 0, __values(fromEnum(l))), _c = _b.next(); !_c.done; _c = _b.next()) {
                        var n = _c.value;
                        m = CodeGeneratingImportManager.merge(m, n);
                    }
                }
                catch (e_4_1) {
                    e_4 = { error: e_4_1 };
                }
                finally {
                    try {
                        if (_c && !_c.done && (_a = _b.return))
                            _a.call(_b);
                    }
                    finally {
                        if (e_4)
                            throw e_4.error;
                    }
                }
                e.add(new Tuple$3(Number_$type, String_$type, CodeGeneratingImportManager.$, j, f, m));
                return null;
            });
        };
        for (var g = 0; g < c.length; g++) {
            _loop_2(g);
        }
        e.aa(function (i, j) { return Base.compareSimple(i.c, j.c); });
        var i = new CodeGeneratingImportManager();
        var _loop_3 = function (j) {
            var k = j.d;
            var l = j.e;
            var m = l.removeDuplicates(i);
            i = CodeGeneratingImportManager.merge(i, l);
            var n = new CodeGeneratingRegexHelper("(?:(<!--)|(\\/\\/))\\s*insert " + k + "(?:(\\s*-->)?)[\\w\\W\\r\\n]*?(?:(<!--)|(\\/\\/))\\s*end " + k + "(?:(\\s*-->)?)((\\r\\n)|(\\n))");
            b = n.execute(b, function (o, p, q) {
                var r = _this.g(a, m, d);
                if (r != null && r.length > 0) {
                    r = _this.e(r, b, p, d);
                }
                return r;
            });
        };
        try {
            for (var _b = __values(fromEnum(e)), _c = _b.next(); !_c.done; _c = _b.next()) {
                var j = _c.value;
                _loop_3(j);
            }
        }
        catch (e_3_1) {
            e_3 = { error: e_3_1 };
        }
        finally {
            try {
                if (_c && !_c.done && (_a = _b.return))
                    _a.call(_b);
            }
            finally {
                if (e_3)
                    throw e_3.error;
            }
        }
        return b;
    };
    CodeGenerationFolderTemplate.prototype.e = function (a, b, c, d) {
        var e = c;
        var f = 0;
        var g = 0;
        var h = "";
        while (e > 0) {
            if (b.charAt(e) == ' ') {
                h += ' ';
            }
            if (b.charAt(e) == '\t') {
                h += '\t';
            }
            if (b.charAt(e) == '\n') {
                break;
            }
            e--;
        }
        var i = new StringBuilder(0);
        var j = stringReplace(a, "\r\n", "\n").split('\n');
        for (var k = 0; k < j.length; k++) {
            if (k == j.length - 1 && j[k].length == 0) {
                continue;
            }
            var l = j[k];
            if (k > 0) {
                i.l(h);
            }
            i.u(l);
        }
        return i.toString();
    };
    CodeGenerationFolderTemplate.prototype.g = function (a, b, c) {
        if (b.hasPackageImports) {
            return b.packageImportsToString();
        }
        else {
            var d = false;
            if (stringEndsWith(a, ".razor")) {
                d = true;
            }
            return b.usingsToString(d);
        }
    };
    CodeGenerationFolderTemplate.prototype.i = function (a, b, c, d) {
        var _this = this;
        var e = new CodeGeneratingRegexHelper("(?:(<!--)|(\\/\\/))\\s*insert " + b + "(?:(\\s*-->)?)[\\w\\W\\r\\n]*?(?:(<!--)|(\\/\\/))\\s*end " + b + "(?:(\\s*-->)?)((\\r\\n)|(\\n))");
        a = e.execute(a, function (f, g, h) {
            if (c != null && c.length > 0) {
                c = _this.e(c, a, g, d);
            }
            return c;
        });
        return a;
    };
    CodeGenerationFolderTemplate.$t = markType(CodeGenerationFolderTemplate, 'CodeGenerationFolderTemplate', Base.$, [ICodeGenerationRendererTemplate_$type]);
    return CodeGenerationFolderTemplate;
}(Base));
export { CodeGenerationFolderTemplate };
