/*
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, 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 } from "./string";
/**
 * @hidden
 */
var CodeGenerationFolderTemplate = /** @class */ /*@__PURE__*/ (function (_super) {
    tslib_1.__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];
            s = this.j(s, o.toArray(), a);
            this.c._inner[n] = s;
        }
        var v = a.getKeys();
        for (var u = 0; u < v.length; u++) {
            var t = v[u];
            for (var w = 0; w < this.b.count; w++) {
                var x = this.c._inner[w];
                var y = a.getContentForKey(t);
                x = this.i(x, t, y, a);
                this.c._inner[w] = x;
            }
        }
        var ab = a.getStringNames();
        for (var aa = 0; aa < ab.length; aa++) {
            var z = ab[aa];
            for (var ac = 0; ac < this.b.count; ac++) {
                var ad = this.c._inner[ac];
                ad = stringReplace(ad, "$$" + z, a.getString(z));
                this.c._inner[ac] = ad;
            }
        }
        try {
            for (var _c = tslib_1.__values(fromEnum(a.getNewFiles())), _d = _c.next(); !_d.done; _d = _c.next()) {
                var ae = _d.value;
                var af = a.getNewFileContent(ae);
                var ag = "";
                try {
                    for (var _e = (e_2 = void 0, tslib_1.__values(fromEnum(this.d))), _f = _e.next(); !_f.done; _f = _e.next()) {
                        var ah = _f.value;
                        if (stringStartsWith(ah, "src")) {
                            ag = "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(ag + ae);
                this.c.add(af);
            }
        }
        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) {
        var e_3, _a;
        var _this = this;
        var d = new List$1(Tuple$3.$.specialize(Number_$type, String_$type, CodeGeneratingImportManager.$), 0);
        var _loop_2 = function (f) {
            var e = b[f];
            var g = new CodeGeneratingRegexHelper("(?:(<!--)|(\\/\\/))\\s*insert " + e + "(?:(\\s*-->)?)[\\w\\W\\r\\n]*?(?:(<!--)|(\\/\\/))\\s*end " + e + "(?:(\\s*-->)?)((\\r\\n)|(\\n))");
            a = g.b(a, function (h, i, j) {
                var e_4, _a;
                var k = c.r(e);
                var l = new CodeGeneratingImportManager();
                try {
                    for (var _b = (e_4 = void 0, tslib_1.__values(fromEnum(k))), _c = _b.next(); !_c.done; _c = _b.next()) {
                        var m = _c.value;
                        l = CodeGeneratingImportManager.merge(l, m);
                    }
                }
                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;
                    }
                }
                d.add(new Tuple$3(Number_$type, String_$type, CodeGeneratingImportManager.$, i, e, l));
                return null;
            });
        };
        for (var f = 0; f < b.length; f++) {
            _loop_2(f);
        }
        d.aa(function (h, i) { return Base.compareSimple(h.c, i.c); });
        var h = new CodeGeneratingImportManager();
        var _loop_3 = function (i) {
            var j = i.d;
            var k = i.e;
            var l = k.removeDuplicates(h);
            h = CodeGeneratingImportManager.merge(h, k);
            var m = new CodeGeneratingRegexHelper("(?:(<!--)|(\\/\\/))\\s*insert " + j + "(?:(\\s*-->)?)[\\w\\W\\r\\n]*?(?:(<!--)|(\\/\\/))\\s*end " + j + "(?:(\\s*-->)?)((\\r\\n)|(\\n))");
            a = m.b(a, function (n, o, p) {
                var q = _this.g(l, c);
                if (q != null && q.length > 0) {
                    q = _this.e(q, a, o, c);
                }
                return q;
            });
        };
        try {
            for (var _b = tslib_1.__values(fromEnum(d)), _c = _b.next(); !_c.done; _c = _b.next()) {
                var i = _c.value;
                _loop_3(i);
            }
        }
        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 a;
    };
    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) {
        if (a.hasPackageImports) {
            return a.packageImportsToString();
        }
        else {
            return a.usingsToString();
        }
    };
    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.b(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 };
