import type { Currency, CurrencyCode } from '../types';

export const currencies: Record<CurrencyCode, Currency> = {
  USD: { code: 'USD', symbol: '$', rate: 1 },
  EUR: { code: 'EUR', symbol: '€', rate: 0.92 },
  GBP: { code: 'GBP', symbol: '£', rate: 0.79 },
  CHF: { code: 'CHF', symbol: 'CHF', rate: 0.88 },
};

export function formatCurrency(amountUSD: number, currency: Currency | undefined): string {
  if (!currency) {
    // Fallback to USD if currency is undefined
    currency = currencies.USD;
  }
  
  // Convert USD amount to target currency
  const convertedAmount = amountUSD * currency.rate;
  
  return `${currency.symbol}${convertedAmount.toLocaleString(undefined, {
    minimumFractionDigits: 0,
    maximumFractionDigits: 0,
  })}`;
}

export function getCurrencyByCode(code: string): Currency {
  return currencies[code as CurrencyCode] || currencies.USD;
}

// Convert amount from USD to target currency
export function convertFromUSD(amountUSD: number, targetCurrency: Currency): number {
  return amountUSD * targetCurrency.rate;
}

// Convert amount to USD from another currency
export function convertToUSD(amount: number, fromCurrency: Currency): number {
  return amount / fromCurrency.rate;
}