# Avatar — accessible headless avatar
#
# Shows an image, falls back to initials or a generic icon placeholder.
# Ships zero CSS.
#
# Usage:
#   Avatar src: user.photoUrl, alt: user.name, fallback: "AC"
#   Avatar fallback: "JD"
#   Avatar

export Avatar = component
  @src:: string := ""
  @alt:: string := ""
  @fallback:: string := ""

  imgError := false

  _onError: -> imgError = true

  _initials ~=
    return @fallback if @fallback
    return '' unless @alt
    parts = @alt.trim().split(/\s+/)
    chars = parts.map (p) -> p[0]?.toUpperCase() or ''
    chars.slice(0, 2).join('')

  render
    span role: "img", aria-label: @alt or 'Avatar'
      $status: if @src and not imgError then 'image' else if _initials then 'fallback' else 'placeholder'
      if @src and not imgError
        img src: @src, alt: @alt, @error: @_onError
      else if _initials
        span $initials: true
          _initials
      else
        span $placeholder: true
          "?"
