"use client";

import { useEffect } from "react";
import { useLocale } from "next-intl";
import { config } from "@/config/config";
import { setLocaleCookie } from "@/lib/cookies";

/**
 * Keeps the active locale where the rest of the app looks for it.
 *
 * - NEXT_LOCALE cookie: proxy.ts reads it to resolve any path that arrives
 *   without a locale prefix. Nothing wrote it, so those paths always fell back
 *   to the default locale — most visibly the URLs Stripe redirects to after
 *   checkout, which dropped a Spanish subscriber onto the English page.
 * - localStorage lang: axios sends it as Accept-Language, and the backend
 *   stores it on the user at registration. Only the landing header used to
 *   write it, and the signup page has no header, so anyone arriving straight
 *   at /es/signup from an ad was registered as an English user.
 *
 * Renders nothing; mounted once per locale layout so it covers every page.
 */
export default function LocaleCookieSync() {
  const locale = useLocale();

  useEffect(() => {
    setLocaleCookie(locale);
    localStorage.setItem(config.languageSupport, locale);
  }, [locale]);

  return null;
}
