// iWiFi Monitor - Site JavaScript

/**
 * Sync theme from app-container to html element for CSS access
 */
(function() {
    function syncTheme() {
        var c = document.querySelector('.app-container[data-theme]');
        if (c) {
            document.documentElement.setAttribute('data-theme', c.getAttribute('data-theme'));
        }
    }

    var observer = new MutationObserver(syncTheme);

    function init() {
        var c = document.querySelector('.app-container');
        if (c) {
            observer.observe(c, { attributes: true, attributeFilter: ['data-theme'] });
            syncTheme();
        } else {
            setTimeout(init, 50);
        }
    }

    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', init);
    } else {
        init();
    }
})();

/**
 * Downloads a file from base64 content
 * @param {string} fileName - The name of the file to download
 * @param {string} base64Content - The base64-encoded content
 * @param {string} mimeType - The MIME type of the file
 */
function downloadFile(fileName, base64Content, mimeType) {
    const byteCharacters = atob(base64Content);
    const byteNumbers = new Array(byteCharacters.length);
    for (let i = 0; i < byteCharacters.length; i++) {
        byteNumbers[i] = byteCharacters.charCodeAt(i);
    }
    const byteArray = new Uint8Array(byteNumbers);
    const blob = new Blob([byteArray], { type: mimeType });

    const link = document.createElement('a');
    link.href = URL.createObjectURL(blob);
    link.download = fileName;
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
    URL.revokeObjectURL(link.href);
}
