import * as _ from 'lodash-es';
import { getDefaultAppName } from './support/index.mjs';
import BaseApplicationGenerator from '../base-application/index.mjs';
import { GENERATOR_PROJECT_NAME } from '../generator-list.mjs';
import { BASE_NAME } from './constants.mjs';
import { getHipster } from '../base/support/index.mjs';
import command from './command.mjs';
export default class ProjectNameGenerator extends BaseApplicationGenerator {
    async beforeQueue() {
        if (this.options.defaults) {
            if (!this.jhipsterConfig.baseName) {
                this.jhipsterConfig.baseName = getDefaultAppName(this);
            }
        }
        this.sharedData.getControl().existingProject =
            this.options.defaults || this.options.applicationWithConfig || (this.jhipsterConfig.baseName !== undefined && this.config.existed);
        if (!this.fromBlueprint) {
            await this.composeWithBlueprints(GENERATOR_PROJECT_NAME);
        }
        if (this.sharedData.getControl().existingProject && !this.jhipsterConfig.baseName) {
            this.jhipsterConfig.baseName = getDefaultAppName(this);
        }
    }
    get initializing() {
        return this.asInitializingTaskGroup({
            loadOptions() {
                this.parseJHipsterOptions(command.options);
            },
        });
    }
    get [BaseApplicationGenerator.INITIALIZING]() {
        return this.delegateTasksToBlueprint(() => this.initializing);
    }
    get prompting() {
        return {
            async showPrompts() {
                await this.prompt([
                    {
                        name: BASE_NAME,
                        type: 'input',
                        validate: input => this.validateBaseName(input),
                        message: 'What is the base name of your application?',
                        default: () => getDefaultAppName(this),
                    },
                ], this.config);
            },
        };
    }
    get [BaseApplicationGenerator.PROMPTING]() {
        return this.delegateTasksToBlueprint(() => this.prompting);
    }
    get loading() {
        return this.asLoadingTaskGroup({
            load({ application }) {
                const { baseName, projectDescription } = this.jhipsterConfig;
                application.baseName = baseName;
                application.projectDescription = projectDescription;
            },
        });
    }
    get [BaseApplicationGenerator.LOADING]() {
        return this.delegateTasksToBlueprint(() => this.loading);
    }
    get preparing() {
        return this.asPreparingTaskGroup({
            preparing({ application }) {
                const { baseName, upperFirstCamelCaseBaseName } = application;
                const humanizedBaseName = baseName.toLowerCase() === 'jhipster' ? 'JHipster' : _.startCase(baseName);
                _.defaults(application, {
                    humanizedBaseName,
                    camelizedBaseName: _.camelCase(baseName),
                    hipster: getHipster(baseName),
                    capitalizedBaseName: _.upperFirst(baseName),
                    dasherizedBaseName: _.kebabCase(baseName),
                    lowercaseBaseName: baseName.toLowerCase(),
                    upperFirstCamelCaseBaseName,
                    projectDescription: `Description for ${humanizedBaseName}`,
                });
            },
        });
    }
    get [BaseApplicationGenerator.PREPARING]() {
        return this.delegateTasksToBlueprint(() => this.preparing);
    }
    validateBaseName(input) {
        if (!/^([\w-]*)$/.test(input)) {
            return 'Your base name cannot contain special characters or a blank space';
        }
        if (/_/.test(input)) {
            return 'Your base name cannot contain underscores as this does not meet the URI spec';
        }
        if (input?.toLowerCase() === 'application') {
            return "Your base name cannot be named 'application' as this is a reserved name for Spring Boot";
        }
        return true;
    }
}
