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';
import { validateProjectName } from './support/name-resolver.mjs';
export default class ProjectNameGenerator extends BaseApplicationGenerator {
    javaApplication;
    async beforeQueue() {
        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);
        }
    }
    get initializing() {
        return this.asInitializingTaskGroup({
            loadOptions() {
                this.parseJHipsterOptions(command.options);
            },
            parseOptions() {
                if (this.options.defaults) {
                    if (!this.jhipsterConfig.baseName) {
                        this.jhipsterConfig.baseName = getDefaultAppName({
                            reproducible: this.options.reproducible,
                            javaApplication: this.javaApplication,
                        });
                    }
                }
            },
        });
    }
    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({ reproducible: this.options.reproducible, javaApplication: this.javaApplication }),
                    },
                ], 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) {
        return validateProjectName(input, { javaApplication: this.javaApplication });
    }
}
