import { useApp } from '../contexts/AppContext';
import { siteData } from '../data/siteData';
import type { BilingualText, BilingualArray } from '../types';

export function useI18n() {
  const { lang } = useApp();

  function t(key: string): string {
    return siteData.i18n[lang]?.[key] || key;
  }

  function bi(obj: BilingualText | BilingualArray | undefined): string {
    if (!obj) return '';
    if (typeof obj === 'object' && 'zh' in obj && 'en' in obj) {
      const val = obj[lang as keyof typeof obj];
      if (Array.isArray(val)) return val.join(', ');
      return val || '';
    }
    return '';
  }

  function biArray(obj: BilingualArray | undefined): string[] {
    if (!obj) return [];
    return obj[lang] || [];
  }

  return { t, bi, biArray, lang };
}
