import type { IconType } from "react-icons";
import { contactWhatsAppHref } from "@/lib/whatsapp";
import { externalHref } from "@/features/events/eventLifecycle";
import {
  FaFacebookF,
  FaGlobe,
  FaInstagram,
  FaLinkedinIn,
  FaWhatsapp,
  FaXTwitter,
} from "react-icons/fa6";

import type { IndividualProfileData } from "./individualProfile";

const LINKS: Array<[keyof IndividualProfileData["social"], IconType, string]> =
  [
    ["twitter", FaXTwitter, "X"],
    ["linkedin", FaLinkedinIn, "LinkedIn"],
    ["instagram", FaInstagram, "Instagram"],
    ["whatsapp", FaWhatsapp, "WhatsApp"],
    ["facebook", FaFacebookF, "Facebook"],
    ["website", FaGlobe, "Website"],
  ];

/** Only the links the person filled in; the row disappears when there are none. */
export default function ProfileSocialRow({
  social,
}: {
  social: IndividualProfileData["social"];
}) {
  // Stored links may lack a scheme (or be a bare WhatsApp number): as a raw
  // href they would resolve as paths on this site.
  const hrefOf = (key: keyof IndividualProfileData["social"]) =>
    key === "whatsapp" ? contactWhatsAppHref(social[key]) : externalHref(social[key]);
  const present = LINKS.filter(([key]) => Boolean(hrefOf(key)));
  if (present.length === 0) return null;

  return (
    <section className="flex flex-wrap justify-center gap-3 rounded-3xl border border-[#29246D14] bg-white p-4">
      {present.map(([key, Icon, label]) => (
        <a
          key={key}
          href={hrefOf(key) as string}
          target="_blank"
          rel="noopener noreferrer"
          aria-label={label}
          className="flex h-11 w-11 items-center justify-center rounded-xl border border-[#29246D26] text-primary-5 hover:bg-[#29246D0A]"
        >
          <Icon size={18} aria-hidden />
        </a>
      ))}
    </section>
  );
}
