import axios from 'axios';
import store from 'store/index';
import queryString from 'query-string';

export const API_CSV = axios.create({
  baseURL: process.env.REACT_APP_STATION_API,
  headers: {
    'X-Requested-With':'station-locator-api'
  },
  responseType: 'text',
});

export const API_UNI_OIL = axios.create({
  baseURL: process.env.REACT_APP_STATION_API,
  headers: {
    'X-Requested_Width':'station-locator-api'
  },
  responseType: 'json',
});

export const fetchData = async (url) => {
  return await false;
};

export const API_GET_BRANCH = async (url, params) => {
  try {
    return await API_UNI_OIL.get(url, { params });
  } catch ({ response }) {
    if (response.status === 422) return response;
    store.dispatch({ type: 'UNCAUGHT_ERROR', payload: response });
  }
};

export const API_POST_BRANCH = async (url, params) => {
  try {
    return await API_UNI_OIL.post(url, params);
  } catch ({ response }) {
    if (response.status === 422) return response;
    store.dispatch({ type: 'UNCAUGHT_ERROR', payload: response });
  }
};
export const API_PUT_BRANCH = async (url, params) => {
  try {
    return await API_UNI_OIL.put(url, params);
  } catch ({ response }) {
    if (response.status === 422) return response;
    store.dispatch({ type: 'UNCAUGHT_ERROR', payload: response });
  }
};
export const API_DELETE_BRANCH = async (url, params) => {
  try {
    return await API_UNI_OIL.delete(url, params);
  } catch ({ response }) {
    if (response.status === 422) return response;
    store.dispatch({ type: 'UNCAUGHT_ERROR', payload: response });
  }
};

export const API_POST_FUEL = async (url, params) => {
  try {
    return await API_UNI_OIL.post(url, params);
  } catch ({ response }) {
    if (response.status === 422) return response;
    store.dispatch({ type: 'UNCAUGHT_ERROR', payload: response });
  }
};
export const API_PUT_FUEL = async (url, params) => {
  try {
    return await API_UNI_OIL.put(url, params);
  } catch ({ response }) {
    if (response.status === 422) return response;
    store.dispatch({ type: 'UNCAUGHT_ERROR', payload: response });
  }
};
export const API_GET_FUELS = async (url, params) => {
  console.log(await API_UNI_OIL.get(url, { params }));
  try {
    return await API_UNI_OIL.get(url, { params });
  } catch ({ response }) {
    if (response.status === 422) return response;
    store.dispatch({ type: 'UNCAUGHT_ERROR', payload: response });
  }
};
