import { WishlistRepository } from './wishlist.repository';

export class LocalWishlistRepository implements WishlistRepository {
  private readonly storageKey = 'dastyab_wishlist';

  getAll(): string[] {
    if (typeof window === 'undefined') return [];
    
    try {
      const stored = localStorage.getItem(this.storageKey);
      return stored ? JSON.parse(stored) : [];
    } catch {
      return [];
    }
  }

  add(productId: string): void {
    if (typeof window === 'undefined') return;
    
    const items = this.getAll();
    if (!items.includes(productId)) {
      items.push(productId);
      localStorage.setItem(this.storageKey, JSON.stringify(items));
    }
  }

  remove(productId: string): void {
    if (typeof window === 'undefined') return;
    
    const items = this.getAll().filter(id => id !== productId);
    localStorage.setItem(this.storageKey, JSON.stringify(items));
  }

  has(productId: string): boolean {
    return this.getAll().includes(productId);
  }
}
