import axios, {AxiosHeaders, AxiosRequestConfig, AxiosRequestHeaders, InternalAxiosRequestConfig} from 'axios';
import * as SecureStore from 'expo-secure-store';
import {useDispatch} from "react-redux";
import {logout} from "../features/auth/authSlice";
import {store} from "../redux/store";
export const baseUrl = 'https://assemblr.denco.store';
// export const baseUrl = 'http://192.168.1.101:5000';
const apiClient = axios.create({
    baseURL: baseUrl
});

apiClient.interceptors.request.use(async (config) => {
    const accessToken = await SecureStore.getItemAsync('accessToken');
    if (!config.headers) {
        config.headers = new AxiosHeaders();
    }
    if (accessToken) {
        config.headers.set('Authorization', `Bearer ${accessToken}`, true);
    }
    config.validateStatus = (status) => {
        if (status == 401) {
            SecureStore.deleteItemAsync('accessToken');
            store.dispatch(logout());
        }
        return true;
    };

    return config;
}, function (error) {
});


export default apiClient;
