import React, { useContext } from 'react';
import { ProductContext } from './ProductCard';
import { ProductImageProps } from '../interfaces';

import noImage from '../assets/no-image.jpg';
import styles from '../styles/styles.module.css';

export const ProductImage = ({ img, className, style }: ProductImageProps) => {

  const { product } = useContext(ProductContext);
  let imgToShow: string;

  if (img) {
    imgToShow = img;
  } else if (product.img) {
    imgToShow = product.img;
  } else {
    imgToShow = noImage;
  }

  return (
    <img
      className={`${styles.productImg} ${className}`}
      src={imgToShow}
      alt="Product Image"
      style={style}
    />
  )
}