import { Injectable } from '@angular/core';
import { ProductListResult } from '../../../core/products/models/product-domain.model';

@Injectable({ providedIn: 'root' })
export class SearchCacheService {
  private readonly cache = new Map<string, ProductListResult>();

  get(key: string): ProductListResult | null {
    return this.cache.get(key) ?? null;
  }

  set(key: string, value: ProductListResult): void {
    this.cache.set(key, value);
    if (this.cache.size > 30) {
      const oldest = this.cache.keys().next().value;
      if (oldest) {
        this.cache.delete(oldest);
      }
    }
  }

  clear(): void {
    this.cache.clear();
  }
}
