import assert from 'assert/strict';
import BaseApplicationGenerator from '../base-application/index.mjs';
import { GENERATOR_GRADLE, GENERATOR_BOOTSTRAP_APPLICATION_SERVER } from '../generator-list.mjs';
import files from './files.mjs';
import { GRADLE } from './constants.mjs';
import cleanupOldServerFilesTask from './cleanup.mjs';
import { applyFromGradleCallback, addGradleDependencyCallback, addGradleMavenRepositoryCallback, addGradlePluginCallback, addGradlePluginManagementCallback, addGradlePropertyCallback, } from './internal/needles.mjs';
export default class GradleGenerator extends BaseApplicationGenerator {
    async beforeQueue() {
        if (!this.fromBlueprint) {
            await this.composeWithBlueprints(GENERATOR_GRADLE);
        }
        if (!this.delegateToBlueprint) {
            await this.dependsOnJHipster(GENERATOR_BOOTSTRAP_APPLICATION_SERVER);
        }
    }
    get configuring() {
        return this.asConfiguringTaskGroup({
            configure() {
                if (this.jhipsterConfigWithDefaults.buildTool !== GRADLE) {
                    this.config.defaults({
                        buildTool: GRADLE,
                    });
                }
            },
        });
    }
    get [BaseApplicationGenerator.CONFIGURING]() {
        return this.delegateTasksToBlueprint(() => this.configuring);
    }
    get preparing() {
        return this.asPreparingTaskGroup({
            async verify({ application }) {
                assert.equal(application.buildTool, GRADLE);
            },
            addSourceNeddles({ source }) {
                source.applyFromGradle = script => this.editFile('build.gradle', applyFromGradleCallback(script));
                source.addGradleDependency = dependency => this.editFile('build.gradle', addGradleDependencyCallback(dependency));
                source.addGradlePlugin = plugin => this.editFile('build.gradle', addGradlePluginCallback(plugin));
                source.addGradleMavenRepository = repository => this.editFile('build.gradle', addGradleMavenRepositoryCallback(repository));
                source.addGradlePluginManagement = plugin => this.editFile('settings.gradle', addGradlePluginManagementCallback(plugin));
                source.addGradleProperty = property => this.editFile('gradle.properties', addGradlePropertyCallback(property));
            },
        });
    }
    get [BaseApplicationGenerator.PREPARING]() {
        return this.delegateTasksToBlueprint(() => this.preparing);
    }
    get writing() {
        return this.asWritingTaskGroup({
            cleanupOldServerFilesTask,
            async writeFiles({ application }) {
                await this.writeFiles({ sections: files, context: application });
            },
        });
    }
    get [BaseApplicationGenerator.WRITING]() {
        return this.delegateTasksToBlueprint(() => this.writing);
    }
}
