import api from "src/api";

export const getUser = async () => {
  const { data } = await api.get("/user/me");
  return data.payload;
};

export const getUsers = async () => {
  const { data } = await api.get("/user");
  return data.payload;
};

export const createUser = async (user) => {
  const { data } = await api.post("/user", user);
  return data.payload;
};

export const updateUser = async (user, id) => {
  const { data } = await api.put(`/user/${id}`, user);
  return data.payload;
};

export const deleteUser = async (id) => {
  const { data } = await api.delete(`/user/${id}`);
  return data.payload;
};

export const userTypes = async () => {
  const { data } = await api.get("/user-types");
  return data.payload;
};

export const sendCode = async (email, phone) => {
  const data = await api.post("/provider-send-code", { email, phone });
  return data;
}

export const validateCode = async (email, phone, code, isLogin = false) => {
  const data = await api.post("/validate-code-provider", { email, phone, code, isLogin });
  return data;
};

// const removeEmptyRegistrationFields = (data) => {
//   if (Array.isArray(data)) {
//     return data.map(removeEmptyRegistrationFields);
//   }

//   if (!data || typeof data !== "object") {
//     return data;
//   }

//   return Object.entries(data).reduce((payload, [key, value]) => {
//     if (
//       value === undefined ||
//       key === "no_complement" ||
//       key.endsWith("_file")
//     ) {
//       return payload;
//     }

//     payload[key] = removeEmptyRegistrationFields(value);
//     return payload;
//   }, {});
// };

export const createUserAndProvider = async (data) => {
  const response = await api.post("/register-provider", data, {
    skipSuccessNotify: true,
  });
  return response;
};
