import fs from 'fs';
import chalk from 'chalk';
import BaseWorkspacesGenerator from '../base-workspaces/index.js';
import prompts from '../kubernetes/prompts.js';
import { checkImages, configureImageNames, generateJwtSecret, loadFromYoRc } from '../base-workspaces/internal/docker-base.js';
import { checkHelm, checkKubernetes, derivedKubernetesPlatformProperties, loadConfig, setupHelmConstants, setupKubernetesConstants, } from '../kubernetes/kubernetes-base.js';
import { messageBrokerTypes } from '../../lib/jhipster/index.js';
import { getJdbcUrl, getR2dbcUrl } from '../spring-data-relational/support/index.js';
import { loadDeploymentConfig, loadDockerDependenciesTask } from '../base-workspaces/internal/index.js';
import { checkDocker } from '../docker/support/index.js';
import { loadDerivedServerConfig } from '../server/support/index.js';
import { loadDerivedAppConfig } from '../app/support/index.js';
import { writeFiles } from './files.js';
const { KAFKA } = messageBrokerTypes;
export default class KubernetesHelmGenerator extends BaseWorkspacesGenerator {
    async beforeQueue() {
        if (!this.fromBlueprint) {
            await this.composeWithBlueprints();
        }
    }
    get initializing() {
        return {
            sayHello() {
                this.log.log(chalk.white(`${chalk.bold('⎈')} Welcome to the JHipster Kubernetes Helm Generator ${chalk.bold('⎈')}`));
                this.log.log(chalk.white(`Files will be generated in folder: ${chalk.yellow(this.destinationRoot())}`));
            },
            existingDeployment() {
                this.regenerate = this.regenerate || this.config.existed;
            },
            loadDockerDependenciesTask,
            checkDocker,
            checkKubernetes,
            checkHelm,
            loadConfig,
            setupKubernetesConstants,
            setupHelmConstants,
        };
    }
    get [BaseWorkspacesGenerator.INITIALIZING]() {
        return this.delegateTasksToBlueprint(() => this.initializing);
    }
    get prompting() {
        return {
            askForApplicationType: prompts.askForApplicationType,
            askForPath: prompts.askForPath,
            askForApps: prompts.askForApps,
            askForMonitoring: prompts.askForMonitoring,
            askForClustersMode: prompts.askForClustersMode,
            askForServiceDiscovery: prompts.askForServiceDiscovery,
            askForAdminPassword: prompts.askForAdminPassword,
            askForKubernetesNamespace: prompts.askForKubernetesNamespace,
            askForDockerRepositoryName: prompts.askForDockerRepositoryName,
            askForDockerPushCommand: prompts.askForDockerPushCommand,
            askForIstioSupport: prompts.askForIstioSupport,
            askForKubernetesServiceType: prompts.askForKubernetesServiceType,
            askForIngressType: prompts.askForIngressType,
            askForIngressDomain: prompts.askForIngressDomain,
        };
    }
    get [BaseWorkspacesGenerator.PROMPTING]() {
        return this.delegateTasksToBlueprint(() => this.prompting);
    }
    get configuring() {
        return {
            generateJwtSecret,
        };
    }
    get [BaseWorkspacesGenerator.CONFIGURING]() {
        return this.delegateTasksToBlueprint(() => this.configuring);
    }
    get loading() {
        return {
            loadFromYoRc,
            loadSharedConfig() {
                for (const app of this.appConfigs) {
                    loadDerivedAppConfig({ application: app });
                    loadDerivedServerConfig({ application: app });
                }
                loadDeploymentConfig.call(this);
                derivedKubernetesPlatformProperties(this);
            },
        };
    }
    get [BaseWorkspacesGenerator.LOADING]() {
        return this.delegateTasksToBlueprint(() => this.loading);
    }
    get preparing() {
        return {
            configureImageNames,
            setPostPromptProp() {
                this.appConfigs.forEach(element => {
                    element.clusteredDb ? (element.dbPeerCount = 3) : (element.dbPeerCount = 1);
                    if (element.messageBroker === KAFKA) {
                        this.useKafka = true;
                    }
                });
                this.useKeycloak = false;
            },
        };
    }
    get [BaseWorkspacesGenerator.PREPARING]() {
        return this.delegateTasksToBlueprint(() => this.preparing);
    }
    get writing() {
        return writeFiles();
    }
    get [BaseWorkspacesGenerator.WRITING]() {
        return this.delegateTasksToBlueprint(() => this.writing);
    }
    get end() {
        return {
            checkImages,
            deploy() {
                if (this.hasWarning) {
                    this.log.warn('Helm configuration generated, but no Jib cache found');
                    this.log.warn('If you forgot to generate the Docker image for this application, please run:');
                    this.log.warn(this.warningMessage);
                }
                else {
                    this.log.verboseInfo(`\n${chalk.bold.green('Helm configuration successfully generated!')}`);
                }
                this.log.warn('You will need to push your image to a registry. If you have not done so, use the following commands to tag and push the images:');
                for (let i = 0; i < this.appsFolders.length; i++) {
                    const originalImageName = this.appConfigs[i].baseName.toLowerCase();
                    const targetImageName = this.appConfigs[i].targetImageName;
                    if (originalImageName !== targetImageName) {
                        this.log.verboseInfo(`  ${chalk.cyan(`docker image tag ${originalImageName} ${targetImageName}`)}`);
                    }
                    this.log.verboseInfo(`  ${chalk.cyan(`${this.dockerPushCommand} ${targetImageName}`)}`);
                }
                this.log.log('\nYou can deploy all your apps by running the following script:');
                this.log.verboseInfo(`  ${chalk.cyan('bash helm-apply.sh')}`);
                this.log.log('\nYou can upgrade (after any changes) all your apps by running the following script:');
                this.log.verboseInfo(`  ${chalk.cyan('bash helm-upgrade.sh')}`);
                try {
                    fs.chmodSync('helm-apply.sh', '755');
                    fs.chmodSync('helm-upgrade.sh', '755');
                }
                catch {
                    this.log.warn("Failed to make 'helm-apply.sh', 'helm-upgrade.sh' executable, you may need to run 'chmod +x helm-apply.sh helm-upgrade.sh");
                }
            },
        };
    }
    get [BaseWorkspacesGenerator.END]() {
        return this.delegateTasksToBlueprint(() => this.end);
    }
    getJDBCUrl(databaseType, options = {}) {
        return getJdbcUrl(databaseType, options);
    }
    getR2DBCUrl(databaseType, options = {}) {
        return getR2dbcUrl(databaseType, options);
    }
}
