/**
 * BoostMedia AI Content Generator Admin - Translation Context Provider
 *
 * Provides locale, direction, and translation function to the React tree.
 *
 * @package BoostMedia_AI
 * @since 1.1.0
 * @license GPL-2.0-or-later
 */

import { createContext, useContext, useMemo, type ReactNode } from 'react'
import { t, tf, getCurrentLocale, getDirection, isRtl, getDateLocale } from './i18n'

interface TranslationContextType {
  locale: string
  direction: 'rtl' | 'ltr'
  isRtl: boolean
  t: (key: string) => string
  tf: (key: string, ...args: (string | number)[]) => string
  dateLocale: string
}

const defaultContext: TranslationContextType = {
  locale: 'he',
  direction: 'rtl',
  isRtl: true,
  t,
  tf,
  dateLocale: 'he-IL',
}

const TranslationContext = createContext<TranslationContextType>(defaultContext)

interface TranslationProviderProps {
  children: ReactNode
}

export function TranslationProvider({ children }: TranslationProviderProps) {
  const value = useMemo<TranslationContextType>(() => ({
    locale: getCurrentLocale(),
    direction: getDirection(),
    isRtl: isRtl(),
    t,
    tf,
    dateLocale: getDateLocale(),
  }), [])

  const fontClass = value.isRtl ? 'font-heebo' : 'font-sans'

  return (
    <TranslationContext.Provider value={value}>
      <div dir={value.direction} lang={value.locale} className={fontClass}>
        {children}
      </div>
    </TranslationContext.Provider>
  )
}

export function useTranslation() {
  return useContext(TranslationContext)
}
