/**
 * Copyright 2013-2026 the original author or authors from the JHipster project.
 *
 * This file is part of the JHipster project, see https://www.jhipster.tech/
 * for more information.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { snakeCase, upperCase } from 'lodash-es';
import bootstrapCommand from "../../generators/bootstrap/command.js";
import clientCommand from "../../generators/client/command.js";
import liquibaseCommand from "../../generators/liquibase/command.js";
import springBootCommand from "../../generators/spring-boot/command.js";
import gatewayCommand from "../../generators/spring-cloud/generators/gateway/command.js";
import { createRuntime } from "../jdl/core/runtime.js";
export const extractJdlDefinitionFromCommandConfig = (configs = {}) => Object.entries(configs)
    .filter(([_name, def]) => def.jdl)
    .map(([name, def]) => ({
    ...def.jdl,
    name,
    knownChoices: def.choices?.map(choice => (typeof choice === 'string' ? choice : choice.value)),
}))
    .sort((a, b) => (b.name.startsWith(a.name) ? 1 : a.name.localeCompare(b.name)));
export const buildJDLApplicationConfig = (configs) => {
    const jdlOptions = extractJdlDefinitionFromCommandConfig(configs);
    return {
        quotedOptionNames: [],
        tokenConfigs: jdlOptions.map(option => ({
            name: upperCase(snakeCase(option.name)),
            pattern: option.name,
        })),
        validatorConfig: Object.fromEntries(jdlOptions.map(option => [
            upperCase(snakeCase(option.name)),
            {
                type: option.tokenType,
                pattern: option.tokenValuePattern,
                msg: `${option.name} property`,
            },
        ])),
        optionsValues: Object.fromEntries(jdlOptions
            .filter(option => option.knownChoices)
            .map(option => [option.name, Object.fromEntries(option.knownChoices.map(choice => [choice, choice]))])),
        optionsTypes: Object.fromEntries(jdlOptions.map(option => [
            option.name,
            {
                type: option.type,
            },
        ])),
    };
};
let defaultJDLApplicationConfig;
export const getDefaultJDLApplicationConfig = () => {
    defaultJDLApplicationConfig ??= Object.freeze(buildJDLApplicationConfig({
        ...springBootCommand.configs,
        ...bootstrapCommand.configs,
        ...clientCommand.configs,
        ...liquibaseCommand.configs,
        ...gatewayCommand.configs,
    }));
    return defaultJDLApplicationConfig;
};
let defaultRuntime;
export const getDefaultRuntime = () => {
    if (!defaultRuntime) {
        defaultRuntime = createRuntime(getDefaultJDLApplicationConfig());
    }
    return defaultRuntime;
};
