import { translations } from '@payloadcms/translations/api';
import merge from 'deepmerge';
import { defaultUserCollection } from '../auth/defaultUser.js';
import sanitizeCollection from '../collections/config/sanitize.js';
import { migrationsCollection } from '../database/migrations/migrationsCollection.js';
import { InvalidConfiguration } from '../errors/index.js';
import sanitizeGlobals from '../globals/config/sanitize.js';
import getPreferencesCollection from '../preferences/preferencesCollection.js';
import checkDuplicateCollections from '../utilities/checkDuplicateCollections.js';
import { isPlainObject } from '../utilities/isPlainObject.js';
import { defaults } from './defaults.js';
const sanitizeAdminConfig = (configToSanitize)=>{
    const sanitizedConfig = {
        ...configToSanitize
    };
    // add default user collection if none provided
    if (!sanitizedConfig?.admin?.user) {
        const firstCollectionWithAuth = sanitizedConfig.collections.find(({ auth })=>Boolean(auth));
        if (firstCollectionWithAuth) {
            sanitizedConfig.admin.user = firstCollectionWithAuth.slug;
        } else {
            sanitizedConfig.admin.user = defaultUserCollection.slug;
            sanitizedConfig.collections.push(defaultUserCollection);
        }
    }
    if (!sanitizedConfig.collections.find(({ slug })=>slug === sanitizedConfig.admin.user)) {
        throw new InvalidConfiguration(`${sanitizedConfig.admin.user} is not a valid admin user collection`);
    }
    return sanitizedConfig;
};
export const sanitizeConfig = (incomingConfig)=>{
    const configWithDefaults = merge(defaults, incomingConfig, {
        isMergeableObject: isPlainObject
    });
    if (!configWithDefaults?.serverURL) {
        configWithDefaults.serverURL = '';
    }
    const config = sanitizeAdminConfig(configWithDefaults);
    if (config.localization && config.localization.locales?.length > 0) {
        // clone localization config so to not break everything
        const firstLocale = config.localization.locales[0];
        if (typeof firstLocale === 'string') {
            config.localization.localeCodes = [
                ...config.localization.locales
            ];
            // is string[], so convert to Locale[]
            config.localization.locales = config.localization.locales.map((locale)=>({
                    code: locale,
                    label: locale,
                    rtl: false,
                    toString: ()=>locale
                }));
        } else {
            // is Locale[], so convert to string[] for localeCodes
            config.localization.localeCodes = config.localization.locales.reduce((locales, locale)=>{
                locales.push(locale.code);
                return locales;
            }, []);
            config.localization.locales = config.localization.locales.map((locale)=>({
                    ...locale,
                    toString: ()=>locale.code
                }));
        }
    }
    config.i18n = {
        fallbackLanguage: 'en',
        supportedLanguages: Object.keys(translations),
        translations,
        ...incomingConfig?.i18n ?? {}
    };
    configWithDefaults.collections.push(getPreferencesCollection(configWithDefaults));
    configWithDefaults.collections.push(migrationsCollection);
    config.collections = config.collections.map((collection)=>sanitizeCollection(configWithDefaults, collection));
    checkDuplicateCollections(config.collections);
    if (config.globals.length > 0) {
        config.globals = sanitizeGlobals(config);
    }
    if (config.serverURL !== '') {
        config.csrf.push(config.serverURL);
    }
    return config;
};

//# sourceMappingURL=sanitize.js.map