import { getPublicTokenHeaders, getStorefrontApiUrl, storefront } from "@/lib/storefront";
import { invariant } from "@esmate/utils";




// CORE

export async function getShopifyData(queryString: string, cursor?: string) {
  const { data } = await executeQuery(queryString, 12);

  invariant(data?.products, "products are not available");

  return data.products;
}


async function executeQuery(query: string, first: number, after?: string) {
  const response = await fetch(getStorefrontApiUrl(), {
    method: "POST",
    headers: getPublicTokenHeaders(),
    body: JSON.stringify({ query, variables: { first: first, after: after } }),
  });

  if (!response.ok) {
    throw new Error("Network response was not ok");
  }

  return response.json();

}


