"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var axios_1 = require("axios");
var fs = require("fs");
// endregion
var OUTPUT_FILE = "./src/modules/modules.tsx";
function camelToConstCase(camelStr) {
    return camelStr
        .replace(/([a-z])([A-Z])/g, "$1_$2")
        .toUpperCase();
}
var writeToFile = function (data) {
    try {
        fs.writeFileSync(OUTPUT_FILE, data.trim());
        console.log("File successfully generated.");
    }
    catch (error) {
        console.error(error);
    }
};
var getImports = function (modules) {
    var prefix = "import { ";
    var postfix = " } from \"@tabler/icons-react\"\n" +
        "import ModulesType from \"./types.tsx\";\n" +
        "import connectModules from \"./connectModules.tsx\";";
    var filteredModules = modules.filter(function (module) { return module.iconName; });
    var icons = filteredModules.map(function (module) { return module.iconName; }).join(", ");
    return prefix + icons + postfix;
};
var getModuleNames = function (modules) {
    return modules.map(function (module) {
        return "".concat(camelToConstCase(module.key), " = \"").concat(module.key, "\"");
    }).join(",\n\t");
};
var getModules = function (modules) {
    return modules.map(function (module) {
        var iconStr = "null";
        if (module.iconName) {
            iconStr = "<" + module.iconName + " />";
        }
        return "[ModuleNames.".concat(camelToConstCase(module.key), "]: { info: { label: \"").concat(module.label, "\", key: \"").concat(module.key, "\", icon: ").concat(iconStr, " } }");
    }).join(",\n\t");
};
var generateRows = function (modules) {
    var imports = getImports(modules);
    var moduleNames = "\n\nexport enum ModuleNames {\n\t".concat(getModuleNames(modules), "\n}\n");
    var modulesStr = "\nconst modules: ModulesType = {\n\t".concat(getModules(modules), "\n};\n");
    var connectModules = "\nexport const MODULES = connectModules(modules);";
    var result = imports + moduleNames + modulesStr + connectModules;
    writeToFile(result);
};
var modulesFileGen = function () {
    console.log("Start file generation...");
    axios_1.default
        .get("http://127.0.0.1:8000/project/modules")
        .then(function (response) {
        generateRows(response.data.modules);
    })
        .catch(function (err) { return console.log(err); });
};
modulesFileGen();
