import WarpError from "../errors/WarpError.js";
/**
 * A simple LRU (Least Recently Used) cache implementation.
 *
 * Uses Map's insertion order to track access recency. When the cache
 * exceeds maxSize, the oldest (least recently used) entry is evicted.
 */
class LRUCache {
    maxSize;
    _cache;
    /**
     * Creates an LRU cache with the specified maximum size.
     *
     * @throws {Error} If maxSize is not a positive integer
     */
    constructor(maxSize) {
        if (!Number.isInteger(maxSize) || maxSize < 1) {
            throw new WarpError('LRUCache maxSize must be a positive integer', 'E_INVALID_ARG');
        }
        this.maxSize = maxSize;
        this._cache = new Map();
    }
    /** Gets a value from the cache and marks it as recently used. */
    get(key) {
        if (!this._cache.has(key)) {
            return undefined;
        }
        // Delete-reinsert maintains insertion order in the underlying Map, which
        // serves as the LRU eviction order. This is O(1) amortized in V8's Map
        // implementation despite appearing wasteful (2x Map ops per get).
        const value = this._cache.get(key);
        this._cache.delete(key);
        this._cache.set(key, value);
        return value;
    }
    /**
     * Sets a value in the cache, evicting the oldest entry if at capacity.
     *
     * If the key already exists, it is updated and marked as recently used.
     */
    set(key, value) {
        // If key exists, delete it first so it moves to the end
        if (this._cache.has(key)) {
            this._cache.delete(key);
        }
        // Add the new entry
        this._cache.set(key, value);
        // Evict oldest entry if over capacity
        if (this._cache.size > this.maxSize) {
            const oldestKey = this._cache.keys().next().value;
            this._cache.delete(oldestKey);
        }
        return this;
    }
    /**
     * Checks if a key exists in the cache.
     *
     * Note: This does NOT update the access order (use get() for that).
     */
    has(key) {
        return this._cache.has(key);
    }
    /** Deletes an entry from the cache. */
    delete(key) {
        return this._cache.delete(key);
    }
    /** Clears all entries from the cache. */
    clear() {
        this._cache.clear();
    }
    /** Gets the current number of entries in the cache. */
    get size() {
        return this._cache.size;
    }
}
export default LRUCache;
