const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
const require_types_plugin = require('../types/plugin.cjs');
const require_join_paths = require('../node_modules/.pnpm/@stryke_path@0.26.4/node_modules/@stryke/path/dist/join-paths.cjs');
const require_helpers_source_file_env = require('./source-file-env.cjs');
const require_defu = require('../node_modules/.pnpm/defu@6.1.4/node_modules/defu/dist/defu.cjs');
const require_index = require('../node_modules/.pnpm/c12@3.3.3_magicast@0.5.1/node_modules/c12/dist/index.cjs');
let powerlines_types_config = require("powerlines/types/config");
let __stryke_env_load_env = require("@stryke/env/load-env");
let __stryke_string_format_kebab_case = require("@stryke/string-format/kebab-case");
let __stryke_type_checks_is_set_object = require("@stryke/type-checks/is-set-object");
let powerlines_lib_constants_environments = require("powerlines/lib/constants/environments");

//#region src/helpers/load.ts
function __assignType(fn, args) {
	fn.__type = args;
	return fn;
}
async function loadEnvFiles(options, mode, cwd) {
	let env = await (0, __stryke_env_load_env.loadEnv)(cwd, mode);
	if (options.additionalFiles && options.additionalFiles?.length > 0) {
		const additionalEnvFiles = await Promise.all(options.additionalFiles.map(__assignType(async (additionalEnvFile) => (0, __stryke_env_load_env.loadEnvFile)(additionalEnvFile, cwd), [
			"additionalEnvFile",
			"",
			"P\"2!\"/\""
		])));
		for (const additionalEnvFile of additionalEnvFiles) env = require_defu.defu(additionalEnvFile, env);
	}
	return require_helpers_source_file_env.removeEnvPrefix(env);
}
loadEnvFiles.__type = [
	() => require_types_plugin.__ΩEnvPluginOptions,
	"options",
	"mode",
	"cwd",
	"loadEnvFiles",
	"Pn!2\"&2#&2$\"`/%"
];
async function loadEnvDirectory(options, directory, mode, cacheDir, packageJson, workspaceConfig) {
	const [envResult, c12Result] = await Promise.all([(loadEnvFiles.Ω = [["!"]], loadEnvFiles(options, mode, directory)), require_index.loadConfig({
		cwd: directory,
		name: "storm",
		envName: mode,
		defaults: {
			NAME: packageJson.name?.replace(`@${workspaceConfig.namespace}/`, ""),
			MODE: mode,
			ORG: workspaceConfig.organization
		},
		globalRc: true,
		packageJson: true,
		dotenv: true,
		jitiOptions: {
			fsCache: require_join_paths.joinPaths(cacheDir, "jiti"),
			moduleCache: true
		}
	})]);
	return require_defu.defu(envResult, c12Result.config, workspaceConfig);
}
loadEnvDirectory.__type = [
	() => require_types_plugin.__ΩEnvPluginOptions,
	"options",
	"directory",
	"mode",
	"cacheDir",
	"packageJson",
	() => powerlines_types_config.__ΩWorkspaceConfig,
	"workspaceConfig",
	"loadEnvDirectory",
	"Pn!2\"&2#&2$&2%!2&n'2(\"`/)"
];
/**
* Retrieves various dotenv configuration parameters from the context.
*
* @param context - The context to retrieve the dotenv configuration from.
* @param parsed - The parsed dotenv configuration.
* @returns An object containing the dotenv configuration.
*/
function loadEnvFromContext(context, parsed) {
	return require_defu.defu({
		APP_NAME: (0, __stryke_string_format_kebab_case.kebabCase)(context.config.name || context.packageJson.name?.replace(`/${context.workspaceConfig.namespace}`, "")),
		APP_VERSION: context.packageJson.version,
		BUILD_ID: context.meta.buildId,
		BUILD_TIMESTAMP: new Date(context.meta.timestamp).toISOString(),
		BUILD_CHECKSUM: context.meta.checksum,
		RELEASE_ID: context.meta.releaseId,
		RELEASE_TAG: `${(0, __stryke_string_format_kebab_case.kebabCase)(context.config.name)}@${context.packageJson.version}`,
		DEFAULT_LOCALE: context.workspaceConfig.locale,
		DEFAULT_TIMEZONE: context.workspaceConfig.timezone,
		LOG_LEVEL: context.config.logLevel === "trace" ? "debug" : context.config.logLevel,
		ERROR_URL: context.workspaceConfig.error?.url,
		ORGANIZATION: context.config.organization || ((0, __stryke_type_checks_is_set_object.isSetObject)(context.workspaceConfig.organization) ? context.workspaceConfig.organization.name : context.workspaceConfig.organization),
		PLATFORM: context.config.build.platform,
		MODE: context.config.mode,
		TEST: context.config.mode === "test",
		DEBUG: context.config.mode === "development",
		STACKTRACE: context.config.mode !== "production",
		ENVIRONMENT: !context.environment.name || context.environment.name === powerlines_lib_constants_environments.DEFAULT_ENVIRONMENT ? context.config.mode : context.environment.name
	}, (0, __stryke_type_checks_is_set_object.isSetObject)(context?.env?.types?.env) ? context.env.types.env?.getProperties().reduce(__assignType((ret, prop) => {
		ret[prop.name] = parsed[prop.name] ?? prop.getDefaultValue();
		return ret;
	}, [
		"ret",
		"prop",
		"",
		"P\"2!\"2\"\"/#"
	]), {}) : {});
}
loadEnvFromContext.__type = [
	() => require_types_plugin.__ΩEnvPluginContext,
	"context",
	"parsed",
	"loadEnvFromContext",
	"Retrieves various dotenv configuration parameters from the context.",
	"Pn!2\"!2#\"/$?%"
];
async function loadEnv(context, options) {
	const [project, workspace, config] = await Promise.all([
		(loadEnvDirectory.Ω = [["!"]], loadEnvDirectory(options, context.config.projectRoot, context.config.mode, context.cachePath, context.packageJson, context.workspaceConfig)),
		(loadEnvDirectory.Ω = [["!"]], loadEnvDirectory(options, context.workspaceConfig.workspaceRoot, context.config.mode, context.cachePath, context.packageJson, context.workspaceConfig)),
		(loadEnvDirectory.Ω = [["!"]], loadEnvDirectory(options, context.envPaths.config, context.config.mode, context.cachePath, context.packageJson, context.workspaceConfig))
	]);
	return require_defu.defu(loadEnvFromContext(context, process.env), project, workspace, config);
}
loadEnv.__type = [
	() => require_types_plugin.__ΩEnvPluginContext,
	"context",
	() => require_types_plugin.__ΩEnvPluginOptions,
	"options",
	"loadEnv",
	"Pn!2\"n#2$\"`/%"
];

//#endregion
exports.loadEnv = loadEnv;
exports.loadEnvFromContext = loadEnvFromContext;