/**
 * `useLocale()` — central hook that resolves the current page's
 * locale from `useParams<{lang}>` and exposes both forms:
 *
 *   - `locale`    — BCP-47 URL code (`"ru-ru"`, `"en-en"`, …) for
 *                   building outgoing links and reading from
 *                   `window.location`.
 *   - `language`  — short code (`"ru"`, `"en"`, …) for the i18n file
 *                   lookup, the API path segment, the
 *                   `Accept-Language` header, the dictionary
 *                   `title[lang]` key, and any `Intl` formatters.
 *
 * Mirrors Angular's `LocalizationService.Country` / `.Language`
 * split. Defaults to `"ru-ru"` / `"ru"` if the URL param is missing
 * (matches the SSR fallback in `[lang]/layout.tsx`).
 */

import { useParams } from "@modern-js/runtime/router";
import {
  DEFAULT_LANGUAGE,
  DEFAULT_LOCALE_CODE,
  localeToLanguage,
  normalizeLocaleParam,
  type Language,
  type LocaleCode,
} from "./resolver.js";

export interface ActiveLocale {
  locale: LocaleCode;
  language: Language;
}

export function useLocale(): ActiveLocale {
  const { lang } = useParams<{ lang: string }>();
  const locale = normalizeLocaleParam(lang) ?? DEFAULT_LOCALE_CODE;
  const language = lang ? localeToLanguage(locale) : DEFAULT_LANGUAGE;
  return { locale, language };
}
