import { axiosInstance } from "../config/api";

export const getMasterData = (params) => {
  let searchParams = new URLSearchParams();
  searchParams.append("limit", params?.pagination?.pageSize ?? 10);
  searchParams.append("page", params?.pagination.pageIndex + 1 ?? 1);
  searchParams.append("search", params?.globalFilter ?? "");

  const url = `/admin/master-data?${searchParams.toString()}`;

  return new Promise((resolve, reject) => {
    axiosInstance
      .get(url)
      .then((response) => resolve(response))
      .catch((err) => reject(err));
  });
};

export const setMasterData = (data) => {
  const url = "/admin/master-data";
  return new Promise((resolve, reject) => {
    axiosInstance
      .post(url, data)
      .then((response) => resolve(response))
      .catch((err) => reject(err));
  });
};

export const updateMasterData = (data, id) => {
  const url = `/admin/master-data/${id}`;
  return new Promise((resolve, reject) => {
    axiosInstance
      .put(url, data)
      .then((response) => resolve(response))
      .catch((err) => reject(err));
  });
};

export const deleteMasterData = (id) => {
  const url = `/admin/master-data/${id}`;
  return new Promise((resolve, reject) => {
    axiosInstance
      .delete(url)
      .then((response) => resolve(response))
      .catch((err) => reject(err));
  });
};
