"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_debug_output = require('../debug/output.js');

//#region src/memo/weak.ts
/**
* @file `memoizeWeak` — memoizer keyed by an object reference via `WeakMap`.
*   Cache entries are eligible for GC as soon as the keying object becomes
*   unreachable, so there's no need for explicit eviction. First (and only)
*   argument must be an object.
*/
/**
* Memoize with WeakMap for object keys. Allows garbage collection when objects
* are no longer referenced. Only works when first argument is an object.
*
* @example
*   import { memoizeWeak } from '@socketsecurity/lib/memo/weak'
*
*   const processConfig = memoizeWeak((config: Config) => {
*     return expensiveTransform(config)
*   })
*
*   processConfig(config1) // Computed
*   processConfig(config1) // Cached
*   // When config1 is no longer referenced, cache entry is GC'd
*
* @param fn - Function to memoize.
*
* @returns Memoized version using WeakMap
*/
function memoizeWeak(fn) {
	const cache = new require_primordials_map_set.WeakMapCtor();
	return function memoized(key) {
		if (cache.has(key)) {
			require_debug_output.debugLog(`[memoizeWeak:${fn.name}] hit`);
			return cache.get(key);
		}
		require_debug_output.debugLog(`[memoizeWeak:${fn.name}] miss`);
		const result = fn(key);
		cache.set(key, result);
		return result;
	};
}

//#endregion
exports.memoizeWeak = memoizeWeak;