import chalk from 'chalk';
import * as _ from 'lodash-es';
import { testFrameworkTypes } from '../../jdl/jhipster/index.mjs';
import { APPLICATION_TYPE_GATEWAY, APPLICATION_TYPE_MICROSERVICE, clientFrameworkTypes } from '../../jdl/index.js';
const { CYPRESS } = testFrameworkTypes;
const { intersection } = _;
const { ANGULAR, REACT, VUE, NO: CLIENT_FRAMEWORK_NO } = clientFrameworkTypes;
const microfrontendsToPromptValue = answer => (Array.isArray(answer) ? answer.map(({ baseName }) => baseName).join(',') : answer);
const promptValueToMicrofrontends = answer => answer
    ? answer
        .split(',')
        .map(baseName => baseName.trim())
        .filter(Boolean)
        .map(baseName => ({ baseName }))
    : [];
const command = {
    options: {},
    configs: {
        clientFramework: {
            description: 'Provide client framework for the application',
            cli: {
                type: String,
            },
            prompt: generator => ({
                type: 'list',
                message: () => generator.jhipsterConfigWithDefaults.applicationType === APPLICATION_TYPE_MICROSERVICE
                    ? `Which ${chalk.yellow('*Framework*')} would you like to use as microfrontend?`
                    : `Which ${chalk.yellow('*Framework*')} would you like to use for the client?`,
            }),
            choices: [
                { value: VUE, name: 'Vue' },
                { value: CLIENT_FRAMEWORK_NO, name: 'No client' },
            ],
        },
        microfrontend: {
            description: 'Enable microfrontend support',
            cli: {
                type: Boolean,
            },
            prompt: ({ jhipsterConfigWithDefaults: config }) => ({
                type: 'confirm',
                when: answers => [ANGULAR, REACT, VUE].includes(answers.clientFramework ?? config.clientFramework) &&
                    config.applicationType === APPLICATION_TYPE_GATEWAY,
                message: `Do you want to enable ${chalk.yellow('*microfrontends*')}?`,
                default: false,
            }),
        },
        microfrontends: {
            description: 'Microfrontends to load',
            cli: {
                type: String,
            },
            prompt: ({ jhipsterConfigWithDefaults: config }) => ({
                when: answers => {
                    const askForMicrofrontends = Boolean((answers.microfrontend ?? config.microfrontend) &&
                        (answers.applicationType ?? config.applicationType) === APPLICATION_TYPE_GATEWAY);
                    if (askForMicrofrontends && answers.microfrontends) {
                        answers.microfrontends = microfrontendsToPromptValue(answers.microfrontends);
                    }
                    else {
                        answers.microfrontends = [];
                    }
                    return askForMicrofrontends;
                },
                type: 'input',
                message: `Comma separated ${chalk.yellow('*microfrontend*')} app names.`,
                filter: promptValueToMicrofrontends,
                transformer: microfrontendsToPromptValue,
            }),
        },
        clientTestFrameworks: {
            description: 'Client test frameworks',
            prompt: ({ jhipsterConfigWithDefaults: config }) => ({
                when: answers => [ANGULAR, REACT, VUE].includes(answers.clientFramework ?? config.clientFramework),
                type: 'checkbox',
                message: 'Besides Jest/Vitest, which testing frameworks would you like to use?',
                default: () => intersection([CYPRESS], config.testFrameworks),
            }),
            choices: [{ name: 'Cypress', value: CYPRESS }],
        },
        withAdminUi: {
            description: 'Generate administrative user interface',
            cli: {
                type: Boolean,
            },
            prompt: ({ jhipsterConfigWithDefaults: config }) => ({
                type: 'confirm',
                when: answers => [ANGULAR, REACT, VUE].includes(answers.clientFramework ?? config.clientFramework),
                message: 'Do you want to generate the admin UI?',
            }),
        },
        clientRootDir: {
            description: 'Client root',
            cli: {
                type: String,
            },
        },
    },
};
export default command;
