'use client'

import axios from 'axios'

axios.defaults.baseURL = process.env.NEXT_PUBLIC_API_SERVER_PATH

const axiosInstance = axios.create({
  // baseURL: process.env.API_SERVER_URL,
  headers: {
    Accept: 'application/json',
  },
})

axiosInstance.interceptors.request.use((config) => {
  // config['Authorization'] = localStorage.getItem('token')
  //TODO: 인터셉터에서 추가 로직 구현
  return config
})

axiosInstance.interceptors.request.use(undefined, (error) => {
  //TODO: 인터셉터에서 에러 처리 로직 구현
  // if (error.isAxiosError && e.response?.status === 401) {
  //   localStorage.removeItem('token')
  // }
})

export const get = (url) =>
  axiosInstance
    .get(url)
    .then((res) => res.data)
    .catch(console.error)

export const post = (url, data) =>
  axiosInstance
    .post(url, data)
    .then((res) => res.data)
    .catch(console.error)

export const put = (url, data) =>
  axiosInstance
    .put(url, data)
    .then((res) => res.data)
    .catch(console.error)

export const patch = (url, data) =>
  axiosInstance
    .patch(url, data)

    .then((res) => res.data)
    .catch(console.error)

export const del = (url) =>
  axiosInstance
    .delete(url)
    .then((res) => res.data)
    .catch(console.error)
