"use strict";
/* Socket Lib - Built with rolldown */
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_primordials_map_set = require('../primordials/map-set.js');
const require_primordials_object = require('../primordials/object.js');
const require_primordials_globals = require('../primordials/globals.js');
const require_env_case_insensitive = require('./case-insensitive.js');

//#region src/env/proxy.ts
/**
* @file `createEnvProxy` — wrap `process.env` (or any env-like record) in a
*   Proxy that adds case-insensitive lookups for known-Windows-sensitive keys
*   (PATH, APPDATA, etc.) and an `overrides` layer. Intended for cross-platform
*   test harnesses and child-process spawn env normalization.
*/
const caseInsensitiveKeys = new require_primordials_map_set.SetCtor([
	"APPDATA",
	"COMSPEC",
	"HOME",
	"LOCALAPPDATA",
	"PATH",
	"PATHEXT",
	"PROGRAMFILES",
	"SYSTEMROOT",
	"TEMP",
	"TMP",
	"USERPROFILE",
	"WINDIR"
]);
/**
* Create a case-insensitive environment variable Proxy for Windows
* compatibility. On Windows, environment variables are case-insensitive (PATH
* vs Path vs path). This Proxy provides consistent access regardless of case,
* with priority given to exact matches, then case-insensitive matches for known
* vars.
*
* **Use Cases:**
*
* - Cross-platform test environments needing consistent env var access
* - Windows compatibility when passing env to child processes
* - Merging environment overrides while preserving case-insensitive lookups
*
* **Performance Note:** Proxy operations have runtime overhead. Only use when
* Windows case-insensitive access is required. For most use cases, process.env
* directly is sufficient.
*
* @example
*   // Create a Proxy with overrides
*   const env = createEnvProxy(process.env, { NODE_ENV: 'test' })
*   console.log(env.PATH) // Works with any case: PATH, Path, path
*   console.log(env.NODE_ENV) // 'test'
*
* @example
*   // Pass to child process spawn
*   import { createEnvProxy } from '@socketsecurity/lib/env/proxy'
*   import { spawn } from '@socketsecurity/lib/spawn'
*
*   spawn('node', ['script.js'], {
*     env: createEnvProxy(process.env, { NODE_ENV: 'test' }),
*   })
*
* @param base - Base environment object (usually process.env)
* @param overrides - Optional overrides to merge.
*
* @returns Proxy that handles case-insensitive env var access
*/
function createEnvProxy(base, overrides) {
	function lookupEnvValue(prop) {
		if (overrides && prop in overrides) return overrides[prop];
		if (prop in base) return base[prop];
		/* c8 ignore start */
		const upperProp = prop.toUpperCase();
		if (caseInsensitiveKeys.has(upperProp)) {
			if (overrides) {
				const key = require_env_case_insensitive.findCaseInsensitiveEnvKey(overrides, upperProp);
				if (key !== void 0) return overrides[key];
			}
			const key = require_env_case_insensitive.findCaseInsensitiveEnvKey(base, upperProp);
			if (key !== void 0) return base[key];
		}
	}
	/* c8 ignore stop */
	return new require_primordials_globals.ProxyCtor({}, {
		get(_target, prop) {
			if (typeof prop !== "string") return;
			return lookupEnvValue(prop);
		},
		ownKeys(_target) {
			return [...new Set([...require_primordials_object.ObjectKeys(base), ...overrides ? require_primordials_object.ObjectKeys(overrides) : []])];
		},
		getOwnPropertyDescriptor(_target, prop) {
			if (typeof prop !== "string") return;
			const value = lookupEnvValue(prop);
			return value !== void 0 ? {
				enumerable: true,
				configurable: true,
				writable: true,
				value
			} : void 0;
		},
		has(_target, prop) {
			/* c8 ignore start */
			if (typeof prop !== "string") return false;
			if (overrides && prop in overrides) return true;
			if (prop in base) return true;
			const upperProp = prop.toUpperCase();
			if (caseInsensitiveKeys.has(upperProp)) {
				if (overrides && require_env_case_insensitive.findCaseInsensitiveEnvKey(overrides, upperProp) !== void 0) return true;
				if (require_env_case_insensitive.findCaseInsensitiveEnvKey(base, upperProp) !== void 0) return true;
			}
			return false;
			/* c8 ignore stop */
		},
		set(_target, prop, value) {
			if (typeof prop === "string" && overrides) {
				overrides[prop] = value;
				return true;
			}
			return false;
		}
	});
}

//#endregion
exports.createEnvProxy = createEnvProxy;