import { base, getToken } from './_config'

export async function getTipoCargo(params) {
    const query = !params ? '' : '?' + (new URLSearchParams(params).toString());
    const res = await fetch(`${base}/tipo-cargo/${query}`, {
        headers: { "Authorization": `Bearer ${getToken()}`, "Content-Type": "application/json" }
    })
    if (!res.ok) throw await res.json()
    return res.json()
}

export async function getTipoCargoId(id) {
    const res = await fetch(`${base}/tipo-cargo/${id}/`, {
        headers: { "Authorization": `Bearer ${getToken()}`, "Content-Type": "application/json" }
    })
    if (!res.ok) throw await res.json()
    return res.json()
}

export async function createTipoCargo(data) {
    const res = await fetch(`${base}/tipo-cargo/`, {
        method: 'POST',
        body: JSON.stringify(data),
        headers: { "Authorization": `Bearer ${getToken()}`, "Content-Type": "application/json" }
    })
    if (!res.ok) throw await res.json()
    return res.json()
}

export async function updateTipoCargo({ id_cargo: id = null, ...data }) {
    const res = await fetch(`${base}/tipo-cargo/${id}/`, {
        method: 'PATCH',
        body: JSON.stringify(data),
        headers: { "Authorization": `Bearer ${getToken()}`, "Content-Type": "application/json" }
    })
    if (!res.ok) throw await res.json()
    return res.json()
}

export async function deleteTipoCargo(id) {
    const res = await fetch(`${base}/tipo-cargo/${id}/`, {
        method: 'DELETE',
        headers: { "Authorization": `Bearer ${getToken()}`, "Content-Type": "application/json" }
    })
    if (!res.ok) throw await res.json()
    return res.text()
}