import { mapSitecoreProfileJsonToSlate } from "./profileMapper";
// Single cache for parsed profiles
const parsedProfileCache = new Map();
// Cache for in-flight requests to prevent duplicate calls
const inflightRequests = new Map();
let serviceStats = {
    hits: 0,
    misses: 0,
    cacheSize: 0,
};
/**
 * Get a parsed profile with service-level caching
 * This combines service calls and parsing into a single cached operation
 */
export async function getCachedParsedProfile(profilePath, serviceCall) {
    //console.log(`[ProfileServiceCache] Getting parsed profile for path: ${profilePath}`);
    // Check if we already have a cached result
    if (parsedProfileCache.has(profilePath)) {
        serviceStats.hits++;
        const cachedResult = parsedProfileCache.get(profilePath);
        //console.log(`[ProfileServiceCache] Cache hit for ${profilePath}`);
        return cachedResult;
    }
    // Check if there's already an in-flight request for this profile
    if (inflightRequests.has(profilePath)) {
        serviceStats.hits++;
        //console.log(
        //  `[ProfileServiceCache] In-flight request found for ${profilePath}`,
        //);
        return inflightRequests.get(profilePath);
    }
    // Make the service call, parse, and cache the result
    serviceStats.misses++;
    // console.log(`[ProfileServiceCache] Making service call for ${profilePath}`);
    const servicePromise = serviceCall(profilePath)
        .then((response) => {
        //console.log(`[ProfileServiceCache] Service call completed for ${profilePath}`, response);
        let parsedProfile = null;
        if (response?.data) {
            const jsonString = JSON.stringify(response.data);
            //console.log(`[ProfileServiceCache] Parsing profile for ${profilePath}, JSON length: ${jsonString.length}`);
            parsedProfile = mapSitecoreProfileJsonToSlate(jsonString);
            if (parsedProfile) {
                //console.log(`[ProfileServiceCache] Successfully parsed profile for ${profilePath}`);
            }
            else {
                console.warn(`[ProfileServiceCache] Failed to parse profile for ${profilePath}`);
            }
        }
        // Cache the parsed result (even if null)
        parsedProfileCache.set(profilePath, parsedProfile);
        serviceStats.cacheSize = parsedProfileCache.size;
        // Remove from in-flight requests
        inflightRequests.delete(profilePath);
        //console.log(        `[ProfileServiceCache] Cached parsed profile for ${profilePath}, cache size: ${serviceStats.cacheSize}`,      );
        return parsedProfile;
    })
        .catch((error) => {
        console.error(`[ProfileServiceCache] Service call failed for ${profilePath}:`, error);
        // Remove from in-flight requests on error
        inflightRequests.delete(profilePath);
        throw error;
    });
    // Cache the promise to prevent duplicate requests
    inflightRequests.set(profilePath, servicePromise);
    return servicePromise;
}
//# sourceMappingURL=profileServiceCache.js.map