import axios from 'axios'
import { authenticationToken } from "../stores.js";
import baseUrl from './baseUrl.js';

let tokenValue;
authenticationToken.subscribe((value) => {
  tokenValue = value;
});

const api = () => {
  let baseInstance = axios.create({
    baseURL: baseUrl,
  })

  baseInstance.interceptors.request.use(
    config => {
      if (tokenValue) {
        config.headers.Authorization = tokenValue;
      } else {
        delete baseInstance.defaults.headers.common.Authorization;
      }
      return config;
    },
  )

  return baseInstance
}

export default api
