import { Image, type ImageContentFit, type ImageProps } from 'expo-image'

type Props = ImageProps & {
  resizeMode?: string
}

const resizeModeMap: Record<string, ImageContentFit> = {
  contain: 'contain',
  cover: 'cover',
  stretch: 'fill',
  center: 'none',
}

export default function ImageComponent({
  resizeMode,
  contentFit,
  placeholderContentFit,
  ...props
}: Props) {
  const resolvedContentFit: ImageContentFit = resizeMode
    ? resizeModeMap[resizeMode] || 'cover'
    : contentFit || 'cover'
  // expo-image defaults placeholderContentFit to 'scale-down'; if the image
  // is 'cover' that mismatch flashes on load. Match the two unless a caller
  // overrides explicitly.
  return (
    <Image
      cachePolicy="memory-disk"
      priority="high"
      contentFit={resolvedContentFit}
      placeholderContentFit={placeholderContentFit ?? resolvedContentFit}
      {...props}
    />
  )
}
