import { usersInstance } from "../apis/users.instance.js";

export const registerNewUser = async (data) => {
  return await usersInstance
    .request({
      method: "POST",
      url: "register/",
      data: { ...data },
      headers: {
        Authorization: null,
      },
    })
    .then((res) => res.data);
};
export const loginRequest = async (data) => {
  return await usersInstance
    .request({
      method: "POST",
      url: "login/",
      data: { ...data },
      headers: {Authorization: null}
    })
    .then((res) => res.data);
};
export const getUser = async () => {
  return await usersInstance
    .request({
      method: "GET",
      url: "api_user/",
      headers: {
        Authorization:
          localStorage.getItem("token") !== null
            ? `Token ${localStorage.getItem("token")}`
            : null,
      },
    })
    .then((res) => res.data);
};
export const changeUser = async ( data) => {
  return await usersInstance
    .request({
      method: "PUT",
      url: "api_user/",
      data: { ...data },
      headers: {
        Authorization:
          localStorage.getItem("token") !== null
            ? `Token ${localStorage.getItem("token")}`
            : null,
      },
    })
    .then((res) => res.data)
    .catch((err) => {
      throw err;
    });
};
export const changePassword = async ( data) => {
  return await usersInstance
    .request({
      method: "POST",
      url: "password/change/",
      data: { ...data },
      headers: {
        Authorization:
          localStorage.getItem("token") !== null
            ? `Token ${localStorage.getItem("token")}`
            : null,
      },
    })
};



export const logoutRequest = async (token) => {
  return await usersInstance
    .request({
      method: "POST",
      url: "logout/",
      headers: {Authorization: `Token ${token}`}
    })
    .then((res) => res.data);
};