/* like mutation */
import ACTION from '../actions/actiontsTypes';

const initialState = {
  goods: [],
  lazyLoadGoodsArray: [],
  settings: {
    gender: [],
    brand: [],
    volume: [],
    madeIn: [],
    typeGoods: [],
    sort: "random",
    order: [],
    rows: [],
    blackListIdsGoods: []
  },
  isFetching: false,
  error: null
};

export default function (state = initialState, action) {
  switch (action.type) {
    case ACTION.GOODS_BY_GROUP_REQUEST: {
      return {
        ...state,
        isFetching: action.isFetching,
        error: null
      }
    }
    case ACTION.GOODS_BY_GROUP_RESPONSE: {

        if( action.isRandom ) {
            action.data.map(elem => {
                state.lazyLoadGoodsArray.push(elem);
            });
        }else {
            state.lazyLoadGoodsArray = action.data;
        }
      return {
        ...state,
        goods: action.data,
        // lazyLoadGoodsArray: [],
        isFetching: false,
        error: null
      }
    }
    case ACTION.FILTER_APPEND_ACTION: {

      return {
        ...state,
        settings: action.filterData
      }
    }
    case ACTION.GOODS_BY_GROUP_ERROR: {
      return {
        ...state,
        error: action.error,
        isFetching: false
      }
    }
    default: {
      return state
    }
  }
}


// const initialState = {
//   test: [],
//   isFetching: false,
//   error: null,
// };
//
// export default function (state = initialState, action) {
//   switch (action.type) {
//     case ACTION.GOODS_REQUEST: {
//       return {
//         ...state,
//         isFetching: true,
//         error: null,
//       };
//     }
//     case ACTION.GOODS_RESPONSE: {
//       return {
//         ...state,
//         goods: action.goods,
//         isFetching: false,
//         error: null,
//       };
//     }
//     case ACTION.SINGLE_GOODS_RESPONSE: {
//       const oneGoods = action.goods;
//       const { goods } = state;
//       const goodsIndex = goods.findIndex(g => g._id === oneGoods._id);
//       if (goodsIndex === -1) {
//         goods.push(oneGoods);
//       } else {
//         goods[goodsIndex] = oneGoods;
//       }
//       return {
//         ...state,
//         goods,
//         isFetching: false,
//         error: null,
//       };
//     }
//     case ACTION.GOODS_ERROR: {
//       return {
//         ...state,
//         error: action.error,
//         isFetching: false,
//       };
//     }
//     default: {
//       return state;
//     }
//   }
// }


