import BaseApplicationGenerator from '../../../base-application/index.js';
import cleanupKafkaFilesTask from './cleanup.js';
import { kafkaFiles } from './files.js';
export default class KafkaGenerator extends BaseApplicationGenerator {
    async beforeQueue() {
        if (!this.fromBlueprint) {
            await this.composeWithBlueprints();
        }
        if (!this.delegateToBlueprint) {
            await this.dependsOnBootstrapApplicationServer();
        }
    }
    get writing() {
        return this.asWritingTaskGroup({
            cleanupKafkaFilesTask,
            async writing({ application }) {
                await this.writeFiles({
                    sections: kafkaFiles,
                    context: application,
                });
            },
        });
    }
    get [BaseApplicationGenerator.WRITING]() {
        return this.delegateTasksToBlueprint(() => this.writing);
    }
    get postWriting() {
        return this.asPostWritingTaskGroup({
            customizeApplicationForKafka({ source, application }) {
                source.addLogbackMainLog?.({ name: 'org.apache.kafka', level: 'INFO' });
                source.addLogbackTestLog?.({ name: 'kafka', level: 'WARN' });
                source.addLogbackTestLog?.({ name: 'org.I0Itec', level: 'WARN' });
                source.addIntegrationTestAnnotation?.({ package: `${application.packageName}.config`, annotation: 'EmbeddedKafka' });
                source.addTestSpringFactory?.({
                    key: 'org.springframework.test.context.ContextCustomizerFactory',
                    value: `${application.packageName}.config.KafkaTestContainersSpringContextCustomizerFactory`,
                });
            },
            applyKafkaGradleConventionPlugin({ source, application }) {
                if (application.buildToolGradle) {
                    source.addGradlePlugin?.({ id: 'jhipster.kafka-conventions' });
                }
            },
            addKafkaMavenDependencies({ application, source }) {
                if (application.buildToolMaven) {
                    source.addMavenDependency?.([
                        {
                            groupId: 'org.springframework.cloud',
                            artifactId: 'spring-cloud-stream',
                        },
                        {
                            groupId: 'org.springframework.cloud',
                            artifactId: 'spring-cloud-starter-stream-kafka',
                        },
                        {
                            groupId: 'org.springframework.cloud',
                            artifactId: 'spring-cloud-stream-test-binder',
                            scope: 'test',
                        },
                        {
                            groupId: 'org.testcontainers',
                            artifactId: 'junit-jupiter',
                            scope: 'test',
                        },
                        {
                            groupId: 'org.testcontainers',
                            artifactId: 'testcontainers',
                            scope: 'test',
                        },
                        {
                            groupId: 'org.testcontainers',
                            artifactId: 'kafka',
                            scope: 'test',
                        },
                    ]);
                }
            },
            addLog({ source }) {
                source.addLogbackMainLog({ name: 'org.apache.kafka', level: 'WARN' });
            },
        });
    }
    get [BaseApplicationGenerator.POST_WRITING]() {
        return this.delegateTasksToBlueprint(() => this.postWriting);
    }
}
