/**
 * Currency formatting utilities tests
 * Tests formatCurrency, formatCurrencyPrecise, formatCurrencyCompact, parseCurrency
 */

import { describe, it, expect } from 'vitest'
import { formatCurrency, formatCurrencyPrecise, formatCurrencyCompact, parseCurrency } from '../currency'

describe('formatCurrency', () => {
  it('formats standard amounts with 2 decimals', () => {
    expect(formatCurrency(1.375)).toBe('$1.38')
    expect(formatCurrency(1234.56)).toBe('$1,234.56')
  })

  it('handles small amounts', () => {
    expect(formatCurrency(0.001)).toBe('$0.00')
    expect(formatCurrency(0.005)).toBe('$0.01')
  })

  it('handles zero and negatives', () => {
    expect(formatCurrency(0)).toBe('$0.00')
    expect(formatCurrency(-100)).toBe('-$100.00')
  })

  it('respects custom minimum fraction digits', () => {
    expect(formatCurrency(1.5, { minimumFractionDigits: 4, maximumFractionDigits: 4 })).toBe('$1.5000')
  })
})

describe('formatCurrencyPrecise', () => {
  it('always formats with 6 decimal places', () => {
    expect(formatCurrencyPrecise(0.001375)).toBe('$0.001375')
    expect(formatCurrencyPrecise(1.5)).toBe('$1.500000')
  })
})

describe('formatCurrencyCompact', () => {
  it('formats thousands with K suffix', () => {
    expect(formatCurrencyCompact(1234)).toBe('$1.2K')
    expect(formatCurrencyCompact(5678)).toBe('$5.7K')
  })

  it('formats millions with M suffix', () => {
    expect(formatCurrencyCompact(1500000)).toBe('$1.5M')
  })

  it('keeps small amounts without suffix', () => {
    expect(formatCurrencyCompact(5)).toBe('$5')
    expect(formatCurrencyCompact(999)).toBe('$999')
  })

  it('handles boundary at 1000', () => {
    expect(formatCurrencyCompact(1000)).toBe('$1K')
  })
})

describe('parseCurrency', () => {
  it('parses formatted currency strings', () => {
    expect(parseCurrency('$1,234.56')).toBe(1234.56)
    expect(parseCurrency('$100')).toBe(100)
  })

  it('parses plain number strings', () => {
    expect(parseCurrency('1234.56')).toBe(1234.56)
  })

  it('returns null for invalid inputs', () => {
    expect(parseCurrency('invalid')).toBeNull()
    expect(parseCurrency('')).toBeNull()
    expect(parseCurrency('abc')).toBeNull()
  })

  it('handles edge cases', () => {
    expect(parseCurrency('$0')).toBe(0)
    expect(parseCurrency('$0.00')).toBe(0)
  })
})
