/**
 * Unified Loader Utility
 * Manages the uniform loader throughout the application
 */

class LoaderManager {
  constructor() {
    this.loaderElement = null;
    this.hideTimer = null;
    this.isVisible = false;
  }

  setupSpinner() {
    if (!this.loaderElement) {
      return;
    }

    const spinner = this.loaderElement.querySelector(".loader-spinner");
    if (!spinner || spinner.dataset.ready === "true") {
      return;
    }

    spinner.innerHTML = `
      <span class="loader-dot" aria-hidden="true"></span>
      <span class="loader-dot" aria-hidden="true"></span>
      <span class="loader-dot" aria-hidden="true"></span>
      <span class="loader-dot" aria-hidden="true"></span>
    `;
    spinner.dataset.ready = "true";
  }

  /**
   * Initialize the loader manager
   */
  init() {
    // Wait for DOM to be ready
    if (document.readyState === "loading") {
      document.addEventListener("DOMContentLoaded", () => {
        this.loaderElement = document.getElementById("mainLoader");
        this.setupSpinner();
        this.isVisible = true;
      });
    } else {
      this.loaderElement = document.getElementById("mainLoader");
      this.setupSpinner();
      this.isVisible = true;
    }
  }

  /**
   * Show the loader
   */
  show() {
    if (this.loaderElement) {
      if (this.hideTimer) {
        clearTimeout(this.hideTimer);
        this.hideTimer = null;
      }

      this.loaderElement.classList.remove("fade-out");
      this.loaderElement.style.display = "flex";
      this.isVisible = true;
    }
  }

  /**
   * Hide the loader with fade-out animation
   * @param {number} delay - Delay before removing from DOM (in ms)
   */
  hide(delay = 1000) {
    if (this.loaderElement) {
      if (this.hideTimer) {
        clearTimeout(this.hideTimer);
      }

      this.loaderElement.classList.add("fade-out");
      this.isVisible = false;

      if (delay > 0) {
        this.hideTimer = setTimeout(() => {
          if (this.loaderElement && this.loaderElement.parentElement) {
            this.loaderElement.style.display = "none";
          }
          this.hideTimer = null;
        }, delay);
      }
    }
  }

  /**
   * Remove the loader from DOM
   */
  remove() {
    if (this.loaderElement && this.loaderElement.parentElement) {
      this.loaderElement.parentElement.removeChild(this.loaderElement);
      this.isVisible = false;
      if (this.hideTimer) {
        clearTimeout(this.hideTimer);
        this.hideTimer = null;
      }
    }
  }

  /**
   * Check if loader is visible
   * @returns {boolean}
   */
  isShowing() {
    return this.isVisible;
  }

  /**
   * Reset loader visibility
   */
  reset() {
    this.loaderElement = document.getElementById("mainLoader");
    this.setupSpinner();
    if (this.loaderElement) {
      this.loaderElement.classList.remove("fade-out");
      this.loaderElement.style.display = "flex";
      this.isVisible = true;
    }
  }
}

// Create global instance
const loaderManager = new LoaderManager();

// Auto-initialize when script loads
document.addEventListener("DOMContentLoaded", () => {
  loaderManager.init();
});

// Also initialize immediately if DOM is ready
if (document.readyState !== "loading") {
  loaderManager.init();
}
