import { fireEvent, render, screen, waitFor } from '@testing-library/react'
import { describe, expect, it, vi } from 'vitest'

import { I18nProvider, useI18n } from './context'
import { normalizeLocale } from './languages'

function Probe() {
  const { locale, setLocale, t } = useI18n()
  return <button onClick={() => void setLocale(locale === 'tr' ? 'en' : 'tr')}>{t.common.save}</button>
}

describe('Turkish language pack', () => {
  it('accepts Turkish aliases', () => {
    for (const value of ['tr', 'TR-TR', 'tr_TR', 'Türkçe', 'turkce', 'Turkish']) expect(normalizeLocale(value)).toBe('tr')
  })
  it('starts in Turkish and persists a language change without losing sibling settings', async () => {
    const saveConfig = vi.fn().mockResolvedValue({ ok: true })
    const config = { display: { density: 'compact' }, model: { default: 'account/model' } }
    render(<I18nProvider configClient={{ getConfig: vi.fn().mockResolvedValue(config), saveConfig }}><Probe /></I18nProvider>)
    expect(screen.getByRole('button', { name: 'Kaydet' })).toBeTruthy()
    await waitFor(() => expect(document.documentElement.lang).toBe('tr'))
    fireEvent.click(screen.getByRole('button', { name: 'Kaydet' }))
    await waitFor(() => expect(screen.getByRole('button', { name: 'Save' })).toBeTruthy())
    expect(saveConfig).toHaveBeenCalledWith({ ...config, display: { ...config.display, language: 'en' } })
  })
})
