import { base, getToken } from "./_config";

export async function createToken({ rut=null, password=null }) {
    const res = await fetch(`${base}/auth/`, {
        method: 'POST',
        body: JSON.stringify({ rut, password }),
        headers: { "Content-Type": "application/json" }
    })
    if (!res.ok) throw await res.text()
    return res.json()
}

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

export async function recuperarAcceso(data) {
    const res = await fetch(`${base}/auth/recuperar/`, {
        method: 'POST',
        body: JSON.stringify(data),
        headers: { "Content-Type": "application/json" }
    })
    if (!res.ok) throw await res.text()
    return res.json()
}

export async function crearPassword(data) {
    const res = await fetch(`${base}/auth/nueva-contrasena/`, {
        method: 'POST',
        body: JSON.stringify(data),
        headers: { "Content-Type": "application/json" }
    })
    if (!res.ok) throw await res.text()
    return res.json()
}

export async function infoTokenPassword(data) {
    const res = await fetch(`${base}/auth/info/`, {
        method: 'POST',
        body: JSON.stringify(data),
        headers: { "Content-Type": "application/json" }
    })
    if (!res.ok) throw await res.text()
    return res.json()
}