import { combineReducers } from 'redux';
import { getSuccessActionType } from '../utils';
import { getErrorReducer, getStatusReducer } from './utils/generators';
import {
  CREATE_ORDER,
  FETCH_ORDER,
  UPDATE_ORDER_CODE,
  UPDATE_ORDER_TO_PAY_FROM_CREDIT
} from '../constants/order';

function data(state = {}, action) {
  switch (action.type) {
    case getSuccessActionType(FETCH_ORDER):
    case getSuccessActionType(CREATE_ORDER):
    case getSuccessActionType(UPDATE_ORDER_CODE):
    case getSuccessActionType(UPDATE_ORDER_TO_PAY_FROM_CREDIT):
      return action.payload;

    default:
      return state;
  }
}

const actionTypes = [FETCH_ORDER, CREATE_ORDER];

export default combineReducers({
  status: getStatusReducer(actionTypes),
  error: getErrorReducer(actionTypes),
  data
});
