const require_rolldown_runtime = require('../../../../../../../../../../../../../../_virtual/rolldown_runtime.cjs');
const require_logger = require('../logger.cjs');
const require_cache = require('../utils/cache.cjs');
const require_getPackageJsonPath = require('../utils/getPackageJsonPath.cjs');
const require_buildConfigurationFields = require('./buildConfigurationFields.cjs');
const require_searchConfigurationFile = require('./searchConfigurationFile.cjs');
const require_loadConfigurationFile = require('./loadConfigurationFile.cjs');
let node_path = require("node:path");
let deepmerge = require("deepmerge");
deepmerge = require_rolldown_runtime.__toESM(deepmerge);

//#region ../@intlayer/webpack/dist/esm/chokidar/dist/esm/core/dist/esm/config/dist/esm/configFile/getConfiguration.mjs
/**
* Get the configuration for the intlayer by reading the configuration file (e.g. intlayer.config.js)
*/
const getConfigurationAndFilePath = (options) => {
	let baseDir;
	try {
		baseDir = options?.baseDir ?? require_getPackageJsonPath.getPackageJsonPath().baseDir;
	} catch (_err) {
		return {
			configuration: require_buildConfigurationFields.buildConfigurationFields({}, options?.baseDir, options?.logFunctions),
			configurationFilePath: void 0
		};
	}
	const cachedConfiguration = require_cache.cache.get(options);
	if (cachedConfiguration) return cachedConfiguration;
	const { configurationFilePath, numCustomConfiguration } = require_searchConfigurationFile.searchConfigurationFile(baseDir);
	if (options?.override?.log?.mode === "verbose") logConfigFileResult(baseDir, numCustomConfiguration, configurationFilePath);
	let storedConfiguration;
	if (configurationFilePath) storedConfiguration = require_buildConfigurationFields.buildConfigurationFields(require_loadConfigurationFile.loadConfigurationFile(configurationFilePath, {
		projectRequire: options?.require,
		envVarOptions: {
			env: options?.env,
			envFile: options?.envFile
		},
		additionalEnvVars: options?.additionalEnvVars,
		aliases: options?.aliases
	}), options?.baseDir, options?.logFunctions);
	const projectRequireConfig = options?.require ? { build: {
		require: options?.require,
		cache: options?.cache
	} } : {};
	const configuration = (0, deepmerge.default)((0, deepmerge.default)(storedConfiguration ?? {}, projectRequireConfig), options?.override ?? {});
	require_cache.cache.set(options, {
		configuration,
		configurationFilePath
	});
	return {
		configuration,
		configurationFilePath
	};
};
/**
* Get the configuration for the intlayer by reading the configuration file (e.g. intlayer.config.js)
*/
const getConfiguration = (options) => getConfigurationAndFilePath(options).configuration;
const logConfigFileResult = (baseDir, numCustomConfiguration, configurationFilePath) => {
	if (numCustomConfiguration === 0) require_logger.logger("Configuration file not found, using default configuration.", { isVerbose: true });
	else {
		const relativeOutputPath = (0, node_path.relative)(baseDir, configurationFilePath);
		if (numCustomConfiguration === 1) require_logger.logger(`Configuration file found: ${relativeOutputPath}.`, { isVerbose: true });
		else require_logger.logger(`Multiple configuration files found, using ${relativeOutputPath}.`, { isVerbose: true });
	}
};

//#endregion
exports.getConfiguration = getConfiguration;
//# sourceMappingURL=getConfiguration.cjs.map