/**
 * Changeloger Frontend Changelog JavaScript
 */
(function() {
    'use strict';

    // Prevent duplicate initialization
    if (window.chaChangelogInitialized) return;
    window.chaChangelogInitialized = true;

    // Initialize immediately if DOM ready, otherwise wait
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', init);
    } else {
        init();
    }

    function init() {
        initToggleButtons();
        initExcerptButtons();
    }

    /**
     * Initialize excerpt expand/collapse buttons and overflow detection
     */
    function initExcerptButtons() {
        // Check which wrappers actually overflow and need the truncation UI.
        checkExcerptOverflow();
        window.addEventListener('resize', debounce(checkExcerptOverflow, 200));

        // Handle click on Continue Reading button — expand and hide.
        document.addEventListener('click', function(e) {
            var button = e.target.closest('.cha-excerpt-read-more');
            if (!button) return;

            e.preventDefault();
            e.stopPropagation();

            var wrapper = button.closest('.cha-release-excerpt-wrapper');
            if (!wrapper) return;

            wrapper.classList.add('cha-expanded');
        });
    }

    /**
     * Check each excerpt wrapper for actual content overflow
     *
     * If the content fits within the max-height, hide the overlay and button.
     */
    function checkExcerptOverflow() {
        var wrappers = document.querySelectorAll('.cha-release-excerpt-wrapper');
        wrappers.forEach(function(wrapper) {
            // Skip already expanded wrappers.
            if (wrapper.classList.contains('cha-expanded')) return;

            var content = wrapper.querySelector('.cha-release-content');
            if (!content) return;

            // Compare content's natural height vs the wrapper's constrained height.
            if (content.scrollHeight <= wrapper.clientHeight) {
                wrapper.classList.add('cha-excerpt-no-overflow');
            } else {
                wrapper.classList.remove('cha-excerpt-no-overflow');
            }
        });
    }

    /**
     * Simple debounce utility
     *
     * @param {Function} fn   Function to debounce.
     * @param {number}   wait Delay in milliseconds.
     * @return {Function} Debounced function.
     */
    function debounce(fn, wait) {
        var timeout;
        return function() {
            clearTimeout(timeout);
            timeout = setTimeout(fn, wait);
        };
    }

    /**
     * Initialize toggle buttons for change items using event delegation
     */
    function initToggleButtons() {
        // Use capture phase to ensure we get the event first
        document.addEventListener('click', function(e) {
            const button = e.target.closest('.cha-change-toggle');
            if (!button) return;

            e.preventDefault();
            e.stopPropagation();

            const section = button.closest('.cha-change-section');
            if (!section) return;

            const items = section.querySelector('.cha-change-items');
            if (!items) return;

            const isExpanded = button.getAttribute('aria-expanded') === 'true';

            button.setAttribute('aria-expanded', !isExpanded);
            
            if (isExpanded) {
                items.setAttribute('hidden', '');
            } else {
                items.removeAttribute('hidden');
            }
        }, true); // Use capture phase
    }

})();
