import React, { createContext, useState, useContext } from 'react';

const ProductContext = createContext({
  currentProduct: null,
  setCurrentProduct: () => {}
});

export const useProduct = () => useContext(ProductContext);

export const withProduct = (Component) => {
  return (props) => {
    const productContext = useProduct();
    return <Component {...props} productContext={productContext} />;
  };
};

export const ProductContextProvider = ({ children }) => {
  const [currentProduct, setCurrentProduct] = useState(null);

  const setCurrentProductWithLog = (product) => {
    console.log('ProductContext: Setting current product to:', product);
    setCurrentProduct(product);
  };

  return (
    <ProductContext.Provider value={{ currentProduct, setCurrentProduct: setCurrentProductWithLog }}>
      {children}
    </ProductContext.Provider>
  );
};

