import { describe, expect, it } from 'vitest'
import { createI18n, t } from './index'

const nested = createI18n({
  locales: ['he', 'en'] as const,
  defaultLocale: 'he',
  messages: {
    he: {
      cart: { title: 'עגלה', checkout: 'לתשלום' },
      'cart.legacy': 'מפתח שטוח',
    },
    en: { cart: { title: 'Cart' } },
  },
})

describe('nested keys', () => {
  it('resolves dot-path against nested tables', () => {
    expect(t(nested, 'he', 'cart.title')).toBe('עגלה')
    expect(t(nested, 'en', 'cart.title')).toBe('Cart')
    expect(t(nested, 'he', 'cart.checkout')).toBe('לתשלום')
  })

  it('prefers a literal flat key over a nested path', () => {
    expect(t(nested, 'he', 'cart.legacy')).toBe('מפתח שטוח')
  })

  it('falls back to default locale then literal key', () => {
    expect(t(nested, 'en', 'cart.checkout')).toBe('לתשלום')
    expect(t(nested, 'he', 'cart.missing')).toBe('cart.missing')
  })
})
