import { shouldShowEmergencyContact } from "@/features/opportunities/registrationEmergencyContact";
import { type RegisteredUser } from "./learnServeRegisterListTypes";

/**
 * One of the four guardian columns.
 *
 * The visibility check runs per row as well as per column: the columns exist
 * because *some* loaded row is a minor-at-registration, so every adult's row
 * still has to render a dash rather than another volunteer's contact details.
 */
export default function RegistrationEmergencyCell({
  columnKey,
  rowData,
  selectedLanguage,
}: {
  columnKey: string;
  rowData: RegisteredUser;
  selectedLanguage: string;
}) {
  /* `?.` below rather than relying on the guard to narrow: the guard is a
     boolean predicate, not a type predicate, so it establishes the invariant
     at runtime but not for the compiler. The chaining is redundant at runtime
     and is what keeps the reads honest about `user` being optional — the
     learn & serve resource sends no nested `user` at all. */
  if (!shouldShowEmergencyContact(rowData)) {
    return <span className="text-gray-400">-</span>;
  }

  if (columnKey === "emergency_contact_name") {
    return <span>{rowData.user?.emergency_contact_name || "-"}</span>;
  }
  if (columnKey === "emergency_contact_phone") {
    return <bdi dir="ltr" className="inline-block whitespace-nowrap">{rowData.user?.emergency_contact_phone || "-"}</bdi>;
  }
  if (columnKey === "emergency_contact_civil_id") {
    return <span>{rowData.user?.emergency_contact_civil_id || "-"}</span>;
  }
  if (columnKey === "emergency_contact_relationship") {
    return (
      <span>
        {rowData.user?.emergency_contact_relationship_display
          ? selectedLanguage === "ar"
            ? rowData.user?.emergency_contact_relationship_display.value_ar
            : rowData.user?.emergency_contact_relationship_display.value_en
          : "-"}
      </span>
    );
  }
  return null;
}
