import { defineRouting } from 'next-intl/routing';

export const locales = ['en', 'fa', 'nl'] as const;
export type AppLocale = (typeof locales)[number];

export const defaultLocale: AppLocale = 'en';

// Include future RTL locales here (e.g. 'ar') when added.
export const rtlLocales = ['fa'] as const satisfies readonly AppLocale[];

export function isRtlLocale(locale: string): boolean {
  return (rtlLocales as readonly string[]).includes(locale);
}

export const routing = defineRouting({
  locales,
  defaultLocale,
  localePrefix: 'always',
});

export function localeHtmlLang(locale: string): string {
  if (locale === 'fa') return 'fa-IR';
  if (locale === 'nl') return 'nl';
  return 'en';
}

export function isAppLocale(value: string): value is AppLocale {
  return locales.includes(value as AppLocale);
}

export function stripLocaleFromPathname(pathname: string): string {
  const segments = pathname.split('/').filter(Boolean);
  if (segments.length > 0 && isAppLocale(segments[0])) {
    const rest = segments.slice(1).join('/');
    return rest ? `/${rest}` : '/';
  }
  return pathname || '/';
}

export function getLocaleFromPathname(pathname: string): AppLocale {
  const segment = pathname.split('/').filter(Boolean)[0];
  return isAppLocale(segment) ? segment : defaultLocale;
}
