import chalk from 'chalk';
import BaseApplicationGenerator from '../base-application/index.js';
import statistics from '../statistics.js';
import { GENERATOR_BOOTSTRAP_APPLICATION, GENERATOR_CI_CD } from '../generator-list.js';
import { clientFrameworkTypes } from '../../jdl/jhipster/index.js';
import { createPomStorage } from '../maven/support/pom-store.js';
import command from './command.js';
import { loadConfig, loadDerivedConfig } from '../../lib/internal/config-def.js';
const { REACT } = clientFrameworkTypes;
export default class CiCdGenerator extends BaseApplicationGenerator {
    insideDocker;
    async beforeQueue() {
        if (!this.fromBlueprint) {
            await this.composeWithBlueprints(GENERATOR_CI_CD);
        }
        if (!this.delegateToBlueprint) {
            await this.dependsOnJHipster(GENERATOR_BOOTSTRAP_APPLICATION);
        }
    }
    get initializing() {
        return this.asInitializingTaskGroup({
            sayHello() {
                this.log.log(chalk.white('🚀 Welcome to the JHipster CI/CD Sub-Generator 🚀'));
            },
            getConfig() {
                this.parseJHipsterCommand(command);
            },
        });
    }
    get [BaseApplicationGenerator.INITIALIZING]() {
        return this.delegateTasksToBlueprint(() => this.initializing);
    }
    get prompting() {
        return this.asPromptingTaskGroup({
            async prompting() {
                await this.prompt(this.prepareQuestions(command.configs));
            },
        });
    }
    get [BaseApplicationGenerator.PROMPTING]() {
        return this.delegateTasksToBlueprint(() => this.prompting);
    }
    get loading() {
        return {
            loadSharedConfig({ application }) {
                loadConfig.call(this, command.configs, { application });
            },
        };
    }
    get [BaseApplicationGenerator.LOADING]() {
        return this.delegateTasksToBlueprint(() => this.loading);
    }
    get preparing() {
        return this.asPreparingTaskGroup({
            setTemplateConstants({ application }) {
                loadDerivedConfig(command.configs, { application });
                if (application.ciCdIntegrations === undefined) {
                    application.ciCdIntegrations = [];
                }
                application.gitLabIndent = application.sendBuildToGitlab ? '    ' : '';
                application.indent = application.insideDocker ? '    ' : '';
                application.indent += application.gitLabIndent;
                if (application.clientFramework === REACT) {
                    application.frontTestCommand = 'test-ci';
                }
                else {
                    application.frontTestCommand = 'test';
                }
            },
        });
    }
    get [BaseApplicationGenerator.PREPARING]() {
        return this.delegateTasksToBlueprint(() => this.preparing);
    }
    get writing() {
        return this.asWritingTaskGroup({
            insight() {
                statistics.sendSubGenEvent('generator', GENERATOR_CI_CD);
            },
            async writeFiles({ application }) {
                await this.writeFiles({
                    blocks: [
                        {
                            condition: ctx => ctx.ciCdJenkins,
                            templates: [
                                {
                                    sourceFile: 'jenkins/Jenkinsfile',
                                    destinationFile: 'Jenkinsfile',
                                },
                                {
                                    sourceFile: 'jenkins/jenkins.yml',
                                    destinationFile: `${application.dockerServicesDir}jenkins.yml`,
                                },
                                {
                                    sourceFile: 'jenkins/idea.gdsl',
                                    destinationFile: `${application.srcMainResources}idea.gdsl`,
                                },
                            ],
                        },
                        {
                            condition: ctx => ctx.ciCdGitlab,
                            templates: ['.gitlab-ci.yml'],
                        },
                        {
                            condition: ctx => ctx.ciCdCircle,
                            templates: ['.circleci/config.yml'],
                        },
                        {
                            condition: ctx => ctx.ciCdTravis,
                            templates: ['.travis.yml'],
                        },
                        {
                            condition: ctx => ctx.ciCdAzure,
                            templates: ['azure-pipelines.yml'],
                        },
                        {
                            condition: ctx => ctx.ciCdGithub,
                            templates: ['.github/workflows/main.yml'],
                        },
                    ],
                    context: application,
                });
                if (application.ciCdIntegrations.includes('publishDocker')) {
                    this.writeFile('docker-registry.yml.ejs', `${application.dockerServicesDir}docker-registry.yml`);
                }
            },
        });
    }
    get [BaseApplicationGenerator.WRITING]() {
        return this.delegateTasksToBlueprint(() => this.writing);
    }
    get postWriting() {
        return this.asPostWritingTaskGroup({
            postWriting({ application }) {
                if (application.ciCdIntegrations.includes('deploy')) {
                    if (application.buildToolMaven) {
                        createPomStorage(this).addDistributionManagement({
                            releasesId: application.artifactoryReleasesId,
                            releasesUrl: application.artifactoryReleasesUrl,
                            snapshotsId: application.artifactorySnapshotsId,
                            snapshotsUrl: application.artifactorySnapshotsUrl,
                        });
                    }
                    else if (application.buildToolGradle) {
                        this.log.warn('No support for Artifactory yet, when using Gradle.\n');
                    }
                }
            },
        });
    }
    get [BaseApplicationGenerator.POST_WRITING]() {
        return this.delegateTasksToBlueprint(() => this.postWriting);
    }
}
