import { ui, defaultLocale, type Locale } from './ui';

export function useTranslations(locale: Locale) {
  return function t(key: keyof typeof ui[typeof defaultLocale]): string {
    const localeUi = ui[locale] as Record<string, string>;
    const defaultUi = ui[defaultLocale] as Record<string, string>;
    return localeUi[key] ?? defaultUi[key] ?? key;
  };
}

/** Returns the path for the given locale, stripping/adding the locale prefix. */
export function getLocalePath(targetLocale: Locale, currentPath: string): string {
  const nonDefaultLocales: Locale[] = ['fr', 'it', 'en'];

  let basePath = currentPath;
  for (const locale of nonDefaultLocales) {
    if (currentPath.startsWith(`/${locale}/`)) {
      basePath = currentPath.slice(locale.length + 1);
      break;
    } else if (currentPath === `/${locale}`) {
      basePath = '/';
      break;
    }
  }

  if (targetLocale === defaultLocale) return basePath || '/';
  return basePath === '/' ? `/${targetLocale}/` : `/${targetLocale}${basePath}`;
}
