import BaseApplicationGenerator from '../base-application/index.js';
import { PaginationTypes } from '../../lib/jhipster/entity-options.js';
import writeCassandraFilesTask from './files.js';
import cleanupCassandraFilesTask from './cleanup.js';
import writeCassandraEntityFilesTask, { cleanupCassandraEntityFilesTask } from './entity-files.js';
const { NO: NO_PAGINATION } = PaginationTypes;
export default class CassandraGenerator extends BaseApplicationGenerator {
    async beforeQueue() {
        if (!this.fromBlueprint) {
            await this.composeWithBlueprints();
        }
        if (!this.delegateToBlueprint) {
            await this.dependsOnBootstrapApplication();
        }
    }
    get configuringEachEntity() {
        return this.asConfiguringEachEntityTaskGroup({
            checkEntities({ entityName, entityConfig }) {
                if (entityConfig.pagination && entityConfig.pagination !== NO_PAGINATION) {
                    const errorMessage = `Pagination is not supported for entity ${entityName} when the app uses Cassandra.`;
                    if (!this.skipChecks) {
                        throw new Error(errorMessage);
                    }
                    this.log.warn(errorMessage);
                    entityConfig.pagination = 'no';
                }
            },
        });
    }
    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, javaDependencies } = application;
                const cassandraStarter = reactive ? 'spring-boot-starter-data-cassandra-reactive' : 'spring-boot-starter-data-cassandra';
                source.addJavaDependencies?.([
                    { groupId: 'org.apache.cassandra', artifactId: 'java-driver-mapper-runtime' },
                    { groupId: 'commons-codec', artifactId: 'commons-codec' },
                    { groupId: 'org.springframework.boot', artifactId: cassandraStarter },
                    { groupId: 'org.lz4', artifactId: 'lz4-java', version: javaDependencies['lz4-java'] },
                    { scope: 'test', groupId: 'org.testcontainers', artifactId: 'junit-jupiter' },
                    { scope: 'test', groupId: 'org.testcontainers', artifactId: 'testcontainers' },
                    { scope: 'test', groupId: 'org.testcontainers', artifactId: 'cassandra' },
                    { scope: 'annotationProcessor', groupId: 'org.apache.cassandra', artifactId: 'java-driver-mapper-processor' },
                ]);
            },
        });
    }
    get [BaseApplicationGenerator.POST_WRITING]() {
        return this.delegateTasksToBlueprint(() => this.postWriting);
    }
}
