/**
 * Auth interceptor: attaches the JWT access token to every outgoing request.
 */

import type { AxiosInstance, InternalAxiosRequestConfig } from 'axios';
import type { TokenProvider } from '../token-provider.js';

export function setupAuthInterceptor(client: AxiosInstance, tokenProvider: TokenProvider): void {
  client.interceptors.request.use(async (config: InternalAxiosRequestConfig) => {
    const token = await tokenProvider.getAccessToken();
    if (token) {
      config.headers.Authorization = `Bearer ${token}`;
    }
    return config;
  });
}
