"use strict";
/* Socket Lib - Built with rolldown */
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_runtime = require('../_virtual/_rolldown/runtime.js');
const require_primordials_map_set = require('../primordials/map-set.js');
const require_primordials_object = require('../primordials/object.js');
const require_objects_predicates = require('../objects/predicates.js');
const require_env_boolean = require('./boolean.js');
let node_process = require("node:process");
node_process = require_runtime.__toESM(node_process);

//#region src/env/rewire.ts
/**
* @file Environment variable rewiring utilities for testing. Uses
*   AsyncLocalStorage for context-isolated overrides that work with concurrent
*   tests. Features:
*
*   - Context-isolated overrides via withEnv() for advanced use cases
*   - Test-friendly setEnv/clearEnv/resetEnv that work in beforeEach/afterEach
*   - Compatible with vi.stubEnv() - reads from process.env as final fallback
*   - Thread-safe for concurrent test execution
*/
let _async_hooks;
/**
* Lazily load the async_hooks module to avoid Webpack errors.
*
* @private
*/
/*@__NO_SIDE_EFFECTS__*/
function getAsyncHooks() {
	if (_async_hooks === void 0) _async_hooks = /*@__PURE__*/ require("node:async_hooks");
	return _async_hooks;
}
const { AsyncLocalStorage } = /* @__PURE__ */ getAsyncHooks();
const isolatedOverridesStorage = new AsyncLocalStorage();
const sharedOverridesSymbol = Symbol.for("@socketsecurity/lib/env/rewire/test-overrides");
const _globalThis = globalThis;
if (/* @__PURE__ */ require_env_boolean.envAsBoolean(node_process.default.env["VITEST"]) && !_globalThis[sharedOverridesSymbol]) _globalThis[sharedOverridesSymbol] = new require_primordials_map_set.MapCtor();
const sharedOverrides = _globalThis[sharedOverridesSymbol];
/**
* Clear a specific environment variable override.
*
* @example
*   ;```typescript
*   import { setEnv, clearEnv } from '@socketsecurity/lib/env/rewire'
*
*   setEnv('CI', '1')
*   clearEnv('CI')
*   ```
*
* @param key - The environment variable name to clear.
*/
function clearEnv(key) {
	sharedOverrides?.delete(key);
}
/**
* Get an environment variable value, checking overrides first.
*
* Resolution order: 1. Isolated overrides (temporary - set via
* withEnv/withEnvSync) 2. Shared overrides (persistent - set via setEnv in
* beforeEach) 3. process.env (including vi.stubEnv modifications)
*
* @example
*   ;```typescript
*   import { getEnvValue } from '@socketsecurity/lib/env/rewire'
*
*   const value = getEnvValue('NODE_ENV')
*   // e.g. 'production' or undefined
*   ```
*
* @internal Used by env getters to support test rewiring
*/
function getEnvValue(key) {
	const isolatedOverrides = isolatedOverridesStorage.getStore();
	if (isolatedOverrides?.has(key)) return isolatedOverrides.get(key);
	if (sharedOverrides?.has(key)) return sharedOverrides.get(key);
	return node_process.default.env[key];
}
/**
* Check if an environment variable has been overridden.
*
* @example
*   ;```typescript
*   import { setEnv, hasOverride } from '@socketsecurity/lib/env/rewire'
*
*   hasOverride('CI') // false
*   setEnv('CI', '1')
*   hasOverride('CI') // true
*   ```
*
* @param key - The environment variable name to check.
*
* @returns `true` if the variable has been overridden, `false` otherwise
*/
function hasOverride(key) {
	return !!(isolatedOverridesStorage.getStore()?.has(key) || sharedOverrides?.has(key));
}
/**
* Check if an environment variable exists (has a key), checking overrides
* first.
*
* Resolution order: 1. Isolated overrides (temporary - set via
* withEnv/withEnvSync) 2. Shared overrides (persistent - set via setEnv in
* beforeEach) 3. process.env (including vi.stubEnv modifications)
*
* @example
*   ;```typescript
*   import { isInEnv } from '@socketsecurity/lib/env/rewire'
*
*   isInEnv('PATH') // true (usually set)
*   isInEnv('MISSING') // false
*   ```
*
* @internal Used by env getters to check for key presence (not value truthiness)
*/
function isInEnv(key) {
	if (isolatedOverridesStorage.getStore()?.has(key)) return true;
	if (sharedOverrides?.has(key)) return true;
	return /* @__PURE__ */ require_objects_predicates.hasOwn(node_process.default.env, key);
}
/**
* Clear all environment variable overrides. Useful in afterEach hooks to ensure
* clean test state.
*
* @example
*   ;```typescript
*   import { resetEnv } from './rewire'
*
*   afterEach(() => {
*     resetEnv()
*   })
*   ```
*/
function resetEnv() {
	sharedOverrides?.clear();
}
/**
* Set an environment variable override for testing. This does not modify
* process.env, only affects env getters.
*
* Works in test hooks (beforeEach) without needing AsyncLocalStorage context.
* Vitest's module isolation ensures each test file has independent overrides.
*
* @example
*   ;```typescript
*   import { setEnv, resetEnv } from './rewire'
*   import { getCI } from './ci'
*
*   beforeEach(() => {
*     setEnv('CI', '1')
*   })
*
*   afterEach(() => {
*     resetEnv()
*   })
*
*   it('should detect CI environment', () => {
*     expect(getCI()).toBe(true)
*   })
*   ```
*/
function setEnv(key, value) {
	sharedOverrides?.set(key, value);
}
/**
* Run code with environment overrides in an isolated AsyncLocalStorage context.
* Creates true context isolation - overrides don't leak to concurrent code.
*
* Useful for tests that need temporary overrides without affecting other tests
* or for nested override scenarios.
*
* @example
*   ;```typescript
*   import { withEnv } from './rewire'
*   import { getCI } from './ci'
*
*   // Temporary override in isolated context
*   await withEnv({ CI: '1' }, async () => {
*     expect(getCI()).toBe(true)
*   })
*   expect(getCI()).toBe(false) // Override is gone
*   ```
*
* @example
*   ;```typescript
*   // Nested overrides work correctly
*   setEnv('CI', '1') // Shared override (persistent)
*
*   await withEnv({ CI: '0' }, async () => {
*     expect(getCI()).toBe(false) // Isolated override takes precedence
*   })
*
*   expect(getCI()).toBe(true) // Back to shared override
*   ```
*/
async function withEnv(overrides, fn) {
	const map = new require_primordials_map_set.MapCtor(require_primordials_object.ObjectEntries(overrides));
	return await isolatedOverridesStorage.run(map, fn);
}
/**
* Synchronous version of withEnv for non-async code.
*
* @example
*   ;```typescript
*   import { withEnvSync } from './rewire'
*   import { getCI } from './ci'
*
*   const result = withEnvSync({ CI: '1' }, () => {
*     return getCI()
*   })
*   expect(result).toBe(true)
*   ```
*/
function withEnvSync(overrides, fn) {
	const map = new require_primordials_map_set.MapCtor(require_primordials_object.ObjectEntries(overrides));
	return isolatedOverridesStorage.run(map, fn);
}

//#endregion
exports.clearEnv = clearEnv;
exports.getAsyncHooks = getAsyncHooks;
exports.getEnvValue = getEnvValue;
exports.hasOverride = hasOverride;
exports.isInEnv = isInEnv;
exports.resetEnv = resetEnv;
exports.setEnv = setEnv;
exports.withEnv = withEnv;
exports.withEnvSync = withEnvSync;