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_reflect = require('./reflect.cjs');
let __powerlines_deepkit_vendor_type = require("@powerlines/deepkit/vendor/type");
let powerlines_types_context = require("powerlines/types/context");
let __powerlines_deepkit_capnp = require("@powerlines/deepkit/capnp");
let __powerlines_deepkit_resolve_reflections = require("@powerlines/deepkit/resolve-reflections");
let __powerlines_deepkit_schemas_reflection = require("@powerlines/deepkit/schemas/reflection");
let __stryke_capnp = require("@stryke/capnp");
__stryke_capnp = require_rolldown_runtime.__toESM(__stryke_capnp);
let __stryke_fs_buffer = require("@stryke/fs/buffer");
let __stryke_type_checks_is_empty_object = require("@stryke/type-checks/is-empty-object");
let node_fs = require("node:fs");

//#region src/helpers/persistence.ts
/**
* Resolves the runtime type definition file for the environment variables.
*
* @param context - The plugin context.
* @returns The runtime type definition file for the environment variables.
*/
async function resolveRuntimeTypeFile(context) {
	const resolved = await context.fs.resolve("@powerlines/plugin-env/types/runtime");
	if (!resolved) throw new Error(`Failed to resolve the runtime type definition file for the environment variables. Please ensure that the "@powerlines/plugin-env" package is installed.`);
	return resolved;
}
resolveRuntimeTypeFile.__type = [
	() => powerlines_types_context.__ΩUnresolvedContext,
	() => require_types_plugin.__ΩEnvPluginResolvedConfig,
	"context",
	"resolveRuntimeTypeFile",
	"Resolves the runtime type definition file for the environment variables.",
	"Pn\"o!\"2#&`/$?%"
];
/**
* Gets the default type definition for the environment variables.
*
* @param context - The plugin context.
* @returns The default type definition for the environment variables.
*/
async function getEnvDefaultTypeDefinition(context) {
	return {
		file: await resolveRuntimeTypeFile(context),
		name: "EnvInterface"
	};
}
getEnvDefaultTypeDefinition.__type = [
	() => powerlines_types_context.__ΩUnresolvedContext,
	() => require_types_plugin.__ΩEnvPluginResolvedConfig,
	"context",
	"getEnvDefaultTypeDefinition",
	"Gets the default type definition for the environment variables.",
	"Pn\"o!\"2#!`/$?%"
];
/** Gets the default type definition for the environment secrets.
*
* @param context - The plugin context.
* @returns The default type definition for the environment secrets.
*/
async function getSecretsDefaultTypeDefinition(context) {
	return {
		file: await resolveRuntimeTypeFile(context),
		name: "SecretsInterface"
	};
}
getSecretsDefaultTypeDefinition.__type = [
	() => powerlines_types_context.__ΩUnresolvedContext,
	() => require_types_plugin.__ΩEnvPluginResolvedConfig,
	"context",
	"getSecretsDefaultTypeDefinition",
	"Pn\"o!\"2#!`/$"
];
/**
* Gets the path to the environment type reflections.
*
* @param context - The plugin context.
* @param name - The name of the type reflections.
* @returns The path to the environment type reflections.
*/
function getEnvTypeReflectionsPath(context, name = "env") {
	return require_join_paths.joinPaths((0, __powerlines_deepkit_resolve_reflections.getReflectionsPath)(context), "env", `${name}-types.bin`);
}
getEnvTypeReflectionsPath.__type = [
	() => powerlines_types_context.__ΩContext,
	() => require_types_plugin.__ΩEnvPluginResolvedConfig,
	"context",
	() => require_types_plugin.__ΩEnvType,
	"name",
	() => "env",
	"getEnvTypeReflectionsPath",
	"Gets the path to the environment type reflections.",
	"Pn\"o!\"2#n$2%>&&/'?("
];
/**
* Reads the environment type reflection from the file system.
*
* @param context - The plugin context.
* @param name - The name of the type reflections.
* @returns The environment type reflection.
*/
async function readEnvTypeReflection(context, name = "env") {
	const filePath = getEnvTypeReflectionsPath(context, name);
	if (!(0, node_fs.existsSync)(filePath)) {
		if (!context.env.types.env || (0, __stryke_type_checks_is_empty_object.isEmptyObject)(context.env.types.env)) {
			const reflection$1 = require_helpers_reflect.createEnvReflection(context);
			const message = new __stryke_capnp.Message();
			reflection$1.messageRoot = message.initRoot(__powerlines_deepkit_schemas_reflection.SerializedTypes);
			reflection$1.dataBuffer = message.toArrayBuffer();
			context.env.types.env = reflection$1;
			await writeEnvTypeReflection(context, context.env.types.env, name);
		}
		return context.env.types.env;
	}
	const buffer = await (0, __stryke_fs_buffer.readFileBuffer)(filePath);
	const messageRoot = new __stryke_capnp.Message(buffer, false).getRoot(__powerlines_deepkit_schemas_reflection.SerializedTypes);
	const reflection = (0, __powerlines_deepkit_vendor_type.resolveClassType)((0, __powerlines_deepkit_vendor_type.deserializeType)((0, __powerlines_deepkit_capnp.convertFromCapnp)(messageRoot.types)));
	context.env.types[name] = reflection;
	context.env.types[name].messageRoot = messageRoot;
	context.env.types[name].dataBuffer = buffer;
	return reflection;
}
readEnvTypeReflection.__type = [
	() => require_types_plugin.__ΩEnvPluginContext,
	"context",
	() => require_types_plugin.__ΩEnvType,
	"name",
	() => "env",
	() => __powerlines_deepkit_vendor_type.ReflectionClass,
	"readEnvTypeReflection",
	"Reads the environment type reflection from the file system.",
	"Pn!2\"n#2$>%P\"7&`/'?("
];
/**
* Writes the environment type reflection to the file system.
*
* @param context - The plugin context.
* @param reflection - The environment type reflection to write.
* @param name - The name of the type reflections.
*/
async function writeEnvTypeReflection(context, reflection, name = "env") {
	const serialized = reflection.serializeType();
	const message = new __stryke_capnp.Message();
	(0, __powerlines_deepkit_capnp.convertToCapnp)(serialized, message.initRoot(__powerlines_deepkit_schemas_reflection.SerializedTypes)._initTypes(serialized.length));
	await (0, __stryke_fs_buffer.writeFileBuffer)(getEnvTypeReflectionsPath(context, name), message.toArrayBuffer());
}
writeEnvTypeReflection.__type = [
	() => require_types_plugin.__ΩEnvPluginContext,
	"context",
	() => __powerlines_deepkit_vendor_type.ReflectionClass,
	"reflection",
	() => require_types_plugin.__ΩEnvType,
	"name",
	() => "env",
	"writeEnvTypeReflection",
	"Writes the environment type reflection to the file system.",
	"Pn!2\"P\"7#2$n%2&>'\"/(?)"
];
function getEnvReflectionsPath(context, name) {
	return require_join_paths.joinPaths((0, __powerlines_deepkit_resolve_reflections.getReflectionsPath)(context), "env", `${name}.bin`);
}
getEnvReflectionsPath.__type = [
	() => require_types_plugin.__ΩEnvPluginContext,
	"context",
	() => require_types_plugin.__ΩEnvType,
	"name",
	"getEnvReflectionsPath",
	"Pn!2\"n#2$&/%"
];
/**
* Reads the environment reflection data from the file system.
*
* @param context - The plugin context.
* @returns The environment reflection data.
*/
async function readEnvReflection(context) {
	const filePath = getEnvReflectionsPath(context, "env");
	if (!(0, node_fs.existsSync)(filePath)) {
		if (!context.env.types.env) context.env.types.env = await readEnvTypeReflection(context, "env");
		if (!context.env.used.env || (0, __stryke_type_checks_is_empty_object.isEmptyObject)(context.env.used.env)) {
			const reflection$1 = require_helpers_reflect.createEnvReflection(context, {
				type: {
					kind: __powerlines_deepkit_vendor_type.ReflectionKind.objectLiteral,
					typeName: "Env",
					description: `An object containing the environment configuration parameters that are used (at least once) by the ${context.config.name ? `${context.config.name} application` : "application"}.`,
					types: []
				},
				superReflection: context.env.types.env
			});
			reflection$1.name = "Env";
			const message = new __stryke_capnp.Message();
			reflection$1.messageRoot = message.initRoot(__powerlines_deepkit_schemas_reflection.SerializedTypes);
			reflection$1.dataBuffer = message.toArrayBuffer();
			context.env.used.env = reflection$1;
			await writeEnvReflection(context, context.env.used.env, "env");
		}
		return context.env.used.env;
	}
	const buffer = await (0, __stryke_fs_buffer.readFileBuffer)(filePath);
	const messageRoot = new __stryke_capnp.Message(buffer, false).getRoot(__powerlines_deepkit_schemas_reflection.SerializedTypes);
	const reflection = (0, __powerlines_deepkit_vendor_type.resolveClassType)((0, __powerlines_deepkit_vendor_type.deserializeType)((0, __powerlines_deepkit_capnp.convertFromCapnp)(messageRoot.types)));
	context.env.used.env = reflection;
	context.env.used.env.messageRoot = messageRoot;
	context.env.used.env.dataBuffer = buffer;
	return reflection;
}
readEnvReflection.__type = [
	() => require_types_plugin.__ΩEnvPluginContext,
	"context",
	() => __powerlines_deepkit_vendor_type.ReflectionClass,
	"readEnvReflection",
	"Reads the environment reflection data from the file system.",
	"Pn!2\"P\"7#`/$?%"
];
/**
* Reads the secret environment reflection data from the file system.
*
* @param context - The plugin context.
* @returns The environment reflection data.
*/
async function readSecretsReflection(context) {
	const filePath = getEnvReflectionsPath(context, "secrets");
	if (!(0, node_fs.existsSync)(filePath)) {
		if (!context.env.types.secrets) context.env.types.secrets = await readEnvTypeReflection(context, "secrets");
		if (!context.env.used.secrets || (0, __stryke_type_checks_is_empty_object.isEmptyObject)(context.env.used.secrets)) {
			const reflection$1 = require_helpers_reflect.createEnvReflection(context, {
				type: {
					kind: __powerlines_deepkit_vendor_type.ReflectionKind.objectLiteral,
					typeName: "Secrets",
					description: `An object containing the secret configuration parameters that are used (at least once) by the ${context.config.name ? `${context.config.name} application` : "application"}.`,
					types: []
				},
				superReflection: context.env.types.secrets
			});
			reflection$1.name = "Secrets";
			const message = new __stryke_capnp.Message();
			reflection$1.messageRoot = message.initRoot(__powerlines_deepkit_schemas_reflection.SerializedTypes);
			reflection$1.dataBuffer = message.toArrayBuffer();
			context.env.used.secrets = reflection$1;
			await writeEnvReflection(context, context.env.used.secrets, "secrets");
		}
		return context.env.used.secrets;
	}
	const buffer = await (0, __stryke_fs_buffer.readFileBuffer)(filePath);
	const messageRoot = new __stryke_capnp.Message(buffer, false).getRoot(__powerlines_deepkit_schemas_reflection.SerializedTypes);
	const reflection = (0, __powerlines_deepkit_vendor_type.resolveClassType)((0, __powerlines_deepkit_vendor_type.deserializeType)((0, __powerlines_deepkit_capnp.convertFromCapnp)(messageRoot.types)));
	context.env.used.secrets = reflection;
	context.env.used.secrets.messageRoot = messageRoot;
	context.env.used.secrets.dataBuffer = buffer;
	return reflection;
}
readSecretsReflection.__type = [
	() => require_types_plugin.__ΩEnvPluginContext,
	"context",
	() => __powerlines_deepkit_vendor_type.ReflectionClass,
	"readSecretsReflection",
	"Reads the secret environment reflection data from the file system.",
	"Pn!2\"P\"7#`/$?%"
];
/**
* Writes the environment reflection data to the file system.
*
* @param context - The plugin context.
* @param reflection - The reflection data to write.
* @param name - The name of the reflection (either "env" or "secrets").
*/
async function writeEnvReflection(context, reflection, name = "env") {
	const serialized = reflection.serializeType();
	const message = new __stryke_capnp.Message();
	(0, __powerlines_deepkit_capnp.convertToCapnp)(serialized, message.initRoot(__powerlines_deepkit_schemas_reflection.SerializedTypes)._initTypes(serialized.length));
	await (0, __stryke_fs_buffer.writeFileBuffer)(getEnvReflectionsPath(context, name), message.toArrayBuffer());
}
writeEnvReflection.__type = [
	() => require_types_plugin.__ΩEnvPluginContext,
	"context",
	() => __powerlines_deepkit_vendor_type.ReflectionClass,
	"reflection",
	() => require_types_plugin.__ΩEnvType,
	"name",
	() => "env",
	"writeEnvReflection",
	"Writes the environment reflection data to the file system.",
	"Pn!2\"P\"7#2$n%2&>'\"/(?)"
];
/**
* Writes the environment reflection data to the file system.
*
* @param context - The plugin context.
* @param reflection - The reflection data to write.
* @param name - The name of the reflection (either "env" or "secrets").
*/
function writeEnvReflectionSync(context, reflection, name = "env") {
	const serialized = reflection.serializeType();
	const message = new __stryke_capnp.Message();
	(0, __powerlines_deepkit_capnp.convertToCapnp)(serialized, message.initRoot(__powerlines_deepkit_schemas_reflection.SerializedTypes)._initTypes(serialized.length));
	(0, __stryke_fs_buffer.writeFileBufferSync)(getEnvReflectionsPath(context, name), message.toArrayBuffer());
}
writeEnvReflectionSync.__type = [
	() => require_types_plugin.__ΩEnvPluginContext,
	"context",
	() => __powerlines_deepkit_vendor_type.ReflectionClass,
	"reflection",
	() => require_types_plugin.__ΩEnvType,
	"name",
	() => "env",
	"writeEnvReflectionSync",
	"Writes the environment reflection data to the file system.",
	"Pn!2\"P\"7#2$n%2&>'\"/(?)"
];

//#endregion
exports.getEnvDefaultTypeDefinition = getEnvDefaultTypeDefinition;
exports.getEnvReflectionsPath = getEnvReflectionsPath;
exports.getEnvTypeReflectionsPath = getEnvTypeReflectionsPath;
exports.getSecretsDefaultTypeDefinition = getSecretsDefaultTypeDefinition;
exports.readEnvReflection = readEnvReflection;
exports.readEnvTypeReflection = readEnvTypeReflection;
exports.readSecretsReflection = readSecretsReflection;
exports.resolveRuntimeTypeFile = resolveRuntimeTypeFile;
exports.writeEnvReflection = writeEnvReflection;
exports.writeEnvReflectionSync = writeEnvReflectionSync;
exports.writeEnvTypeReflection = writeEnvTypeReflection;