import { useQuery } from '@tanstack/react-query';
import { orderService } from '../api/order.service';

/**
 * Hook to fetch order items for a specific order.
 *
 * @param orderId - The ID of the order to fetch items for
 * @returns Query result with order items data, loading state, and error
 */
export function useOrderItems(orderId: number | null | undefined) {
  return useQuery({
    queryKey: ['orderItems', orderId],
    enabled: orderId != null && orderId > 0,
    queryFn: () => orderService.findOrderItems(orderId!),
    staleTime: 1000 * 60 * 5, // 5 minutes
    retry: 1,
    retryOnMount: false,
    refetchOnWindowFocus: false,
  });
}
