 export async function getLastMessages(chatId,msgLoaded){
   
   let token = localStorage.getItem('BPChat')
   const response = await fetch(`https://docs.black-phoenix.ru/api/chat/get_some_messages/${chatId}?messages_loaded=${msgLoaded}&messages_to_get=15`,
   {
     method:'GET',
     credentials:'include',
     headers: {'accept': 'application/json',
     'Authorization': token,
     },
   })

   if(response.ok){
     let data = await response.json()
     let msgMassive = data.messages
     console.log(msgMassive, " аа сообщения", chatId, " - айди")

     if(msgMassive === null){
      return []
     }
     else{
      msgMassive.reverse();
      return msgMassive
     }
   }

    else if(response.status === 404){
    console.log(response)
   }
    
    else if(!response.ok)
    console.log(response.status)
    
   }
 

 export async function getMessageById(chatId,msgId){

  let token = localStorage.getItem('BPChat')
  const response = await fetch(`https://docs.black-phoenix.ru/chat/message/${chatId}?message_id=${msgId}`,{
    method:"GET",
    credentials:'include',
    headers: {'Authorization': token },
    
  })

  if(response.ok){
    let data = await response.json(); 
    return data;
  }
  else{
    return { message: "Сообщение не найдено" };
  }

 }

 export async function MessagePicToUrl(messagePic){

     let token = localStorage.getItem('BPChat')

     const DataForm = new FormData();
     DataForm.append('file', messagePic)

     const response = await fetch('https://docs.black-phoenix.ru/api/images/upload_image',
     {
       method:"POST",
       body:DataForm,
       headers: {'Authorization': token },
     })

     if(!response.ok)
     console.log("ошибка", response.status)
  

     if(response.ok){
       const data = await response.json();
       console.log("картинка принята")
       return data.image_url;
       
     }
 }

 export async function getAllChats(){

  let token = localStorage.getItem('BPChat')
  const response = await fetch(`https://docs.black-phoenix.ru/api/chat`,
  {
    method:"GET",
    credentials:'include',
    headers: {'Authorization': token },

  })
  if(response.ok){
    let data = await response.json();
    return data.allowed_chats
  }
  else{
    console.log(response, "ты еблан")
  }
 }

 export async function getPinnedMsg(ID){

  let token = localStorage.getItem('BPChat')
  const response = await fetch(`https://docs.black-phoenix.ru/api/chat/pinned_messages/${ID}`,{
    method:"GET",
    credentials:'include',
    headers: {'Authorization': token },
  })

  if(response.ok){
    const data = await response.json()
    return data.pinned_messages
  }
  else{
    console.log(response.status)
  }
 }

 export async function pinMessage(chatId,messageId){

  let token = localStorage.getItem('BPChat')
  const response = await fetch(`https://docs.black-phoenix.ru/api/chat/pinn_message?chat_id=${chatId}&message_id=${messageId}`,{
  method:"POST",
  credentials:'include',
  headers: {'Authorization': token },
  })
  if(response.ok){
    const data = await response.json()
    return data.pinned_message
  }
  else
  console.log(response.status)

 }

 export async function unpinMessage(chatId, messageId){

  let token = localStorage.getItem('BPChat')
  const response = await fetch(`https://docs.black-phoenix.ru/api/chat/unpinn_message?chat_id=${chatId}&message_id=${messageId}`,{
    method:"DELETE",
    credentials:'include',
    headers: {'Authorization': token },
    })
    if(response.ok){
      return await response.json();
    }
    else
      console.log(response.status)
  
   }
 
   export async function uploadImages(image) {

    let token = localStorage.getItem('BPChat')
    const formData = new FormData();
      formData.append('file', image);

    const response = await fetch('https://docs.black-phoenix.ru/api/images/upload_image', {
      method: 'POST',
      credentials: 'include',
      body: formData,
      headers: {'Authorization': token },
    });
  
    if (response.ok) {
      let jopa = await response.json();
      return jopa.image_url
    } else {
      console.log(response);
    }
  }
  