function memoize(fn, getCacheKey, options = {}) {
    const cache = new Map();
    const { maxEntries } = options;
    return {
        fn: async (...args)=>{
            const key = getCacheKey(...args);
            if (cache.has(key)) {
                const cachedPromise = cache.get(key);
                cache.delete(key);
                cache.set(key, cachedPromise);
                return cachedPromise;
            }
            const promise = fn(...args).catch((error)=>{
                cache.delete(key);
                throw error;
            });
            cache.set(key, promise);
            if (void 0 !== maxEntries && cache.size > maxEntries) {
                const lruKey = cache.keys().next().value;
                if (void 0 !== lruKey) cache.delete(lruKey);
            }
            return promise;
        },
        clearCache: ()=>{
            cache.clear();
        }
    };
}
export { memoize };
