/**
 * Application initialization
 */

// Initialize when DOM is loaded
document.addEventListener('DOMContentLoaded', function () {
    // Check File System API availability and update UI
    initializeApiAvailability();
    
    setupEventListeners();
    initializeFileNavResizer();
    setupTocDepthSelector();
    startFileChangeMonitoring();
    
    // Show scratchpad in file tree on startup
    renderFileTree();

    // Always start with scratchpad selected and loaded
    openScratchpad();
    const scratchpadEl = document.querySelector(`.file-item[data-path="${SCRATCHPAD_ID}"]`);
    if (scratchpadEl) scratchpadEl.classList.add('active-file');

    // In server (HTTP) mode, fetch and render the current directory subtree.
    if (location.protocol === 'http:' || location.protocol === 'https:') {
        loadServerDirectory().catch((err) => {
            if (DEBUG) console.warn('Server directory load failed:', err);
        });
    }
});

/**
 * Initialize UI based on File System API availability
 */
function initializeApiAvailability() {
    const selectDirectoryBtn = document.getElementById('addDirectoryBtn');
    const welcomeHint = document.getElementById('welcome-hint');
    const welcomeFirefox = document.getElementById('welcome-firefox');
    
    if (!hasFileSystemAccess) {
        // Disable file system buttons in Firefox and other unsupported browsers
        if (selectDirectoryBtn) {
            selectDirectoryBtn.disabled = true;
            selectDirectoryBtn.title = 'File System API not supported in this browser';
        }
        // Show Firefox warning, hide normal hint
        if (welcomeHint) welcomeHint.classList.add('hidden');
        if (welcomeFirefox) welcomeFirefox.classList.remove('hidden');
        
    }
}
