import { z } from "@hono/zod-openapi";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import dotenv from "dotenv";
import { expand } from "dotenv-expand";
import { findUpSync } from "find-up";

//#region src/env.ts
const loadEnvironmentFiles = () => {
	const environmentFiles = [];
	const currentEnv = path.resolve(process.cwd(), ".env");
	if (fs.existsSync(currentEnv)) environmentFiles.push(currentEnv);
	const rootEnv = findUpSync(".env", { cwd: path.dirname(process.cwd()) });
	if (rootEnv) {
		if (rootEnv !== currentEnv) environmentFiles.push(rootEnv);
	}
	const userConfigPath = path.join(os.homedir(), ".inkeep", "config");
	if (fs.existsSync(userConfigPath)) dotenv.config({
		path: userConfigPath,
		override: true,
		quiet: true
	});
	if (environmentFiles.length > 0) {
		dotenv.config({
			path: environmentFiles,
			override: false,
			quiet: true
		});
		expand({ processEnv: process.env });
	}
};
loadEnvironmentFiles();
const envSchema = z.object({
	ENVIRONMENT: z.enum([
		"development",
		"production",
		"pentest",
		"test"
	]).optional(),
	DATABASE_URL: z.string().optional(),
	POSTGRES_POOL_SIZE: z.string().optional(),
	INKEEP_AGENTS_JWT_SIGNING_SECRET: z.string().min(32, "INKEEP_AGENTS_JWT_SIGNING_SECRET must be at least 32 characters").optional(),
	INKEEP_AGENTS_MANAGE_UI_URL: z.string().optional(),
	INKEEP_AGENTS_MANAGE_API_URL: z.string().optional(),
	BETTER_AUTH_SECRET: z.string().optional()
});
const parseEnv = () => {
	try {
		return envSchema.parse(process.env);
	} catch (error) {
		if (error instanceof z.ZodError) {
			const missingVars = error.issues.map((issue) => issue.path.join("."));
			throw new Error(`❌ Invalid environment variables: ${missingVars.join(", ")}\n${error.message}`);
		}
		throw error;
	}
};
const env = parseEnv();

//#endregion
export { loadEnvironmentFiles as n, env as t };
//# sourceMappingURL=env.js.map