// Load a storyModal first time the page is requested,
// then set a cookie and don't load it again until it expires (1 day)

document.addEventListener("DOMContentLoaded", (event) => {
  const path = window.location.pathname; // Get the current path
  // Encode the path to base64 to ensure a valid cookie name format
  const encodedPath = btoa(path).replace(/=/g, ""); // Remove base64 padding to further ensure validity
  const cookieName = "modalShown_" + encodedPath; // Use an underscore for better readability

  if (
    document.cookie
      .split(";")
      .some((item) => item.trim().startsWith(cookieName + "="))
  ) {
    console.log("Modal has been shown before on this path.");
  } else {
    // Show the modal
    new bootstrap.Modal(document.getElementById("storyModal")).show();

    // Set a "modalShown" cookie for the current path to prevent the modal from auto-showing next time
    // Include SameSite=None; Secure to comply with modern browser policies
    document.cookie = `${cookieName}=true; max-age=86400; path=${path}; SameSite=Strict`;
  }
});
