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("/franchisee/users");
  return data.payload;
};

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

export const getUsersByUnit = async () => {
  const { data } = await api.get("/selects/franchisee/users");
  return data.payload;
};

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

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

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

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

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