import { useQuery } from '@tanstack/react-query';
import { apiClient } from './api-client';

interface Currency {
  code: string;
  symbol: string;
  name: string;
}

const FALLBACK_SYMBOLS: Record<string, string> = {
  ETB: 'Br',
  DJF: 'Fdj',
  USD: '$',
};

export function useCurrencies() {
  return useQuery<Currency[]>({
    queryKey: ['currencies'],
    queryFn: () => apiClient.get<Currency[]>('/currencies'),
    staleTime: 5 * 60 * 1000,
  });
}

export function useCurrencySymbol(code: string): string {
  const { data, isLoading, isError } = useCurrencies();
  if (isLoading || isError || !data) return FALLBACK_SYMBOLS[code] ?? code;
  return data.find(c => c.code === code)?.symbol ?? FALLBACK_SYMBOLS[code] ?? code;
}
