function cartReducer (state = {}, { type, good = {}, count = 1 }) {
  const types = {

    CART_ADD () {
      const { _id } = good;

      return {
        ...state,
        [_id]: { good, count: count + (state[_id]?.count || 0) }
      };
    },
    CART_MINUS () {
      const { _id } = good;

      return {
        ...state,
        [_id]: { good, count: (-count + (state[_id]?.count || 0)) < 1 ? 0 : -count + (state[_id]?.count || 0) }
      };
    },
    CART_REMOVE () {
      const { _id } = good;
      const newState = { ...state };
      const arrKeys = Object.keys(newState);
      for (const key of arrKeys) {
        if (_id === key) {
          delete newState[_id];
        }
      }

      return {
        ...newState
      };
    },
    CART_CHANGE () {
      const { _id } = good;
      return {
        ...state,
        [_id]: { good, count }
      };
    },
    CART_CLEAR () {
      return {

      };
    },
    AUTH_LOGOUT () {
      localStorage.clear();
      return {};
    }
  };

  if (type in types) { return types[type](); };

  return state;
}

export default cartReducer;
