// ==========================================
// Debug Bootstrap (v0.37.18)
// ==========================================
// Thin entry point: initializes the debug engine (synchronous,
// captures early errors) then mounts the Preact debug modal
// after Preact globals are available.
//
// Replaces the 673-line imperative debug.js from v0.37.14.
// Engine: src/js/sw/components/debug/engine.js
// Modal:  src/js/sw/components/debug/index.js

import { debugEngine } from './sw/components/debug/engine.js';

// Initialize ASAP — before SDK boot so we capture everything.
debugEngine.init();

// Backward-compat globals (used by badge click, command palette, etc.)
window.DebugLog           = debugEngine;
window.openDebugModal     = () => window.dispatchEvent(new CustomEvent('debug:toggle'));
window.copyDebugLog       = () => debugEngine.copyLog();
window.exportDebugLog     = () => debugEngine.downloadLog();
window.runDebugDiagnostics = () => debugEngine.runDiagnostics();
window.purgeCache         = () => debugEngine.purgeCache();

// Mount Preact modal once the DOM + Preact globals are ready.
// Preact globals are set by the surface template's script block,
// which loads before this module (type=module is deferred).
function mountWhenReady() {
    if (!window.preact || !window.html || !window.hooks) {
        // Surface script hasn't loaded yet — retry on next tick.
        requestAnimationFrame(mountWhenReady);
        return;
    }
    import('./sw/components/debug/index.js').then(({ mountDebugModal }) => {
        mountDebugModal(document.getElementById('debugMount'));
    });
}
if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', mountWhenReady);
} else {
    mountWhenReady();
}
