<?php
namespace Sifency\Includes;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
final class Sifency_Addons_Elementor_Widgets_Asset_Manager {
	private static $enqueued = array();
	public static function init() {
		add_action( 'elementor/frontend/before_render', array( __CLASS__, 'scan_elements_for_assets' ) );
	}
	public static function scan_elements_for_assets( $element ) {
		$settings = $element->get_settings();
		if ( isset( $settings['sifency_animation_type'] ) && ! empty( $settings['sifency_animation_type'] ) ) {
			self::sifency_enqueue_animation_library( $settings['sifency_animation_type'] );
		}
		if ( $element->get_children() ) {
			foreach ( $element->get_children() as $child_element ) {
				self::scan_elements_for_assets( $child_element );
			}
		}
	}
	private static function sifency_enqueue_animation_library( $library ) {
		if ( isset( self::$enqueued[ $library ] ) ) {
			return;
		}
		switch ( $library ) {
			case 'aos':
				wp_enqueue_style( 'sifency-aos-style' );
				wp_enqueue_script( 'sifency-aos-script' );
				wp_enqueue_script( 'sifency-frontend-script' );
				break;
			case 'gsap':
				wp_enqueue_script( 'gsap' );
				wp_enqueue_script( 'gsap-scrolltrigger' );
				wp_enqueue_script( 'sifency-frontend-script' );
				break;
			case 'lenis':
				wp_enqueue_script( 'lenis' );
				wp_enqueue_script( 'sifency-lenis' );
				break;
			case 'anime':
				wp_enqueue_script( 'sifency-anime-script' );
				wp_enqueue_script( 'sifency-frontend-script' );
				break;
			case 'barba':
				wp_enqueue_script( 'sifency-anime-script' ); // barba transitions use anime.js
				wp_enqueue_script( 'sifency-barba-script' );
				wp_enqueue_script( 'sifency-frontend-script' );
				break;
		}
		self::$enqueued[ $library ] = true;
	}

}
