"use client";

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

import { resolveAdminPageTitle } from "@/lib/admin-page-title";

/** 随界面语言更新 `document.title`（弥补 Next 静态 metadata 无法按用户语言切换）。 */
export function AdminDocumentTitle() {
  const pathname = usePathname();
  const { t, i18n } = useTranslation();

  useEffect(() => {
    const spec = resolveAdminPageTitle(pathname);
    const appTitle = t("app.title", { ns: "common" });

    if (!spec) {
      document.title = appTitle;
      return;
    }

    const pageTitle = t(spec.key, { ns: spec.ns, ...(spec.params ?? {}) });
    document.title = `${pageTitle} · ${appTitle}`;
  }, [pathname, i18n.language, t]);

  return null;
}
