import Axios, { AxiosError, AxiosInstance, AxiosResponse } from 'axios'
import { getQueryString } from 'utils/getQueryString'
import { Cookie } from 'utils/cookie'

const fromString = getQueryString('From')
if (fromString) {
  Cookie.set('From', fromString)
}
const NewAxiosInstanceFunc: AxiosInstance = Axios.create({
  baseURL: process.env.REACT_APP_API_NEW,
  timeout: 500000
  //withCredentials: true,
})

NewAxiosInstanceFunc.defaults.headers.common['Authorization'] = localStorage.getItem('smtoken') || ''
if (Cookie.get('From') || fromString)
  NewAxiosInstanceFunc.defaults.headers['From'] = Cookie.get('From') || fromString || ''

NewAxiosInstanceFunc.interceptors.response.use(
  (response: AxiosResponse): AxiosResponse => response,
  (error: AxiosError): Promise<AxiosError> => Promise.reject(error)
)
export default NewAxiosInstanceFunc
