"use client";

/**
 * The interest tag on an opportunity card.
 *
 * A `<button>` when the tag filters the list, a plain `<span>` when it does
 * not — rather than one `<span>` carrying a conditional handler. That span was
 * keyboard-dead even when it *was* clickable and announced nothing either way;
 * switching the element keeps the non-actionable case out of the tab order
 * instead of putting a disabled control in it.
 *
 * `type="button"`: the card wraps this block in a `<Link>`, so the tag cannot
 * be an `<a>` — nested anchors are invalid HTML — and the `stopPropagation` is
 * what stops the tag filter from also triggering the card's own navigation.
 */
export default function OpportunityCardTag({
  tag,
  onTagClick,
}: {
  tag: string;
  onTagClick?: (tag: string) => void;
}) {
  const clamp = "line-clamp-1 overflow-hidden text-ellipsis";

  if (!onTagClick || !tag) {
    return <span className={clamp}>{tag}</span>;
  }

  return (
    <button
      type="button"
      className={`${clamp} cursor-pointer hover:text-primary-5 hover:underline`}
      onClick={(event) => {
        event.preventDefault();
        event.stopPropagation();
        onTagClick(tag);
      }}
    >
      {tag}
    </button>
  );
}
