async function getAllItemsFromList(siteUrl, listName, query = "") {
  // Default: fetch up to 5000 items
  let endpoint = `${siteUrl}/_api/web/lists/getbytitle('${listName}')/items?$top=100000`;

  // If the user provided a query, append it properly
  if (query) {
    // remove leading ? or & if present
    query = query.replace(/^[?&]/, "");
    endpoint += `&${query}`;
  }

  const response = await fetch(endpoint, {
    method: "GET",
    headers: {
      Accept: "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();
  return data.d.results;
}

// Example usage:
(async () => {
  const siteUrl = "https://portal.mapnamd2.com/HRM/meetings";
  const listName = "meetings";

  // Example query: get active projects with only Id, Title, Status columns, ordered by Title
  const query = "$select=Id,Title,Status&$orderby=Title asc";

  try {
    const items = await getAllItemsFromList(siteUrl, listName, query);
    console.log("✅ Queried items:", items);
  } catch (err) {
    console.error("❌ Error getting items:", err);
  }
})();
