"use client";

import { useMemo } from "react";
import { getLocalizedLanguageName } from "../lib/localization";

interface LanguageDisplayProps {
  languageCode: string | null | undefined;
  className?: string;
}

/**
 * Component to display a language name from its ISO 639-1 code
 * Uses Intl.DisplayNames API when available, falls back to the code
 */
export default function LanguageDisplay({
  languageCode,
  className,
}: LanguageDisplayProps) {
  // Compute directly - Intl.DisplayNames is synchronous
  const languageName = useMemo(
    () => (languageCode ? getLocalizedLanguageName(languageCode) : "Unknown"),
    [languageCode]
  );

  return <span className={className}>{languageName}</span>;
}
