import { n as __ΩSecretsInterface, t as __ΩEnvInterface } from "./runtime-C5ukCv3P.mjs";
import { a as __ΩEnvType, r as __ΩEnvPluginResolvedConfig, t as __ΩEnvPluginContext } from "./plugin-eBjQJP6x.mjs";
import { n as slash, t as joinPaths } from "./join-paths-BMg51Ddi.mjs";
import { ReflectionClass, ReflectionKind, __ΩTypeClass, __ΩTypeObjectLiteral, deserializeType, merge, resolveClassType } from "@powerlines/deepkit/vendor/type";
import { titleCase } from "@stryke/string-format/title-case";
import { __ΩContext, __ΩUnresolvedContext } from "powerlines/types/context";
import { convertFromCapnp, convertToCapnp } from "@powerlines/deepkit/capnp";
import { getReflectionsPath } from "@powerlines/deepkit/resolve-reflections";
import { SerializedTypes } from "@powerlines/deepkit/schemas/reflection";
import * as capnp from "@stryke/capnp";
import { readFileBuffer, writeFileBuffer, writeFileBufferSync } from "@stryke/fs/buffer";
import { isEmptyObject } from "@stryke/type-checks/is-empty-object";
import { existsSync } from "node:fs";
import { reflectType } from "@powerlines/deepkit/reflect-type";

//#region ../../node_modules/.pnpm/@stryke+path@0.26.3/node_modules/@stryke/path/dist/is-parent-path.mjs
/**
* Check if a given path is a parent of another path.
*
* @example
* ```ts
* isParentPath("/home/user/project/src/index.ts", "/home/user/project/src");
* // returns true
* isParentPath("/home/user/project/src/index.ts", "/home/user/project");
* // returns true
* isParentPath("/home/user/project/src/index.ts", "/home/user/project/src/other");
* // returns false
* isParentPath("/home/user/project/src/index.ts", "/home/user/other");
* // returns false
* isParentPath("/home/user/project/src/index.ts", "/home/user/project/src/index.ts");
* // returns false
* ```
*
* @param childPath - The path to check if it is a child of the parent path.
* @param parentPath - The path to check if it is a parent of the child path.
* @returns `true` if `childPath` is a child of `parentPath`, otherwise `false`.
*/
function isParentPath(childPath, parentPath) {
	const normalizedChild = slash(childPath.replaceAll(/\\/g, "/").replace(/\/*$/, ""))?.toLowerCase();
	const normalizedParent = slash(parentPath.replaceAll(/\\/g, "/").replace(/\/*$/, ""))?.toLowerCase();
	return childPath !== parentPath && normalizedChild !== normalizedParent && normalizedChild.startsWith(`${normalizedParent}/`);
}

//#endregion
//#region src/helpers/reflect.ts
function __assignType(fn, args) {
	fn.__type = args;
	return fn;
}
function mergeEnvReflections(context, reflections) {
	return createEnvReflection(context, { type: merge(reflections.map(__assignType((reflection) => reflection.type, [
		"reflection",
		"",
		"P\"2!\"/\""
	]))) });
}
mergeEnvReflections.__type = [
	() => __ΩEnvPluginContext,
	"context",
	() => ReflectionClass,
	"reflections",
	() => ReflectionClass,
	"mergeEnvReflections",
	"Pn!2\"P\"7#F2$P\"7%/&"
];
function mergeSecretsReflections(context, reflections) {
	return createSecretsReflection(context, { type: merge(reflections.map(__assignType((reflection) => reflection.type, [
		"reflection",
		"",
		"P\"2!\"/\""
	]))) });
}
mergeSecretsReflections.__type = [
	() => __ΩEnvPluginContext,
	"context",
	() => ReflectionClass,
	"reflections",
	() => ReflectionClass,
	"mergeSecretsReflections",
	"Pn!2\"P\"7#F2$P\"7%/&"
];
const __ΩCreateEnvReflectionOptions = [
	() => __ΩTypeObjectLiteral,
	() => __ΩTypeClass,
	"type",
	() => ReflectionClass,
	"superReflection",
	"CreateEnvReflectionOptions",
	"PPn!n\"J4#8P\"7$4%8Mw&y"
];
var BaseEnv = class {
	PLATFORM = "neutral";
	MODE = "production";
	DEBUG = false;
	TEST = false;
	MINIMAL = false;
	NO_COLOR = false;
	FORCE_COLOR = false;
	FORCE_HYPERLINK = false;
	STACKTRACE = false;
	INCLUDE_ERROR_DATA = false;
	CI = false;
	static __type = [
		"APP_NAME",
		"APP_VERSION",
		"BUILD_ID",
		"BUILD_TIMESTAMP",
		"BUILD_CHECKSUM",
		"RELEASE_ID",
		"RELEASE_TAG",
		"ORGANIZATION",
		"node",
		"browser",
		"neutral",
		"PLATFORM",
		function() {
			return "neutral";
		},
		"development",
		"test",
		"production",
		"MODE",
		function() {
			return "production";
		},
		"ENVIRONMENT",
		"DEBUG",
		function() {
			return false;
		},
		"TEST",
		function() {
			return false;
		},
		"MINIMAL",
		function() {
			return false;
		},
		"NO_COLOR",
		function() {
			return false;
		},
		"FORCE_COLOR",
		function() {
			return false;
		},
		"FORCE_HYPERLINK",
		function() {
			return false;
		},
		"STACKTRACE",
		function() {
			return false;
		},
		"INCLUDE_ERROR_DATA",
		function() {
			return false;
		},
		"ERROR_URL",
		"DEFAULT_TIMEZONE",
		"DEFAULT_LOCALE",
		"CI",
		function() {
			return false;
		},
		() => __ΩEnvInterface,
		"BaseEnv",
		"&3!&3\"&3#&3$&3%&3&&3'&3(P.).*.+J3,>-P.../.0J31>2&33)34>5)36>7)38>9)3:>;P')J3<>=P')J3>>?)3@>A)3B>C&3D&3E&3F)3G>H5nIx\"wJ"
	];
};
var BaseSecrets = class {
	static __type = [
		"ENCRYPTION_KEY",
		() => __ΩSecretsInterface,
		"BaseSecrets",
		"&3!5n\"x\"w#"
	];
};
function createEnvReflection(context, options = {}) {
	const parent = options.superReflection ?? new ReflectionClass({
		kind: ReflectionKind.class,
		description: `The base environment configuration definition for the ${titleCase(context.config.name)} project.`,
		classType: BaseEnv,
		types: [],
		implements: [{
			kind: ReflectionKind.objectLiteral,
			typeName: "EnvInterface",
			description: `The environment configuration interface definition for the ${titleCase(context.config.name)} project.`,
			types: []
		}]
	});
	parent.name = "Env";
	const result = new ReflectionClass(options.type ?? {
		kind: ReflectionKind.objectLiteral,
		typeName: "Env",
		description: `A schema describing the list of available environment variables that can be used by the ${context.config.name ? `${titleCase(context.config.name)} application` : "application"}.`,
		types: []
	}, parent);
	result.name = "Env";
	return result;
}
createEnvReflection.__type = [
	() => __ΩEnvPluginContext,
	"context",
	() => __ΩCreateEnvReflectionOptions,
	"options",
	() => ({}),
	() => ReflectionClass,
	"createEnvReflection",
	"Pn!2\"n#2$>%P\"7&/'"
];
function createSecretsReflection(context, options = {}) {
	const parent = options.superReflection ?? new ReflectionClass({
		kind: ReflectionKind.class,
		description: `The base secrets configuration definition for the ${titleCase(context.config.name)} project.`,
		classType: BaseSecrets,
		types: [],
		implements: [{
			kind: ReflectionKind.objectLiteral,
			typeName: "SecretsInterface",
			description: `The secrets configuration interface definition for the ${titleCase(context.config.name)} project.`,
			types: []
		}]
	});
	parent.name = "Secrets";
	const result = new ReflectionClass(options.type ?? {
		kind: ReflectionKind.objectLiteral,
		typeName: "Secrets",
		description: `A schema describing the list of available environment secrets that can be used by the ${context.config.name ? `${titleCase(context.config.name)} application` : "application"}.`,
		types: []
	}, parent);
	result.name = "Secrets";
	return result;
}
createSecretsReflection.__type = [
	() => __ΩEnvPluginContext,
	"context",
	() => __ΩCreateEnvReflectionOptions,
	"options",
	() => ({}),
	() => ReflectionClass,
	"createSecretsReflection",
	"Pn!2\"n#2$>%P\"7&/'"
];
async function reflectEnv(context, file, name) {
	let config;
	if (file) config = resolveClassType(await reflectType(context, {
		file: !isParentPath(file, context.workspaceConfig.workspaceRoot) ? joinPaths(context.workspaceConfig.workspaceRoot, file) : file,
		name
	}));
	const defaultConfigType = await reflectType(context, await getEnvDefaultTypeDefinition(context));
	return mergeEnvReflections(context, [
		await readEnvTypeReflection(context, "env"),
		config,
		resolveClassType(defaultConfigType)
	].filter(Boolean));
}
reflectEnv.__type = [
	() => __ΩEnvPluginContext,
	"context",
	"file",
	"name",
	"reflectEnv",
	"Pn!2\"&2#8&2$8\"/%"
];
async function reflectSecrets(context, file, name) {
	let config;
	if (file) config = resolveClassType(await reflectType(context, {
		file: !isParentPath(file, context.workspaceConfig.workspaceRoot) ? joinPaths(context.workspaceConfig.workspaceRoot, file) : file,
		name
	}));
	const defaultSecretsType = await reflectType(context, await getSecretsDefaultTypeDefinition(context));
	return mergeSecretsReflections(context, [
		await readSecretsReflection(context),
		config,
		resolveClassType(defaultSecretsType)
	].filter(Boolean));
}
reflectSecrets.__type = [
	() => __ΩEnvPluginContext,
	"context",
	"file",
	"name",
	"reflectSecrets",
	"Pn!2\"&2#8&2$8\"/%"
];

//#endregion
//#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 = [
	() => __ΩUnresolvedContext,
	() => __Ω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 = [
	() => __ΩUnresolvedContext,
	() => __Ω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 = [
	() => __ΩUnresolvedContext,
	() => __Ω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 joinPaths(getReflectionsPath(context), "env", `${name}-types.bin`);
}
getEnvTypeReflectionsPath.__type = [
	() => __ΩContext,
	() => __ΩEnvPluginResolvedConfig,
	"context",
	() => __Ω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 (!existsSync(filePath)) {
		if (!context.env.types.env || isEmptyObject(context.env.types.env)) {
			const reflection$1 = createEnvReflection(context);
			const message = new capnp.Message();
			reflection$1.messageRoot = message.initRoot(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 readFileBuffer(filePath);
	const messageRoot = new capnp.Message(buffer, false).getRoot(SerializedTypes);
	const reflection = resolveClassType(deserializeType(convertFromCapnp(messageRoot.types)));
	context.env.types[name] = reflection;
	context.env.types[name].messageRoot = messageRoot;
	context.env.types[name].dataBuffer = buffer;
	return reflection;
}
readEnvTypeReflection.__type = [
	() => __ΩEnvPluginContext,
	"context",
	() => __ΩEnvType,
	"name",
	() => "env",
	() => 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 capnp.Message();
	convertToCapnp(serialized, message.initRoot(SerializedTypes)._initTypes(serialized.length));
	await writeFileBuffer(getEnvTypeReflectionsPath(context, name), message.toArrayBuffer());
}
writeEnvTypeReflection.__type = [
	() => __ΩEnvPluginContext,
	"context",
	() => ReflectionClass,
	"reflection",
	() => __Ω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 joinPaths(getReflectionsPath(context), "env", `${name}.bin`);
}
getEnvReflectionsPath.__type = [
	() => __ΩEnvPluginContext,
	"context",
	() => __Ω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 (!existsSync(filePath)) {
		if (!context.env.types.env) context.env.types.env = await readEnvTypeReflection(context, "env");
		if (!context.env.used.env || isEmptyObject(context.env.used.env)) {
			const reflection$1 = createEnvReflection(context, {
				type: {
					kind: 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 capnp.Message();
			reflection$1.messageRoot = message.initRoot(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 readFileBuffer(filePath);
	const messageRoot = new capnp.Message(buffer, false).getRoot(SerializedTypes);
	const reflection = resolveClassType(deserializeType(convertFromCapnp(messageRoot.types)));
	context.env.used.env = reflection;
	context.env.used.env.messageRoot = messageRoot;
	context.env.used.env.dataBuffer = buffer;
	return reflection;
}
readEnvReflection.__type = [
	() => __ΩEnvPluginContext,
	"context",
	() => 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 (!existsSync(filePath)) {
		if (!context.env.types.secrets) context.env.types.secrets = await readEnvTypeReflection(context, "secrets");
		if (!context.env.used.secrets || isEmptyObject(context.env.used.secrets)) {
			const reflection$1 = createEnvReflection(context, {
				type: {
					kind: 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 capnp.Message();
			reflection$1.messageRoot = message.initRoot(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 readFileBuffer(filePath);
	const messageRoot = new capnp.Message(buffer, false).getRoot(SerializedTypes);
	const reflection = resolveClassType(deserializeType(convertFromCapnp(messageRoot.types)));
	context.env.used.secrets = reflection;
	context.env.used.secrets.messageRoot = messageRoot;
	context.env.used.secrets.dataBuffer = buffer;
	return reflection;
}
readSecretsReflection.__type = [
	() => __ΩEnvPluginContext,
	"context",
	() => 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 capnp.Message();
	convertToCapnp(serialized, message.initRoot(SerializedTypes)._initTypes(serialized.length));
	await writeFileBuffer(getEnvReflectionsPath(context, name), message.toArrayBuffer());
}
writeEnvReflection.__type = [
	() => __ΩEnvPluginContext,
	"context",
	() => ReflectionClass,
	"reflection",
	() => __Ω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 capnp.Message();
	convertToCapnp(serialized, message.initRoot(SerializedTypes)._initTypes(serialized.length));
	writeFileBufferSync(getEnvReflectionsPath(context, name), message.toArrayBuffer());
}
writeEnvReflectionSync.__type = [
	() => __ΩEnvPluginContext,
	"context",
	() => ReflectionClass,
	"reflection",
	() => __ΩEnvType,
	"name",
	() => "env",
	"writeEnvReflectionSync",
	"Writes the environment reflection data to the file system.",
	"Pn!2\"P\"7#2$n%2&>'\"/(?)"
];

//#endregion
export { mergeEnvReflections as _, readEnvReflection as a, reflectSecrets as b, resolveRuntimeTypeFile as c, writeEnvTypeReflection as d, BaseEnv as f, createSecretsReflection as g, createEnvReflection as h, getSecretsDefaultTypeDefinition as i, writeEnvReflection as l, __ΩCreateEnvReflectionOptions as m, getEnvReflectionsPath as n, readEnvTypeReflection as o, BaseSecrets as p, getEnvTypeReflectionsPath as r, readSecretsReflection as s, getEnvDefaultTypeDefinition as t, writeEnvReflectionSync as u, mergeSecretsReflections as v, isParentPath as x, reflectEnv as y };
//# sourceMappingURL=persistence-BsBxs9wt.mjs.map