'use client';

import { createContext, useContext } from 'react';
import zhCN from './zh-CN';
import enUS from './en-US';

export type Locale = 'zh-CN' | 'en-US';

const locales: Record<Locale, Record<string, string>> = {
  'zh-CN': zhCN,
  'en-US': enUS,
};

export function t(key: string, locale: Locale = 'zh-CN'): string {
  return locales[locale]?.[key] ?? locales['zh-CN']?.[key] ?? key;
}

export const LocaleContext = createContext<{
  locale: Locale;
  setLocale: (l: Locale) => void;
}>({
  locale: 'zh-CN',
  setLocale: () => {},
});

export function useLocale() {
  return useContext(LocaleContext);
}

export function useT() {
  const { locale } = useLocale();
  return (key: string) => t(key, locale);
}
