import api from "src/api";

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

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

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

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

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

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

export const userTypes = async (accessScope = null, unitId = null) => {
  const { data } = await api.get("/selects/user-types", {
    params: {
      ...(accessScope ? { access_scope: accessScope } : {}),
      ...(unitId ? { unit_id: unitId } : {}),
      include_system_admin: accessScope === "unit",
    },
  });
  return Object.fromEntries(data.payload.map((type) => [type.value, type.label]));
};
