// a library to wrap and simplify api calls
import apisauce from 'apisauce'
import Constant from '../library/Constant'

// our "constructor"
const create = (baseURL = 'https://trftia.eksad.com/tia-reporting-dev/public/') => {
  // ------
  // STEP 1
  // ------
  //
  // Create and configure an apisauce-based api object.
  //
  const api = apisauce.create({
    // base URL is read from the "constructor"
    baseURL,
    // here are some default headers
    headers: {
      'Cache-Control': 'no-cache',
      Accept: 'application/json',
      'Content-Type': 'application/json',
    },
    // 10 second timeout...
    timeout: 10000
  })

  api.addAsyncRequestTransform(request => async () => {
    var token
    try {
      const res = await localStorage.getItem(Constant.TOKEN)
      if (token != null) {
        token = res
        // alert(url)
        // api.setBaseURL(`${url}/api/`)
      } else {
        token = res
        // url = Constant.BASE_URL.MASTER + '/api/'
        // alert(url)
        // api.setBaseURL(`${url}/api/`)
      }
    } catch (error) {
      // console.tron.log(error)
    }
    // console.log(token)
    request.headers['token'] = token
    // console.tron.log(url)
  })

  // ------
  // STEP 2
  // ------
  //
  // Define some functions that call the api.  The goal is to provide
  // a thin wrapper of the api layer providing nicer feeling functions
  // rather than "get", "post" and friends.
  //
  // I generally don't like wrapping the output at this level because
  // sometimes specific actions need to be take on `403` or `401`, etc.
  //
  // Since we can't hide from that, we embrace it by getting out of the
  // way at this level.
  //
  const getRoot = () => api.get('')

  //Auth
  const login = (body) => api.post('auth/login', body)
  const resetPassword = (body) => api.post('auth/reset_password', body)
  const verification = (body) => api.post('email/reset_password', body)
  const isResetPassword = (userId) => api.post(`auth/is_reset_password/${userId}`)
  
  //Role
  const getRole = () => api.get('role/get_all_role')
  const getDetailRole = (roleId) => api.get(`role/get_role_by_id/${roleId}`)
  const searchRole = (body) => api.post('/role/search_role', body)
  const addRole = (body) => api.post('role/create_role', body)
  const editRole = (body) => api.post('role/update_role', body)
  const deleteRole = (roleId) => api.post(`role/delete_role/${roleId}`)

  //Menu
  const getMenu = () => api.get('menu/get_menu_hierarki')

  //UNIT BISNIS
  const getUnitBisnis = () => api.get('business_unit/get_all_business_unit')
  const createUnitBisnis = (body) => api.post('/business_unit/create_business_unit', body)
  const updateUnitBisnis = (body) => api.post('/business_unit/update_business_unit', body)
  const searchUnitBisnis = (body) => api.post('/business_unit/search_business_unit', body) 

  // APPROVAL MATRIX
  const getAM = () => api.get('approval_matrix/get_all_approval_matrix')

  // ------
  // STEP 3
  // ------
  //
  // Return back a collection of functions that we would consider our
  // interface.  Most of the time it'll be just the list of all the
  // methods in step 2.
  //
  // Notice we're not returning back the `api` created in step 1?  That's
  // because it is scoped privately.  This is one way to create truly
  // private scoped goodies in JavaScript.
  //
  return {
    // a list of the API functions from step 2
    getRoot,
    login,
    verification,
    resetPassword,
    isResetPassword,
    getRole,
    getDetailRole,
    searchRole,
    addRole,
    editRole,
    deleteRole,
    getMenu,
    getUnitBisnis,
    createUnitBisnis,
    updateUnitBisnis,
    searchUnitBisnis,
    getAM
  }
}

// let's return back our create method as the default.
export default {
  create
}