/**
 * 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 { APPLICATION_TYPE_GATEWAY, APPLICATION_TYPE_MICROSERVICE, APPLICATION_TYPE_MONOLITH } from "../core/application-types.js";
import { isWin32 } from "../utils/index.js";
import applicationOptions from "./application-options.js";
import authenticationTypes from "./authentication-types.js";
import buildToolTypes from "./build-tool-types.js";
import cacheTypes from "./cache-types.js";
import clientFrameworkTypes from "./client-framework-types.js";
import databaseTypes from "./database-types.js";
import serviceDiscoveryTypes from "./service-discovery-types.js";
const { CONSUL } = serviceDiscoveryTypes;
const { SQL, POSTGRESQL } = databaseTypes;
const { OptionNames, OptionValues } = applicationOptions;
const { JWT, OAUTH2 } = authenticationTypes;
const { ANGULAR, NO: NO_CLIENT_FRAMEWORK } = clientFrameworkTypes;
const { EHCACHE, HAZELCAST } = cacheTypes;
const { NO: NO_CACHE_PROVIDER, MEMCACHED } = cacheTypes;
const NO_SERVICE_DISCOVERY = serviceDiscoveryTypes.NO;
const { MAVEN } = buildToolTypes;
const { APPLICATION_TYPE, AUTHENTICATION_TYPE, BASE_NAME, BUILD_TOOL, CACHE_PROVIDER, CLIENT_FRAMEWORK, CLIENT_THEME, CLIENT_THEME_VARIANT, WITH_ADMIN_UI, DATABASE_TYPE, DEV_DATABASE_TYPE, DTO_SUFFIX, ENABLE_HIBERNATE_CACHE, ENABLE_SWAGGER_CODEGEN, ENABLE_TRANSLATION, ENTITY_SUFFIX, JHI_PREFIX, LANGUAGES, NATIVE_LANGUAGE, PACKAGE_FOLDER, PACKAGE_NAME, PROD_DATABASE_TYPE, REACTIVE, SEARCH_ENGINE, SERVER_PORT, SERVICE_DISCOVERY_TYPE, SKIP_CLIENT, SKIP_USER_MANAGEMENT, TEST_FRAMEWORKS, WEBSOCKET, ENABLE_GRADLE_DEVELOCITY, GRADLE_DEVELOCITY_HOST, } = OptionNames;
const commonDefaultOptions = {
    [AUTHENTICATION_TYPE]: JWT,
    [BUILD_TOOL]: MAVEN,
    [DTO_SUFFIX]: OptionValues[DTO_SUFFIX],
    [ENABLE_SWAGGER_CODEGEN]: OptionValues[ENABLE_SWAGGER_CODEGEN],
    [ENABLE_TRANSLATION]: OptionValues[ENABLE_TRANSLATION],
    [ENTITY_SUFFIX]: OptionValues[ENTITY_SUFFIX],
    [JHI_PREFIX]: OptionValues[JHI_PREFIX],
    messageBroker: 'no',
    [SEARCH_ENGINE]: OptionValues[SEARCH_ENGINE].no,
    [WEBSOCKET]: OptionValues[WEBSOCKET].no,
};
export function getConfigWithDefaults(customOptions = {}) {
    const options = { ...customOptions };
    const { applicationType } = options;
    if (isWin32) {
        options.autoCrlf ??= true;
    }
    if (options.graalvmSupport) {
        options[CACHE_PROVIDER] = NO_CACHE_PROVIDER;
    }
    if (applicationType === APPLICATION_TYPE_GATEWAY) {
        return getConfigForGatewayApplication(options);
    }
    if (applicationType === APPLICATION_TYPE_MICROSERVICE) {
        return getConfigForMicroserviceApplication(options);
    }
    return getConfigForMonolithApplication(options);
}
function getConfigForClientApplication(options = {}) {
    if (options[SKIP_CLIENT]) {
        options[CLIENT_FRAMEWORK] = NO_CLIENT_FRAMEWORK;
    }
    const clientFramework = options[CLIENT_FRAMEWORK];
    if (clientFramework === NO_CLIENT_FRAMEWORK) {
        return options;
    }
    if (!options[CLIENT_THEME]) {
        options[CLIENT_THEME] = OptionValues[CLIENT_THEME];
    }
    else if (options[CLIENT_THEME] !== OptionValues[CLIENT_THEME] && !options[CLIENT_THEME_VARIANT]) {
        options[CLIENT_THEME_VARIANT] = 'primary';
    }
    return options;
}
function getConfigForAuthenticationType(options = {}) {
    if (typeof options[SKIP_USER_MANAGEMENT] !== 'boolean') {
        if (options[AUTHENTICATION_TYPE] === OAUTH2) {
            options[SKIP_USER_MANAGEMENT] = true;
        }
        else {
            options[SKIP_USER_MANAGEMENT] = OptionValues[SKIP_USER_MANAGEMENT];
        }
    }
    return options;
}
function getConfigForPackageName(options = {}) {
    if (!options[PACKAGE_NAME]) {
        if (options[PACKAGE_FOLDER]) {
            options[PACKAGE_NAME] = options[PACKAGE_FOLDER].split('/').filter(Boolean).join('.');
        }
        else {
            options[PACKAGE_NAME] = OptionValues[PACKAGE_NAME];
        }
    }
    return options;
}
function getConfigForCacheProvider(options = {}) {
    if (options[REACTIVE] || options[CACHE_PROVIDER] === undefined) {
        options[CACHE_PROVIDER] = NO_CACHE_PROVIDER;
    }
    options[ENABLE_HIBERNATE_CACHE] ??=
        options[DATABASE_TYPE] === SQL && !options[REACTIVE] && ![NO_CACHE_PROVIDER, MEMCACHED].includes(options[CACHE_PROVIDER]);
    return options;
}
function getConfigForReactive(options = {}) {
    options[REACTIVE] ??= false;
    return options;
}
function getConfigForTranslation(options = {}) {
    options[ENABLE_TRANSLATION] ??= true;
    options[NATIVE_LANGUAGE] ??= 'en';
    if (options[ENABLE_TRANSLATION] && options[LANGUAGES] === undefined) {
        options[LANGUAGES] = [];
    }
    return options;
}
function getConfigForDatabaseType(options = {}) {
    options[DATABASE_TYPE] ??= SQL;
    if (options[DATABASE_TYPE] === SQL) {
        options[PROD_DATABASE_TYPE] ??= POSTGRESQL;
        options[DEV_DATABASE_TYPE] ??= options[PROD_DATABASE_TYPE];
    }
    if (options[DATABASE_TYPE] === 'no') {
        options[SKIP_USER_MANAGEMENT] = true;
    }
    options.databaseMigration ??= options.databaseType === SQL ? 'liquibase' : 'no';
    return options;
}
function getServerConfigForMonolithApplication(customOptions = {}) {
    const options = {
        ...commonDefaultOptions,
        [CACHE_PROVIDER]: EHCACHE,
        [CLIENT_FRAMEWORK]: ANGULAR,
        [SERVER_PORT]: OptionValues[SERVER_PORT],
        [SERVICE_DISCOVERY_TYPE]: NO_SERVICE_DISCOVERY,
        [WITH_ADMIN_UI]: true,
        ...customOptions,
    };
    return {
        ...options,
        [APPLICATION_TYPE]: APPLICATION_TYPE_MONOLITH,
    };
}
export function getConfigForMonolithApplication(customOptions = {}) {
    let options = getServerConfigForMonolithApplication(customOptions);
    options = getConfigForClientApplication(options);
    options = getConfigForPackageName(options);
    options = getConfigForDatabaseType(options);
    options = getConfigForCacheProvider(options);
    options = getConfigForReactive(options);
    options = getConfigForTranslation(options);
    return getConfigForAuthenticationType(options);
}
function getServerConfigForGatewayApplication(customOptions = {}) {
    const options = {
        ...commonDefaultOptions,
        [CLIENT_FRAMEWORK]: ANGULAR,
        [SERVER_PORT]: OptionValues[SERVER_PORT],
        [SERVICE_DISCOVERY_TYPE]: CONSUL,
        [WITH_ADMIN_UI]: true,
        ...customOptions,
    };
    options[CACHE_PROVIDER] = NO_CACHE_PROVIDER;
    options[ENABLE_HIBERNATE_CACHE] = false;
    return {
        [REACTIVE]: true,
        ...options,
        [APPLICATION_TYPE]: APPLICATION_TYPE_GATEWAY,
    };
}
export function getConfigForGatewayApplication(customOptions = {}) {
    let options = getServerConfigForGatewayApplication(customOptions);
    options = getConfigForClientApplication(options);
    options = getConfigForPackageName(options);
    options = getConfigForDatabaseType(options);
    options = getConfigForCacheProvider(options);
    options = getConfigForReactive(options);
    options = getConfigForTranslation(options);
    return getConfigForAuthenticationType(options);
}
function getServerConfigForMicroserviceApplication(customOptions = {}) {
    const DEFAULT_SERVER_PORT = 8081;
    const options = {
        ...commonDefaultOptions,
        [CACHE_PROVIDER]: HAZELCAST,
        [SERVER_PORT]: DEFAULT_SERVER_PORT,
        [SERVICE_DISCOVERY_TYPE]: CONSUL,
        [SKIP_USER_MANAGEMENT]: true,
        [CLIENT_FRAMEWORK]: NO_CLIENT_FRAMEWORK,
        ...customOptions,
    };
    options[WITH_ADMIN_UI] = false;
    return {
        ...options,
        [APPLICATION_TYPE]: APPLICATION_TYPE_MICROSERVICE,
    };
}
export function getConfigForMicroserviceApplication(customOptions = {}) {
    let options = getServerConfigForMicroserviceApplication(customOptions);
    options = getConfigForClientApplication(options);
    options = getConfigForPackageName(options);
    options = getConfigForDatabaseType(options);
    options = getConfigForCacheProvider(options);
    options = getConfigForReactive(options);
    options = getConfigForTranslation(options);
    return getConfigForAuthenticationType(options);
}
export function getDefaultConfigForNewApplication(customOptions = {}) {
    const options = {
        ...commonDefaultOptions,
        [BASE_NAME]: OptionValues[BASE_NAME],
        [LANGUAGES]: OptionValues[LANGUAGES],
        [TEST_FRAMEWORKS]: [],
        [ENABLE_GRADLE_DEVELOCITY]: OptionValues[ENABLE_GRADLE_DEVELOCITY],
        [GRADLE_DEVELOCITY_HOST]: OptionValues[GRADLE_DEVELOCITY_HOST],
        ...customOptions,
    };
    return getConfigWithDefaults(options);
}
