import Axios from 'axios';
import AsyncStorage from '@react-native-async-storage/async-storage';

// const {persistor, store} = configureStore();
const authAxios = Axios.create();

Axios.interceptors.request.use(
  async config => {
    const token = await AsyncStorage.getItem('token');
    if (token) {
      config.headers.Authorization = 'Bearer ' + token;
    }
    return config;
  },
  error => {
    return Promise.reject(error);
  },
);

export const get = async (url, config = null) => {
  console.log(config);
  return Axios.get(url, config)
    .then(response => {
      return response;
    })
    .catch(err => {
      return err;
    });
};

export const post = (url, data, config = null) => {
  return Axios.post(url, data, config)
    .then(response => {
      return response;
    })
    .catch(err => {
      return err;
    });
};

export const postAuth = (url, data, config = null) => {
  return authAxios.post(url, data, config)
    .then(response => {
      return response;
    })
    .catch(err => {
      return err;
    });
};

export const put = (url, data, config = null) => {
  return Axios.put(url, data, config)
    .then(response => {
      return response;
    })
    .catch(err => {
      return err;
    });
};
