import { useQuery } from '@tanstack/react-query';
import { useIsAuthenticated } from '@/hooks';
import type { PageParams } from '@/lib/api/types';
import { addressesApi } from '../apis';
import { addressKeys } from '../keys';
import { ADDRESSES_STALE_TIME } from '../constants';

/**
 * The customer's addresses (primary first). A deliberate staleTime keeps the book warm across
 * remounts; every mutation invalidates it, so it always reflects reality without over-fetching.
 */
export function useAddresses(params?: PageParams) {
  const isAuthenticated = useIsAuthenticated();
  return useQuery({
    queryKey: addressKeys.list(params),
    queryFn: () => addressesApi.list(params),
    enabled: isAuthenticated,
    staleTime: ADDRESSES_STALE_TIME,
  });
}
