"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PromptSession = void 0;
const cli_core_1 = require("@igniteui/cli-core");
const inquirer = require("inquirer");
const path = require("path");
const add_1 = require("./commands/add");
const start_1 = require("./commands/start");
const upgrade_1 = require("./commands/upgrade");
class PromptSession extends cli_core_1.BasePromptSession {
    constructor(templateManager) {
        super(templateManager);
    }
    static chooseTerm() {
        return __awaiter(this, void 0, void 0, function* () {
            const answers = yield inquirer.prompt({
                default: null,
                message: "Enter a search term",
                name: "term",
                type: "input"
            });
            if (answers.term) {
                return answers.term;
            }
            else {
                const retProm = yield this.chooseTerm();
                return retProm;
            }
        });
    }
    /**
     * Start questions session for project creation
     */
    start() {
        return __awaiter(this, void 0, void 0, function* () {
            cli_core_1.GoogleAnalytics.post({
                t: "screenview",
                cd: "Wizard"
            });
            let projLibrary;
            let theme;
            add_1.default.templateManager = this.templateManager;
            this.config = cli_core_1.ProjectConfig.getConfig();
            const defaultProjName = "IG Project";
            if (cli_core_1.ProjectConfig.hasLocalConfig() && !this.config.project.isShowcase) {
                projLibrary = this.templateManager.getProjectLibrary(this.config.project.framework, this.config.project.projectType);
                theme = this.config.project.theme;
            }
            else {
                cli_core_1.Util.log(""); /* new line */
                const projectName = yield this.getUserInput({
                    type: "input",
                    name: "projectName",
                    message: "Enter a name for your project:",
                    default: cli_core_1.Util.getAvailableName(defaultProjName, true),
                    validate: this.nameIsValid
                });
                const frameRes = yield this.getUserInput({
                    type: "list",
                    name: "framework",
                    message: "Choose framework:",
                    choices: this.getFrameworkNames(),
                    default: "jQuery"
                });
                const framework = this.templateManager.getFrameworkByName(frameRes);
                // app name validation???
                projLibrary = yield this.getProjectLibrary(framework);
                if (frameRes === "Angular" && projLibrary.projectType === "igx-ts") {
                    cli_core_1.Util.log("Psst! Did you know you can also use our schematics package with Angular CLI to create and modify your projects?", "yellow");
                    cli_core_1.Util.log("Read more at: https://www.infragistics.com/products/ignite-ui-angular/angular/components/general/cli-overview", "yellow");
                }
                const projTemplate = yield this.getProjectTemplate(projLibrary);
                // project options:
                theme = yield this.getTheme(projLibrary);
                cli_core_1.Util.log("  Generating project structure.");
                const config = projTemplate.generateConfig(projectName, theme);
                for (const templatePath of projTemplate.templatePaths) {
                    yield cli_core_1.Util.processTemplates(templatePath, path.join(process.cwd(), projectName), config, projTemplate.delimiters, false);
                }
                cli_core_1.Util.log(cli_core_1.Util.greenCheck() + " Project structure generated.");
                if (!this.config.skipGit) {
                    cli_core_1.Util.gitInit(process.cwd(), projectName);
                }
                // move cwd to project folder
                process.chdir(projectName);
            }
            yield this.chooseActionLoop(projLibrary);
            //TODO: restore cwd?
        });
    }
    completeAndRun(port) {
        return __awaiter(this, void 0, void 0, function* () {
            yield cli_core_1.PackageManager.flushQueue(true);
            if (true) { // TODO: Make conditional?
                yield start_1.default.start({ port });
            }
        });
    }
    upgradePackages() {
        return __awaiter(this, void 0, void 0, function* () {
            upgrade_1.default.templateManager = this.templateManager;
            yield upgrade_1.default.upgrade({ skipInstall: true });
        });
    }
    /**
     * Get user name and set template's extra configurations if any
     * @param projectLibrary to add component to
     * @param component to get template for
     */
    templateSelectedTask(type = "component") {
        return (_runner, context) => __awaiter(this, void 0, void 0, function* () {
            const name = yield this.chooseTemplateName(context.template, type);
            if (context.template.hasExtraConfiguration) {
                yield this.customizeTemplateTask(context.template);
            }
            const res = yield add_1.default.addTemplate(name, context.template);
            return res;
        });
    }
}
exports.PromptSession = PromptSession;
