/**
 * env() — Typed environment variable access for configuration files.
 *
 * Provides a statically-analyzable alternative to raw `process.env` access.
 * Tooling can detect which environment variables a config file depends on
 * by scanning for `env("VAR_NAME")` calls.
 *
 * Usage:
 * ```typescript
 * import { env, defineConfig } from "@sanity/ailf"
 *
 * export default defineConfig({
 *   source: {
 *     projectId: env("SANITY_PROJECT_ID", "3do82whm"),
 *     dataset: env("SANITY_DATASET", "next"),
 *     apiToken: env("SANITY_API_TOKEN"), // throws if missing
 *   },
 * })
 * ```
 *
 * YAML configs continue using the existing `${{ VAR | default }}` syntax.
 * The `env()` helper is for TypeScript configuration files only.
 *
 * @see docs/design-docs/architecture-overhaul/typescript-configuration.md
 */
/**
 * Read an environment variable, with an optional default.
 *
 * @param name - The environment variable name (e.g., `"SANITY_API_TOKEN"`)
 * @param defaultValue - Fallback value if the variable is not set.
 *   When omitted, a missing variable throws an error.
 * @returns The environment variable's value, or the default.
 * @throws {Error} When the variable is not set and no default is provided.
 */
export function env(name, defaultValue) {
    const value = process.env[name];
    if (value !== undefined && value !== "") {
        return value;
    }
    if (defaultValue !== undefined) {
        return defaultValue;
    }
    throw new Error(`Missing required environment variable: ${name}\n` +
        `Set it in your shell, .env file, or provide a default: env("${name}", "fallback")`);
}
