import BaseApplicationGenerator from '../base-application/index.mjs';
import { GENERATOR_INIT, GENERATOR_GIT, GENERATOR_PROJECT_NAME } from '../generator-list.mjs';
import { defaultConfig } from './config.mjs';
import { SKIP_COMMIT_HOOK } from './constants.mjs';
import { files, commitHooksFiles } from './files.mjs';
import command from './command.mjs';
import { packageJson } from '../../lib/index.mjs';
export default class InitGenerator extends BaseApplicationGenerator {
    async beforeQueue() {
        if (!this.fromBlueprint) {
            await this.composeWithBlueprints(GENERATOR_INIT);
        }
        if (!this.delegateToBlueprint) {
            await this.dependsOnJHipster(GENERATOR_PROJECT_NAME);
        }
    }
    get initializing() {
        return this.asInitializingTaskGroup({
            loadOptions() {
                this.parseJHipsterOptions(command.options);
            },
        });
    }
    get [BaseApplicationGenerator.INITIALIZING]() {
        return this.delegateTasksToBlueprint(() => this.initializing);
    }
    get loading() {
        return this.asLoadingTaskGroup({
            loadConfig({ application }) {
                const config = { ...defaultConfig, ...this.config.getAll() };
                application.applicationNodeEngine = packageJson.engines.node;
                application[SKIP_COMMIT_HOOK] = config[SKIP_COMMIT_HOOK];
            },
            loadNodeDependencies({ application }) {
                this.loadNodeDependenciesFromPackageJson(application.nodeDependencies, this.fetchFromInstalledJHipster(GENERATOR_INIT, 'resources', 'package.json'));
            },
        });
    }
    get [BaseApplicationGenerator.LOADING]() {
        return this.delegateTasksToBlueprint(() => this.loading);
    }
    get composing() {
        return this.asComposingTaskGroup({
            async compose() {
                await this.composeWithJHipster(GENERATOR_GIT);
            },
        });
    }
    get [BaseApplicationGenerator.COMPOSING]() {
        return this.delegateTasksToBlueprint(() => this.composing);
    }
    get writing() {
        return this.asWritingTaskGroup({
            cleanup() {
                if (this.isJhipsterVersionLessThan('7.5.1')) {
                    this.removeFile('.lintstagedrc.js');
                }
            },
            async writeFiles({ application }) {
                await this.writeFiles({ sections: files, context: application });
            },
            async writeCommitHookFiles({ application }) {
                if (application.skipCommitHook)
                    return;
                await this.writeFiles({ sections: commitHooksFiles, context: application });
            },
        });
    }
    get [BaseApplicationGenerator.WRITING]() {
        return this.delegateTasksToBlueprint(() => this.writing);
    }
    get postWriting() {
        return this.asPostWritingTaskGroup({
            addPrettierDependencies({ application }) {
                this.packageJson.merge({
                    scripts: {
                        'prettier-check': 'prettier --check "{,src/**/}*.{md,json,yml,html,js,ts,tsx,css,scss,vue,java}"',
                        'prettier-format': 'prettier --write "{,src/**/}*.{md,json,yml,html,js,ts,tsx,css,scss,vue,java}"',
                    },
                    devDependencies: {
                        prettier: application.nodeDependencies.prettier,
                        'prettier-plugin-packagejson': application.nodeDependencies['prettier-plugin-packagejson'],
                    },
                    engines: {
                        node: application.applicationNodeEngine,
                    },
                });
            },
            addCommitHookDependencies({ application }) {
                if (application.skipCommitHook)
                    return;
                this.packageJson.merge({
                    scripts: {
                        prepare: 'husky install',
                    },
                    devDependencies: {
                        husky: application.nodeDependencies.husky,
                        'lint-staged': application.nodeDependencies['lint-staged'],
                    },
                });
            },
        });
    }
    get [BaseApplicationGenerator.POST_WRITING]() {
        return this.delegateTasksToBlueprint(() => this.postWriting);
    }
}
