/**
 * Book Action
 */
import * as types from './types';

export const searchBookRequest = (params: any) => ({
  type: types.SEARCH_BOOK_REQUEST,
  payload: { params }
});

export const searchBookSuccess = (searchBookList: any) => ({
  type: types.SEARCH_BOOK_SUCCESS,
  payload: { searchBookList }
});

export const searchBookFail = (message: string) => ({
  type: types.SEARCH_BOOK_FAIL,
  payload: { message }
});

export const bookReserveRequest = (id: any, setCurrentTime?: any) => ({
  type: types.BOOK_RESERVE_REQUEST,
  payload: { id, setCurrentTime }
});

export const bookReserveSuccess = () => ({
  type: types.BOOK_RESERVE_SUCCESS,
  payload: {}
});

export const bookReserveFail = (message: string) => ({
  type: types.BOOK_RESERVE_FAIL,
  payload: { message }
});

export const bookUnreserveRequest = (
  id: any,
  setCurrentTime?: any,
  from?: any
) => ({
  type: types.BOOK_UNRESERVE_REQUEST,
  payload: { id, setCurrentTime, from }
});

export const bookUnreserveSuccess = () => ({
  type: types.BOOK_UNRESERVE_SUCCESS,
  payload: {}
});

export const bookUnreserveFail = (message: string) => ({
  type: types.BOOK_UNRESERVE_FAIL,
  payload: { message }
});

export const changeBookStatusRequest = (id: any, status: any): any => {
  return (dispatch: any, getState: any) => {
    let { book } = getState();
    let searchBookList = book.searchBookList || [];
    let indexOfObj = searchBookList.findIndex((obj: any) => obj.id === id);
    searchBookList[indexOfObj].status = status;
    searchBookList[indexOfObj].is_reserved = status === 2 ? true : false;
    dispatch({
      type: types.SEARCH_BOOK_SUCCESS,
      payload: {
        searchBookList
      }
    });
  };
};

export const changeBookHistoryStatusRequest = (id: any, status: any): any => {
  return (dispatch: any, getState: any) => {
    let { book } = getState();
    let bookHistoryList = book.bookHistoryList || [];
    let indexOfObj = bookHistoryList.findIndex((obj: any) => obj.id === id);
    bookHistoryList[indexOfObj].status = status;
    dispatch({
      type: types.BOOK_HISTORY_SUCCESS,
      payload: {
        bookHistoryList
      }
    });
  };
};

//Book History

export const bookHistoryRequest = (params: any) => ({
  type: types.BOOK_HISTORY_REQUEST,
  payload: { params }
});

export const bookHistorySuccess = (bookHistoryList: any) => ({
  type: types.BOOK_HISTORY_SUCCESS,
  payload: { bookHistoryList }
});

export const bookHistoryFail = (message: string) => ({
  type: types.SEARCH_BOOK_FAIL,
  payload: { message }
});

export const bookHistoryDetailsRequest = (
  params: any,
  setCurrentTime?: any,
  book_item_id?: any
) => ({
  type: types.BOOK_HISTORY_DETAILS_REQUEST,
  payload: { params, setCurrentTime, book_item_id }
});

export const bookHistoryDetailsSuccess = (bookHistoryDetails: any) => ({
  type: types.BOOK_HISTORY_DETAILS_SUCCESS,
  payload: { bookHistoryDetails }
});

export const bookHistoryDetailsFail = (message: string) => ({
  type: types.BOOK_HISTORY_DETAILS_FAIL,
  payload: { message }
});

//Book Request

export const bookRequestListRequest = (params: any): any => ({
  type: types.BOOK_REQUEST_LIST_REQUEST,
  payload: { params }
});

export const bookRequestListSuccess = (bookRequestList: any) => ({
  type: types.BOOK_REQUEST_LIST_SUCCESS,
  payload: { bookRequestList }
});

export const bookRequestListFail = (message: string) => ({
  type: types.BOOK_REQUEST_LIST_FAIL,
  payload: { message }
});

export const bookRequestDetailsRequest = (
  id: any,
  shouldNavigate?: any,
  editModal?: any,
  filterParams?: any
) => ({
  type: types.BOOK_REQUEST_DETAILS_REQUEST,
  payload: { id, shouldNavigate, editModal, filterParams }
});

export const bookRequestDetailsSuccess = (bookRequestDetails: any) => ({
  type: types.BOOK_REQUEST_DETAILS_SUCCESS,
  payload: { bookRequestDetails }
});

export const bookRequestDetailsFail = (message: string) => ({
  type: types.BOOK_REQUEST_DETAILS_FAIL,
  payload: { message }
});

export const addBookRequestRequest = (params: any, filterParams: any) => ({
  type: types.ADD_BOOK_REQUEST_REQUEST,
  payload: { params, filterParams }
});

export const addBookRequestSuccess = (): any => {
  return (dispatch: any, getState: any) => {
    dispatch({
      type: types.ADD_BOOK_REQUEST_SUCCESS,
      payload: {}
    });
  };
};

export const addBookRequestFail = (message: string) => ({
  type: types.ADD_BOOK_REQUEST_FAIL,
  payload: { message }
});

export const editBookRequestRequest = (params: any, filterParams?: any) => ({
  type: types.EDIT_BOOK_REQUEST_REQUEST,
  payload: { params, filterParams }
});

export const editBookRequestSuccess = () => ({
  type: types.EDIT_BOOK_REQUEST_SUCCESS,
  payload: {}
});

export const editBookRequestFail = (message: string) => ({
  type: types.EDIT_BOOK_REQUEST_FAIL,
  payload: { message }
});

export const deleteBookRequestRequest = (params: any, filterParams?: any) => ({
  type: types.DELETE_BOOK_REQUEST_REQUEST,
  payload: { params, filterParams }
});

export const deleteBookRequestSuccess = () => ({
  type: types.DELETE_BOOK_REQUEST_SUCCESS,
  payload: {}
});

export const deleteBookRequestFail = (message: string) => ({
  type: types.DELETE_BOOK_REQUEST_FAIL,
  payload: { message }
});
