import { afterEach, describe, expect, it, vi } from 'vitest'
import { formatCurrency, formatDate, formatNumber } from './format'

describe('format', () => {
  it('formats currency with explicit currency code', () => {
    const out = formatCurrency('he', 1234.5, 'ILS')
    expect(out).toContain('₪')
  })
  it('formats numbers per locale', () => {
    expect(formatNumber('en', 1000)).toBe('1,000')
  })
  it('formats date', () => {
    const d = new Date(2024, 0, 1) // 2024-01-01
    const out = formatDate('en', d, { year: 'numeric', month: 'long', day: 'numeric' })
    expect(out).toContain('2024')
  })

  describe('memoization', () => {
    afterEach(() => vi.restoreAllMocks())

    it('reuses Intl.NumberFormat for identical args (no second construction)', () => {
      // Unique locale+options combo not touched by any other test — cache cold on first call
      const spy = vi.spyOn(Intl, 'NumberFormat')
      formatNumber('fr-CA', 1, { minimumFractionDigits: 7 })
      formatNumber('fr-CA', 1, { minimumFractionDigits: 7 })
      expect(spy).toHaveBeenCalledTimes(1)
    })

    it('reuses Intl.DateTimeFormat for identical args (no second construction)', () => {
      const spy = vi.spyOn(Intl, 'DateTimeFormat')
      formatDate('fr-CA', new Date(), { weekday: 'long', era: 'long' })
      formatDate('fr-CA', new Date(), { weekday: 'long', era: 'long' })
      expect(spy).toHaveBeenCalledTimes(1)
    })
  })
})
