// ==========================================
// Debug Modal — Badge Component
// ==========================================
// Bug badge that shows error count. Subscribes to engine
// for reactive updates.
//
// v0.37.18: Preact rebuild from debug.js _updateBadge().

const html = window.html;
const { useEffect } = window.hooks;

/**
 * Updates the sidebar .avatar-bug element with current error count.
 * Not a visible Preact component — just a side-effect hook.
 *
 * @param {{ engine: object }} props
 */
export function DebugBadge({ engine }) {
    useEffect(() => {
        function update() {
            const bug = document.querySelector('.avatar-bug');
            if (!bug) return;
            if (engine.errorCount > 0) {
                bug.title = `${engine.errorCount} error${engine.errorCount > 1 ? 's' : ''} \u2014 click for Debug Log`;
                bug.classList.add('has-errors');
            } else {
                bug.title = 'Debug available';
                bug.classList.remove('has-errors');
            }
        }

        update(); // initial
        return engine.subscribe(update);
    }, [engine]);

    return null; // invisible — side-effect only
}
