import axios from 'axios'
import { user } from '~/store/user'

const token = user.value?.token || user.value?.last_token

// const isDev = false

const baseURL = window.GLOBAL_CONFIG.api

const Request = axios.create({
  baseURL,
  method: 'post',
  timeout: 60000,
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
  },
})

Request.interceptors.request.use(
  (config) => {
    config.data = Object.assign(
      {
        token,
        api: 'json',
        site: 'aimk',
        issubmit: 1
      },
      config.data || {}
    )
    if (config.method === 'get') {
      config.params = Object.assign(
        {
          token,
          api: 'json',
          fake: 1,
        },
        config.params || {}
      )
    }
    return config
  },
  (error) => {
    console.error('request error: ', error)
    return Promise.reject(error)
  }
)

// response interceptor
Request.interceptors.response.use(
  (response) => {
    return response.data
  },
  (error) => {
    console.error('response error: ' + error)
    return Promise.reject(error)
  }
)

export default Request
