import { PUBLIC_URL } from '$env/static/public';

export async function getAvatarHistory(token){

    let response = await fetch(`${PUBLIC_URL}/api/users/avatars`,{
        method: 'GET',    
        credentials:'include',
        headers: {'Authorization': token },
    })

    if(response.ok){
        let data = await response.json();
        data = data.user_avatars
        data.reverse();
        return data;
    } else {
        console.log(response)
    }
}

export async function getConfirmationCode(token, email){
    const response = await fetch(`${PUBLIC_URL}/api/users/send_confirmation_code`,{
        method: 'POST',
        credentials:'include',
        headers: { 'Content-Type': 'application/json',
                   'Authorization': token },
        body: JSON.stringify({'email': email})
    })

    if(response.ok){
        return true
    } else {
        console.log(response)
    }
}

export async function changeUserData(token, username, email, password, avatar, code){
    let response = await fetch(`${PUBLIC_URL}/api/users/change_data`,{

        method: 'POST',
        credentials:'include',
        headers: {'Content-Type': 'application/json',
                  'Authorization': token },
        body: JSON.stringify({"verification_code": code,
                              "email": email,
                              "username": username,
                              "new_password": password,
                              "avatar_url": avatar})
    })

    if(response.ok){
        return true
    } else if (response.status == 409){
        
    } else {
        console.log(response)
    }
}