
export const formatCurrency = (amount, currency = 'USD') => {
  return new Intl.NumberFormat('en-US', {
    style: 'currency',
    currency: currency,
    minimumFractionDigits: 2,
    maximumFractionDigits: 2,
  }).format(amount || 0);
};

export const formatDate = (dateString) => {
  if (!dateString) return '';
  const date = new Date(dateString);
  return new Intl.DateTimeFormat('en-US', {
    year: 'numeric',
    month: 'short',
    day: 'numeric',
  }).format(date);
};

export const calculateTotalBalance = (accounts) => {
  if (!accounts || !Array.isArray(accounts)) return 0;
  return accounts.reduce((total, acc) => total + (Number(acc.opening_balance) || Number(acc.current_balance) || 0), 0);
};

export const groupTransactionsByMonth = (transactions) => {
  if (!transactions || !Array.isArray(transactions)) return {};
  return transactions.reduce((groups, tx) => {
    const month = new Date(tx.transaction_date).toLocaleString('default', { month: 'long', year: 'numeric' });
    if (!groups[month]) {
      groups[month] = [];
    }
    groups[month].push(tx);
    return groups;
  }, {});
};

/**
 * Standardizes account presentation, ensuring bank accounts display as 'Current asset'
 */
export const standardizeAccountDisplay = (account) => {
  if (!account) return account;
  
  const name = (account.account_name || '').toLowerCase();
  const isBank = name.includes('bank') || name.includes('cash') || name.includes('checking');
  
  if (isBank) {
    return {
      ...account,
      account_type: 'Current asset',
      display_type: 'Current asset'
    };
  }
  
  return {
    ...account,
    display_type: account.account_type
  };
};
