import * as React from 'react';
import { Component ,useContext} from 'react';
import {appSetStateContext} from '../AppState'
import {Robotprops} from './robots'
export const withAddToCart =(ChildComponent:React.ComponentType<Robotprops>)=>{
    return (props)=>{
        const setState = useContext(appSetStateContext);
        const addToCart = (id,name) => {
          if (setState) {
            setState((state) => {
              return {
                ...state,
                shoppingCart: {
                  items: [...state.shoppingCart.items, { id, name }],
                },
              };
            });
          }
        };
        return <ChildComponent {...props} addToCart={addToCart}/>
    }
}
export const useAddToCart =() =>{
    const setState = useContext(appSetStateContext);
    const addToCart = (id,name) => {
      if (setState) {
        setState((state) => {
          return {
            ...state,
            shoppingCart: {
              items: [...state.shoppingCart.items, { id, name }],
            },
          };
        });
      }
    };
    return addToCart;
}