// /services/webservice/fetchUserRightsService.js
export const fetchUserRightsService = async () => {
  const mode = process.env.NEXT_PUBLIC_API_PORT_MODE;
  const apiBaseUrl = mode === "dev" ? `${window.location.protocol}//${window.location.hostname}:80/talas5/ClientData/WebServiceMap.asmx` : `${window.location.origin}/talas5/ClientData/WebServiceMap.asmx`;

  const params = new URLSearchParams(window.location.search);
  const idMap = params.get("m");
  const idUser = params.get("u");

  const url = `${apiBaseUrl}/GetUserRights?idMap=${idMap}&idUser=${idUser}`;

  const response = await fetch(url, {
    method: "GET",
    headers: {
      Connection: "close",
    },
  });

  if (!response.ok) {
    throw new Error("Fehler beim Abrufen der Benutzerrechte");
  }

  const json = await response.json();
  return json.Rights || [];
};
