let tempCount = 1;
window.addEventListener("scroll", function () {
  tempCount = 1;
});
let appearObserver = new IntersectionObserver(
  (entries, observer) => {
    entries.forEach((entry) => {
      if (entry.isIntersecting) {
        entry.target.classList.add("has-been-seen");
        entry.target.classList.add("active");
        entry.target.style.setProperty("--i", tempCount);
        setTimeout(function () {
          entry.target.classList.add("appeared");
        }, tempCount * 120 + 1000);
        tempCount++;
      } else {
        entry.target.classList.remove("active");
      }
    });
  },
  { rootMargin: "0px 0px -15% 0px" }
);
const cols = document.querySelectorAll(".appear:not(.appeared)");
cols.forEach((col) => {
  appearObserver.observe(col);
});
let animationsObserver = new IntersectionObserver(
  (entries, observer) => {
    entries.forEach((entry) => {
      if (entry.isIntersecting) {
        entry.target.classList.add("animate");
      }
    });
  },
  { rootMargin: "0px 0px -10% 0px" }
);
const animations = document.querySelectorAll(".trigger-animation");
animations.forEach((col) => {
  animationsObserver.observe(col);
});

function getScrollLookup(targets, { start, pinnedContainer, containerAnimation }) {
  let triggers = gsap.utils.toArray(targets).map((el) =>
      ScrollTrigger.create({
        trigger: el,
        start: start || "top top",
        pinnedContainer: pinnedContainer,
        refreshPriority: -10,
        containerAnimation: containerAnimation,
      })
    ),
    st = containerAnimation && containerAnimation.scrollTrigger;
  return (target) => {
    let t = gsap.utils.toArray(target)[0],
      i = triggers.length;
    while (i-- && triggers[i].trigger !== t) {}
    if (i < 0) {
      return console.warn("target not found", target);
    }
    return containerAnimation ? st.start + (triggers[i].start / containerAnimation.duration()) * (st.end - st.start) : triggers[i].start;
  };
}

function getScrollPosition(animation, progress) {
  let p = gsap.utils.clamp(0, 1, progress || 0),
    st = animation.scrollTrigger,
    containerAnimation = st.vars.containerAnimation;
  if (containerAnimation) {
    let time = st.start + (st.end - st.start) * p;
    st = containerAnimation.scrollTrigger;
    return st.start + (st.end - st.start) * (time / containerAnimation.duration());
  }
  return st.start + (st.end - st.start) * p;
}

document.addEventListener("DOMContentLoaded", () => {
  gsap.registerPlugin(ScrollTrigger, ScrollToPlugin);

  const countups = document.querySelectorAll(".count-up");
  countups.forEach((countup) => {
    const start = parseFloat(countup.getAttribute("data-start"));
    const end = parseFloat(countup.getAttribute("data-end"));
    const decimalsAttr = parseFloat(countup.getAttribute("data-decimals"));
    // console.log(decimalsAttr);
    let decimals = 0;
    if (decimalsAttr) {
      decimals = decimalsAttr;
    }
    const options = {
      startVal: start,
      enableScrollSpy: true,
      scrollSpyOnce: true,
      duration: 2,
      decimalPlaces: decimals,
    };
    let numAnim = new countUp.CountUp(countup, end, options);
  });

  const quoteSliders = document.querySelectorAll(".quote-slider");
  quoteSliders.forEach((quoteSlider) => {
    quoteSlider.classList.remove("is-hidden");
    quoteSlider.offsetHeight;
    var flkty = new Flickity(quoteSlider, {
      cellAlign: "left",
      contain: true,
      dragThreshold: 10,
      percentPosition: true,
      groupCells: true,
      pageDots: false,
      arrowShape: "M2.72108 45.7939C2.26007 46.2626 1.89146 46.7887 1.61551 47.3492C1.1725 48.2487 0.968963 49.2352 1.00382 50.2144C1.05551 51.6663 1.63136 53.1018 2.72782 54.2129L45.7089 98.1936C48.025 100.563 51.8237 100.607 54.1936 98.2911C56.5636 95.975 56.6072 92.1763 54.2912 89.8064L21.2531 56H93C96.3137 56 99 53.3137 99 50C99 46.6863 96.3137 44 93 44H21.2531L54.2911 10.1936C56.6072 7.82368 56.5635 4.02494 54.1936 1.70888C51.8237 -0.607181 48.0249 -0.563517 45.7089 1.80641L2.72108 45.7939Z",
    });
  });

  const roiSliders = document.querySelectorAll(".roi-slider");
  roiSliders.forEach((roiSlider) => {
    roiSlider.classList.remove("is-hidden");
    roiSlider.offsetHeight;
    var flkty = new Flickity(roiSlider, {
      cellAlign: "left",
      contain: true,
      dragThreshold: 10,
      percentPosition: true,
      groupCells: true,
      adaptiveHeight: false,
      pageDots: false,
      arrowShape: "M2.72108 45.7939C2.26007 46.2626 1.89146 46.7887 1.61551 47.3492C1.1725 48.2487 0.968963 49.2352 1.00382 50.2144C1.05551 51.6663 1.63136 53.1018 2.72782 54.2129L45.7089 98.1936C48.025 100.563 51.8237 100.607 54.1936 98.2911C56.5636 95.975 56.6072 92.1763 54.2912 89.8064L21.2531 56H93C96.3137 56 99 53.3137 99 50C99 46.6863 96.3137 44 93 44H21.2531L54.2911 10.1936C56.6072 7.82368 56.5635 4.02494 54.1936 1.70888C51.8237 -0.607181 48.0249 -0.563517 45.7089 1.80641L2.72108 45.7939Z",
    });
  });

  let windowHeight = window.innerHeight * 0.75;
  let fullHeightTriggered = false;
  const revealAfterScrolls = document.querySelectorAll(".reveal-after-full-scroll");
  if (revealAfterScrolls) {
    const triggerReveals = () => {
      const scroll = window.scrollY;
      if (scroll > windowHeight && fullHeightTriggered == false) {
        fullHeightTriggered = true;
        revealAfterScrolls.forEach((revealAfterScroll) => {
          revealAfterScroll.classList.add("show");
        });
      } else if (scroll < windowHeight && fullHeightTriggered == true) {
        fullHeightTriggered = false;
        revealAfterScrolls.forEach((revealAfterScroll) => {
          revealAfterScroll.classList.remove("show");
        });
      }
    };
    document.addEventListener("scroll", (e) => {
      triggerReveals();
    });
    window.addEventListener("load", () => {
      triggerReveals();
    });
    window.addEventListener("resize", () => {
      windowHeight = window.innerHeight * 0.75;
    });
  }
});

window.addEventListener("load", () => {
  ScrollTrigger.refresh();
});
