(function () {
    setTimeout(function () {
        if (typeof window.React !== 'undefined' && typeof window.ReactDOM !== 'undefined') {
            return;
        }

        var appIds = [
            'xhtheme-ai-toolbox-wrapper',
            'xhtheme-ai-toolbox-queues-wrapper',
            'xhtheme-ai-toolbox-automate-wrapper',
            'xhtheme-ai-toolbox-wizard-wrapper',
            'xhtheme-ai-interact-wrapper'
        ];

        var targetDiv = null;
        for (var i = 0; i < appIds.length; i++) {
            var el = document.getElementById(appIds[i]);
            if (el) {
                targetDiv = el;
                break;
            }
        }

        if (!targetDiv) {
            return;
        }

        if (targetDiv.innerHTML && targetDiv.innerHTML.length > 1500) {
            console.log('[XHTheme AI] React has loaded successfully.');
            return;
        }

        console.error('[XHTheme AI] React critical failure detected.');

        if (typeof xhtheme_react_check_vars !== 'undefined') {
            var xhr = new XMLHttpRequest();
            xhr.open('POST', xhtheme_react_check_vars.ajax_url);
            xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            xhr.send('action=xhtheme_log_react_error&error=React_Core_Library_Load_Failed&_wpnonce=' + xhtheme_react_check_vars.nonce);
        }

        var showNotice = function () {
            if (document.getElementById('xhtheme-react-error')) {
                return;
            }

            targetDiv.innerHTML = '';

            var wizardError = document.createElement('div');
            wizardError.style.maxWidth = '600px';
            wizardError.style.width = '100%';
            wizardError.style.margin = '50px auto';
            wizardError.style.padding = '40px';
            wizardError.style.backgroundColor = '#fff';
            wizardError.style.border = '1px solid #e5e7eb';
            wizardError.style.borderRadius = '12px';
            wizardError.style.boxShadow = '0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06)';
            wizardError.style.textAlign = 'center';
            wizardError.style.boxSizing = 'border-box';
            wizardError.id = 'xhtheme-react-error';

            if (targetDiv.id === 'xhtheme-ai-toolbox-wizard-wrapper') {
                wizardError.style.margin = '0 auto';
                wizardError.style.boxShadow = '0 10px 25px -5px rgba(0, 0, 0, 0.1), 0 8px 10px -6px rgba(0, 0, 0, 0.1)';
            }

            var content = '<div style="margin-bottom:24px;color:#ef4444;"><svg style="width:64px;height:64px;margin:0 auto;" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"></path></svg></div>';
            content += '<h2 style="font-size:24px;line-height:32px;font-weight:700;color:#1f2937;margin:0 0 20px 0;">React 核心库加载失败</h2>';

            content += '<div style="text-align:center; max-width:520px; margin:0 auto 32px auto; color:#4b5563; font-size:15px; line-height:1.6;">';
            content += '<p style="margin-bottom:12px;">系统检测到 React 组件缺失，导致当前页面无法正常工作。</p>';
            content += '<p style="margin-bottom:12px;">React 已在 WordPress 后台广泛使用（如古腾堡编辑器、自定义、小工具、块模版编辑等）。您可以切换到<strong>文章发布页面</strong>验证编辑器是否工作正常。</p>';
            content += '<p>如果同样异常，请参考 <a href="https://www.xhtheme.com/docs/wordpress-react-fix" target="_blank" style="color:#2563eb;text-decoration:underline;">教程指引</a> 排查并解决服务器拦截问题。</p>';
            content += '</div>';

            content += '<div style="display:flex;justify-content:center;gap:16px;flex-wrap:wrap;">';

            if (targetDiv.id === 'xhtheme-ai-toolbox-wizard-wrapper') {
                content += '<a href="javascript:void(0)" onclick="location.reload()" style="display:inline-block;padding:12px 24px;background-color:#fff;border:1px solid #d1d5db;color:#374151;border-radius:8px;text-decoration:none;font-weight:500;transition:all 0.2s;font-size:15px;line-height:20px;">刷新重试</a>';
                content += '<a href="admin.php?page=xhtheme-ai-toolbox" style="display:inline-block;padding:12px 24px;background-color:#2563eb;color:#fff;border:1px solid #2563eb;border-radius:8px;text-decoration:none;font-weight:500;transition:all 0.2s;font-size:15px;line-height:20px;">退出引导</a>';
            } else {
                content += '<a href="https://www.xhtheme.com/docs/wordpress-react-fix" target="_blank" style="display:inline-block;padding:12px 24px;background-color:#fff;border:1px solid #d1d5db;color:#374151;border-radius:8px;text-decoration:none;font-weight:500;transition:all 0.2s;font-size:15px;line-height:20px;">查看解决方案</a>';
                content += '<a href="javascript:void(0)" onclick="location.reload()" style="display:inline-block;padding:12px 24px;background-color:#2563eb;color:#fff;border:1px solid #2563eb;border-radius:8px;text-decoration:none;font-weight:500;transition:all 0.2s;font-size:15px;line-height:20px;">刷新重试</a>';
            }
            content += '</div>';

            wizardError.innerHTML = content;

            targetDiv.style.display = 'flex';
            targetDiv.style.flexDirection = 'column';
            targetDiv.style.justifyContent = 'center';
            targetDiv.style.alignItems = 'center';

            if (targetDiv.id !== 'xhtheme-ai-toolbox-wizard-wrapper') {
                targetDiv.style.minHeight = '60vh';
            }

            targetDiv.appendChild(wizardError);
        };

        if (document.readyState === 'loading') {
            document.addEventListener('DOMContentLoaded', showNotice);
        } else {
            showNotice();
        }
    }, 2000);
})();
