import {
  cdcGpsfMeetingRequestMessages,
  type CdcGpsfMeetingRequestUiLang,
} from "./cdc-gpsf-meeting-request-i18n";

export function cdcGpsfMeetingRequestKey(path: string) {
  return `cdc_ui.${path.replace(/([a-z0-9])([A-Z])/g, "$1_$2").toLowerCase()}`;
}

export function resolveCdcGpsfMeetingRequestLabels(
  language: CdcGpsfMeetingRequestUiLang,
  dictionary: Record<string, string>,
) {
  const defaults = cdcGpsfMeetingRequestMessages[language];
  function visit(value: unknown, path: string): unknown {
    if (typeof value === "string") {
      const override = dictionary[cdcGpsfMeetingRequestKey(path)];
      return typeof override === "string" && override.trim() ? override : value;
    }
    if (!value || typeof value !== "object") return value;
    return Object.fromEntries(Object.entries(value).map(([key, child]) => [
      key,
      visit(child, path ? `${path}.${key}` : key),
    ]));
  }
  return visit(defaults, "") as typeof defaults;
}
