import BaseApplicationGenerator from '../base-application/index.js';
import writeMongodbFilesTask from './files.js';
import cleanupMongodbFilesTask from './cleanup.js';
import writeMongodbEntityFilesTask, { cleanupMongodbEntityFilesTask } from './entity-files.js';
export default class MongoDBGenerator extends BaseApplicationGenerator {
    async beforeQueue() {
        if (!this.fromBlueprint) {
            await this.composeWithBlueprints();
        }
        if (!this.delegateToBlueprint) {
            await this.dependsOnBootstrapApplication();
        }
    }
    get writing() {
        return {
            cleanupMongodbFilesTask,
            writeMongodbFilesTask,
        };
    }
    get [BaseApplicationGenerator.WRITING]() {
        return this.delegateTasksToBlueprint(() => this.writing);
    }
    get writingEntities() {
        return {
            cleanupMongodbEntityFilesTask,
            writeMongodbEntityFilesTask,
        };
    }
    get [BaseApplicationGenerator.WRITING_ENTITIES]() {
        return this.delegateTasksToBlueprint(() => this.writingEntities);
    }
    get postWriting() {
        return this.asPostWritingTaskGroup({
            addTestSpringFactory({ source, application }) {
                source.addTestSpringFactory({
                    key: 'org.springframework.test.context.ContextCustomizerFactory',
                    value: `${application.packageName}.config.TestContainersSpringContextCustomizerFactory`,
                });
            },
            addDependencies({ application, source }) {
                const { reactive, javaDependencies } = application;
                source.addJavaDependencies?.([
                    { groupId: 'io.mongock', artifactId: 'mongock-bom', type: 'pom', version: javaDependencies['mongock-bom'], scope: 'import' },
                    { groupId: 'io.mongock', artifactId: 'mongock-springboot-v3' },
                    { groupId: 'org.springframework.boot', artifactId: `spring-boot-starter-data-mongodb${reactive ? '-reactive' : ''}` },
                    { groupId: 'io.mongock', artifactId: reactive ? 'mongodb-reactive-driver' : 'mongodb-springdata-v4-driver' },
                    { scope: 'test', groupId: 'org.testcontainers', artifactId: 'junit-jupiter' },
                    { scope: 'test', groupId: 'org.testcontainers', artifactId: 'testcontainers' },
                    { scope: 'test', groupId: 'org.testcontainers', artifactId: 'mongodb' },
                ]);
                if (reactive) {
                    source.addJavaDependencies?.([
                        { groupId: 'org.springframework.boot', artifactId: 'spring-boot-starter-data-mongodb' },
                        { groupId: 'io.mongock', artifactId: 'mongodb-springdata-v4-driver' },
                    ]);
                }
            },
            blockhound({ application, source }) {
                const { reactive } = application;
                if (reactive) {
                    source.addAllowBlockingCallsInside({ classPath: 'com.mongodb.internal.Locks', method: 'checkedWithLock' });
                }
            },
        });
    }
    get [BaseApplicationGenerator.POST_WRITING]() {
        return this.delegateTasksToBlueprint(() => this.postWriting);
    }
}
