import * as types from '../actions/types';

const initialState = {
  loading: false,
  searchBookLoading: false,
  bookRequestListLoading: false,
  editModalVisible: false,
  deleteModalVisible: false,
  bookHistoryListLoading: false
};

const reducer = (state = initialState, action: any) => {
  switch (action.type) {
    case types.SEARCH_BOOK_REQUEST:
      return {
        ...state,
        ...action.payload,
        loading: true,
        searchBookLoading: true
      };
    case types.SEARCH_BOOK_SUCCESS:
    case types.SEARCH_BOOK_FAIL:
    case types.CHNAGE_BOOK_STATUS:
      return {
        ...state,
        ...action.payload,
        loading: false,
        searchBookLoading: false
      };
    case types.BOOK_REQUEST_LIST_REQUEST:
      return {
        ...state,
        ...action.payload,
        loading: false,
        bookRequestListLoading: true
      };
    case types.BOOK_HISTORY_REQUEST:
      return {
        ...state,
        ...action.payload,
        loading: false,
        bookHistoryListLoading: true
      };
    case types.BOOK_REQUEST_LIST_SUCCESS:
    case types.BOOK_REQUEST_LIST_FAIL:
      return {
        ...state,
        ...action.payload,
        loading: false,
        bookRequestListLoading: false
      };
    case types.BOOK_HISTORY_SUCCESS:
    case types.BOOK_HISTORY_FAIL:
      return {
        ...state,
        ...action.payload,
        loading: false,
        bookHistoryListLoading: false
      };
    case types.BOOK_RESERVE_REQUEST:
    case types.BOOK_UNRESERVE_REQUEST:
    case types.BOOK_HISTORY_DETAILS_REQUEST:
    case types.ADD_BOOK_REQUEST_REQUEST:
    case types.BOOK_REQUEST_DETAILS_REQUEST:
    case types.EDIT_BOOK_REQUEST_REQUEST:
    case types.DELETE_BOOK_REQUEST_REQUEST:
      return { ...state, ...action.payload, loading: true };
    case types.BOOK_RESERVE_SUCCESS:
    case types.BOOK_RESERVE_FAIL:
    case types.BOOK_UNRESERVE_SUCCESS:
    case types.BOOK_UNRESERVE_FAIL:
    case types.BOOK_HISTORY_DETAILS_SUCCESS:
    case types.BOOK_HISTORY_DETAILS_FAIL:
    case types.ADD_BOOK_REQUEST_SUCCESS:
    case types.ADD_BOOK_REQUEST_FAIL:
    case types.BOOK_REQUEST_DETAILS_SUCCESS:
    case types.BOOK_REQUEST_DETAILS_FAIL:
    case types.EDIT_BOOK_REQUEST_SUCCESS:
    case types.EDIT_BOOK_REQUEST_FAIL:
    case types.DELETE_BOOK_REQUEST_SUCCESS:
    case types.DELETE_BOOK_REQUEST_FAIL:
    case types.SHOW_EDIT_MODAL:
    case types.HIDE_EDIT_MODAL:
    case types.SHOW_DELETE_MODAL:
    case types.HIDE_DELETE_MODAL:
      return { ...state, ...action.payload, loading: false };

    default:
      return state;
  }
};

export default reducer;
