const { api } = require("../config/api.config");

export const getData = async (
  endpoint,
  { page = 1, limit = 100 },
  sort = { name: 1 },
  search = null
) => {
  const [sortBy, order] = Object.entries(sort)[0];
  const roleOfSort = { asc: 1, desc: -1 };
  const orderBy = Object.entries(roleOfSort).find(
    ([_, value]) => value === order
  )[0];

  const params = { page, limit, sortBy, with: orderBy };
  if (search) params.search = search;

  const response = await api.get(endpoint, { params });

  return response?.data;
};
