import React, { useContext } from 'react';
import { ProductContext } from './ProductCard';
import noImage from '../assets/no-image.jpg';
import styles from '../styles/styles.module.css';

export interface Props extends React.HTMLAttributes<HTMLImageElement> {
  img?: string;
}

export const ProductImage = ({ img = '', className, ...restProps }: Props) => {
  const { product } = useContext(ProductContext);
  const imgToUse = img || product.img || noImage;

  return (
    <img
      className={`${styles.productImg} ${className}`}
      {...restProps}
      src={imgToUse}
      alt="Product"
    />
  );
};

export default ProductImage;
