import { __ΩEnvPluginContext, __ΩEnvPluginOptions } from "../types/plugin.mjs";
import { joinPaths } from "../node_modules/.pnpm/@stryke_path@0.26.4/node_modules/@stryke/path/dist/join-paths.mjs";
import { removeEnvPrefix } from "./source-file-env.mjs";
import { defu } from "../node_modules/.pnpm/defu@6.1.4/node_modules/defu/dist/defu.mjs";
import { loadConfig } from "../node_modules/.pnpm/c12@3.3.3_magicast@0.5.1/node_modules/c12/dist/index.mjs";
import { __ΩWorkspaceConfig } from "powerlines/types/config";
import { loadEnv as loadEnv$1, loadEnvFile } from "@stryke/env/load-env";
import { kebabCase } from "@stryke/string-format/kebab-case";
import { isSetObject } from "@stryke/type-checks/is-set-object";
import { DEFAULT_ENVIRONMENT } from "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 loadEnv$1(cwd, mode);
	if (options.additionalFiles && options.additionalFiles?.length > 0) {
		const additionalEnvFiles = await Promise.all(options.additionalFiles.map(__assignType(async (additionalEnvFile) => loadEnvFile(additionalEnvFile, cwd), [
			"additionalEnvFile",
			"",
			"P\"2!\"/\""
		])));
		for (const additionalEnvFile of additionalEnvFiles) env = defu(additionalEnvFile, env);
	}
	return removeEnvPrefix(env);
}
loadEnvFiles.__type = [
	() => __Ω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)), 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: joinPaths(cacheDir, "jiti"),
			moduleCache: true
		}
	})]);
	return defu(envResult, c12Result.config, workspaceConfig);
}
loadEnvDirectory.__type = [
	() => __ΩEnvPluginOptions,
	"options",
	"directory",
	"mode",
	"cacheDir",
	"packageJson",
	() => __Ω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 defu({
		APP_NAME: 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: `${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 || (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 === DEFAULT_ENVIRONMENT ? context.config.mode : context.environment.name
	}, 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 = [
	() => __Ω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 defu(loadEnvFromContext(context, process.env), project, workspace, config);
}
loadEnv.__type = [
	() => __ΩEnvPluginContext,
	"context",
	() => __ΩEnvPluginOptions,
	"options",
	"loadEnv",
	"Pn!2\"n#2$\"`/%"
];

//#endregion
export { loadEnv, loadEnvFromContext };
//# sourceMappingURL=load.mjs.map