import { normalizePath } from "../utils/normalizePath.mjs";
import { CACHE, IMPORT_MODE, OPTIMIZE, OUTPUT_FORMAT, TRAVERSE_PATTERN } from "../defaultValues/build.mjs";
import { CACHE_DIR, CONFIG_DIR, CONTENT_DIR, DICTIONARIES_DIR, DYNAMIC_DICTIONARIES_DIR, EXCLUDED_PATHS, FETCH_DICTIONARIES_DIR, FILE_EXTENSIONS, MAIN_DIR, MODULE_AUGMENTATION_DIR, REMOTE_DICTIONARIES_DIR, TYPES_DIR, UNMERGED_DICTIONARIES_DIR, WATCH } from "../defaultValues/content.mjs";
import { FILL } from "../defaultValues/dictionary.mjs";
import { APPLICATION_URL, BACKEND_URL, CMS_URL, DICTIONARY_PRIORITY_STRATEGY, EDITOR_URL, IS_ENABLED, LIVE_SYNC, LIVE_SYNC_PORT, PORT } from "../defaultValues/editor.mjs";
import { DEFAULT_LOCALE, LOCALES, REQUIRED_LOCALES, STRICT_MODE } from "../defaultValues/internationalization.mjs";
import { MODE, PREFIX } from "../defaultValues/log.mjs";
import { BASE_PATH, ROUTING_MODE, STORAGE } from "../defaultValues/routing.mjs";
import { isAbsolute, join } from "node:path";

//#region ../react-intlayer/dist/esm/@intlayer/config/dist/esm/configFile/buildConfigurationFields.mjs
let storedConfiguration;
const buildInternationalizationFields = (customConfiguration) => ({
	locales: customConfiguration?.locales ?? LOCALES,
	requiredLocales: customConfiguration?.requiredLocales ?? customConfiguration?.locales ?? REQUIRED_LOCALES,
	strictMode: customConfiguration?.strictMode ?? STRICT_MODE,
	defaultLocale: customConfiguration?.defaultLocale ?? DEFAULT_LOCALE
});
const buildRoutingFields = (customConfiguration) => ({
	mode: customConfiguration?.mode ?? ROUTING_MODE,
	storage: customConfiguration?.storage ?? STORAGE,
	basePath: customConfiguration?.basePath ?? BASE_PATH
});
const buildContentFields = (customConfiguration, baseDir) => {
	const notDerivedContentConfig = {
		fileExtensions: customConfiguration?.fileExtensions ?? FILE_EXTENSIONS,
		baseDir: customConfiguration?.baseDir ?? baseDir ?? process.cwd(),
		excludedPath: customConfiguration?.excludedPath ?? EXCLUDED_PATHS,
		watch: customConfiguration?.watch ?? WATCH,
		formatCommand: customConfiguration?.formatCommand
	};
	const optionalJoinBaseDir = (path) => {
		if (isAbsolute(path)) return path;
		return join(notDerivedContentConfig.baseDir, path);
	};
	const baseDirDerivedConfiguration = {
		contentDir: (customConfiguration?.contentDir ?? CONTENT_DIR).map(optionalJoinBaseDir),
		moduleAugmentationDir: optionalJoinBaseDir(customConfiguration?.moduleAugmentationDir ?? MODULE_AUGMENTATION_DIR),
		unmergedDictionariesDir: optionalJoinBaseDir(customConfiguration?.unmergedDictionariesDir ?? UNMERGED_DICTIONARIES_DIR),
		remoteDictionariesDir: optionalJoinBaseDir(customConfiguration?.remoteDictionariesDir ?? REMOTE_DICTIONARIES_DIR),
		dictionariesDir: optionalJoinBaseDir(customConfiguration?.dictionariesDir ?? DICTIONARIES_DIR),
		dynamicDictionariesDir: optionalJoinBaseDir(customConfiguration?.dynamicDictionariesDir ?? DYNAMIC_DICTIONARIES_DIR),
		fetchDictionariesDir: optionalJoinBaseDir(customConfiguration?.fetchDictionariesDir ?? FETCH_DICTIONARIES_DIR),
		typesDir: optionalJoinBaseDir(customConfiguration?.typesDir ?? TYPES_DIR),
		mainDir: optionalJoinBaseDir(customConfiguration?.mainDir ?? MAIN_DIR),
		configDir: optionalJoinBaseDir(customConfiguration?.configDir ?? CONFIG_DIR),
		cacheDir: optionalJoinBaseDir(customConfiguration?.cacheDir ?? CACHE_DIR)
	};
	const patternsConfiguration = {
		watchedFilesPattern: notDerivedContentConfig.fileExtensions.map((ext) => `/**/*${ext}`),
		watchedFilesPatternWithPath: notDerivedContentConfig.fileExtensions.flatMap((ext) => baseDirDerivedConfiguration.contentDir.map((contentDir) => `${normalizePath(contentDir)}/**/*${ext}`)),
		outputFilesPatternWithPath: `${normalizePath(baseDirDerivedConfiguration.dictionariesDir)}/**/*.json`
	};
	return {
		...notDerivedContentConfig,
		...baseDirDerivedConfiguration,
		...patternsConfiguration
	};
};
const buildEditorFields = (customConfiguration) => ({
	applicationURL: customConfiguration?.applicationURL ?? APPLICATION_URL,
	editorURL: customConfiguration?.editorURL ?? EDITOR_URL,
	cmsURL: customConfiguration?.cmsURL ?? CMS_URL,
	backendURL: customConfiguration?.backendURL ?? BACKEND_URL,
	port: customConfiguration?.port ?? PORT,
	enabled: customConfiguration?.enabled ?? IS_ENABLED,
	clientId: customConfiguration?.clientId ?? void 0,
	clientSecret: customConfiguration?.clientSecret ?? void 0,
	dictionaryPriorityStrategy: customConfiguration?.dictionaryPriorityStrategy ?? DICTIONARY_PRIORITY_STRATEGY,
	liveSync: customConfiguration?.liveSync ?? LIVE_SYNC,
	liveSyncPort: customConfiguration?.liveSyncPort ?? LIVE_SYNC_PORT,
	liveSyncURL: customConfiguration?.liveSyncURL ?? `http://localhost:${customConfiguration?.liveSyncPort ?? LIVE_SYNC_PORT}`
});
const buildLogFields = (customConfiguration, logFunctions) => ({
	mode: customConfiguration?.mode ?? MODE,
	prefix: customConfiguration?.prefix ?? PREFIX,
	error: logFunctions?.error,
	log: logFunctions?.log,
	info: logFunctions?.info,
	warn: logFunctions?.warn
});
const buildAiFields = (customConfiguration) => ({
	provider: customConfiguration?.provider,
	apiKey: customConfiguration?.apiKey,
	model: customConfiguration?.model,
	temperature: customConfiguration?.temperature,
	applicationContext: customConfiguration?.applicationContext
});
const buildBuildFields = (customConfiguration) => ({
	optimize: customConfiguration?.optimize ?? OPTIMIZE,
	importMode: customConfiguration?.importMode ?? IMPORT_MODE,
	traversePattern: customConfiguration?.traversePattern ?? TRAVERSE_PATTERN,
	outputFormat: customConfiguration?.outputFormat ?? OUTPUT_FORMAT,
	cache: customConfiguration?.cache ?? CACHE,
	require: customConfiguration?.require
});
const buildDictionaryFields = (customConfiguration) => ({ fill: customConfiguration?.fill ?? FILL });
/**
* Build the configuration fields by merging the default values with the custom configuration
*/
const buildConfigurationFields = (customConfiguration, baseDir, logFunctions) => {
	storedConfiguration = {
		internationalization: buildInternationalizationFields(customConfiguration?.internationalization),
		routing: buildRoutingFields(customConfiguration?.routing),
		content: buildContentFields(customConfiguration?.content, baseDir),
		editor: buildEditorFields(customConfiguration?.editor),
		log: buildLogFields(customConfiguration?.log, logFunctions),
		ai: buildAiFields(customConfiguration?.ai),
		build: buildBuildFields(customConfiguration?.build),
		dictionary: buildDictionaryFields(customConfiguration?.dictionary),
		plugins: customConfiguration?.plugins
	};
	return storedConfiguration;
};

//#endregion
export { buildConfigurationFields };
//# sourceMappingURL=buildConfigurationFields.mjs.map