import { getAccessToken } from "@/lib/auth";
import { AxiosResponse, InternalAxiosRequestConfig } from "axios";
import { normalizeApiError } from "./errors";

export function requestInterceptor(config: InternalAxiosRequestConfig) {
  const token = getAccessToken();

  if (token) {
    config.headers.Authorization = `Bearer ${token}`;
  }

  return config;
}

export const responseSuccessInterceptor = (response: AxiosResponse) => response;

export function responseErrorInterceptor(error: unknown) {
  const normalizedError = normalizeApiError(error);

  return Promise.reject(normalizedError);
}
