"use client";

import Image from "next/image";

/**
 * A small decorative icon shipped in `public/assets`.
 *
 * Why this exists rather than a bare `<img>`: with `images.unoptimized: true`
 * (`next.config.ts`) `next/image` issues the same single request to the same URL,
 * so nothing about the image pipeline changes — but it requires `width` and
 * `height`, and those attributes are what let the browser reserve the box before
 * the SVG arrives. A row of unsized `<img>` icons reflows its text as each one
 * loads. The Tailwind classes still decide the rendered size; these numbers only
 * set the intrinsic aspect box.
 *
 * `alt` defaults to `""`, which is the correct value for the icons this replaces:
 * each one sits immediately beside the text it decorates, so a screen reader
 * announcing "Date Icon" before the date is noise. A caller passing a real `alt`
 * is saying the icon carries information the text does not.
 *
 * Only used where the rendered size is written down (`w-5 h-5` and friends) and
 * can therefore be stated honestly. Icons sized by a parent, by `fill`, or by
 * their own intrinsic dimensions are left as they are rather than given an
 * invented number.
 */
export default function AssetIcon({
  src,
  className,
  alt = "",
  size = 20,
  width,
  height,
}: {
  src: string;
  className?: string;
  alt?: string;
  /**
   * Rendered px for a square icon, matching the Tailwind class — `w-4` is 16,
   * `w-5` is 20, `w-6` is 24.
   */
  size?: number;
  /** For a non-square icon; both must be given together. */
  width?: number;
  height?: number;
}) {
  return (
    <Image
      src={src}
      alt={alt}
      width={width ?? size}
      height={height ?? size}
      className={className}
      /* `alt=""` already hides a decorative image from the accessibility tree;
         `aria-hidden` is what several of the call sites set explicitly, kept so
         the behaviour is identical whichever one a reviewer looks at. */
      aria-hidden={alt === "" ? "true" : undefined}
      unoptimized
    />
  );
}
