function isChatPage() {
  return window.location.href.includes("chats");
}

if (isChatPage()) {
  function addOverlayToSwipeoutItems() {
    var swipeoutItems = document.querySelectorAll(".swipeout.swipeout-list-item");

    swipeoutItems.forEach(function (item) {
      if (!item.querySelector(".overlayxry")) {
        // Create an overlay for each swipeout item
        var overlay = document.createElement("div");
        overlay.className = "overlayxry";

        // Style the overlay
        overlay.style.position = "absolute";
        overlay.style.top = "0";
        overlay.style.left = "0";
        overlay.style.width = "18%";
        overlay.style.height = "100%";
        overlay.style.backgroundColor = "rgba(0, 0, 0, 0.5)";
        overlay.style.zIndex = "9000";
        overlay.style.pointerEvents = "all"; // Make the overlay fully absorb interactions

        // Append the overlay to the swipeout item
        item.appendChild(overlay);
      }
    });
  }

  var intervalIdxx = setInterval(addOverlayToSwipeoutItems, 10);

  // general variables
  var amountsbyid = [];
  var isProgrammaticClick = false;
  var automodule = false;
  var jaja = false;

  // add style for the user info
  var styleElement = document.createElement("style");
  styleElement.id = "yourDynamicStyleId";
  styleElement.innerHTML = "#ModalSubscribersItem___BV_modal_outer_ { z-index: -1 !important; /* your styles here */ } .global_preloader.active { opacity: 0 !important; } ";
  document.head.appendChild(styleElement);

  function checkAndSetZIndex() {
    var checkInterval = setInterval(() => {
      var infodiv = document.getElementById("ModalSubscribersItem___BV_modal_outer_");

      if (infodiv) {
        // infodiv.style.zIndex = "-1";
        // // console.log("Element found and zIndex set to -1");
        var tabsNavItemx = document.getElementsByClassName("b-tabs__nav m-tabs-default m-flex-width m-size-sm")[0].children[1];
        if (!tabsNavItemx.classList.contains("m-current") && automodule) {
          document.getElementsByClassName("b-tabs__nav m-tabs-default m-flex-width m-size-sm")[0].children[1].children[0].click();
          automodule = false;
        }
        if (tabsNavItemx.classList.contains("m-current") && automodule) {
          document.getElementsByClassName("b-tabs__nav m-tabs-default m-flex-width m-size-sm")[0].children[1].children[0].click();
          automodule = false;
        }
        // clearInterval(checkInterval); // Stop checking once the element is found
      }
    }, 1); // Adjust the interval as needed (e.g., check every 10 milliseconds)
  }

  // Start checking for the element
  checkAndSetZIndex();

  var elmnt = document.getElementsByClassName("swipeout swipeout-list-item");
  var ielmnt = elmnt.length;

  for (var i = 0; i < ielmnt; i++) {
    addOverlayToSwipeoutItems();
    jaja = true;
    (function (index) {
      setTimeout(function () {
        if (amountsbyid.some((item) => item.id === elmnt[index].children[1].children[0].children[0].id)) {
          setTimeout(function () {
            if (index === ielmnt - 1) {
              // console.log("Loop has finished");
              clearInterval(intervalIdxx);
              var elements = document.querySelectorAll(".overlayxry");
              // Iterate through each element and remove it from the DOM
              elements.forEach(function (element) {
                element.remove();
              });
              jaja = false;
              var intervalIdxy = setInterval(checkElementLength, 100);
            }
          }, 1000);
          return;
        }

        isProgrammaticClick = true;
        automodule = true;
        document.getElementsByClassName("swipeout swipeout-list-item")[index].children[1].children[0].children[0].children[1].click();
        isProgrammaticClick = false;

        document.getElementsByClassName("swipeout swipeout-list-item")[index].children[1].children[0].children[0].children[1].classList.add("mutezinde");
        setTimeout(function () {
          var elementtotal = document.querySelector(".b-fans__item__list.m-native-custom-scrollbar.m-scrollbar-y.m-invisible-scrollbar.m-earnings-list");
          var lastDivtotal = elementtotal.lastElementChild;
          var totalamount = lastDivtotal.lastElementChild.innerText;

          totalamount = totalamount.slice(0, -1);
          var numericValue = parseFloat(totalamount.replace("$", ""));
          var roundedNumber = Math.round(numericValue);
          totalamount = "$" + roundedNumber;

          document.getElementsByClassName("swipeout swipeout-list-item")[index].children[1].children[0].children[0].children[1].children[0].innerHTML = '<span class="mutezinde ijefie" style="font-size: 16px; color: #fb3a3a; font-weight: 700; width: 100%; display: flex; height: 100%; margin: auto; justify-content: center; align-items: center; background-color: #f1f2f4; border-radius: 50%">' + totalamount + "</span>";

          document.getElementsByClassName("swipeout swipeout-list-item")[index].children[1].children[0].children[0].children[1].children[0].classList.add("mutezinde");

          document.getElementsByClassName("swipeout swipeout-list-item")[index].children[1].children[0].children[0].children[1].classList.add("mutezinde");

          var data = {
            id: elmnt[index].children[1].children[0].children[0].id,
            amountx: totalamount,
          };

          amountsbyid.push(data);
          setTimeout(function () {
            if (index === ielmnt - 1) {
              // console.log("Loop has finished");
              clearInterval(intervalIdxx);
              var elements = document.querySelectorAll(".overlayxry");
              // Iterate through each element and remove it from the DOM
              elements.forEach(function (element) {
                element.remove();
              });
              jaja = false;
              var intervalIdxy = setInterval(checkElementLength, 100);
            }
          }, 1000);
        }, 1000);
      }, 1000 * index); // Delay logging and clicking based on the index
    })(i);
  }

  var currentLength = ielmnt;
  var thedifference;

  // Function to check and update element length
  function checkElementLength() {
    // console.log("started checkelemet length");
    currentLength = document.getElementsByClassName("swipeout swipeout-list-item").length;

    // Check if the length has changed
    if (currentLength !== ielmnt) {
      // console.log("more users appeared in the chat");
      thedifference = currentLength - ielmnt;
      ielmnt = currentLength;
      if (!jaja) {
        starty(currentLength, thedifference);
      }
    }
  }

  function starty(currentLength, thedifference) {
    // Set the flag to indicate that starty is running
    // isStartyRunning = true;
    addOverlayToSwipeoutItems();

    styleElement.innerHTML = "#ModalSubscribersItem___BV_modal_outer_ { z-index: -1 !important; /* your styles here */ } .global_preloader.active { opacity: 0 !important;}";

    for (var i = currentLength - thedifference; i < currentLength; i++) {
      jaja = true;
      // console.log("starty is starting now");
      (function (index) {
        setTimeout(function () {
          if (amountsbyid.some((item) => item.id === elmnt[index].children[1].children[0].children[0].id)) {
            setTimeout(function () {
              if (index === currentLength - 1) {
                // Reset the flag to indicate that starty is done
                // console.log("Loop has finished for starty");
                clearInterval(intervalIdxx);
                // Remove elements with class "overlayxry" from the DOM
                var elements = document.querySelectorAll(".overlayxry");
                elements.forEach(function (element) {
                  element.remove();
                });
                jaja = false;
              }
            }, 1000);
            return;
          }

          isProgrammaticClick = true;
          automodule = true;
          document.getElementsByClassName("swipeout swipeout-list-item")[index].children[1].children[0].children[0].children[1].click();
          isProgrammaticClick = false;

          // Check if the click was successful before adding the class
          document.getElementsByClassName("swipeout swipeout-list-item")[index].children[1].children[0].children[0].children[1].classList.add("mutezinde");

          setTimeout(function () {
            var elementtotal = document.querySelector(".b-fans__item__list.m-native-custom-scrollbar.m-scrollbar-y.m-invisible-scrollbar.m-earnings-list");
            var lastDivtotal = elementtotal.lastElementChild;
            var totalamount = lastDivtotal.lastElementChild.innerText;

            totalamount = totalamount.slice(0, -1);
            var numericValue = parseFloat(totalamount.replace("$", ""));
            var roundedNumber = Math.round(numericValue);
            totalamount = "$" + roundedNumber;

            document.getElementsByClassName("swipeout swipeout-list-item")[index].children[1].children[0].children[0].children[1].children[0].innerHTML = '<span class="mutezinde ijefie" style="font-size: 16px; color: #fb3a3a; font-weight: 700; width: 100%; display: flex; height: 100%; margin: auto; justify-content: center; align-items: center; background-color: #f1f2f4; border-radius: 50%">' + totalamount + "</span>";

            document.getElementsByClassName("swipeout swipeout-list-item")[index].children[1].children[0].children[0].children[1].children[0].classList.add("mutezinde");

            document.getElementsByClassName("swipeout swipeout-list-item")[index].children[1].children[0].children[0].children[1].classList.add("mutezinde");

            var data = {
              id: elmnt[index].children[1].children[0].children[0].id,
              amountx: totalamount,
            };

            amountsbyid.push(data);
            setTimeout(function () {
              if (index === currentLength - 1) {
                // Reset the flag to indicate that starty is done
                // console.log("Loop has finished for starty");
                clearInterval(intervalIdxx);
                // console.log("isStartyRunning set to false");
                // Remove elements with class "overlayxry" from the DOM
                var elements = document.querySelectorAll(".overlayxry");
                elements.forEach(function (element) {
                  element.remove();
                });
                jaja = false;
              }
            }, 1000);
          }, 1000);
        }, 1000 * (index - (currentLength - thedifference)));
      })(i);
    }
  }

  // this is consuming too much data because its non stop adding divs to the dom

  function updateContent() {
    var elements = document.getElementsByClassName("swipeout swipeout-list-item");

    for (var i = 0; i < elements.length; i++) {
      var findthe = elements[i].children[1].children[0].children[0].id;
      // // // console.log(findthe);

      var matchingamnt = null;

      for (var xxi = 0; xxi < amountsbyid.length; xxi++) {
        if (amountsbyid[xxi].id === findthe) {
          matchingamnt = amountsbyid[xxi].amountx;
          // // // console.log(matchingamnt);

          if (elements[i].children[1].children[0].children[0].children[1].children[0].innerHTML.includes(matchingamnt)) {
            // console.log("not going to add anything");
          } else {
            elements[i].children[1].children[0].children[0].children[1].classList.add("mutezinde");
            elements[i].children[1].children[0].children[0].children[1].children[0].innerHTML = '<span class="mutezinde ijefie" style="font-size: 16px; color: #fb3a3a; font-weight: 700; width: 100%; display: flex; height: 100%; margin: auto; justify-content: center; align-items: center; background-color: #f1f2f4; border-radius: 50%;">' + matchingamnt + "</span>";
            break;
          }
        }
      }
    }
  }

  // Execute the code every 1 second
  setInterval(updateContent, 1000);

  ///// stuff shit

  document.addEventListener("click", function (event) {
    // Check if the clicked element has the class "mutezinde" and if starty is not running
    if (event.target.classList.contains("mutezinde")) {
      if (isProgrammaticClick) {
        // console.log("Click triggered by JavaScript.");
      } else {
        // console.log("Manual mouse click.");
        styleElement.innerHTML = "#ModalSubscribersItem___BV_modal_outer_ { z-index: 1040 !important; /* your styles here */ } .global_preloader.active { opacity: 1 !important;}";
      }
    }
  });
} else {
  // console.log("Not on a chat page. The code will not run.");
}
