import { useTranslation } from 'react-i18next';
import type en from '../locales/en.json';

// Derive the full key union from the English locale so translations are type-safe
type TranslationKeys = typeof en;
type DotPrefix<T extends string, K extends string> = `${T}.${K}`;

type LeafKeys<T, Prefix extends string = ''> = {
  [K in keyof T]: T[K] extends object
    ? LeafKeys<T[K], Prefix extends '' ? Extract<K, string> : DotPrefix<Prefix, Extract<K, string>>>
    : Prefix extends ''
    ? Extract<K, string>
    : DotPrefix<Prefix, Extract<K, string>>;
}[keyof T];

export type AppTranslationKey = LeafKeys<TranslationKeys>;

/**
 * Typed wrapper around react-i18next's useTranslation.
 * Keys are inferred from en.json so you get autocompletion + compile-time safety.
 *
 * @example
 *   const { t } = useAppTranslation();
 *   <Text>{t('auth.signIn')}</Text>
 */
export function useAppTranslation() {
  return useTranslation();
}
