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

//#region src/paths/rewire.ts
/**
* @file Path rewiring utilities for testing. Allows tests to override
*   os.tmpdir() and os.homedir() without directly modifying them. Features:
*
*   - Test-friendly setPath/clearPath/resetPaths that work in
*     beforeEach/afterEach
*   - Automatic cache invalidation for path-dependent modules
*   - Thread-safe for concurrent test execution
*/
const stateSymbol = Symbol.for("@socketsecurity/lib/paths/rewire/state");
const _globalThis = globalThis;
if (!_globalThis[stateSymbol]) _globalThis[stateSymbol] = {
	testOverrides: new require_primordials_map_set.MapCtor(),
	valueCache: new require_primordials_map_set.MapCtor(),
	cacheInvalidationCallbacks: []
};
const sharedState = _globalThis[stateSymbol];
const testOverrides = sharedState.testOverrides;
const valueCache = sharedState.valueCache;
const cacheInvalidationCallbacks = sharedState.cacheInvalidationCallbacks;
/**
* Clear a specific path override.
*/
function clearPath(key) {
	testOverrides.delete(key);
	invalidateCaches();
}
/**
* Get a path value, checking overrides first.
*
* Resolution order: 1. Test overrides (set via setPath in beforeEach) 2. Cached
* value (for performance) 3. Original function call (cached for subsequent
* calls)
*
* @internal Used by path getters to support test rewiring
*/
function getPathValue(key, originalFn) {
	if (testOverrides.has(key)) return testOverrides.get(key);
	if (valueCache.has(key)) return valueCache.get(key);
	const value = originalFn();
	valueCache.set(key, value);
	return value;
}
/**
* Check if a path has been overridden.
*/
function hasOverride(key) {
	return testOverrides.has(key);
}
/**
* Invalidate all cached paths. Called automatically when
* setPath/clearPath/resetPaths are used. Can also be called manually for
* advanced testing scenarios.
*
* @internal Primarily for internal use, but exported for advanced testing
*/
function invalidateCaches() {
	valueCache.clear();
	for (const callback of cacheInvalidationCallbacks) try {
		callback();
	} catch {}
}
/**
* Register a cache invalidation callback. Called by modules that need to clear
* their caches when paths change.
*
* @internal Used by paths.ts and fs.ts
*/
function registerCacheInvalidation(callback) {
	cacheInvalidationCallbacks.push(callback);
}
/**
* Clear all path overrides and reset caches. Useful in afterEach hooks to
* ensure clean test state.
*
* @example
*   ;```typescript
*   import { resetPaths } from '#paths/rewire'
*
*   afterEach(() => {
*     resetPaths()
*   })
*   ```
*/
function resetPaths() {
	testOverrides.clear();
	invalidateCaches();
}
/**
* Set a path override for testing. This triggers cache invalidation for
* path-dependent modules.
*
* @example
*   ;```typescript
*   import { setPath, resetPaths } from '#paths/rewire'
*   import { getOsTmpDir } from './'
*
*   beforeEach(() => {
*     setPath('tmpdir', '/custom/tmp')
*   })
*
*   afterEach(() => {
*     resetPaths()
*   })
*
*   it('should use custom temp directory', () => {
*     expect(getOsTmpDir()).toBe('/custom/tmp')
*   })
*   ```
*/
function setPath(key, value) {
	testOverrides.set(key, value);
	invalidateCaches();
}

//#endregion
exports.clearPath = clearPath;
exports.getPathValue = getPathValue;
exports.hasOverride = hasOverride;
exports.invalidateCaches = invalidateCaches;
exports.registerCacheInvalidation = registerCacheInvalidation;
exports.resetPaths = resetPaths;
exports.setPath = setPath;