import { cacheLogger } from "@/services/dataCache/logger"

import { cacheMap } from "./cacheMap"

export async function get<T>(key: string): Promise<T | undefined> {
  const cached = cacheMap.get(key)
  if (!cached) {
    return undefined
  }

  if (cached.expiresAt < Date.now()) {
    cacheLogger.debug(`Expired for key '${key}'`)
    cacheMap.delete(key)
    return undefined
  }
  if (cached.data === undefined) {
    cacheLogger.debug(`Data is undefined for key '${key}'`)
    cacheMap.delete(key)
    return undefined
  }

  return cached.data as T
}
