'use client';

import { ReactNode, useEffect } from 'react';
import { I18nextProvider } from 'react-i18next';
import i18n from '@/lib/i18n/config';

interface I18nProviderProps {
  children: ReactNode;
}

/**
 * Provider component for i18next internationalization
 * Initializes i18n and provides translation context to all children
 */
export function I18nProvider({ children }: I18nProviderProps) {
  useEffect(() => {
    // Initialize i18n language from localStorage or browser
    const savedLanguage = localStorage.getItem('preferred-language');
    if (savedLanguage && i18n.language !== savedLanguage) {
      i18n.changeLanguage(savedLanguage);
    }
  }, []);

  return <I18nextProvider i18n={i18n}>{children}</I18nextProvider>;
}
