/**
 * Memory Cache Adapter
 * Simple in-memory implementation
 */

import type { CacheAdapter, CacheConfig } from './interfaces';

export class MemoryCacheAdapter implements CacheAdapter {
  private cache = new Map<string, { value: any; expiry: number }>();
  private defaultTTL: number;

  constructor(config: CacheConfig = {}) {
    this.defaultTTL = config.defaultTTL || 3600;
  }

  async get<T>(key: string): Promise<T | null> {
    const item = this.cache.get(key);
    if (!item) return null;

    if (Date.now() > item.expiry) {
      this.cache.delete(key);
      return null;
    }

    return item.value as T;
  }

  async set<T>(key: string, value: T, ttl?: number): Promise<void> {
    const finalTTL = ttl || this.defaultTTL;
    this.cache.set(key, {
      value,
      expiry: Date.now() + (finalTTL * 1000)
    });
  }

  async del(key: string): Promise<void> {
    this.cache.delete(key);
  }

  async clear(): Promise<void> {
    this.cache.clear();
  }
}