"use client";

import { useQuery, useConvexAuth } from "convex/react";
import { useMemo } from "react";
import { api } from "../../../../../convex/_generated/api";
import type { WishlistItem } from "./types";

export function useWishlist(): {
  items: WishlistItem[];
  isLoading: boolean;
  isEmpty: boolean;
} {
  const { isAuthenticated } = useConvexAuth();
  const data = useQuery(api.wishlists.list, isAuthenticated ? {} : "skip");

  return useMemo(() => {
    if (!isAuthenticated) {
      return { items: [], isLoading: false, isEmpty: true };
    }
    if (data === undefined) {
      return { items: [], isLoading: true, isEmpty: false };
    }
    const items = data as WishlistItem[];
    return {
      items,
      isLoading: false,
      isEmpty: items.length === 0,
    };
  }, [isAuthenticated, data]);
}
