import { packageJson } from '../../../../lib/index.js';
import BaseApplicationGenerator from '../../../base-application/index.js';
import { GENERATOR_PROJECT_NAME } from '../../../generator-list.js';
export default class BootstrapGenerator extends BaseApplicationGenerator {
    async beforeQueue() {
        if (!this.fromBlueprint) {
            await this.composeWithBlueprints();
        }
        if (!this.delegateToBlueprint) {
            await this.dependsOnJHipster(GENERATOR_PROJECT_NAME);
        }
    }
    get loading() {
        return this.asLoadingTaskGroup({
            loadNodeDependencies({ application }) {
                this.loadNodeDependenciesFromPackageJson(application.nodeDependencies, this.fetchFromInstalledJHipster('javascript', 'resources', 'package.json'));
            },
            jsExtensions({ applicationDefaults, application }) {
                applicationDefaults({
                    cjsExtension: application.packageJsonTypeCommonjs ? '.js' : '.cjs',
                    mjsExtension: application.packageJsonTypeModule ? '.js' : '.mjs',
                });
            },
        });
    }
    get [BaseApplicationGenerator.LOADING]() {
        return this.delegateTasksToBlueprint(() => this.loading);
    }
    get preparing() {
        return this.asPreparingTaskGroup({
            addSource({ application, source }) {
                source.mergeClientPackageJson = args => {
                    this.mergeDestinationJson(`${application.clientRootDir}package.json`, args);
                };
            },
        });
    }
    get [BaseApplicationGenerator.PREPARING]() {
        return this.delegateTasksToBlueprint(() => this.preparing);
    }
    get writing() {
        return this.asWritingTaskGroup({
            async writing({ application }) {
                await this.writeFiles({
                    blocks: [{ templates: [{ override: false, file: 'package.json' }] }],
                    context: application,
                });
            },
        });
    }
    get [BaseApplicationGenerator.WRITING]() {
        return this.delegateTasksToBlueprint(() => this.writing);
    }
    get postWriting() {
        return this.asPostWritingTaskGroup({
            mergePackageJson({ application, source }) {
                const { packageJsonNodeEngine, packageJsonType, dasherizedBaseName, projectDescription, packageJsonScripts, clientPackageJsonScripts, } = application;
                this.packageJson.merge({ scripts: packageJsonScripts });
                this.packageJson.defaults({
                    name: dasherizedBaseName,
                    version: '0.0.0',
                    description: projectDescription,
                    license: 'UNLICENSED',
                });
                if (packageJsonType === 'module') {
                    this.packageJson.merge({ type: packageJsonType });
                }
                if (packageJsonNodeEngine) {
                    const packageJsonEngines = this.packageJson.get('engines') ?? {};
                    this.packageJson.set('engines', {
                        ...packageJsonEngines,
                        node: typeof packageJsonNodeEngine === 'string' ? packageJsonNodeEngine : packageJson.engines.node,
                    });
                }
                if (clientPackageJsonScripts && Object.keys(clientPackageJsonScripts).length > 0) {
                    source.mergeClientPackageJson({ scripts: clientPackageJsonScripts });
                }
            },
        });
    }
    get [BaseApplicationGenerator.POST_WRITING]() {
        return this.delegateTasksToBlueprint(() => this.postWriting);
    }
}
