"use client";

/**
 * An icon that performs an action.
 *
 * Several screens rendered these as `<Image onClick>` — including the
 * **delete** control on the event and opportunity tiles. A clickable image is
 * not reachable by keyboard, is not announced as something activatable, and the
 * `alt` text is read as a graphic's description rather than as a control's name.
 * For a destructive action that is the difference between "a keyboard user can
 * delete their event" and "they cannot find the control at all".
 *
 * `type="button"` because these sit inside forms and inside `<Link>`-wrapped
 * cards; the default `submit` would post the surrounding form.
 *
 * `label` becomes the accessible name. The icon it wraps should carry
 * `alt=""` — the name belongs to the button, and a described image inside a
 * named button is announced twice.
 */
export default function IconButton({
  label,
  onClick,
  className,
  children,
  stopPropagation = false,
  disabled = false,
}: {
  /** The accessible name, already translated. */
  label: string;
  disabled?: boolean;
  onClick: () => void;
  className?: string;
  children: React.ReactNode;
  /** Set when the button sits inside a clickable card or link. */
  stopPropagation?: boolean;
}) {
  return (
    <button
      type="button"
      disabled={disabled}
      aria-label={label}
      className={className}
      onClick={(event) => {
        if (stopPropagation) {
          event.preventDefault();
          event.stopPropagation();
        }
        onClick();
      }}
    >
      {children}
    </button>
  );
}
