"use client";

import { createContext, type ReactNode, useContext, useMemo } from "react";
import { defaultLocale, type Locale } from "@/translations/config";
import { type Dictionary, getDictionary } from "@/translations/dictionaries";

type I18nContextValue = {
  locale: Locale;
  dictionary: Dictionary;
};

const I18nContext = createContext<I18nContextValue>({
  locale: defaultLocale,
  dictionary: getDictionary(defaultLocale),
});

type I18nProviderProps = {
  children: ReactNode;
  locale: Locale;
};

export function I18nProvider({ children, locale }: I18nProviderProps) {
  const value = useMemo(
    () => ({
      locale,
      dictionary: getDictionary(locale),
    }),
    [locale],
  );

  return <I18nContext.Provider value={value}>{children}</I18nContext.Provider>;
}

export function useI18n() {
  return useContext(I18nContext);
}
