Object.defineProperty(exports, '__esModule', { value: true });
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
const require_types_runtime = require('./types/runtime.cjs');
const require_types_plugin = require('./types/plugin.cjs');
require('./types/index.cjs');
const require_join_paths = require('./node_modules/.pnpm/@stryke_path@0.26.4/node_modules/@stryke/path/dist/join-paths.cjs');
const require_is_parent_path = require('./node_modules/.pnpm/@stryke_path@0.26.4/node_modules/@stryke/path/dist/is-parent-path.cjs');
const require_helpers_reflect = require('./helpers/reflect.cjs');
const require_helpers_persistence = require('./helpers/persistence.cjs');
const require_helpers_template_helpers = require('./helpers/template-helpers.cjs');
const require_helpers_source_file_env = require('./helpers/source-file-env.cjs');
const require_defu = require('./node_modules/.pnpm/defu@6.1.4/node_modules/defu/dist/defu.cjs');
const require_helpers_load = require('./helpers/load.cjs');
const require_helpers_create_reflection_resource = require('./helpers/create-reflection-resource.cjs');
require('./helpers/index.cjs');
const require_helpers_docs_helper = require('./helpers/docs-helper.cjs');
const require_helpers_automd_generator = require('./helpers/automd-generator.cjs');
const require_components_docs = require('./components/docs.cjs');
const require_components_env = require('./components/env.cjs');
require('./components/index.cjs');
const require_babel_plugin = require('./babel/plugin.cjs');
require('./babel/index.cjs');
const require_parse_type_definition = require('./node_modules/.pnpm/@stryke_convert@0.6.38/node_modules/@stryke/convert/dist/parse-type-definition.cjs');
const require_to_array = require('./node_modules/.pnpm/@stryke_convert@0.6.38/node_modules/@stryke/convert/dist/to-array.cjs');
let __powerlines_deepkit_vendor_type = require("@powerlines/deepkit/vendor/type");
let __stryke_env_types = require("@stryke/env/types");
let __alloy_js_core_jsx_runtime = require("@alloy-js/core/jsx-runtime");
let __powerlines_plugin_alloy = require("@powerlines/plugin-alloy");
__powerlines_plugin_alloy = require_rolldown_runtime.__toESM(__powerlines_plugin_alloy);
let __powerlines_plugin_alloy_render = require("@powerlines/plugin-alloy/render");
let __powerlines_plugin_automd = require("@powerlines/plugin-automd");
__powerlines_plugin_automd = require_rolldown_runtime.__toESM(__powerlines_plugin_automd);
let __powerlines_plugin_babel = require("@powerlines/plugin-babel");
__powerlines_plugin_babel = require_rolldown_runtime.__toESM(__powerlines_plugin_babel);
let __stryke_fs_exists = require("@stryke/fs/exists");
let __stryke_string_format_constant_case = require("@stryke/string-format/constant-case");

//#region src/index.tsx
/**
* A Powerlines plugin to inject environment variables into the source code.
*/
const plugin = (options = {}) => {
	return [
		(0, __powerlines_plugin_alloy.default)(options.alloy),
		(0, __powerlines_plugin_babel.default)(options.babel),
		{
			name: "env",
			async config() {
				this.debug("Providing default configuration for the Powerlines `env` build plugin.");
				const config = {
					env: require_defu.defu(options, {
						types: {},
						prefix: []
					}),
					transform: { babel: { plugins: [require_babel_plugin.envBabelPlugin] } }
				};
				if (config.env.types) config.env.types = require_parse_type_definition.parseTypeDefinition(config.env.types);
				else {
					this.warn("The `env.types` configuration parameter was not provided. Please ensure this is expected.");
					const envDefaultTypeDefinition = await require_helpers_persistence.getEnvDefaultTypeDefinition(this);
					config.env.types = require_parse_type_definition.parseTypeDefinition(`${envDefaultTypeDefinition.file}#${envDefaultTypeDefinition.name}`);
				}
				if (config.env.secrets) config.env.secrets = require_parse_type_definition.parseTypeDefinition(config.env.secrets);
				else {
					const secretsDefaultTypeDefinition = await require_helpers_persistence.getSecretsDefaultTypeDefinition(this);
					config.env.secrets = require_parse_type_definition.parseTypeDefinition(`${secretsDefaultTypeDefinition.file}#${secretsDefaultTypeDefinition.name}`);
				}
				config.env.prefix = require_to_array.toArray(config.env.prefix ?? []).reduce((ret, prefix) => {
					const formattedPrefix = (0, __stryke_string_format_constant_case.constantCase)(prefix);
					if (!ret.includes(formattedPrefix)) ret.push(formattedPrefix);
					return ret;
				}, [
					...__stryke_env_types.ENV_PREFIXES,
					"POWERLINES_",
					this.config.framework && this.config.framework !== "powerlines" && `${(0, __stryke_string_format_constant_case.constantCase)(this.config.framework)}_`
				].filter(Boolean));
				config.env.prefix = require_to_array.toArray(config.env.prefix).reduce((ret, prefix) => {
					if (!ret.includes(prefix.replace(/_$/g, ""))) ret.push(prefix.replace(/_$/g, ""));
					return ret;
				}, []);
				return config;
			},
			async configResolved() {
				this.debug(`Environment plugin configuration has been resolved for the Powerlines project.`);
				this.env = require_defu.defu({ parsed: await require_helpers_load.loadEnv(this, this.config.env) }, this.env ?? {}, {
					types: { env: {} },
					used: {
						env: {},
						secrets: {}
					},
					parsed: {},
					injected: {}
				});
				if (this.config.command !== "prepare" && !this.config.skipCache && this.persistedMeta?.checksum === this.meta.checksum && (0, __stryke_fs_exists.existsSync)(require_helpers_persistence.getEnvTypeReflectionsPath(this, "env"))) {
					this.debug(`Skipping reflection initialization as the meta checksum has not changed.`);
					this.env.types.env = await require_helpers_persistence.readEnvTypeReflection(this, "env");
					if ((0, __stryke_fs_exists.existsSync)(require_helpers_persistence.getEnvReflectionsPath(this, "env"))) this.env.used.env = await require_helpers_persistence.readEnvReflection(this);
					if ((0, __stryke_fs_exists.existsSync)(require_helpers_persistence.getEnvTypeReflectionsPath(this, "secrets"))) this.env.types.secrets = await require_helpers_persistence.readEnvTypeReflection(this, "secrets");
					if ((0, __stryke_fs_exists.existsSync)(require_helpers_persistence.getEnvReflectionsPath(this, "secrets"))) this.env.used.secrets = await require_helpers_persistence.readSecretsReflection(this);
				} else {
					this.debug(`Starting environment configuration reflection initialization.`);
					this.env.types.env = await require_helpers_reflect.reflectEnv(this, this.config.env.types?.file ? require_is_parent_path.isParentPath(this.config.env.types?.file, this.workspaceConfig.workspaceRoot) ? this.config.env.types?.file : require_join_paths.joinPaths(this.config.projectRoot, this.config.env.types?.file) : void 0, this.config.env.types?.name);
					if (!this.env.types.env) throw new Error("Failed to find the environment configuration type reflection in the context.");
					await require_helpers_persistence.writeEnvTypeReflection(this, this.env.types.env, "env");
					this.env.types.secrets = await require_helpers_reflect.reflectSecrets(this, this.config.env.secrets?.file ? require_is_parent_path.isParentPath(this.config.env.secrets?.file, this.workspaceConfig.workspaceRoot) ? this.config.env.secrets?.file : require_join_paths.joinPaths(this.config.projectRoot, this.config.env.secrets?.file) : void 0, this.config.env.secrets?.name);
					if (!this.env.types.secrets) throw new Error("Failed to find the secrets configuration type reflection in the context.");
					await require_helpers_persistence.writeEnvTypeReflection(this, this.env.types.secrets, "secrets");
					this.debug(`Resolved ${this.env.types.env.getProperties().length ?? 0} environment configuration parameters and ${this.env.types.secrets?.getProperties().length ?? 0} secret configuration parameters`);
					const envWithAlias = this.env.types.env.getProperties().filter((prop) => prop.getAlias().length > 0);
					Object.entries(await require_helpers_load.loadEnv(this, this.config.env)).forEach(([key, value]) => {
						const unprefixedKey = this.config.env.prefix.reduce((ret, prefix) => {
							if (key.replace(/_$/g, "").startsWith(prefix)) return key.replace(/_$/g, "").slice(prefix.length);
							return ret;
						}, key);
						const aliasKey = envWithAlias.find((prop) => prop?.getAlias().reverse().includes(unprefixedKey));
						if (this.env.types.env?.hasProperty(unprefixedKey) || aliasKey) this.env.types.env.getProperty(unprefixedKey).setDefaultValue(value);
					});
					this.env.used.env = new __powerlines_deepkit_vendor_type.ReflectionClass({
						kind: __powerlines_deepkit_vendor_type.ReflectionKind.objectLiteral,
						typeName: "Env",
						description: `An object containing the environment configuration parameters used by the ${this.config.name ? `${this.config.name} application` : "application"}.`,
						types: []
					}, this.env.types.env);
					await require_helpers_persistence.writeEnvReflection(this, this.env.used.env, "env");
					if (this.env.types.secrets) {
						await require_helpers_persistence.writeEnvTypeReflection(this, this.env.types.secrets, "secrets");
						this.env.used.secrets = new __powerlines_deepkit_vendor_type.ReflectionClass({
							kind: __powerlines_deepkit_vendor_type.ReflectionKind.objectLiteral,
							typeName: "Secrets",
							description: `An object containing the secret configuration parameters used by the ${this.config.name ? `${this.config.name} application` : "application"}.`,
							types: []
						}, this.env.types.secrets);
						await require_helpers_persistence.writeEnvReflection(this, this.env.used.secrets, "secrets");
					}
				}
			},
			async prepare() {
				const _self$ = this;
				this.debug(`Preparing the Environment runtime artifacts for the Powerlines project.`);
				const result = await require_helpers_persistence.readEnvTypeReflection(this, "env");
				return (0, __powerlines_plugin_alloy_render.render)(this, (0, __alloy_js_core_jsx_runtime.createComponent)(require_components_env.EnvBuiltin, {
					get defaultConfig() {
						return _self$.config.env.defaultConfig;
					},
					reflection: result
				}));
			},
			transform: {
				order: "post",
				async handler() {
					if (this.env.used.env.getProperties().length > 0) {
						this.trace(`Persisting used environment configuration reflections to ${require_helpers_persistence.getEnvReflectionsPath(this, "env")}.`);
						await require_helpers_persistence.writeEnvReflection(this, this.env.used.env, "env");
					}
					if (this.env.used.secrets.getProperties().length > 0) {
						this.trace(`Persisting used secret configuration reflections to ${require_helpers_persistence.getEnvReflectionsPath(this, "secrets")}.`);
						await require_helpers_persistence.writeEnvReflection(this, this.env.used.secrets, "secrets");
					}
				}
			},
			async docs() {
				this.debug(`Documenting environment variables configuration values in "${require_join_paths.joinPaths(require_helpers_docs_helper.getDocsOutputPath(this), "env.md")}"`);
				const result = await require_helpers_persistence.readEnvTypeReflection(this, "env");
				return (0, __powerlines_plugin_alloy_render.render)(this, (0, __alloy_js_core_jsx_runtime.createComponent)(require_components_docs.EnvDocsFile, {
					levelOffset: 0,
					reflection: result
				}));
			},
			async buildEnd() {
				const reflectionPath = require_helpers_persistence.getEnvReflectionsPath(this, "env");
				this.debug(`Writing env reflection types to ${reflectionPath}.`);
				await require_helpers_persistence.writeEnvReflection(this, this.env.used.env, "env");
			}
		},
		{
			name: "env:automd-generator",
			config() {
				return { automd: require_defu.defu(options.automd ?? {}, { generators: { env: require_helpers_automd_generator.env(this) } }) };
			}
		},
		{
			name: "env:vite",
			vite: { configResolved() {
				return { envPrefix: this.config?.env?.prefix };
			} }
		},
		(0, __powerlines_plugin_automd.default)(options.automd)
	];
};
var src_default = plugin;

//#endregion
exports.BaseEnv = require_helpers_reflect.BaseEnv;
exports.BaseSecrets = require_helpers_reflect.BaseSecrets;
exports.EnvBuiltin = require_components_env.EnvBuiltin;
exports.EnvDocsFile = require_components_docs.EnvDocsFile;
exports.EnvTypeDefinition = require_components_env.EnvTypeDefinition;
exports.__ΩCreateEnvReflectionOptions = require_helpers_reflect.__ΩCreateEnvReflectionOptions;
exports.__ΩEnvInterface = require_types_runtime.__ΩEnvInterface;
exports.__ΩEnvPluginContext = require_types_plugin.__ΩEnvPluginContext;
exports.__ΩEnvPluginOptions = require_types_plugin.__ΩEnvPluginOptions;
exports.__ΩEnvPluginResolvedConfig = require_types_plugin.__ΩEnvPluginResolvedConfig;
exports.__ΩEnvPluginUserConfig = require_types_plugin.__ΩEnvPluginUserConfig;
exports.__ΩEnvType = require_types_plugin.__ΩEnvType;
exports.__ΩSecretsInterface = require_types_runtime.__ΩSecretsInterface;
exports.createEnvReflection = require_helpers_reflect.createEnvReflection;
exports.createReflection = require_helpers_create_reflection_resource.createReflection;
exports.createSecretsReflection = require_helpers_reflect.createSecretsReflection;
exports.createTemplateReflection = require_helpers_template_helpers.createTemplateReflection;
exports.default = src_default;
exports.envBabelPlugin = require_babel_plugin.envBabelPlugin;
exports.formatEnvField = require_helpers_source_file_env.formatEnvField;
exports.getEnvDefaultTypeDefinition = require_helpers_persistence.getEnvDefaultTypeDefinition;
exports.getEnvReflectionsPath = require_helpers_persistence.getEnvReflectionsPath;
exports.getEnvTypeReflectionsPath = require_helpers_persistence.getEnvTypeReflectionsPath;
exports.getSecretsDefaultTypeDefinition = require_helpers_persistence.getSecretsDefaultTypeDefinition;
exports.loadEnv = require_helpers_load.loadEnv;
exports.loadEnvFromContext = require_helpers_load.loadEnvFromContext;
exports.mergeEnvReflections = require_helpers_reflect.mergeEnvReflections;
exports.mergeSecretsReflections = require_helpers_reflect.mergeSecretsReflections;
exports.plugin = plugin;
exports.readEnvReflection = require_helpers_persistence.readEnvReflection;
exports.readEnvTypeReflection = require_helpers_persistence.readEnvTypeReflection;
exports.readSecretsReflection = require_helpers_persistence.readSecretsReflection;
exports.reflectEnv = require_helpers_reflect.reflectEnv;
exports.reflectSecrets = require_helpers_reflect.reflectSecrets;
exports.removeEnvPrefix = require_helpers_source_file_env.removeEnvPrefix;
exports.resolveRuntimeTypeFile = require_helpers_persistence.resolveRuntimeTypeFile;
exports.writeEnvReflection = require_helpers_persistence.writeEnvReflection;
exports.writeEnvReflectionSync = require_helpers_persistence.writeEnvReflectionSync;
exports.writeEnvTypeReflection = require_helpers_persistence.writeEnvTypeReflection;