"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ReactTemplate = void 0;
const cli_core_1 = require("@igniteui/cli-core");
const fs = require("fs-extra");
const path = require("path");
class ReactTemplate {
    /**
     * Base ReactTemplate constructor
     * @param rootPath The template folder path. Pass in `__dirname`
     */
    constructor(rootPath) {
        this.rootPath = rootPath;
        this.listInComponentTemplates = true;
        this.listInCustomTemplates = false;
        this.dependencies = [];
        this.framework = "react";
        this.hasExtraConfiguration = false;
        this.packages = [];
        this.delimiters = cli_core_1.defaultDelimiters;
        this.appRoutsPath = "./src/routes.json";
        this.igniteResources = "src/igniteuiResources.js";
    }
    get templatePaths() {
        return [path.join(this.rootPath, "files")];
    }
    generateConfig(name, options) {
        let config = {};
        if (options["extraConfig"]) {
            config = options["extraConfig"];
        }
        config["path"] = this.folderName(name); //folder name allowed spaces, any casing
        config["name"] = cli_core_1.Util.nameFromPath(name); // this name should not have restrictions
        config["ClassName"] = cli_core_1.Util.className(cli_core_1.Util.nameFromPath(name)); //first letter capital, no spaces and no dashes,
        config["cliVersion"] = cli_core_1.Util.version();
        if (this.widget) {
            config["widget"] = this.widget;
            config["Control"] = cli_core_1.Util.className(this.widget);
        }
        if (this.description) {
            config["description"] = this.description;
        }
        return config;
    }
    registerInProject(projectPath, name, options) {
        if (options && options.skipRoute) {
            return;
        }
        // add Routing config for each added component
        const appRoutsFile = fs.readFileSync(path.join(projectPath, this.appRoutsPath), "utf8");
        const viewsArr = JSON.parse(appRoutsFile);
        viewsArr.push({
            // tslint:disable:object-literal-sort-keys
            path: "/" + this.folderName(cli_core_1.Util.nameFromPath(name)),
            componentPath: this.getViewLink(name),
            text: this.getNavText(name)
        });
        fs.writeFileSync(path.join(projectPath, this.appRoutsPath), JSON.stringify(viewsArr, null, 4));
        this.registerSourceFiles(projectPath);
    }
    getExtraConfiguration() {
        throw new Error("Method not implemented.");
    }
    setExtraConfiguration(extraConfigKeys) {
        throw new Error("Method not implemented.");
    }
    folderName(pathName) {
        //TODO: should remove the spaces
        const parts = path.parse(pathName);
        let folderName = pathName;
        if (parts.dir) {
            folderName = path.join(parts.dir, parts.name);
            folderName = folderName.replace(/\\/g, "/");
            // TODO: config-based "src/app"?
            const relative = path.join(process.cwd(), "components", folderName);
            // path.join will also resolve any '..' segments
            // so if relative result doesn't start with CWD it's out of project root
            if (!relative.startsWith(process.cwd())) {
                cli_core_1.Util.error(`Path ${"components/" + folderName} is not valid!`, "red");
                process.exit(1);
            }
            //clean up potential leading spaces in folder names (`path/    name`):
            folderName = folderName.replace(/\/\s+/g, "/");
        }
        return cli_core_1.Util.lowerDashed(folderName);
    }
    getViewLink(name) {
        return "./components/" + this.folderName(name);
    }
    registerSourceFiles(projectPath) {
        // tslint:disable:no-submodule-imports
        const components = require("@igniteui/cli-core/packages/components");
        const igResPath = path.join(projectPath, this.igniteResources);
        if (fs.existsSync(igResPath)) {
            let igniteuiResFile = fs.readFileSync(igResPath, "utf8");
            const freeVersionPath = "ignite-ui/";
            const fullVersionPath = "@infragistics/ignite-ui-full/en/";
            const dvPath = "@infragistics/ignite-ui-full/en/js/infragistics.dv.js";
            const dvDep = this.dependencies.filter(x => components.dv.indexOf(x) !== -1).length !== 0;
            const fullDep = this.dependencies.filter(x => components.full.indexOf(x) !== -1).length !== 0;
            if (fullDep) {
                while (igniteuiResFile.includes(freeVersionPath)) {
                    igniteuiResFile = igniteuiResFile.replace(freeVersionPath, fullVersionPath);
                    igniteuiResFile = igniteuiResFile.replace("-lite", "");
                }
                fs.writeFileSync(igResPath, igniteuiResFile);
            }
            if (dvDep && !igniteuiResFile.includes(dvPath)) {
                fs.appendFileSync(igResPath, `${'\r\n// Ignite UI Charts Required JavaScript File\r\nimport "'
                    + dvPath + '";\r\n'}`);
            }
        }
        else {
            cli_core_1.Util.log(`igniteuiResources.js file NOT found!`);
        }
    }
    getNavText(name) {
        name = cli_core_1.Util.nameFromPath(name);
        return name.replace(/\w\S*/g, txt => txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase());
    }
}
exports.ReactTemplate = ReactTemplate;
