import type { AppLocale } from "@/i18n";

export function parseLangParam(v: string | null): AppLocale | null {
  const x = v?.trim().toLowerCase();
  if (x === "ru" || x === "en") return x;
  return null;
}

/** Updates `?lang=` without reload; fires `locationchange` for listeners. */
export function setLangInUrl(lang: AppLocale): void {
  const url = new URL(window.location.href);
  url.searchParams.set("lang", lang);
  window.history.replaceState({}, "", url.toString());
  window.dispatchEvent(new Event("locationchange"));
}
