import '../css/styles.css';

import { initApp } from './core/init.js';
import { Logger } from './core/logger.js';
import { CsrfAutoRefresh } from './modules/csrf-auto-refresh.js';
import { FormAutoSave } from './modules/form-autosave.js';
// WebPushManager is now loaded via framework module system
// LiveComponent is now loaded via framework module system

// Import DevTools in development
if (import.meta.env.DEV) {
    import('./modules/LiveComponentDevTools.js').then(() => {
        console.log('🛠️ LiveComponent DevTools loaded (Ctrl+Shift+D to toggle)');
    });
}

// Import Hot Reload in development
if (import.meta.env.DEV) {
    import('./modules/hot-reload.js').then(({ HotReload }) => {
        console.log('🔥 Hot Reload module loaded');
    });
}

// PWA Service Worker temporarily disabled
// TODO: Re-enable after fixing build issues

document.addEventListener("DOMContentLoaded", async () => {
    try {
        console.log('🚀 Starting app initialization...');
        await initApp();
        console.log('✅ App initialized successfully!');
        
        // Initialize CSRF Auto-Refresh for all forms
        const csrfInstances = CsrfAutoRefresh.initializeAll();
        console.log(`🔒 CSRF Auto-Refresh initialized for ${csrfInstances.length} forms`);
        
        // Initialize Form Auto-Save for all forms
        const autosaveInstances = FormAutoSave.initializeAll();
        console.log(`💾 Form Auto-Save initialized for ${autosaveInstances.length} forms`);

        // WebPush Manager is initialized by framework module system
        // Access via window.webPushManager after module initialization

        // Debug info
        setTimeout(() => {
            console.log('📊 Debug Info:');
            console.log('- Forms found:', document.querySelectorAll('form').length);
            console.log('- Links found:', document.querySelectorAll('a[href^="/"]').length);
            console.log('- SPA Router:', window.spaRouter ? 'Active' : 'Missing');
            console.log('- Enhanced forms:', document.querySelectorAll('form[data-auto-enhanced]').length);
            console.log('- CSRF protected forms:', document.querySelectorAll('input[name="_token"]').length);
        }, 500);
    } catch (error) {
        console.error('❌ App initialization failed:', error);
        console.error('Stack trace:', error.stack);
    }
});

function isHtmlAttributeSupported(elementName, attribute) {
    const element = document.createElement(elementName);
    return attribute in element;
}


/*let closedAttr = document.getElementById('my-dialog');
if(closedAttr && !('closedby' in closedAttr)) {
    alert('oh no');
}*/

/*
if (isHtmlAttributeSupported('dialog', 'closedby')) {
    alert('Attribut wird unterstützt!');
} else {
    alert('Nicht unterstützt!');
}
*/
