React/Nextjs | Cannot figure out how to properly loop image back to start/end when pressing previous and next arrows

I am using heroicons, tailwindCSS, nextjs/react.

I have tried creating a useState for the index and using useEffect to update it every time. selectedIMG has a state but that didnt work as well. I cannot seem to understand why it is not looping the array when clicking on the arrow icons.

  const [selectedIMG, setSelectedIMG] = useState();


        {/* Popup Image */}
        {selectedIMG && (
          <div className="fixed top-0 z-50 w-[100vw] h-[100vh] flex justify-center items-center backdrop-blur-sm select-none">
            <div className="absolute w-[100vw] h-[100vh] bg-custom-black bg-opacity-60 " />
            <XIcon
              className="w-12 h-12 absolute top-10 right-10 cursor-pointer z-10 p-2 border rounded-full xl:scale-150 bg-opacity-10 bg-white"
              onClick={() => setSelectedIMG()}
            />
            <ArrowLeftIcon
              className="absolute text-white z-50 w-12 h-12 p-2 border rounded-full left-5 lg:left-[10vw] xl:scale-150 cursor-pointer shadow-2xl bg-white bg-opacity-10"
              onClick={() => {
                const selectedIndex = galleryImages.findIndex(
                  (item) => item == selectedIMG
                );

                console.log(selectedIndex);

                if (selectedIndex <= 0) {
                  setSelectedIMG(galleryImages[galleryImages.length]);
                } else {
                  setSelectedIMG(galleryImages[selectedIndex - 1]);
                }
              }}
            />
            <ArrowRightIcon
              className="absolute text-white z-50 w-12 h-12 p-2 border rounded-full right-5 lg:right-[10vw] xl:scale-150 cursor-pointer shadow-2xl bg-white bg-opacity-10"
              onClick={() => {
                const selectedIndex = galleryImages.findIndex(
                  (item) => item.src == selectedIMG.src
                );

                if (selectedIndex == galleryImages.length) {
                  setSelectedIMG(galleryImages[0]);
                } else {
                  setSelectedIMG(galleryImages[selectedIndex + 1]);
                }
              }}
            />
            <div className="relative w-full h-3/4">
              <Image
                priority
                layout="fill"
                objectFit="contain"
                src={selectedIMG.src}
                alt=""
              />
            </div>
          </div>
        )}
        {/*  */}
        <section>
          <PageHeader title={"Gallery"} info={"Showcase of work"} />

          <div className="grid grid-cols-2 sm:grid-cols-3  gap-y-1 gap-x-1 justify-center items-center mt-5">
            {galleryImages.map((img) => (
              <div
                className="relative rounded-sm min-w-[100px] w-full h-full overflow-hidden mx-auto p-3 cursor-pointer"
                key={img.src}
                whileHover={{
                  scale: 1.1,
                  transition: {
                    ease: [0.6, 0.01, -0.05, 0.95],
                  },
                }}
                onClick={() => setSelectedIMG(img)}
              >
                <div>
                  <Image
                    width={"100%"}
                    height={"100%"}
                    layout="responsive"
                    objectFit="cover"
                    src={img.src}
                    alt={img.caption}
                  />
                </div>
              </div>
            ))}
          </div>
        </section>