"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProjectNameService = void 0;
const helpers_1 = require("../common/helpers");
const yok_1 = require("../common/yok");
class ProjectNameService {
    constructor($projectNameValidator, $errors, $logger, $prompter) {
        this.$projectNameValidator = $projectNameValidator;
        this.$errors = $errors;
        this.$logger = $logger;
        this.$prompter = $prompter;
    }
    async ensureValidName(projectName, validateOptions) {
        if (validateOptions && validateOptions.force) {
            return projectName;
        }
        if (!this.$projectNameValidator.validate(projectName)) {
            return await this.promptForNewName("The project name is invalid.", projectName, validateOptions);
        }
        const userCanInteract = (0, helpers_1.isInteractive)();
        if (!this.checkIfNameStartsWithLetter(projectName)) {
            if (!userCanInteract) {
                this.$errors.fail("The project name does not start with letter and will fail to build for Android. If You want to create project with this name add --force to the create command.");
            }
            return await this.promptForNewName("The project name does not start with letter and will fail to build for Android.", projectName, validateOptions);
        }
        if (projectName.toUpperCase() === "APP") {
            if (!userCanInteract) {
                this.$errors.fail("You cannot build applications named 'app' in Xcode. Consider creating a project with different name. If You want to create project with this name add --force to the create command.");
            }
            return await this.promptForNewName("You cannot build applications named 'app' in Xcode. Consider creating a project with different name.", projectName, validateOptions);
        }
        return projectName;
    }
    checkIfNameStartsWithLetter(projectName) {
        const startsWithLetterExpression = /^[a-zA-Z]/;
        return startsWithLetterExpression.test(projectName);
    }
    async promptForNewName(warningMessage, projectName, validateOptions) {
        if (await this.promptForForceNameConfirm(warningMessage)) {
            return projectName;
        }
        const newProjectName = await this.$prompter.getString("Enter the new project name:");
        return await this.ensureValidName(newProjectName, validateOptions);
    }
    async promptForForceNameConfirm(warningMessage) {
        this.$logger.warn(warningMessage);
        return await this.$prompter.confirm("Do you want to create the project with this name?");
    }
}
exports.ProjectNameService = ProjectNameService;
yok_1.injector.register("projectNameService", ProjectNameService);
