/*
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 { Base, String_$type, typeCast, fromEnum, Number_$type, markType } from "./type";
import { ICodeGenerationRendererTemplate_$type } from "./ICodeGenerationRendererTemplate";
import { List$1 } from "./List$1";
import { StringBuilder } from "./StringBuilder";
import { JsonDictionaryParser } from "./JsonDictionaryParser";
import { JsonDictionaryObject } from "./JsonDictionaryObject";
import { JsonDictionaryArray } from "./JsonDictionaryArray";
import { HashSet$1 } from "./HashSet$1";
import { CodeGeneratingImportManager } from "./CodeGeneratingImportManager";
import { CodeGeneratingRegexHelper } from "./CodeGeneratingRegexHelper";
import { Tuple$3 } from "./Tuple$3";
import { stringReplace, stringStartsWith } from "./string";
/**
 * @hidden
 */
export let CodeGenerationFolderTemplate = /*@__PURE__*/ (() => {
    class CodeGenerationFolderTemplate extends Base {
        constructor() {
            super(...arguments);
            this.d = new List$1(String_$type, 0);
            this.b = new List$1(String_$type, 0);
            this.c = new List$1(String_$type, 0);
        }
        l(a, b) {
            this.d.add(a);
            this.b.add(b);
        }
        a() {
            return this.d.toArray();
        }
        f(a) {
            for (let b = 0; b < this.d.count; b++) {
                if (this.d._inner[b] == a) {
                    return this.c._inner[b];
                }
            }
            return null;
        }
        k() {
            let a = new StringBuilder(0);
            a.u("{");
            a.u("\"files\": [");
            for (let b = 0; b < this.d.count; b++) {
                if (b >= 1) {
                    a.u(",");
                }
                a.u("{");
                a.l("\"path\": \"");
                a.l(this.d._inner[b]);
                a.u("\"");
                a.l("\"content\": \"");
                a.l(stringReplace(this.b._inner[b], "\"", "\\\""));
                a.u("\"");
                a.u("}");
            }
            a.u("]");
            a.u("}");
            return a.toString();
        }
        m(a) {
            let b = new JsonDictionaryParser();
            let c = b.parse(a);
            if (typeCast(JsonDictionaryObject.$, c) !== null) {
                let d = c;
                if (d.g("files")) {
                    let e = d.item("Files");
                    if (typeCast(JsonDictionaryArray.$, e) !== null) {
                        let f = e;
                        for (let g = 0; g < f.items.length; g++) {
                            let h = f.items[g];
                            if (typeCast(JsonDictionaryObject.$, h) !== null) {
                                let 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());
                                }
                            }
                        }
                    }
                }
            }
        }
        execute(a) {
            this.c.clear();
            for (let b = 0; b < this.b.count; b++) {
                this.c.add(this.b._inner[b]);
            }
            let c = a.getKeys();
            let d = new HashSet$1(String_$type, 0);
            for (let e = 0; e < c.length; e++) {
                d.add_1(c[e]);
            }
            let f = a.getImportsKeys();
            for (let g = 0; g < f.length; g++) {
                d.add_1(f[g]);
            }
            let h = a.getStringNames();
            for (let i = 0; i < h.length; i++) {
                d.add_1(h[i]);
            }
            for (let j = 0; j < this.b.count; j++) {
                let k = this.c._inner[j];
                k = this.h(k, d);
                this.c._inner[j] = k;
            }
            for (let l = 0; l < this.b.count; l++) {
                let m = new List$1(String_$type, 0);
                let p = a.getImportsKeys();
                for (let o = 0; o < p.length; o++) {
                    let n = p[o];
                    m.add(n);
                }
                let q = this.c._inner[l];
                q = this.j(q, m.toArray(), a);
                this.c._inner[l] = q;
            }
            let t = a.getKeys();
            for (let s = 0; s < t.length; s++) {
                let r = t[s];
                for (let u = 0; u < this.b.count; u++) {
                    let v = this.c._inner[u];
                    let w = a.getContentForKey(r);
                    v = this.i(v, r, w, a);
                    this.c._inner[u] = v;
                }
            }
            let z = a.getStringNames();
            for (let y = 0; y < z.length; y++) {
                let x = z[y];
                for (let aa = 0; aa < this.b.count; aa++) {
                    let ab = this.c._inner[aa];
                    ab = stringReplace(ab, "$$" + x, a.getString(x));
                    this.c._inner[aa] = ab;
                }
            }
            for (let ac of fromEnum(a.getNewFiles())) {
                let ad = a.getNewFileContent(ac);
                let ae = "";
                for (let af of fromEnum(this.d)) {
                    if (stringStartsWith(af, "src")) {
                        ae = "src/";
                    }
                }
                this.d.add(ae + ac);
                this.c.add(ad);
            }
        }
        h(a, b) {
            return a;
        }
        j(a, b, c) {
            let d = new List$1(Tuple$3.$.specialize(Number_$type, String_$type, CodeGeneratingImportManager.$), 0);
            for (let f = 0; f < b.length; f++) {
                let e = b[f];
                let g = new CodeGeneratingRegexHelper("(?:(<!--)|(\\/\\/))\\s*insert " + e + "(?:(\\s*-->)?)[\\w\\W\\r\\n]*?(?:(<!--)|(\\/\\/))\\s*end " + e + "(?:(\\s*-->)?)((\\r\\n)|(\\n))");
                a = g.b(a, (h, i, j) => {
                    let k = c.o(e);
                    let l = new CodeGeneratingImportManager();
                    for (let m of fromEnum(k)) {
                        l = CodeGeneratingImportManager.merge(l, m);
                    }
                    d.add(new Tuple$3(Number_$type, String_$type, CodeGeneratingImportManager.$, i, e, l));
                    return null;
                });
            }
            d.aa((h, i) => Base.compareSimple(h.c, i.c));
            let h = new CodeGeneratingImportManager();
            for (let i of fromEnum(d)) {
                let j = i.d;
                let k = i.e;
                let l = k.removeDuplicates(h);
                h = CodeGeneratingImportManager.merge(h, k);
                let m = new CodeGeneratingRegexHelper("(?:(<!--)|(\\/\\/))\\s*insert " + j + "(?:(\\s*-->)?)[\\w\\W\\r\\n]*?(?:(<!--)|(\\/\\/))\\s*end " + j + "(?:(\\s*-->)?)((\\r\\n)|(\\n))");
                a = m.b(a, (n, o, p) => {
                    let q = this.g(l, c);
                    if (q != null && q.length > 0) {
                        q = this.e(q, a, o, c);
                    }
                    return q;
                });
            }
            return a;
        }
        e(a, b, c, d) {
            let e = c;
            let f = 0;
            let g = 0;
            let h = "";
            while (e > 0) {
                if (b.charAt(e) == ' ') {
                    h += ' ';
                }
                if (b.charAt(e) == '\t') {
                    h += '\t';
                }
                if (b.charAt(e) == '\n') {
                    break;
                }
                e--;
            }
            let i = new StringBuilder(0);
            let j = stringReplace(a, "\r\n", "\n").split('\n');
            for (let k = 0; k < j.length; k++) {
                if (k == j.length - 1 && j[k].length == 0) {
                    continue;
                }
                let l = j[k];
                if (k > 0) {
                    i.l(h);
                }
                i.u(l);
            }
            return i.toString();
        }
        g(a, b) {
            if (a.hasPackageImports) {
                return a.packageImportsToString();
            }
            else {
                return a.usingsToString();
            }
        }
        i(a, b, c, d) {
            let e = new CodeGeneratingRegexHelper("(?:(<!--)|(\\/\\/))\\s*insert " + b + "(?:(\\s*-->)?)[\\w\\W\\r\\n]*?(?:(<!--)|(\\/\\/))\\s*end " + b + "(?:(\\s*-->)?)((\\r\\n)|(\\n))");
            a = e.b(a, (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;
})();
