import { describe, expect, it, vi, afterEach } from 'vitest'
import { formatDate as formatLegacyDate, formatCurrencyILS } from '../src/lib/format'
import { toFormattingLocale } from '../src/lib/locale'
import { formatDate, formatTime } from '../src/lib/format-date'
import { formatCurrency, formatNumber } from '../src/lib/format-number'
import { relativeTime } from '../src/lib/relative-time'
import { formatWeekLabel } from '../../../packages/time/src/summary'

describe('hebrew locale dates', () => {
  afterEach(() => {
    vi.useRealTimers()
  })

  it('expands stored locales to Israeli regional formatting tags', () => {
    expect(toFormattingLocale('he')).toBe('he-IL')
    expect(toFormattingLocale('en')).toBe('en-IL')
    expect(toFormattingLocale(null)).toBe('en-IL')
  })

  it('formats English dates with en-IL rather than en-US ordering', () => {
    const date = new Date(2026, 4, 31, 14, 30)

    expect(formatLegacyDate(date, 'en')).toBe(
      new Intl.DateTimeFormat('en-IL').format(date),
    )
    expect(formatLegacyDate(date, 'en')).not.toBe(
      new Intl.DateTimeFormat('en-US').format(date),
    )
  })

  it('formats canonical date and time variants via Intl', () => {
    const date = new Date(2026, 4, 31, 14, 30)

    expect(formatDate(date, 'he-IL', 'short')).toBe('31.05.2026')
    expect(formatDate(date, 'he-IL', 'long')).toBe('31 במאי 2026')
    expect(formatDate(date, 'he-IL', 'month')).toBe('מאי 2026')
    expect(formatTime(date, 'he-IL')).toBe('14:30')
  })

  it('formats numbers and currency with Israeli regional Intl tags', () => {
    expect(formatNumber(1234567, 'he-IL')).toBe('1,234,567')
    expect(formatCurrency(1500, 'ILS', 'en-IL')).toBe(
      new Intl.NumberFormat('en-IL', {
        style: 'currency',
        currency: 'ILS',
        minimumFractionDigits: 0,
        maximumFractionDigits: 2,
      }).format(1500),
    )
    expect(formatCurrencyILS(1500, 'en')).toBe(
      new Intl.NumberFormat('en-IL', {
        style: 'currency',
        currency: 'ILS',
        minimumFractionDigits: 0,
      }).format(1500),
    )
  })

  it('returns localized relative time strings', () => {
    vi.useFakeTimers()
    vi.setSystemTime(new Date(2026, 4, 31, 12, 0, 0))

    expect(relativeTime(new Date(2026, 4, 31, 13, 0, 0), 'he-IL')).toContain('בעוד שעה')
    expect(relativeTime(new Date(2026, 4, 28, 12, 0, 0), 'he-IL')).toBe('לפני 3 ימים')
  })

  it('formats week labels with en-IL for English users', () => {
    expect(formatWeekLabel('2026-W23', 'en')).toBe('1 Jun – 7 Jun')
    expect(formatWeekLabel('2026-W23', 'he')).toBe('1 ביוני – 7 ביוני')
  })
})
