// import axios, { AxiosError, AxiosPromise, AxiosRequestConfig, AxiosResponse } from "axios"
// import { API_URL, reportAxiosError } from "../globals";
// import { authStore } from "../stores/AuthStore";

// export const useAuthorizedRequests = () => {
//     const sendAuthorizedRequestAsync = async (requestConfig: AxiosRequestConfig, errorMessage: string):  => {
//         try {
//             const response = fetch(requestConfig);
//             return response;
//         } catch (error) {
//             if (error.response.status == 401) {
//                 console.info("An authorized request failed, trying to refresh access token...")

//                 if (authStore.refreshToken) {
//                     const tokenData = new URLSearchParams();
//                     tokenData.append('grant_type', 'refresh_token');
//                     tokenData.append('refresh_token', authStore.refreshToken);
//                     tokenData.append('client_id', 'atlas.mobile');

//                     try {
//                         const { data: refreshResponseData } = await axios.post(API_URL + "/o/token/", tokenData, {
//                             headers: { 'Content-Type': "application/x-www-form-urlencoded" }
//                         });   

//                         await authStore.setRefreshTokenAsync(refreshResponseData.refresh_token);
//                         await authStore.setAccessTokenAsync(refreshResponseData.access_token);

//                         console.info('Successfully refreshed access token, re-attempting initial request...')

//                         try {
//                             const response = await axios(requestConfig);  
//                         } catch (error) {
//                             reportAxiosError("Something went wrong when re-attempting initial request", error);                
//                         } 
//                     } catch (error) {
//                         reportAxiosError("Error when trying to refresh access token", error);            
//                     }
//                 }

//                 // no refresh token, user will be directed back to login
//                 else {
//                     await authStore.setAccessTokenAsync(null);
//                     await authStore.setRefreshTokenAsync(null);
//                     return
//                 }
//             }
//             else {
//                 reportAxiosError(errorMessage, error);
//             }
//         }
//     }

//     return { sendAuthorizedRequestAsync }
// }