import { localeCookie, localeCookieMaxAgeSeconds, localeStorageKey, type Locale } from './config';

export function persistLocale(locale: Locale): void {
  try {
    window.localStorage.setItem(localeStorageKey, locale);
  } catch {
    // The route and first-party cookie remain sufficient when storage is unavailable.
  }

  const secure = window.location.protocol === 'https:' ? '; Secure' : '';
  document.cookie =
    [
      `${localeCookie}=${encodeURIComponent(locale)}`,
      'Path=/',
      `Max-Age=${localeCookieMaxAgeSeconds}`,
      'SameSite=Lax',
    ].join('; ') + secure;
}
