import axios from 'axios'
import { get_user_id } from './user'

const LARAVEL_API_URL = import.meta.env.VITE_LARAVEL_API_URL
const API_URL = import.meta.env.VITE_API_URL

export const laravel_api_instance = axios.create({
    baseURL: LARAVEL_API_URL,
    withCredentials: true,
    headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
    }
})

export const api_instance = axios.create({
    baseURL: API_URL,
    withCredentials: true
})

export const callLaravelApi = (endpoint, params = {}) => {
    return laravel_api_instance.post(endpoint, {
        user_id: get_user_id(),
        ...params
    })
}

export const callApi = (request, params = {}) => {
    return api_instance.post('', {
        request,
        user_id: get_user_id(),
        ...params
    })
}

export const callWithFormData = (request, formData) => {
    formData.append('request', request)
    formData.append('user_id', get_user_id())
    return api_instance.post('', formData, {
        headers: { 'Content-Type': 'multipart/form-data' }
    })
}
