/**
 * Theme management functions for AstroManager
 */

// Set the application theme
window.setTheme = function (theme, isDark) {
    // Hide body to prevent FOUC (Flash of Unstyled Content)
    document.getElementsByTagName('body')[0].style.display = 'none';

    // Update Syncfusion theme CSS link
    let syncLink = document.getElementById('syncfusion-theme');
    if (!syncLink) {
        // Create the link if it doesn't exist
        syncLink = document.createElement('link');
        syncLink.id = 'syncfusion-theme';
        syncLink.rel = 'stylesheet';
        document.head.appendChild(syncLink);
    }
    syncLink.href = '_content/Syncfusion.Blazor.Themes/' + theme + '.css';

    // Update HTML tag class for global theming
    const htmlElement = document.documentElement;
    if (isDark) {
        htmlElement.classList.add('dark');
        htmlElement.classList.remove('light');
    } else {
        htmlElement.classList.add('light');
        htmlElement.classList.remove('dark');
    }

    // Show body after a short delay to ensure theme is applied
    setTimeout(function () {
        document.getElementsByTagName('body')[0].style.display = 'block';
    }, 300);
}

// Initialize theme based on system preference or saved preference
window.initializeTheme = function (savedTheme) {
    const effectiveTheme = 'tailwind3-dark';
    window.setTheme(effectiveTheme, true);
    return effectiveTheme;
}
