import BaseApplicationGenerator from '../base-application/index.mjs';
import { GENERATOR_SPRING_DATA_CASSANDRA, GENERATOR_BOOTSTRAP_APPLICATION } from '../generator-list.mjs';
import writeCassandraFilesTask from './files.mjs';
import cleanupCassandraFilesTask from './cleanup.mjs';
import writeCassandraEntityFilesTask, { cleanupCassandraEntityFilesTask } from './entity-files.mjs';
export default class CassandraGenerator extends BaseApplicationGenerator {
    async beforeQueue() {
        if (!this.fromBlueprint) {
            await this.composeWithBlueprints(GENERATOR_SPRING_DATA_CASSANDRA);
        }
        if (!this.delegateToBlueprint) {
            await this.dependsOnJHipster(GENERATOR_BOOTSTRAP_APPLICATION);
        }
    }
    get configuringEachEntity() {
        return this.asConfiguringEachEntityTaskGroup({
            checkEntities({ entityConfig }) {
                if (entityConfig.pagination && entityConfig.pagination !== 'no') {
                    throw new Error("Pagination isn't allowed when the app uses Cassandra.");
                }
            },
        });
    }
    get [BaseApplicationGenerator.CONFIGURING_EACH_ENTITY]() {
        return this.delegateTasksToBlueprint(() => this.configuringEachEntity);
    }
    get writing() {
        return this.asWritingTaskGroup({
            cleanupCassandraFilesTask,
            writeCassandraFilesTask,
        });
    }
    get [BaseApplicationGenerator.WRITING]() {
        return this.delegateTasksToBlueprint(() => this.writing);
    }
    get writingEntities() {
        return this.asWritingEntitiesTaskGroup({
            cleanupCassandraEntityFilesTask,
            writeCassandraEntityFilesTask,
        });
    }
    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.CassandraTestContainersSpringContextCustomizerFactory`,
                });
            },
            addDependencies({ application, source }) {
                const { reactive } = application;
                if (application.buildToolMaven) {
                    source.addMavenProperty?.({
                        property: 'cassandra-driver.version',
                        value: application.javaDependencies.cassandra,
                    });
                    source.addMavenAnnotationProcessor?.({
                        groupId: 'com.datastax.oss',
                        artifactId: 'java-driver-mapper-processor',
                        version: '${cassandra-driver.version}',
                    });
                    source.addMavenDependency?.([
                        {
                            groupId: 'com.datastax.oss',
                            artifactId: 'java-driver-mapper-runtime',
                        },
                        {
                            groupId: 'commons-codec',
                            artifactId: 'commons-codec',
                        },
                        {
                            groupId: 'org.lz4',
                            artifactId: 'lz4-java',
                        },
                        {
                            groupId: 'org.springframework.boot',
                            artifactId: `spring-boot-starter-data-cassandra${reactive ? '-reactive' : ''}`,
                        },
                        {
                            groupId: 'org.testcontainers',
                            artifactId: 'junit-jupiter',
                            scope: 'test',
                        },
                        {
                            groupId: 'org.testcontainers',
                            artifactId: 'testcontainers',
                            scope: 'test',
                        },
                        {
                            groupId: 'org.testcontainers',
                            artifactId: 'cassandra',
                            scope: 'test',
                        },
                    ]);
                }
            },
        });
    }
    get [BaseApplicationGenerator.POST_WRITING]() {
        return this.asPostWritingTaskGroup(this.delegateTasksToBlueprint(() => this.postWriting));
    }
}
