import BaseApplicationGenerator from '../../../base-application/index.js';
import { createNeedleCallback } from '../../../base/support/needles.js';
export default class EslintGenerator extends BaseApplicationGenerator {
    async beforeQueue() {
        if (!this.fromBlueprint) {
            await this.composeWithBlueprints();
        }
        if (!this.delegateToBlueprint) {
            await this.dependsOnJHipster('begcode:javascript:bootstrap');
        }
    }
    get loading() {
        return this.asLoadingTaskGroup({
            loadNodeDependencies({ application }) {
                this.loadNodeDependenciesFromPackageJson(application.nodeDependencies, this.fetchFromInstalledJHipster('javascript', 'resources', 'package.json'));
            },
        });
    }
    get [BaseApplicationGenerator.LOADING]() {
        return this.delegateTasksToBlueprint(() => this.loading);
    }
    get preparing() {
        return this.asPreparingTaskGroup({
            source({ application, source }) {
                application.eslintConfigFile = `eslint.config.${application.packageJsonType === 'module' ? 'js' : 'mjs'}`;
                source.addEslintConfig = ({ import: importToAdd, config }) => this.editFile(application.eslintConfigFile, config ? createNeedleCallback({ needle: 'eslint-add-config', contentToAdd: config }) : content => content, importToAdd ? createNeedleCallback({ needle: 'eslint-add-import', contentToAdd: importToAdd }) : content => content);
                source.addEslintIgnore = ({ ignorePattern }) => source.addEslintConfig({ config: `{ ignores: '${ignorePattern.replaceAll("'", "\\')}")},` });
            },
        });
    }
    get [BaseApplicationGenerator.PREPARING]() {
        return this.delegateTasksToBlueprint(() => this.preparing);
    }
    get writing() {
        return this.asWritingTaskGroup({
            async writing({ application }) {
                await this.writeFiles({
                    blocks: [
                        {
                            templates: [{ sourceFile: 'eslint.config.js.jhi', destinationFile: ctx => `${ctx.eslintConfigFile}.jhi` }],
                        },
                    ],
                    context: application,
                });
            },
        });
    }
    get [BaseApplicationGenerator.WRITING]() {
        return this.delegateTasksToBlueprint(() => this.writing);
    }
    get postWriting() {
        return this.asPostWritingTaskGroup({
            addDependencies({ application }) {
                this.packageJson.merge({
                    devDependencies: {
                        'eslint-config-prettier': application.nodeDependencies['eslint-config-prettier'],
                        'eslint-plugin-prettier': application.nodeDependencies['eslint-plugin-prettier'],
                    },
                });
            },
        });
    }
    get [BaseApplicationGenerator.POST_WRITING]() {
        return this.delegateTasksToBlueprint(() => this.postWriting);
    }
}
