const intialState = {
  data: [],
};

const clearState = {
  data: [],
};

export default (state = intialState, action) => {
  switch (action.type) {
    case "ADD_FAILED_DATA": {
      const newItem = action.payload;
      const exist = state.data.find(
        (item) =>
          item?.imageName === newItem?.imageName &&
          item?.activityId === newItem?.activityId
      );

      if (exist) {
        return { data: state.data };
      }

      console.log("[INFO] . [REDUX] . failedData . data - ", newItem);

      return { data: [...state.data, newItem] };
    }
    case "REMOVE_FAILED_DATA": {
      const { imageName, activityId } = action.payload;
      const newState = state.data.filter(
        (item) => item.imageName === imageName && item.activityId === activityId
      );
      return { data: newState };
    }
    case "CLEAR_FAILED_DATA":
      return clearState;
    default:
      return state;
  }
};
