---
/** An image, or a soft watercolor placeholder when there is none. */
import { hasImage, imageUrl, imageSrcSet, type ImageValue } from '../lib/image';

interface Props { image: ImageValue; width?: number; aspect?: number; sizes?: string; eager?: boolean; class?: string }
const { image, width = 800, aspect, sizes = '(max-width: 700px) 100vw, 400px', eager = false, class: cls } = Astro.props;
const src = imageUrl(image, width, aspect ? Math.round(width / aspect) : undefined);
const srcset = imageSrcSet(image, [400, 800, 1200], aspect);
---
{hasImage(image) && src ? (
  <img
    class={cls}
    src={src}
    srcset={srcset}
    sizes={srcset ? sizes : undefined}
    alt={image?.alt ?? ''}
    loading={eager ? 'eager' : 'lazy'}
    decoding="async"
    fetchpriority={eager ? 'high' : undefined}
  />
) : (
  <div class="media-placeholder" aria-hidden="true">
    <img src="/images/leaves-top.webp" alt="" loading="lazy" />
  </div>
)}
