import { ui, type Lang, type TranslationKey } from './ui';

export const LANGUAGES: { code: Lang; label: string }[] = [
  { code: 'es', label: 'ES' },
  { code: 'en', label: 'EN' },
];

export const DEFAULT_LANG: Lang = 'es';

export function getLangFromUrl(url: URL): Lang {
  const [, lang] = url.pathname.split('/');
  return lang === 'en' ? 'en' : 'es';
}

export function useTranslations(lang: Lang) {
  return function t(key: TranslationKey): string {
    return ui[lang][key] ?? ui.es[key];
  };
}

export function pathForLang(path: string, targetLang: Lang): string {
  const cleanPath = path.replace(/^\/(en|es)/, '') || '/';
  if (targetLang === DEFAULT_LANG) return cleanPath;
  return `/${targetLang}${cleanPath === '/' ? '' : cleanPath}`;
}
