import { useEffect } from "react";
import { useLocation } from "wouter";
import { useLanguage } from "../contexts/LanguageContext";

function pageNameForPath(path: string, meta: Record<string, string>): string {
  if (path === "/" || path === "") return meta.home;
  if (path.startsWith("/opportunities")) return meta.opportunities;
  if (path.startsWith("/cart")) return meta.cart;
  if (path.startsWith("/about")) return meta.about;
  if (path.startsWith("/track")) return meta.track;
  if (path.startsWith("/donate")) return meta.donate;
  if (path.startsWith("/request")) return meta.request;
  if (path.startsWith("/waqf")) return meta.waqf;
  if (path.startsWith("/baraem")) return meta.baraem;
  if (path.startsWith("/admin")) return meta.admin;
  if (path.startsWith("/whatsapp-log")) return meta.whatsappLog;
  if (path.startsWith("/thank-you")) return meta.thankYou;
  if (path.startsWith("/login")) return meta.login;
  return "";
}

export function DocumentTitle() {
  const [location] = useLocation();
  const { t } = useLanguage();

  useEffect(() => {
    const meta = t.meta as Record<string, string>;
    const name = pageNameForPath(location, meta);
    document.title = name ? `${name} - ${meta.platform}` : meta.platform;
  }, [location, t]);

  return null;
}
