<script>
(function() {
  // ----- 1. CONFIGURE YOUR ID MAPPING -----
  // Map Meta product IDs (from your Facebook/Instagram catalog)
  // to YOUR Squarespace variant IDs.
  const META_TO_SQ_VARIANT = {
    "ef3d89f7-9688-4181-b50b-f55c107e399f": "SQ4073136",
    "8ffc2c85-f7ca-4bac-937f-68bdf6362d92": "SQ1072964",
    "b2318924-6e23-41e5-b89d-b84dbba77058": "SQ6029708",
    "effd0cfb-c15f-468c-8635-1a381cc29020": "SQ2340563",
    "1b962882-e48b-4c08-82f5-5dcce2d0fbd4": "SQ3493870",
    "d4b0007a-840e-40c1-83c0-364d3c008dcb": "SQ8885403",
    "0ab8787a-81e1-47ef-b9f5-74882e08c697": "SQ2687978",
    "589dfb05-9409-4f1d-a411-ed320b13a449": "SQ3011937",
    "a645ca55-1084-417b-aaed-1e792c6fc793": "SQ4559856",
    "8e4b4792-4aa0-4a22-88f0-52e4db3e2147": "SQ5825773",
    "e2edb78b-4983-4264-97d2-62fbc50485da": "SQ4379549",
    "1a02d777-e274-4f37-b879-260018eb2564": "SQ6838484",
    "33d4daa7-ab00-4881-bc1a-d6936975bbd5": "SQ9357952",
    "84ee1e10-0a59-419e-af5b-a43d4a61d714": "SQ0580150",
    "479689f9-1d47-41ba-8b27-31a105572182": "SQ0322832",
    "2481e975-8adf-4f01-b2ad-d3176a324f13": "SQ4552654",
    "820b47ff-315b-46c3-8b8d-9d3bf896e0b9": "SQ4464744",
    "9a86b0f6-0fc7-462a-912c-2c08d49b5102": "SQ8079652",
    "4567deb4-a795-444f-a8be-daf47dc2f051": "SQ4015822",
    "7452876e-aed5-4225-b9cd-87966ecad34e": "SQ7997636",
    "aeb4ef80-589c-4e5e-ada6-03dcbebffdc7": "SQ7953507",
    "746ad509-04f2-4db2-b6a1-07b5732b1a8e": "SQ2579447",
    "27bd0434-3576-4deb-a3a4-08f1a6bd63be": "SQ8015469",
    "9a87fcca-0500-4cdd-961d-41554e3e7a10": "SQ5431600",
    "b15a2796-d953-44f0-bd4c-79ddd1e77e9c": "SQ4506861",
    "93964f58-9f5b-4b94-98a2-a33e10de8f54": "SQ3111829",
    "ada2e53e-2b86-42fa-957f-68ac08886449": "SQ2882717",
    "cc713af2-251e-43c0-a416-33e8fcc8242e": "SQ3905854",
    "6ea51ede-eb1e-4621-bfba-79847a456c24": "SQ6056171",
    "f88edec5-2957-46d6-b56b-7c3fa50257c9": "SQ1154726",
    "58722f50-4f3f-4403-b7d2-cf8b0b032d9b": "SQ2647174",
    "3311a0ef-fadb-444f-9278-9cbf0486ddb4": "SQ6528557",
    "cd31900e-b35b-4949-be62-e34356f49862": "SQ1706353",
    "9309e7a8-1eda-41e9-9616-f370f333307a": "SQ9231050",
    "5b55be48-274d-41ec-8c69-63ab0f7ab67d": "SQ6947641",
    "f9cc15ac-bea7-4d48-a1d2-98ff82ca7abd": "SQ9546366",
    "66ff6cdf-73ad-4d40-a89d-f61d4a4f50f5": "SQ4818432",
    "f1f7a5f7-8ba0-45af-8f12-2d53ddab2bd8": "SQ0786758",
    "b0fdb377-8f5f-4f80-96fe-e031569793e8": "SQ4418424",
    "5b46d2a0-d850-474f-b4ce-96dc0ca68d97": "SQ1542476",
    "5d4ada6d-c29a-4a3b-8f60-26a6d6b7d20f": "SQ4073327",
    "ded392bb-921f-4262-a4d6-2eb2df55335d": "SQ2411106",
    "6bd0f7f0-a5eb-4ed8-b975-73857fecd6a7": "SQ1286829",
    "2d286104-29aa-48cd-bfc2-a7006cba695f": "SQ8336266",
    "22e0b138-2a01-499e-af79-b004a44463d6": "SQ7599237",
    "4123b98d-522d-43ea-904c-c802459d3e08": "SQ3058598",
    "afe2d242-ff82-433e-a9a0-bfedcffbc2a3": "SQ0717623",
    "04abcd3b-5333-44c2-9b34-b9d42372c06f": "SQ9648456",
    "7d63b447-1991-42fc-8e9b-fe0c7b604c2d": "SQ3323855",
    "ce333f31-0939-421b-9cce-24456b3bb86e": "SQ2138623",
    "47c73f3e-c87c-45e9-ac0e-2b6b7e3dac7e": "SQ8229838",
    "55fcd35c-d7b2-41d4-9b69-114983b42640": "SQ2116940",
    "702f602b-b38c-46cf-9fdc-5735bab8023f": "SQ0989007",
    "40934673-812d-477d-8eba-68c27aa8ba93": "SQ2509779",
    "9c2319d9-957b-4483-a267-f337b7a6726c": "SQ2243458",
    "81c6a702-1593-4693-832d-25b182088a3f": "SQ1607810",
    "2be4d8b9-d744-43de-88ee-4a2d25e7635a": "SQ5027943",
    "adfd436a-2004-4e3a-9e5a-1b0521b40c2f": "SQ4194829",
    "60dc6209-5236-436f-bd0d-0a6e6df0d032": "SQ6510582",
    "8c658525-4f52-461a-9deb-da51ce0221e7": "SQ8892938",
    "5e092365-5d16-4309-9f8e-adebfe0eb471": "SQ5928435",
    "3e1c7042-ffd4-42da-8302-a513190beed6": "SQ5005521",
    "12cb73ff-a1d5-4cf2-9fee-df2fe15ee134": "SQ3838333",
    "960dedc7-8312-4972-9aba-df04642920e4": "SQ1319501",
    "06058b61-cf2e-4ed1-b7bd-2f0646fe9ed0": "SQ0667980",
    "2e9d407d-5244-465c-bfe1-ac94c26079d3": "SQ6368519",
    "85fc233a-010e-472d-9718-d8bb60b40f6f": "SQ2249539",
    "5acabd88-b4b9-411d-aaf0-4860cbc0dfa6": "SQ6268464",
    "a0ca0d25-a5ee-42e1-9551-cb2041c9b113": "SQ6194599",
    "d8d47571-8d52-4146-9530-1f0af9ea6cd0": "SQ3605795"
  };

  // Turn this to true if you want to clear the cart first
  const CLEAR_CART_FIRST = true;

  // Path to your cart page (usually "/cart")
  const CART_PATH = "/cart";

  // ----- 2. HELPER: READ QUERY PARAMETERS -----
  function getQueryParams() {
    const params = {};
    const query = window.location.search.substring(1);
    if (!query) return params;

    query.split("&").forEach(function(part) {
      if (!part) return;
      const [rawKey, rawValue] = part.split("=");
      const key = decodeURIComponent(rawKey || "");
      const value = decodeURIComponent(rawValue || "");
      params[key] = value;
    });

    return params;
  }

  // ----- 3. HELPER: ADD A PRODUCT TO CART -----
  function addProductToCart(variantId, quantity) {
    // Fallback quantity
    const qty = quantity && quantity > 0 ? quantity : 1;

    // Squarespace AJAX cart add endpoint may differ by template;
    // this is the common pattern.
    return fetch("/cart/add", {
      method: "POST",
      headers: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      body:
        "variant=" + encodeURIComponent(variantId) +
        "&quantity=" + encodeURIComponent(qty)
    });
  }

  // ----- 4. HELPER: CLEAR CART (OPTIONAL) -----
  function clearCartIfNeeded() {
    if (!CLEAR_CART_FIRST) {
      return Promise.resolve();
    }
    // Common Squarespace clear endpoint; adjust if your template differs.
    return fetch("/cart/clear", {
      method: "POST"
    }).catch(function() {
      // If clear fails, continue anyway (do not block user)
      return;
    });
  }

  // ----- 5. HELPER: REDIRECT TO CART -----
  function redirectToCart() {
    // Preserve tracking parameters if you want attribution:
    // e.g. include original query string.
    var query = window.location.search || "";
    window.location.href = CART_PATH + query;
  }

  // ----- 6. MAIN LOGIC -----
  const params = getQueryParams();
  const productsParam = params.products || "";
  const coupon = params.coupon || "";

  if (!productsParam) {
    // No products passed – show a simple message or redirect.
    document.body.insertAdjacentHTML(
      "beforeend",
      "<p>No products were passed from the shop. Please return and try again.</p>"
    );
    return;
  }

  // products param is like "12345:3,23456:1"
  const productEntries = productsParam.split(",");

  // Parse into an array of { variantId, quantity }
  const itemsToAdd = [];
  productEntries.forEach(function(entry) {
    if (!entry) return;
    const parts = entry.split(":");
    const metaId = parts[0];
    const qty = parseInt(parts[1] || "1", 10);

    if (!metaId || !qty || qty < 1) return;

    const variantId = META_TO_SQ_VARIANT[metaId];
    if (!variantId) {
      // Optional: record unknown mapping or show a message
      console.warn("No Squarespace variant mapping for Meta product ID:", metaId);
      return;
    }

    itemsToAdd.push({ variantId: variantId, quantity: qty });
  });

  if (!itemsToAdd.length) {
    document.body.insertAdjacentHTML(
      "beforeend",
      "<p>We couldn't match the selected products to our store. Please try again later.</p>"
    );
    return;
  }

  // Show a basic message while loading
  document.body.insertAdjacentHTML(
    "beforeend",
    "<p>Loading your cart, please wait…</p>"
  );

  // 7. Clear cart (optional) → add all products → show coupon → go to cart
  clearCartIfNeeded()
    .then(function() {
      const addRequests = itemsToAdd.map(function(item) {
        return addProductToCart(item.variantId, item.quantity);
      });
      return Promise.all(addRequests);
    })
    .then(function() {
      // Show coupon reminder if present
      if (coupon) {
        document.body.insertAdjacentHTML(
          "beforeend",
          "<p>Use coupon code <strong>" +
            coupon +
            "</strong> at checkout.</p>"
        );
      }
      redirectToCart();
    })
    .catch(function(err) {
      console.error("Error adding products to cart:", err);
      document.body.insertAdjacentHTML(
        "beforeend",
        "<p>There was a problem preparing your cart. Please refresh the page or try again later.</p>"
      );
    });
})();
</script>