import BaseApplicationGenerator from '../../../base-application/index.js';
import { GRADLE_BUILD_SRC_MAIN_DIR } from '../../../generator-constants.js';
import { javaMainResourceTemplatesBlock } from '../../support/files.js';
export default class OpenapiGeneratorGenerator extends BaseApplicationGenerator {
    async beforeQueue() {
        if (!this.fromBlueprint) {
            await this.composeWithBlueprints();
        }
        if (!this.delegateToBlueprint) {
            await this.dependsOnBootstrapApplication();
            await this.dependsOnJHipster('begcode:java:build-tool');
        }
    }
    get writing() {
        return this.asWritingTaskGroup({
            async cleanup({ application, control }) {
                await control.cleanupFiles({
                    '8.6.1': [[application.buildToolGradle, 'gradle/swagger.gradle']],
                });
            },
            async writing({ application }) {
                await this.writeFiles({
                    blocks: [
                        { templates: ['README.md.jhi.openapi-generator'] },
                        javaMainResourceTemplatesBlock({ templates: ['swagger/api.yml'] }),
                        {
                            condition: ctx => ctx.buildToolGradle && ctx.addOpenapiGeneratorPlugin,
                            templates: [`${GRADLE_BUILD_SRC_MAIN_DIR}/jhipster.openapi-generator-conventions.gradle`],
                        },
                    ],
                    context: application,
                });
            },
        });
    }
    get [BaseApplicationGenerator.WRITING]() {
        return this.delegateTasksToBlueprint(() => this.writing);
    }
    get postWriting() {
        return this.asPostWritingTaskGroup({
            addDependencies({ source, application }) {
                const { addOpenapiGeneratorPlugin, buildToolGradle, javaDependencies } = application;
                source.addJavaDefinitions({
                    dependencies: [
                        {
                            groupId: 'org.openapitools',
                            artifactId: 'jackson-databind-nullable',
                            version: javaDependencies['jackson-databind-nullable'],
                        },
                    ],
                }, {
                    condition: addOpenapiGeneratorPlugin,
                    mavenDefinition: {
                        properties: [
                            { property: 'openapi-generator-maven-plugin.version', value: javaDependencies['openapi-generator-maven-plugin'] },
                        ],
                        plugins: [{ groupId: 'org.openapitools', artifactId: 'openapi-generator-maven-plugin' }],
                        pluginManagement: [
                            {
                                groupId: 'org.openapitools',
                                artifactId: 'openapi-generator-maven-plugin',
                                version: '${openapi-generator-maven-plugin.version}',
                                additionalContent: `                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>\${project.basedir}/${application.srcMainResources}swagger/api.yml</inputSpec>
                            <generatorName>spring</generatorName>
                            <apiPackage>${application.packageName}.web.api</apiPackage>
                            <modelPackage>${application.packageName}.service.api.dto</modelPackage>
                            <supportingFilesToGenerate>ApiUtil.java</supportingFilesToGenerate>
                            <skipValidateSpec>false</skipValidateSpec>
                            <configOptions>${application.reactive
                                    ? `
                                <reactive>true</reactive>
`
                                    : ''}
                                <delegatePattern>true</delegatePattern>
                                <title>${application.dasherizedBaseName}</title>
                                <useSpringBoot3>true</useSpringBoot3>
                            </configOptions>
                        </configuration>
                    </execution>
                </executions>
`,
                            },
                        ],
                    },
                });
                if (addOpenapiGeneratorPlugin) {
                    if (buildToolGradle) {
                        source.addGradleBuildSrcDependencyCatalogLibraries?.([
                            {
                                libraryName: 'openapi-generator',
                                module: 'org.openapitools:openapi-generator-gradle-plugin',
                                version: javaDependencies['gradle-openapi-generator'],
                                scope: 'implementation',
                            },
                        ]);
                        source.addGradlePlugin?.({ id: 'jhipster.openapi-generator-conventions' });
                    }
                }
            },
        });
    }
    get [BaseApplicationGenerator.POST_WRITING]() {
        return this.delegateTasksToBlueprint(() => this.postWriting);
    }
}
