// src/components/carousel/fade-controller.ts
var FadeController = class {
  constructor(carousel) {
    this.heightCalculated = false;
    this.isEnabled = false;
    this.isCalculating = false;
    this.fadeClasses = {
      base: [
        "absolute",
        "top-0",
        "left-0",
        "w-full",
        "h-full",
        "visible",
        "transition-opacity",
        "duration-x-slow",
        "ease-in-out"
      ],
      active: ["opacity-100", "z-[1]"],
      inactive: ["opacity-0", "z-0"]
    };
    this.handleResize = () => {
      if (!this.isEnabled) {
        return;
      }
      if (this.resizeTimeout) {
        clearTimeout(this.resizeTimeout);
      }
      this.resizeTimeout = setTimeout(() => {
        if (this.isEnabled) {
          this.heightCalculated = false;
          this.calculateHeight();
        }
      }, 250);
    };
    this.carousel = carousel;
  }
  enable() {
    if (this.isEnabled) {
      return;
    }
    this.isEnabled = true;
    this.calculateHeight();
    window.addEventListener("resize", this.handleResize);
  }
  disable() {
    if (!this.isEnabled) {
      return;
    }
    this.isEnabled = false;
    this.removeFadeStyles();
    this.carousel.style.removeProperty("--carousel-height");
    if (this.carousel.scrollContainer) {
      this.carousel.scrollContainer.style.removeProperty("height");
    }
    this.heightCalculated = false;
    this.isCalculating = false;
    window.removeEventListener("resize", this.handleResize);
    if (this.resizeTimeout) {
      clearTimeout(this.resizeTimeout);
      this.resizeTimeout = void 0;
    }
  }
  updateActiveSlide(activeIndex) {
    if (!this.isEnabled) {
      return;
    }
    const slides = this.carousel.getSlides();
    slides.forEach((slide, index) => {
      if (index === activeIndex) {
        this.show(slide);
      } else {
        this.hide(slide);
      }
    });
  }
  show(slide) {
    if (!this.isEnabled) {
      return;
    }
    const classList = slide.classList;
    classList.remove(...this.getAllFadeClasses());
    classList.add(...this.fadeClasses.base, ...this.fadeClasses.active);
    slide.removeAttribute("inert");
    slide.setAttribute("aria-hidden", "false");
  }
  hide(slide) {
    if (!this.isEnabled) {
      return;
    }
    const classList = slide.classList;
    classList.remove(...this.getAllFadeClasses());
    classList.add(...this.fadeClasses.base, ...this.fadeClasses.inactive);
    slide.toggleAttribute("inert", true);
    slide.setAttribute("aria-hidden", "true");
  }
  removeFadeStyles() {
    const slides = this.carousel.getSlides();
    slides.forEach((slide) => {
      slide.classList.remove(...this.getAllFadeClasses());
      slide.removeAttribute("inert");
      slide.removeAttribute("aria-hidden");
    });
  }
  getAllFadeClasses() {
    return [...this.fadeClasses.base, ...this.fadeClasses.active, ...this.fadeClasses.inactive];
  }
  calculateHeight() {
    if (this.heightCalculated || !this.isEnabled || this.isCalculating) {
      return;
    }
    const slides = this.carousel.getSlides();
    const scrollContainer = this.carousel.scrollContainer;
    if (slides.length === 0 || !scrollContainer) {
      return;
    }
    this.isCalculating = true;
    scrollContainer.style.removeProperty("height");
    this.carousel.style.removeProperty("--carousel-height");
    const originalStyles = slides.map((slide) => {
      const element = slide;
      const original = {
        position: element.style.position,
        opacity: element.style.opacity,
        zIndex: element.style.zIndex,
        transform: element.style.transform,
        classes: element.className
      };
      element.classList.remove(...this.getAllFadeClasses());
      element.classList.add("relative", "opacity-1", "z-auto", "transform-none");
      return original;
    });
    requestAnimationFrame(() => {
      if (!this.isEnabled) {
        this.isCalculating = false;
        return;
      }
      const heights = slides.map((slide) => slide.getBoundingClientRect().height);
      const maxHeight = Math.max(...heights);
      slides.forEach((slide, index) => {
        const element = slide;
        const original = originalStyles[index];
        element.className = original.classes;
        ["position", "opacity", "zIndex", "transform"].forEach((prop) => {
          const value = original[prop];
          if (value) {
            element.style.setProperty(prop, value);
          } else {
            element.style.removeProperty(prop);
          }
        });
      });
      if (maxHeight > 0) {
        this.carousel.style.setProperty("--carousel-height", `${maxHeight}px`);
        scrollContainer.style.height = `${maxHeight}px`;
        this.heightCalculated = true;
        this.updateActiveSlide(this.carousel.activeSlide);
      }
      this.isCalculating = false;
    });
  }
  recalculateHeight() {
    if (!this.isEnabled) return;
    if (this.resizeTimeout) {
      clearTimeout(this.resizeTimeout);
      this.resizeTimeout = void 0;
    }
    this.heightCalculated = false;
    this.calculateHeight();
  }
};

export {
  FadeController
};
