/**
 * Translation Context Provider
 * Provides locale and direction info to React tree
 * 
 * @package BoostBot
 * @since 1.7.0
 */
import { createContext, useContext, useMemo, ReactNode } from 'react';

interface TranslationContextType {
  locale: string;
  direction: 'ltr' | 'rtl';
  isRtl: boolean;
  language: string;
}

// Uses global type from types/index.ts

const defaultContext: TranslationContextType = {
  locale: 'en_US',
  direction: 'ltr',
  isRtl: false,
  language: 'en',
};

const TranslationContext = createContext<TranslationContextType>(defaultContext);

interface TranslationProviderProps {
  children: ReactNode;
}

/**
 * Translation Provider Component
 * Wraps the app and provides locale/direction context
 */
export function TranslationProvider({ children }: TranslationProviderProps) {
  const value = useMemo(() => {
    const adminData = window.boostbotAdmin;
    const locale = adminData?.locale || 'en_US';
    
    return {
      locale,
      direction: adminData?.direction || 'ltr',
      isRtl: adminData?.isRtl || false,
      language: locale.substring(0, 2),
    };
  }, []);
  
  return (
    <TranslationContext.Provider value={value}>
      <div 
        dir={value.direction} 
        lang={value.language}
        className={value.isRtl ? 'boostbot-rtl' : 'boostbot-ltr'}
      >
        {children}
      </div>
    </TranslationContext.Provider>
  );
}

/**
 * Hook to access translation context
 * @returns Translation context with locale, direction, isRtl, language
 */
export function useTranslation(): TranslationContextType {
  return useContext(TranslationContext);
}

/**
 * Hook to check if RTL
 * @returns true if current locale is RTL
 */
export function useIsRtl(): boolean {
  const { isRtl } = useTranslation();
  return isRtl;
}

/**
 * Hook to get direction
 * @returns 'ltr' or 'rtl'
 */
export function useDirection(): 'ltr' | 'rtl' {
  const { direction } = useTranslation();
  return direction;
}

/**
 * Hook to get current locale
 * @returns locale string (e.g., 'en_US', 'he_IL')
 */
export function useLocale(): string {
  const { locale } = useTranslation();
  return locale;
}

/**
 * Hook to get language code
 * @returns 2-letter language code (e.g., 'en', 'he')
 */
export function useLanguage(): string {
  const { language } = useTranslation();
  return language;
}

