(function () {
    'use strict';

    /**
     * Monthly Horoscope Frontend Script
     * Handles any client-side interactivity for the monthly horoscope block
     */

    /**
     * Initialize smooth scroll for anchor links within the horoscope
     */
    function initSmoothScroll() {
        var blocks = document.querySelectorAll('.ez-monthly-horoscope-block');
        
        blocks.forEach(function (block) {
            var links = block.querySelectorAll('a[href^="#"]');
            
            links.forEach(function (link) {
                link.addEventListener('click', function (e) {
                    var targetId = this.getAttribute('href').slice(1);
                    var target = document.getElementById(targetId);
                    
                    if (target) {
                        e.preventDefault();
                        target.scrollIntoView({
                            behavior: 'smooth',
                            block: 'start'
                        });
                    }
                });
            });
        });
    }

    /**
     * Add active state styling to expandable sections if any
     */
    function initExpandableSections() {
        var blocks = document.querySelectorAll('.ez-monthly-horoscope-block');
        
        blocks.forEach(function (block) {
            var focusItems = block.querySelectorAll('.ez-monthly-focus');
            
            focusItems.forEach(function (item) {
                var title = item.querySelector('.ez-monthly-focus-title');
                
                if (title) {
                    title.style.cursor = 'pointer';
                    
                    title.addEventListener('click', function () {
                        var content = item.querySelector('.ez-monthly-focus-content');
                        
                        if (content) {
                            var isExpanded = item.classList.contains('expanded');
                            
                            // Close all other items first
                            focusItems.forEach(function (otherItem) {
                                otherItem.classList.remove('expanded');
                                var otherContent = otherItem.querySelector('.ez-monthly-focus-content');
                                if (otherContent) {
                                    otherContent.style.maxHeight = null;
                                }
                            });
                            
                            // Toggle current item
                            if (!isExpanded) {
                                item.classList.add('expanded');
                                content.style.maxHeight = content.scrollHeight + 'px';
                            }
                        }
                    });
                }
            });
        });
    }

    /**
     * Lazy load images within the horoscope block
     */
    function initLazyLoading() {
        if ('IntersectionObserver' in window) {
            var imageObserver = new IntersectionObserver(function (entries, observer) {
                entries.forEach(function (entry) {
                    if (entry.isIntersecting) {
                        var img = entry.target;
                        if (img.dataset.src) {
                            img.src = img.dataset.src;
                            img.removeAttribute('data-src');
                        }
                        observer.unobserve(img);
                    }
                });
            });

            var lazyImages = document.querySelectorAll('.ez-monthly-horoscope-block img[data-src]');
            lazyImages.forEach(function (img) {
                imageObserver.observe(img);
            });
        }
    }

    /**
     * Process all blocks on the page
     */
    function processAllBlocks() {
        var blocks = document.querySelectorAll('.ez-monthly-horoscope-block');
        
        blocks.forEach(function (block) {
            // Skip if already processed
            if (block.getAttribute('data-processed') === '1') {
                return;
            }
            
            // Mark as processed
            block.setAttribute('data-processed', '1');
        });
    }

    /**
     * Initialize - run immediately and set up observer for dynamically loaded content
     */
    function init() {
        // Process any existing blocks
        processAllBlocks();
        initSmoothScroll();
        initLazyLoading();

        // Set up MutationObserver to catch dynamically loaded content
        var observer = new MutationObserver(function (mutations) {
            var shouldProcess = false;

            mutations.forEach(function (mutation) {
                if (mutation.addedNodes.length > 0) {
                    mutation.addedNodes.forEach(function (node) {
                        if (node.nodeType === 1) {
                            if (node.classList && node.classList.contains('ez-monthly-horoscope-block')) {
                                shouldProcess = true;
                            } else if (node.querySelector && node.querySelector('.ez-monthly-horoscope-block')) {
                                shouldProcess = true;
                            }
                        }
                    });
                }
            });

            if (shouldProcess) {
                setTimeout(function () {
                    processAllBlocks();
                    initSmoothScroll();
                    initLazyLoading();
                }, 100);
            }
        });

        // Observe the entire document for changes
        observer.observe(document.body, {
            childList: true,
            subtree: true
        });
    }

    // Run on DOM ready
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', init);
    } else {
        init();
    }
})();