"use client";
import { useState, useEffect, useCallback } from "react";
import { listingService } from "@/services/marketplace/listing.service";
import type { Listing } from "@/types/marketplace";

export function useWishlist() {
  const [items, setItems] = useState<Listing[]>([]);
  const [loading, setLoading] = useState(true);

  useEffect(() => {
    listingService
      .getWishlist()
      .then(setItems)
      .catch(() => setItems([]))
      .finally(() => setLoading(false));
  }, []);

  const remove = useCallback(async (listingId: string) => {
    await listingService.toggleWishlist(listingId);
    setItems((prev) => prev.filter((i) => i.id !== listingId));
  }, []);

  return { items, loading, remove, count: items.length };
}
