import { renderHook, act } from '@testing-library/react';
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { useTranslation } from './useTranslation';

// Mock react-i18next
const mockT = vi.fn((key: string) => key);
const mockChangeLanguage = vi.fn();
const mockI18n = {
  changeLanguage: mockChangeLanguage,
  isInitialized: true,
};

vi.mock('react-i18next', () => ({
  useTranslation: () => ({
    i18n: mockI18n,
    t: mockT,
  }),
}));

// Mock UI store
const mockSetLanguage = vi.fn();
vi.mock('@/stores/ui', () => ({
  useUIStore: () => ({
    language: 'en',
    setLanguage: mockSetLanguage,
  }),
}));

describe('useTranslation', () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  it('should return translation hook', () => {
    const { result } = renderHook(() => useTranslation());

    expect(result.current).toBeDefined();
    expect(result.current).toHaveProperty('t');
    expect(result.current).toHaveProperty('language');
    expect(result.current).toHaveProperty('changeLanguage');
    expect(result.current).toHaveProperty('isReady');
  });

  it('should translate text', () => {
    const { result } = renderHook(() => useTranslation());

    result.current.t('test.key');

    expect(mockT).toHaveBeenCalledWith('test.key');
  });

  it('should change language', () => {
    const { result } = renderHook(() => useTranslation());

    act(() => {
      result.current.changeLanguage('fr');
    });

    expect(mockChangeLanguage).toHaveBeenCalledWith('fr');
    expect(mockSetLanguage).toHaveBeenCalledWith('fr');
  });

  it('should return current language', () => {
    const { result } = renderHook(() => useTranslation());

    expect(result.current.language).toBe('en');
  });

  it('should return isReady status', () => {
    const { result } = renderHook(() => useTranslation());

    expect(result.current.isReady).toBe(true);
  });
});
