import type { CacheBackend } from './index.js'

/**
 * Isolate-local in-memory backend for dev, test, and fallback use only.
 * Unbounded growth (no LRU/size cap in v1) and evaporates on restart — not a
 * production cache.
 */
export function createMemoryBackend(): CacheBackend {
  const store = new Map<string, { value: string; expiresAt?: number }>()

  return {
    async get(key) {
      const entry = store.get(key)
      if (!entry) return undefined
      if (entry.expiresAt !== undefined && entry.expiresAt <= Date.now()) {
        store.delete(key)
        return undefined
      }
      return entry.value
    },

    async set(key, value, ttlSeconds) {
      const expiresAt =
        ttlSeconds !== undefined ? Date.now() + ttlSeconds * 1000 : undefined
      store.set(key, { value, expiresAt })
    },

    async del(key) {
      store.delete(key)
    },
  }
}
