import assert from 'assert/strict';
import BaseApplicationGenerator from '../base-application/index.js';
import files from './files.js';
import { MAVEN } from './constants.js';
import cleanupOldServerFilesTask from './cleanup.js';
import { createPomStorage } from './support/index.js';
export default class MavenGenerator extends BaseApplicationGenerator {
    pomStorage;
    sortMavenPom;
    async beforeQueue() {
        if (!this.fromBlueprint) {
            await this.composeWithBlueprints();
        }
        if (!this.delegateToBlueprint) {
            await this.dependsOnBootstrapApplicationServer();
        }
    }
    get initializing() {
        return this.asInitializingTaskGroup({
            async parseCommand() {
                await this.parseCurrentJHipsterCommand();
            },
            pomStorage() {
                this.pomStorage = createPomStorage(this, { sortFile: this.sortMavenPom });
            },
        });
    }
    get [BaseApplicationGenerator.INITIALIZING]() {
        return this.delegateTasksToBlueprint(() => this.initializing);
    }
    get configuring() {
        return this.asConfiguringTaskGroup({
            configure() {
                if (this.jhipsterConfigWithDefaults.buildTool !== MAVEN) {
                    this.config.defaults({
                        buildTool: MAVEN,
                    });
                }
            },
        });
    }
    get [BaseApplicationGenerator.CONFIGURING]() {
        return this.delegateTasksToBlueprint(() => this.configuring);
    }
    get preparing() {
        return this.asPreparingTaskGroup({
            async verify({ application }) {
                assert.equal(application.buildTool, MAVEN);
            },
            addSourceNeddles({ application, source }) {
                const { javaProperties } = application;
                function createForEach(callback) {
                    return (arg) => {
                        const argArray = Array.isArray(arg) ? arg : [arg];
                        for (const item of argArray) {
                            callback(item);
                        }
                    };
                }
                source.mergeMavenPomContent = content => this.pomStorage.merge(content);
                source.addMavenAnnotationProcessor = createForEach(artifact => this.pomStorage.addAnnotationProcessor(artifact));
                source.addMavenDependency = createForEach(artifact => this.pomStorage.addDependency(artifact));
                source.addMavenDependencyManagement = createForEach(artifact => this.pomStorage.addDependencyManagement(artifact));
                source.addMavenDistributionManagement = createForEach(artifact => this.pomStorage.addDistributionManagement(artifact));
                source.addMavenPlugin = createForEach(plugin => this.pomStorage.addPlugin(plugin));
                source.addMavenPluginManagement = createForEach(plugin => this.pomStorage.addPluginManagement(plugin));
                source.addMavenPluginRepository = createForEach(repository => this.pomStorage.addPluginRepository(repository));
                source.addMavenProfile = createForEach(profile => this.pomStorage.addProfile(profile));
                source.addMavenProperty = properties => {
                    properties = Array.isArray(properties) ? properties : [properties];
                    for (const property of properties) {
                        javaProperties[property.property] = property.value;
                        this.pomStorage.addProperty(property);
                    }
                };
                source.addMavenRepository = createForEach(repository => this.pomStorage.addRepository(repository));
                source.addMavenDefinition = definition => {
                    definition.profiles?.forEach(profile => this.pomStorage.addProfile(profile));
                    definition.pluginManagement?.forEach(plugin => this.pomStorage.addPluginManagement(plugin));
                    definition.dependencies?.forEach(dependency => this.pomStorage.addDependency(dependency));
                    definition.dependencyManagement?.forEach(dependency => this.pomStorage.addDependencyManagement(dependency));
                    definition.distributionManagement?.forEach(distribution => this.pomStorage.addDistributionManagement(distribution));
                    definition.plugins?.forEach(plugin => this.pomStorage.addPlugin(plugin));
                    definition.pluginRepositories?.forEach(repository => this.pomStorage.addPluginRepository(repository));
                    if (definition.properties) {
                        source.addMavenProperty(definition.properties);
                    }
                    definition.repositories?.forEach(repository => this.pomStorage.addRepository(repository));
                    definition.annotationProcessors?.forEach(annotation => this.pomStorage.addAnnotationProcessor(annotation));
                };
            },
        });
    }
    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);
    }
    get postWriting() {
        return this.asPostWritingTaskGroup({
            sortPom() {
                this.pomStorage.save();
            },
        });
    }
    get [BaseApplicationGenerator.POST_WRITING]() {
        return this.delegateTasksToBlueprint(() => this.postWriting);
    }
}
