import _ from 'lodash';
window._ = _;

/**
 * We'll load the axios HTTP library which allows us to easily issue requests
 * to our Laravel back-end. This library automatically handles sending the
 * CSRF token as a header based on the value of the "XSRF" token cookie.
 */

import axios from 'axios';
window.axios = axios;

window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';


// Reload page when active profile timeouts. Important for Banks ans Admin profiles.
if (window.axios) {
    window.axios.interceptors.response.use(
        response => response,
        error => {
            // Check specifically for the 419 status code
            if (error.response && error.response.status === 419) {
                if (error.response.data && error.response.data.action === 'redirect' && error.response.data.redirect_url) {
                    window.location.href = error.response.data.redirect_url; //  Use the URL from payload
                } else {
                    window.location.href = '/login'; // Redirect to generic login as fallback
                }
            }
            // Important: Reject the promise so other error handlers can process it
            return Promise.reject(error);
        }
    );
}


