/**
 * Utility for caching font data (base64) to avoid repeated downloads.
 * Used primarily by the PDF generator.
 */

const fontCache = new Map();

/**
 * Caches font data.
 * @param {string} fontName - The name of the font
 * @param {string} style - Font style (normal, bold, italic, etc.)
 * @param {string} data - Base64 encoded font data
 */
export const cacheFont = (fontName, style, data) => {
    const key = `${fontName}-${style}`;
    fontCache.set(key, {
        data,
        timestamp: Date.now()
    });
    console.log(`Font cached: ${key}`);
};

/**
 * Retrieves cached font data.
 * @param {string} fontName 
 * @param {string} style 
 * @returns {string|null} Base64 data or null if not found
 */
export const getCachedFont = (fontName, style = 'normal') => {
    const key = `${fontName}-${style}`;
    const cached = fontCache.get(key);
    if (cached) {
        return cached.data;
    }
    return null;
};

/**
 * Checks if a font is cached.
 * @param {string} fontName 
 * @param {string} style 
 */
export const isFontCached = (fontName, style = 'normal') => {
    return fontCache.has(`${fontName}-${style}`);
};

/**
 * Clears the entire font cache.
 */
export const clearFontCache = () => {
    fontCache.clear();
    console.log('Font cache cleared');
};

/**
 * Returns a list of all currently cached keys.
 */
export const getAllCachedFonts = () => {
    return Array.from(fontCache.keys());
};