import {
  dashboardDefaults,
  dashboardTranslationKey,
  fetchDashboardDictionary,
  type DashboardModule,
} from "../../cdc-gpsf/dashboard/dashboard-translations";
import { normalizeDashboardLanguage } from "../../cdc-gpsf/dashboard/cdc-gpsf-dashboard-data";
import { plenaryText } from "../../cdc-gpsf/plenary/dashboard/plenary-i18n";
import { progressReportText } from "../../cdc-gpsf/progress-report/progress-report-i18n";

export function plenaryPage(owner: DashboardModule) {
  return ["pswg", "cdc", "cefp"].includes(owner)
    ? "dashboard_plenary"
    : "plenary_dashboard";
}
export function plenaryDefaults(language: string) {
  const lang = normalizeDashboardLanguage(language);
  return {
    ...dashboardDefaults(lang),
    ...plenaryText[lang],
    ...progressReportText[lang].draftDashboard.plenaryCharts,
    notPublished:
      lang === "kh"
        ? "CDC G-PSF មិនទាន់បានចេញផ្សាយផ្ទាំងគ្រប់គ្រងវេទិការនៅឡើយទេ។"
        : "CDC G-PSF has not published a plenary dashboard yet.",
  };
}
export type PlenaryLabels = Record<
  keyof ReturnType<typeof plenaryDefaults>,
  string
>;
export function resolvePlenaryLabels(
  language: string,
  dictionary: Record<string, string>,
): PlenaryLabels {
  return Object.fromEntries(
    Object.entries(plenaryDefaults(language)).map(([key, fallback]) => {
      const value = dictionary[dashboardTranslationKey(key)];
      return [
        key,
        typeof value === "string" && value.trim() ? value : fallback,
      ];
    }),
  ) as PlenaryLabels;
}
export function fetchPlenaryDictionary(
  owner: DashboardModule,
  language: string,
  signal?: AbortSignal,
) {
  return fetchDashboardDictionary(owner, language, signal, plenaryPage(owner));
}
