import { upperFirstCamelCase } from '../../generators/base/support/string.js';
export const convertConfigToOption = (name, config) => {
    if (!config?.cli?.type)
        return undefined;
    const choices = config.choices?.map(choice => (typeof choice === 'string' ? choice : choice.value));
    return {
        name,
        description: config.description,
        choices,
        scope: config.scope ?? 'storage',
        ...(config.prompt && typeof config.default !== 'function' ? {} : { default: config.default }),
        ...config.cli,
    };
};
export function loadConfig(configsDef, { application, config }) {
    if (configsDef) {
        for (const [name, def] of Object.entries(configsDef)) {
            let value = application[name];
            if (value === undefined || value === null) {
                let source = config;
                if (!source) {
                    if (def.scope === 'generator') {
                        source = this;
                    }
                    else if (def.scope === 'blueprint') {
                        source = this.blueprintStorage.getAll();
                    }
                    else {
                        source = this.jhipsterConfigWithDefaults;
                    }
                }
                value = application[name] = source[name] ?? undefined;
                if (value === undefined && def.default) {
                    application[name] = typeof def.default === 'function' ? def.default(source) : def.default;
                }
            }
        }
    }
}
export const loadDerivedConfig = (configsDef, { application }) => {
    if (configsDef) {
        for (const [name, def] of Object.entries(configsDef)) {
            if (def.choices) {
                const configVal = application[name];
                for (const choice of def.choices) {
                    const choiceVal = typeof choice === 'string' ? choice : choice.value;
                    const prop = `${name}${upperFirstCamelCase(choiceVal)}`;
                    application[prop] = application[prop] ?? [].concat(configVal).includes(choiceVal);
                }
                application[`${name}Any`] = application[`${name}Any`] ?? !application[`${name}No`];
            }
        }
    }
};
