/**
 * Sifency Frontend Script
 *
 * Handles frontend animation initialization (AOS, WOW)
 * and third-party UI components (Navbar, etc).
 *
 * @package Sifency
 * @since 1.0.4
 */

(function ($) {
    'use strict';

    const SifencyFrontend = {
        animationsInitialized: false,

        init() {
            this.initAnimations();
            this.bindElementorHooks();
        },

        bindElementorHooks() {
            $(window).on('elementor/frontend/init', () => {
                elementorFrontend.hooks.addAction('frontend/element_ready/sifency-product-short-description.default', ($scope) => {
                    this.handleContentWrapperAnimations($scope);
                });

            });
        },
        
        /**
         * This function is now more robust. It handles both cases:
         * Content with child elements (like <p> tags).
         * Content with only plain text (no tags).
         * @param {jQuery} $scope The scope of the widget being rendered.
         */
        handleContentWrapperAnimations($scope) {
            const wrapper = $scope[0].querySelector('.sifency-product-short-description[data-aos]');
            if (!wrapper) return;

            let children = Array.from(wrapper.children);
            const baseDelay = parseInt(wrapper.dataset.aosDelay, 10) || 0;
            const stagger = 100;

            if (children.length === 0 && wrapper.innerHTML.trim() !== '') {
                const content = wrapper.innerHTML;
                wrapper.innerHTML = '';
                const newContentWrapper = document.createElement('div');
                newContentWrapper.className = 'sifency-inner-content';
                newContentWrapper.innerHTML = content;
                wrapper.appendChild(newContentWrapper);
                children = [newContentWrapper];
            }
            
            if (children.length === 0) return;

            children.forEach((child, index) => {
                for (const attr in wrapper.dataset) {
                    if (attr.startsWith('aos')) {
                        child.dataset[attr] = wrapper.dataset[attr];
                    }
                }
                child.dataset.aosDelay = baseDelay + (index * stagger);
            });

            for (const attr in wrapper.dataset) {
                if (attr.startsWith('aos')) {
                    delete wrapper.dataset[attr];
                }
            }

            if (typeof AOS !== 'undefined') {
                AOS.refresh();
            }
        },

        /**
         * Initialize animation libraries (AOS and WOW).
         */
        initAnimations() {
            if (window.elementor && elementor.isEditMode()) {
                return;
            }
            if (this.animationsInitialized) {
                return;
            }

            // Initialize AOS
            if (typeof AOS !== 'undefined' && typeof AOS.init === 'function') {
                AOS.init();
            }
            
            if (typeof WOW !== 'undefined' && typeof WOW === 'function') {
                new WOW().init();
            }

            this.animationsInitialized = true;
        },
    };

    $(window).on('elementor/frontend/init', () => {
        SifencyFrontend.init();
    });

})(jQuery);