
import { transactionUrl } from "./IlinkConstants";
import { fetchDepositError, fetchDepositSuccess, fetchDepositPending, fetchDepositReset } from "../redux/actions/DepositAction";
import { store } from "../redux/store";
import axios from "axios";
import I18n from 'react-native-i18n'

/*
{
  "type" : "debit",
  "numCarte" : 12345454445,
  "montant":10000,
  "cvv": 325,
  "expiration_date": "03/2024",
  "id_wallet":1
}
*/
export const depositAction = (data) => {

     const auth = store.getState().authKeyReducer;
     const authKey = auth !== null ? `${auth.authKey.token_type} ${auth.authKey.access_token}` : '';

     return dispatch => {
          dispatch(fetchDepositPending());

          axios({
               url: transactionUrl,
               method: 'POST',
               headers: {
                    'Authorization': authKey,
                    'X-Localization': I18n.currentLocale()
               },
               data
          })
               .then(response => {
                    console.log('DEPOSIT ACTION', response);
                    dispatch(fetchDepositSuccess(response));
               })
               .catch(error => {
                    if (error.response)
                         dispatch(fetchDepositError(error.response));
                    else if (error.request)
                         dispatch(fetchDepositError(error.request))
                    else
                         dispatch(fetchDepositError(error.message))
               });
     }
}

export const depositActionReset = () => {
     return dispatch => {
          dispatch(fetchDepositReset());
     }
}