// Generate a unique ID for the user (if not already generated)
const generateUserID = () => {
  // Check if the user ID is already stored in cookies or local storage
  const storedUserID = getStoredUserID();

  if (storedUserID) {
    return storedUserID;
  }

  // Example: Generating a random string
  const characters =
    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  let id = "";
  for (let i = 0; i < 10; i++) {
    id += characters.charAt(Math.floor(Math.random() * characters.length));
  }

  // Store the generated user ID in cookies or local storage
  storeUserID(id);

  return id;
};

// Get the stored user ID from cookies or local storage
const getStoredUserID = () => {
  return localStorage.getItem("userID");
};

// Store the user ID in cookies or local storage
const storeUserID = (userID) => {
  localStorage.setItem("userID", userID);
};

// Get the current date in YYYY-MM-DD format
const getCurrentDate = () => {
  const today = new Date();
  const year = today.getFullYear();
  const month = String(today.getMonth() + 1).padStart(2, "0");
  const day = String(today.getDate()).padStart(2, "0");
  return `${year}-${month}-${day}`;
};

let userTrackingCountLimit = "20";
const checkUsage = () => {
  const today = new Date().toDateString();
  const usageCount = getCookie("featureUsageCount");

  if (usageCount) {
    // User has already used the feature today
    if (usageCount >= Number(userTrackingCountLimit)) {
      showShareModal();
    } else {
      incrementUsageCount(usageCount);
    }
  } else {
    // First usage of the day
    setCookie("featureUsageCount", 1, getExpirationDate());
  }
  const userID = generateUserID();
  try {
    if ((usageCount + 1) % 5 === 0) {
      gtag("event", `feature_used_${usageCount + 1}_times`, {
        user_id: userID,
        userID: userID,
        feature: window.location.pathname + location.search,
      });
    }
  } catch (error) {
    console.log(error);
  }

  try {
    gtag("event", "feature_used", {
      user_id: userID,
      userID: userID,
      feature: window.location.pathname + location.search,
    });
  } catch (error) {
    console.log(error);
  }
};
const incrementUsageCount = (count) => {
  const today = new Date().toDateString();

  if (count >= Number(userTrackingCountLimit)) {
    showShareModal();
  } else {
    count++;
    setCookie("featureUsageCount", count, getExpirationDate());
  }
};

const showShareModal = () => {
  document.getElementById("shareModal").style.display = "block";
};

const handleShare = () => {
  const usageCount = getCookie("featureUsageCount");
  if (usageCount) {
    setCookie("featureUsageCount", 0, getExpirationDate());
  }
  document.getElementById("shareModal").style.display = "none";
  try {
    const userID = generateUserID();
    gtag("event", "feature_shared", {
      user_id: userID,
      userID: userID,
      feature: window.location.pathname + location.search,
    });
  } catch (error) {
    console.log(error);
  }
};

const getCookie = (name) => {
  const cookies = document.cookie.split(";");
  for (let i = 0; i < cookies.length; i++) {
    const cookie = cookies[i].trim();
    if (cookie.indexOf(name + "=") === 0) {
      return parseInt(cookie.substring((name + "=").length, cookie.length));
    }
  }
  return null;
};

const setCookie = (name, value, expires) => {
  document.cookie = `${name}=${value};expires=${expires};path=/`;
};

const getExpirationDate = () => {
  const date = new Date();
  date.setTime(date.getTime() + 24 * 60 * 60 * 1000); // 24 hours in milliseconds
  return date.toUTCString();
};
const socialShare = document.querySelectorAll(".social-share");

if (socialShare) {
  Array.from(socialShare).map((item) => {
    item.addEventListener("click", () => {
      handleShare();
    });
  });
}