async function getAllItemsFromList(siteUrl, listName) {
  const endpoint = `${siteUrl}/_api/web/lists/getbytitle('${listName}')/items?$top=100000`;

  const response = await fetch(endpoint, {
    method: "GET",
    headers: {
      Accept: "application/json;odata=verbose",
      "Content-Type": "application/json;odata=verbose",
    },
  });

  if (!response.ok) {
    const errText = await response.text();
    throw new Error(`Failed to fetch items: ${response.status} – ${errText}`);
  }

  const data = await response.json();

  // SharePoint sometimes returns "value" instead of "d.results"
  if (data?.d?.results) return data.d.results;
  if (data?.value) return data.value;

  return [];
}
