import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root'
})
export class UiAnimationsService {

  /**
   * Add animation class to element with optional completion callback
   */
  animate(element: HTMLElement, animationClass: string, onComplete?: () => void): Promise<void> {
    return new Promise((resolve) => {
      const handleAnimationEnd = () => {
        element.removeEventListener('animationend', handleAnimationEnd);
        if (onComplete) onComplete();
        resolve();
      };

      element.addEventListener('animationend', handleAnimationEnd);
      element.classList.add(animationClass);
    });
  }

  /**
   * Remove animation class from element
   */
  removeAnimation(element: HTMLElement, animationClass: string): void {
    element.classList.remove(animationClass);
  }

  /**
   * Add animation class and automatically remove it after completion
   */
  animateOnce(element: HTMLElement, animationClass: string): Promise<void> {
    return this.animate(element, animationClass, () => {
      this.removeAnimation(element, animationClass);
    });
  }

  /**
   * Check if element has animation class
   */
  hasAnimation(element: HTMLElement, animationClass: string): boolean {
    return element.classList.contains(animationClass);
  }
}