"use client";

import { useEffect } from "react";
import { usePathname } from "next/navigation";

import { recordAppView, recordTabBaseline } from "@/lib/history-back";

/**
 * Keeps the tab's history bookkeeping current, so the player's Back control can
 * tell whether the History API has anywhere to go.
 *
 * The reasoning — why that cannot simply be asked, and what is recorded instead —
 * lives in `lib/history-back`. This component is only the lifecycle hookup:
 *
 *   • the baseline is taken once per tab, on mount, and re-running it is a no-op
 *     because the recorder ignores a second call;
 *   • a view is counted per path, which also absorbs `reactStrictMode`'s
 *     double-invoked effects.
 *
 * It renders nothing and is mounted once in the root layout, so it is active on
 * every route and cannot itself affect navigation.
 */
export function HistoryTracker() {
  const pathname = usePathname();

  useEffect(() => {
    recordTabBaseline();
  }, []);

  useEffect(() => {
    recordAppView(pathname);
  }, [pathname]);

  return null;
}
