import { AxiosRequestConfig } from 'axios'
import axios from '../../config/axios'

export const createUpdateExperience = async ({ id, ...postBody }) => {
  const config: AxiosRequestConfig = {
    method: id ? 'PUT' : 'POST',
    url: id ? `/square/experience/${id}` : '/square/experience',
    data: postBody,
  }
  const res = await axios(config)
  return res.data
}

export const createUpdatePaperPublicaton = async ({ id, ...postBody }) => {
  const config: AxiosRequestConfig = {
    method: id ? 'PUT' : 'POST',
    url: id ? `/square/paper-publications/${id}` : '/square/paper-publications',
    data: postBody,
  }
  const res = await axios(config)
  return res.data
}

export const createUpdateEducation = async ({ id, ...postBody }) => {
  const config: AxiosRequestConfig = {
    method: id ? 'PUT' : 'POST',
    url: id ? `/square/education/${id}` : '/square/education',
    data: postBody,
  }
  const res = await axios(config)
  return res.data
}

export const createUpdateWorkshop = async ({ id, ...postBody }) => {
  const config: AxiosRequestConfig = {
    method: id ? 'PUT' : 'POST',
    url: id ? `/square/workshops/${id}` : '/square/workshops',
    data: postBody,
  }
  const res = await axios(config)
  return res.data
}

export const getPaperPublicationSubtypes = async (publicationType) => {
  if (publicationType !== 'Other' && publicationType !== undefined) {
    const config: AxiosRequestConfig = {
      method: 'GET',
      url: '/square/paper-publications/sub-types',
      params: {
        type: publicationType,
      },
    }
    const res = await axios(config)
    return res.data
  } else {
    return []
  }
}
