import enIntegrationDocs from "@/i18n/locales/en/integrationDocs.json";
import neIntegrationDocs from "@/i18n/locales/ne/integrationDocs.json";
import zhIntegrationDocs from "@/i18n/locales/zh/integrationDocs.json";

const ADMIN_DEFAULT_LANGUAGE = "zh";

const NAV_RESOURCES = {
  zh: zhIntegrationDocs,
  en: enIntegrationDocs,
  ne: neIntegrationDocs,
} as const;

/** SSR / i18n 未就绪时同步解析 nav 文案，避免侧栏 hydration 显示 nav.xxx */
export function resolveDocsNavLabel(
  key: string,
  language: string = ADMIN_DEFAULT_LANGUAGE,
): string {
  const base = language.split("-")[0]?.toLowerCase();
  const resource =
    base === "ne" ? NAV_RESOURCES.ne : base === "en" ? NAV_RESOURCES.en : NAV_RESOURCES.zh;

  let node: unknown = resource;
  for (const part of key.split(".")) {
    if (node === null || typeof node !== "object") {
      return key;
    }
    node = (node as Record<string, unknown>)[part];
  }

  return typeof node === "string" ? node : key;
}
