"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.axiosApiInstance = exports.axio2 = void 0;
var _axios = _interopRequireDefault(require("axios"));
var _authService = require("../services/authService");
var _tokenService = require("../services/tokenService");
const axiosApiInstance = exports.axiosApiInstance = _axios.default.create();
const axio2 = exports.axio2 = _axios.default.create();
axio2.interceptors.request.use(async config => {
  const token = (0, _tokenService.getStorageToken)();
  config.headers = {
    Authorization: "Bearer ".concat(token),
    Accept: "application/json",
    "Content-Type": "multipart/form-data"
  };
  config.maxBodyLength = 350000;
  return config;
}, error => {
  Promise.reject(error);
});

// Response interceptor for API calls
axio2.interceptors.response.use(response => {
  return response;
}, async function (error) {
  const originalRequest = error.config;
  if (error.response !== undefined && originalRequest !== undefined) {
    if (error.response.status === 403 && !originalRequest._retry) {
      originalRequest._retry = true;
      const access_token = await (0, _authService.refreshToken)();
      _axios.default.defaults.headers.common["Authorization"] = "Bearer " + access_token;
      return axiosApiInstance(originalRequest);
    }
  }
  return Promise.reject(error);
});
axiosApiInstance.interceptors.request.use(async config => {
  const token = (0, _tokenService.getStorageToken)();
  config.headers = {
    Authorization: "Bearer ".concat(token),
    Accept: "*/*",
    "Content-Type": "application/json"
  };
  config.maxBodyLength = 350000;
  return config;
}, error => {
  Promise.reject(error);
});

// Response interceptor for API calls
axiosApiInstance.interceptors.response.use(response => {
  return response;
}, async function (error) {
  const originalRequest = error.config;
  if (error.response.status === 403 && !originalRequest._retry) {
    originalRequest._retry = true;
    const access_token = await (0, _authService.refreshToken)();
    _axios.default.defaults.headers.common["Authorization"] = "Bearer " + access_token;
    return axiosApiInstance(originalRequest);
  }
  return Promise.reject(error);
});