(function () {
    'use strict';

    function initAuroraEffect() {
        try {
            var containers = document.querySelectorAll('.eb-aurora-container:not([data-aurora-initialized])');
            containers.forEach(function (container) {
                try {
                    container.setAttribute('data-aurora-initialized', 'true');
                    var textElement = container.querySelector('.eb-aurora-text');
                    if (!textElement) return;

                    var fullText = textElement.getAttribute('data-text') || '';
                    if (!fullText) return;

                    var containerId = 'aurora-' + Math.random().toString(36).substr(2, 9);
                    container.setAttribute('data-aurora-id', containerId);
                    textElement.textContent = '';
                    container.setAttribute('data-aurora-index', '0');

                    setTimeout(function () { typeChar(containerId); }, 10);
                } catch (e) { console.error('[Aurora]', e); }
            });
        } catch (e) { console.error('[Aurora]', e); }
    }

    function typeChar(containerId) {
        try {
            var container = document.querySelector('[data-aurora-id="' + containerId + '"]');
            if (!container) return;

            var textEl = container.querySelector('.eb-aurora-text');
            if (!textEl) return;

            var index = parseInt(container.getAttribute('data-aurora-index') || '0', 10) || 0;
            var text = textEl.getAttribute('data-text') || '';

            if (index < text.length) {
                var char = text.charAt(index);
                textEl.textContent = text.substring(0, index + 1);
                container.setAttribute('data-aurora-index', String(index + 1));

                var delay = 25 + Math.random() * 20;
                if ('，。：！？,.：!?'.indexOf(char) !== -1) delay = 200;

                setTimeout(function () { typeChar(containerId); }, delay);
            } else {
                container.classList.add('finished');
                var status = container.querySelector('.eb-aurora-status-text');
                if (status) status.textContent = container.getAttribute('data-finished') || 'Summary Generated';
            }
        } catch (e) { console.error('[Aurora]', e); }
    }

    // 初始执行
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', initAuroraEffect);
    } else {
        initAuroraEffect();
    }

    // DOM 变化监听
    var timer = null;
    try {
        var observer = new MutationObserver(function () {
            clearTimeout(timer);
            timer = setTimeout(function () {
                if (document.querySelectorAll('.eb-aurora-container:not([data-aurora-initialized])').length) {
                    initAuroraEffect();
                }
            }, 100);
        });
        if (document.body) {
            observer.observe(document.body, { childList: true, subtree: true });
        } else {
            document.addEventListener('DOMContentLoaded', function () {
                observer.observe(document.body, { childList: true, subtree: true });
            });
        }
    } catch (e) { console.error('[Aurora]', e); }
})();
