"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.IgniteUIForWebComponentsTemplate = void 0;
const cli_core_1 = require("@igniteui/cli-core");
const path = require("path");
const WebComponentsTypeScriptFileUpdate_1 = require("../../templates/webcomponents/WebComponentsTypeScriptFileUpdate");
class IgniteUIForWebComponentsTemplate {
    get templatePaths() {
        return [path.join(this.rootPath, "files")];
    }
    constructor(rootPath) {
        this.rootPath = rootPath;
        this.listInComponentTemplates = true;
        this.listInCustomTemplates = false;
        this.framework = "webcomponents";
        this.projectType = "igc-ts";
        this.hasExtraConfiguration = false;
        this.packages = [];
        this.dependencies = [];
        this.delimiters = cli_core_1.defaultDelimiters;
    }
    generateConfig(name, options) {
        if (options["modulePath"] && !cli_core_1.Util.fileExists(path.join(process.cwd(), `src\\app`, options["modulePath"]))) {
            cli_core_1.Util.error(`Wrong module path provided: ${options["modulePath"]}. No components were added!`);
            return Promise.resolve(false);
        }
        return Object.assign({}, options["extraConfig"], this.getBaseVariables(name));
    }
    registerInProject(projectPath, fullName, options, defaultPath = false) {
        if (!options.parentName) {
            return;
        }
        const routeModulePath = options.parentRoutingModulePath;
        const routingModule = new WebComponentsTypeScriptFileUpdate_1.WebComponentsTypeScriptFileUpdate(path.join(projectPath, routeModulePath), { convertTabsToSpaces: false, indentSize: 4, singleQuotes: false });
        if (!(options && options.skipRoute) && cli_core_1.App.container.get(cli_core_1.FS_TOKEN)
            .fileExists(routeModulePath)) {
            const modulePath = `./${cli_core_1.Util.lowerDashed(fullName)}/${cli_core_1.Util.lowerDashed(fullName)}-routing`;
            const child = {
                identifierName: cli_core_1.ROUTES_VARIABLE_NAME,
                aliasName: options.routerChildren,
                modulePath
            };
            const children = routeModulePath.includes(cli_core_1.WC_APP_ROUTING_NAME)
                ? child
                : undefined;
            if (defaultPath) {
                routingModule.addRoute({
                    path: "",
                    redirectTo: options.selector,
                    name: cli_core_1.Util.nameFromPath(fullName),
                    children
                });
            }
            if (options.hasChildren) {
                routingModule.addRoute({
                    path: this.fileName(fullName),
                    identifierName: options.selector,
                    name: cli_core_1.Util.nameFromPath(fullName),
                    children
                });
            }
            else {
                routingModule.addRoute({
                    path: this.fileName(fullName),
                    identifierName: options.selector,
                    name: cli_core_1.Util.nameFromPath(fullName)
                }, false, // multiline
                false // prepend
                );
            }
            routingModule.finalize();
        }
    }
    getExtraConfiguration() {
        throw new Error("Method not implemented.");
    }
    setExtraConfiguration(extraConfigKeys) {
        throw new Error("Method not implemented.");
    }
    getBaseVariables(name) {
        const config = {};
        config["name"] = cli_core_1.Util.nameFromPath(name);
        config["ClassName"] = cli_core_1.Util.className(cli_core_1.Util.nameFromPath(name));
        config["path"] = this.folderName(name);
        config["filePrefix"] = this.fileName(name);
        config["description"] = this.description;
        config["cliVersion"] = cli_core_1.Util.version();
        config["camelCaseName"] = cli_core_1.Util.camelCase(name);
        config["dockManagerPackage"] = (0, cli_core_1.resolvePackage)(cli_core_1.NPM_DOCK_MANAGER);
        return config;
    }
    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(), "src/app", 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 ${"src/app/" + 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);
    }
    fileName(pathName) {
        const name = cli_core_1.Util.nameFromPath(pathName);
        return cli_core_1.Util.lowerDashed(name);
    }
}
exports.IgniteUIForWebComponentsTemplate = IgniteUIForWebComponentsTemplate;
