/*
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, markType } from "./type";
import { CodeGeneratingCodeWriter } from "./CodeGeneratingCodeWriter";
import { CodeGeneratingImportManager } from "./CodeGeneratingImportManager";
import { HashSet$1 } from "./HashSet$1";
import { JsonDictionaryParser } from "./JsonDictionaryParser";
import { stringReplace, stringContains } from "./string";
/**
 * @hidden
 */
export let CodeGeneratingComponentRendererHandlerEmitter = /*@__PURE__*/ (() => {
    class CodeGeneratingComponentRendererHandlerEmitter extends Base {
        constructor(a, b) {
            super();
            this.o = null;
            this.m = null;
            this.e = null;
            this.a = null;
            this.k = new CodeGeneratingImportManager();
            this.b = null;
            this.d = null;
            this.c = null;
            this.p = new HashSet$1(String_$type, 0);
            this.o = a;
            this.m = b;
            this.e = new CodeGeneratingCodeWriter(this.o);
            this.b = new CodeGeneratingCodeWriter(this.o);
            this.d = new CodeGeneratingCodeWriter(this.o);
            this.c = new CodeGeneratingCodeWriter(this.o);
            this.a = new CodeGeneratingCodeWriter(this.o);
        }
        get n() {
            return this.m;
        }
        get j() {
            return this.e;
        }
        get f() {
            return this.a;
        }
        get l() {
            return this.k;
        }
        get g() {
            return this.b;
        }
        get i() {
            return this.d;
        }
        get h() {
            return this.c;
        }
        u(a, b) {
        }
        t(a, b) {
        }
        s(a, b) {
            let c = new HashSet$1(String_$type, 0);
            if (a != null && a.length > 0) {
                for (let d = 0; d < a.length; d++) {
                    let e = a[d];
                    if (e != null) {
                        this.r(e, c, true, false);
                    }
                }
            }
            if (b != null && b.length > 0) {
                for (let f = 0; f < b.length; f++) {
                    let g = b[f];
                    if (g != null) {
                        this.r(g, c, false, true);
                    }
                }
            }
            for (let h = 0; h < this.m.t.count; h++) {
                let i = this.m.t._inner[h];
                if (i.a != null && i.c == null) {
                    this.r(i.a.e, c, false, false);
                }
            }
            this.m.addContainerResult("eventHandlers", this.j.toString());
            this.m.addContainerResult("eventScript", this.f.toString());
            this.m.addImportResult("handlersImports", this.l);
            this.m.addContainerResult("onInit", this.g.toString());
            this.m.addContainerResult("handlersStyles", this.i.toString());
            this.m.addContainerResult("onViewInit", this.h.toString());
        }
        r(a, b, c, d) {
            if (this.o.library != null && this.o.library.hasItem(a)) {
                if (b.contains(a)) {
                    return;
                }
                b.add_1(a);
                let e = this.o.library.getItem(a);
                if (e.type == 1) {
                    let f = false;
                    let g = e.getContentForPlatform(this.m.platform);
                    if (g != null) {
                        if (g.isJson) {
                            let h = new JsonDictionaryParser();
                            let i = h.parse(g.content);
                            if (i.g("imports")) {
                                let j = i.item("imports").value;
                                let k = stringReplace(j, "\r\n", "\n").split('\n');
                                for (let m = 0; m < k.length; m++) {
                                    let l = k[m];
                                    if (l.trim().length == 0) {
                                        continue;
                                    }
                                    if (!this.p.contains(l)) {
                                        this.p.add_1(l);
                                        this.v(this.l, l);
                                    }
                                }
                            }
                            if (i.g("styles")) {
                                this.i.k(i.item("styles").value);
                            }
                            if (i.g("handler")) {
                                let n = this.j;
                                if (g.isJSContent) {
                                    n = this.f;
                                }
                                let o = i.item("handler").value;
                                o = this.q(o);
                                if (!stringContains(o, "OMIT HANDLER")) {
                                    let p = stringReplace(o, "\r\n", "\n").split('\n');
                                    let q = 0x7FFFFFFF;
                                    let r = "";
                                    for (let s = 0; s < this.o.indentSpaces; s++) {
                                        r += " ";
                                    }
                                    for (let u = 0; u < p.length; u++) {
                                        let t = p[u];
                                        let v = t;
                                        if (v.trim().length == 0) {
                                            continue;
                                        }
                                        v = stringReplace(t, "\t", r);
                                        let w = 0;
                                        for (let x = 0; x < v.length; x++) {
                                            if (v.charAt(x) != ' ' && v.charAt(x) != '\t') {
                                                break;
                                            }
                                            w++;
                                        }
                                        if (w < q) {
                                            q = w;
                                        }
                                    }
                                    for (let z = 0; z < p.length; z++) {
                                        let y = p[z];
                                        let aa = y;
                                        let ab = 0;
                                        if (aa.trim().length > 0) {
                                            while (aa.length > 0 && (aa.charAt(0) == ' ' || aa.charAt(0) == '\t') && ab < q) {
                                                if (aa.charAt(0) == ' ') {
                                                    aa = aa.substr(1);
                                                    ab++;
                                                }
                                                if (aa.charAt(0) == '\t') {
                                                    aa = aa.substr(1);
                                                    ab += this.o.indentSpaces;
                                                }
                                            }
                                        }
                                        n.l(aa);
                                    }
                                }
                                else {
                                    f = true;
                                }
                            }
                        }
                        else {
                            let ac = g.content;
                            this.j.l(ac);
                        }
                    }
                    if (c && !f) {
                        this.t(a, g.isJSContent);
                    }
                    if (d && !f) {
                        this.u(a, g.isJSContent);
                    }
                }
            }
        }
        v(a, b) {
        }
        q(a) {
            return a;
        }
    }
    CodeGeneratingComponentRendererHandlerEmitter.$t = markType(CodeGeneratingComponentRendererHandlerEmitter, 'CodeGeneratingComponentRendererHandlerEmitter');
    return CodeGeneratingComponentRendererHandlerEmitter;
})();
