import { PROCEDURE_LOCALES, type ProcedureLocale } from "@/lib/procedure-db"

export type ProcedureDoctorsSectionDefaults = {
  doctorsSectionEyebrow: string
  doctorsSectionTitle: string
}

export const PROCEDURE_DOCTORS_SECTION_STRING_KEYS = [
  "doctorsSectionEyebrow",
  "doctorsSectionTitle",
] as const satisfies ReadonlyArray<keyof ProcedureDoctorsSectionDefaults>

const DOCTORS_SECTION_BY_LOCALE: Record<ProcedureLocale, ProcedureDoctorsSectionDefaults> = {
  en: {
    doctorsSectionEyebrow: "Our Specialists",
    doctorsSectionTitle: "Meet Our Expert Doctors",
  },
  ar: {
    doctorsSectionEyebrow: "أخصاؤنا",
    doctorsSectionTitle: "تعرّف على أطبائنا الخبراء",
  },
  tr: {
    doctorsSectionEyebrow: "Uzmanlarımız",
    doctorsSectionTitle: "Uzman Doktorlarımızla Tanışın",
  },
}

function parseTrimmedString(value: unknown): string {
  return typeof value === "string" ? value.trim() : ""
}

export function getProcedureDoctorsSectionDefaults(locale: ProcedureLocale): ProcedureDoctorsSectionDefaults {
  return { ...DOCTORS_SECTION_BY_LOCALE[locale] }
}

/** Fills empty Doctors Section heading fields for one locale payload. */
export function applyProcedureDoctorsSectionDefaults(
  localeData: unknown,
  locale: ProcedureLocale
): Record<string, unknown> {
  const src =
    localeData && typeof localeData === "object" && !Array.isArray(localeData)
      ? (localeData as Record<string, unknown>)
      : {}
  const defaults = getProcedureDoctorsSectionDefaults(locale)
  const out: Record<string, unknown> = { ...src }

  for (const key of PROCEDURE_DOCTORS_SECTION_STRING_KEYS) {
    if (!parseTrimmedString(out[key])) {
      out[key] = defaults[key]
    }
  }

  return out
}

/** Applies Doctors Section defaults for every procedure locale. */
export function applyProcedureTranslationsDoctorsDefaults(
  translations: Record<string, unknown>
): Record<string, unknown> {
  const out: Record<string, unknown> = { ...translations }

  for (const locale of PROCEDURE_LOCALES) {
    const rawLocale = out[locale]
    if (!rawLocale || typeof rawLocale !== "object" || Array.isArray(rawLocale)) continue
    out[locale] = applyProcedureDoctorsSectionDefaults(rawLocale, locale)
  }

  return out
}
