import { useCallback, useMemo } from "react";
// Cache for simplified profiles converted from complex profiles
const simplifiedProfileCache = new Map();
// Cache for raw profile JSON strings (for backward compatibility)
const rawProfileCache = new Map();
let cacheStats = {
    simplifiedCacheSize: 0,
    rawCacheSize: 0,
    simplifiedHits: 0,
    simplifiedMisses: 0,
    rawHits: 0,
    rawMisses: 0,
};
/**
 * Convert complex profile to simplified profile
 */
const convertToSimplifiedProfile = (profile) => {
    if (!profile) {
        return {
            marks: ["bold", "italic"],
            blocks: ["paragraph"],
            alignment: [],
            lists: false,
            links: false,
        };
    }
    // Build arrays separately to avoid readonly conflicts
    const marks = [];
    const blocks = [];
    const alignment = [];
    let lists = false;
    let links = false;
    // Extract capabilities from the profile groups
    profile.toolbar.groups.forEach((group) => {
        group.options.forEach((option) => {
            switch (option.type) {
                case "mark":
                    if (!marks.includes(option.id)) {
                        marks.push(option.id);
                    }
                    break;
                case "block":
                    if (!blocks.includes(option.id)) {
                        blocks.push(option.id);
                    }
                    break;
                case "alignment":
                    if (!alignment.includes(option.id)) {
                        alignment.push(option.id);
                    }
                    break;
                case "list":
                    lists = true;
                    break;
                case "link":
                    links = true;
                    break;
            }
        });
    });
    // Ensure we have at least one block type
    if (blocks.length === 0) {
        blocks.push("paragraph");
    }
    // Construct the final object
    return {
        marks,
        blocks,
        alignment,
        lists,
        links,
    };
};
/**
 * Get a simplified profile from cache or convert and cache it
 */
export function getCachedSimplifiedProfile(profile) {
    const cacheKey = profile ? JSON.stringify(profile) : "null";
    if (simplifiedProfileCache.has(cacheKey)) {
        cacheStats.simplifiedHits++;
        return simplifiedProfileCache.get(cacheKey);
    }
    cacheStats.simplifiedMisses++;
    const simplifiedProfile = convertToSimplifiedProfile(profile);
    simplifiedProfileCache.set(cacheKey, simplifiedProfile);
    cacheStats.simplifiedCacheSize = simplifiedProfileCache.size;
    return simplifiedProfile;
}
/**
 * Get a parsed profile from cache or parse and cache it (backward compatibility)
 */
export function getCachedProfile(profileJson) {
    if (rawProfileCache.has(profileJson)) {
        cacheStats.rawHits++;
        return rawProfileCache.get(profileJson);
    }
    cacheStats.rawMisses++;
    const parsedProfile = JSON.parse(profileJson);
    rawProfileCache.set(profileJson, parsedProfile);
    cacheStats.rawCacheSize = rawProfileCache.size;
    return parsedProfile;
}
/**
 * Clear all profile caches
 */
export function clearProfileCache() {
    simplifiedProfileCache.clear();
    rawProfileCache.clear();
    cacheStats = {
        simplifiedCacheSize: 0,
        rawCacheSize: 0,
        simplifiedHits: 0,
        simplifiedMisses: 0,
        rawHits: 0,
        rawMisses: 0,
    };
}
/**
 * Remove a specific profile from cache
 */
export function evictProfileFromCache(profileKey) {
    const simplifiedEvicted = simplifiedProfileCache.delete(profileKey);
    const rawEvicted = rawProfileCache.delete(profileKey);
    if (simplifiedEvicted) {
        cacheStats.simplifiedCacheSize = simplifiedProfileCache.size;
    }
    if (rawEvicted) {
        cacheStats.rawCacheSize = rawProfileCache.size;
    }
    return simplifiedEvicted || rawEvicted;
}
/**
 * Check if a profile is cached without accessing it
 */
export function isProfileCached(profileKey) {
    return (simplifiedProfileCache.has(profileKey) || rawProfileCache.has(profileKey));
}
/**
 * Get all cached profile keys (useful for debugging)
 */
export function getCachedProfileKeys() {
    const simplifiedKeys = Array.from(simplifiedProfileCache.keys());
    const rawKeys = Array.from(rawProfileCache.keys());
    return [...new Set([...simplifiedKeys, ...rawKeys])];
}
/**
 * Get cache statistics
 */
export function getProfileCacheStats() {
    return { ...cacheStats };
}
/**
 * React hook for managing profile cache
 * This provides memoized cache functions for React components
 */
export function useProfileCache() {
    const getCachedSimplifiedProfileMemo = useCallback((profile) => {
        return getCachedSimplifiedProfile(profile);
    }, []);
    const getCachedProfileMemo = useCallback((profileJson) => {
        return getCachedProfile(profileJson);
    }, []);
    const clearCacheMemo = useCallback(() => {
        clearProfileCache();
    }, []);
    const evictProfileMemo = useCallback((profileKey) => {
        return evictProfileFromCache(profileKey);
    }, []);
    const isProfileCachedMemo = useCallback((profileKey) => {
        return isProfileCached(profileKey);
    }, []);
    const getCachedKeysMemo = useCallback(() => {
        return getCachedProfileKeys();
    }, []);
    const getStatsMemo = useCallback(() => {
        return getProfileCacheStats();
    }, []);
    return useMemo(() => ({
        getCachedSimplifiedProfile: getCachedSimplifiedProfileMemo,
        getCachedProfile: getCachedProfileMemo,
        clearCache: clearCacheMemo,
        evictProfile: evictProfileMemo,
        isProfileCached: isProfileCachedMemo,
        getCachedKeys: getCachedKeysMemo,
        getStats: getStatsMemo,
    }), [
        getCachedSimplifiedProfileMemo,
        getCachedProfileMemo,
        clearCacheMemo,
        evictProfileMemo,
        isProfileCachedMemo,
        getCachedKeysMemo,
        getStatsMemo,
    ]);
}
/**
 * Simple hook that just returns a cached simplified profile
 * Use this when you only need to get a simplified profile and don't need cache management
 */
export function useCachedSimplifiedProfile(profile) {
    return useMemo(() => {
        return getCachedSimplifiedProfile(profile);
    }, [profile]);
}
//# sourceMappingURL=useProfileCache.js.map