// @flow
import React, { createContext, useContext, useReducer } from "react"
import { cartTypes } from "constants/cart"

const storageKey = "dscCart"

const CartContext: Object = createContext()

const initialState = {
  addedItems: JSON.parse(localStorage.getItem(storageKey) || "[]"),
  showCartDialog: false,
  maxItemsInCart: false,
}

type CartItem = {
  id: string,
  name: string,
  summary: string,
  type: string,
}

const cartReducer = (
  state: Object,
  action: {
    type: string,
    payload: {
      ...CartItem,
      fee: string,
      removeIndex: number, // used to remove item
    },
  },
) => {
  switch (action.type) {
    case cartTypes.ADD_TO_CART:
      const newItems = state.addedItems.concat({
        id: action.payload.id,
        name: action.payload.name,
        summary: action.payload.summary,
        fee: action.payload.fee,
      })
      if (newItems.length > 12) {
        return {
          ...state,
          maxItemsInCart: true,
        }
      }
      localStorage.setItem(storageKey, JSON.stringify(newItems))
      return {
        addedItems: newItems,
        showCartDialog: true,
        maxItemsInCart: false,
      }
    case cartTypes.REMOVE_FROM_CART:
      const updatedItems = [
        ...state.addedItems.slice(0, action.payload.removeIndex),
        ...state.addedItems.slice(action.payload.removeIndex + 1),
      ]
      localStorage.setItem(storageKey, JSON.stringify(updatedItems))
      return {
        addedItems: updatedItems,
      }
    case cartTypes.HIDE_CART_DIALOG:
      return {
        ...state,
        showCartDialog: false,
      }
    default:
      return state
  }
}

/**
 * CartProvider contains global state used for the shopping cart.
 */

const CartProvider = ({ children }: any) => {
  const [state, dispatch] = useReducer(cartReducer, initialState)

  return (
    <CartContext.Provider value={[state, dispatch]}>
      {children}
    </CartContext.Provider>
  )
}

const useCartStore = () => {
  const context = useContext(CartContext)
  if (!context) {
    throw new Error("useCartStore must be used within a CartProvider")
  }
  return context
}

export { CartContext, cartReducer, CartProvider, useCartStore }

@@ -139,24 +139,53 @@
  = %22dscCart%22
+%0Aconst maxKey = %22dscMaxItems%22
 %0A%0Aconst Cart
@@ -353,21 +353,67 @@
 InCart: 
+JSON.parse(localStorage.getItem(maxKey) %7C%7C %22
 false
+%22)
 ,%0A%7D%0A%0Atyp
@@ -782,16 +782,25 @@
 dedItems
+%0A        
 .concat(
@@ -801,16 +801,18 @@
 oncat(%7B%0A
+  
         
@@ -842,16 +842,18 @@
         
+  
 name: ac
@@ -879,16 +879,18 @@
         
+ 
+ 
 summary:
@@ -918,24 +918,26 @@
 ry,%0A        
+  
 fee: action.
@@ -959,11 +959,35 @@
     
+  
 %7D)%0A
+        .slice(0, 12)%0A
     
@@ -1008,17 +1008,19 @@
 .length 
-%3E
+===
  12) %7B%0A 
@@ -1030,34 +1030,204 @@
     
-return %7B%0A          ...stat
+localStorage.setItem(storageKey, JSON.stringify(newItems))%0A        localStorage.setItem(maxKey, JSON.stringify(true))%0A        return %7B%0A          addedItems: newItems,%0A          showCartDialog: tru
 e,%0A 
@@ -1332,32 +1332,90 @@
 gify(newItems))%0A
+      localStorage.setItem(maxKey, JSON.stringify(false))%0A
       return %7B%0A 
@@ -1504,32 +1504,33 @@
  false,%0A      %7D%0A
+%0A
     case cartTyp
@@ -1785,24 +1785,82 @@
 atedItems))%0A
+      localStorage.setItem(maxKey, JSON.stringify(false))%0A
       return
@@ -1892,24 +1892,55 @@
 datedItems,%0A
+        maxItemsInCart: false,%0A
       %7D%0A    

put maxItemsInCart in localstorage