import {store} from "../redux/store";
import axios from "axios";
import {getBankUrl} from "./IlinkConstants";
import I18n from "react-native-i18n";
import {
    fetchGetBankListError,
    fetchGetBankListPending,
    fetchGetBankListReset,
    fetchGetBankListSucsess
} from "../redux/actions/BankAction";

export const getBankListAction = (idWallet) => {

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

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

        axios({
            url: `${getBankUrl}/${idWallet}`,
            method: 'GET',
            headers: {
                'Authorization': authKey,
                'X-Localization': I18n.currentLocale()
            }
        })
            .then(response => {
                console.log(response);
                dispatch(fetchGetBankListSucsess(response));
            })
            .catch(error => {
                if (error.response)
                    dispatch(fetchGetBankListError(error.response));
                else if (error.request)
                    dispatch(fetchGetBankListError(error.request))
                else
                    dispatch(fetchGetBankListError(error.message))
            });
    }
}

export const getBankListReset = () => {
    return dispatch => {
        dispatch(fetchGetBankListReset());
    }
}