"use client";
import { useMemo } from "react";
import { usePathname } from "next/navigation";
import { useQuery } from "@tanstack/react-query";
import { useAppLanguage } from "@/components/providers/app-language-provider";
import { dashboardOwner } from "@/features/cdc-gpsf/dashboard/dashboard-translations";
import {
  ministryPlenaryDefaults,
  ministryPlenaryPage,
  fetchDashboardDictionary,
  resolveReportLabels,
} from "./plenary-labels";

export function usePlenaryLabels(language?: string | null) {
  const { language: appLanguage } = useAppLanguage();
  const lang = (language ?? appLanguage) === "en" ? "en" : "kh";
  const path = usePathname() ?? "";
  const owner = dashboardOwner(path);
  const page = ministryPlenaryPage(path);
  const { data } = useQuery({
    queryKey: ["translations", owner, page, lang],
    queryFn: ({ signal }) =>
      owner
        ? fetchDashboardDictionary(owner, lang, signal, page)
        : Promise.resolve({}),
    enabled: owner !== null,
    staleTime: 30000,
    refetchOnMount: "always",
    refetchOnWindowFocus: "always",
    retry: 1,
  });
  return useMemo(
    () => resolveReportLabels(ministryPlenaryDefaults(lang), data ?? {}, ""),
    [lang, data],
  );
}
export function useMinistryPlenaryText(language?: string | null) {
  return usePlenaryLabels(language).core;
}
