import { describe, it, expect } from 'vitest';
import { renderHook } from '@testing-library/react';
import { type ReactNode } from 'react';
import { AppProvider } from '../contexts/AppContext';
import { useI18n } from '../hooks/useI18n';

function wrapper({ children }: { children: ReactNode }) {
  return <AppProvider>{children}</AppProvider>;
}

describe('useI18n', () => {
  it('returns zh as default lang', () => {
    const { result } = renderHook(() => useI18n(), { wrapper });
    expect(result.current.lang).toBe('zh');
  });

  it('t() returns translated text for known key', () => {
    const { result } = renderHook(() => useI18n(), { wrapper });
    const text = result.current.t('hero.title');
    expect(text).toBeTruthy();
    expect(typeof text).toBe('string');
  });

  it('t() returns key itself for unknown key', () => {
    const { result } = renderHook(() => useI18n(), { wrapper });
    const text = result.current.t('nonexistent.key');
    expect(text).toBe('nonexistent.key');
  });

  it('bi() returns zh value for bilingual text', () => {
    const { result } = renderHook(() => useI18n(), { wrapper });
    const text = result.current.bi({ zh: '你好', en: 'Hello' });
    expect(text).toBe('你好');
  });

  it('bi() returns empty string for undefined', () => {
    const { result } = renderHook(() => useI18n(), { wrapper });
    expect(result.current.bi(undefined)).toBe('');
  });

  it('biArray() returns zh array for bilingual array', () => {
    const { result } = renderHook(() => useI18n(), { wrapper });
    const arr = result.current.biArray({ zh: ['甲', '乙'], en: ['A', 'B'] });
    expect(arr).toEqual(['甲', '乙']);
  });

  it('biArray() returns empty array for undefined', () => {
    const { result } = renderHook(() => useI18n(), { wrapper });
    expect(result.current.biArray(undefined)).toEqual([]);
  });
});
