// Cache for generated contexts to avoid repeated API calls
const contextCache = new Map();
const CONTEXT_CACHE_DURATION = 5 * 60 * 1000; // 5 minutes
/**
 * Generates a concise page abstract for use in completion prompts
 * @param editContext - The current edit context
 * @param pageViewContext - The page view context
 * @returns Promise<PageContext> - The generated page context
 */
export async function generatePageContext(editContext, pageViewContext) {
    if (!editContext.item) {
        return null;
    }
    // Only generate context if completions are enabled
    if (!editContext.enableCompletions) {
        return null;
    }
    const cacheKey = `${editContext.item.id}-${editContext.item.language}-${editContext.item.version}`;
    // Check cache first
    const cached = contextCache.get(cacheKey);
    if (cached &&
        Date.now() - cached.lastGenerated.getTime() < CONTEXT_CACHE_DURATION) {
        return cached;
    }
    try {
        const pageId = editContext.item.id;
        const language = editContext.item.language || "en";
        const endpoint = `/parhelia/agent/GetPageSummary?pageId=${encodeURIComponent(pageId)}&language=${encodeURIComponent(language)}`;
        const response = await fetch(endpoint);
        if (!response.ok) {
            throw new Error(`Failed to get page summary: ${response.statusText}`);
        }
        const data = await response.json();
        const pageContext = {
            abstract: data.pageSummary?.substring(0, 2000) || "", // Ensure we don't exceed reasonable length
            pageTitle: editContext.item.name || "Page",
            pageType: editContext.item.templateName || "Content",
            lastGenerated: new Date(),
        };
        // Cache the result
        contextCache.set(cacheKey, pageContext);
        return pageContext;
    }
    catch (error) {
        console.error("Error generating page context:", error);
        // Return a fallback context
        const fallbackContext = {
            abstract: "This page contains content that can be edited and enhanced with AI assistance.",
            pageTitle: editContext.item.name || "Page",
            pageType: editContext.item.templateName || "Content",
            lastGenerated: new Date(),
        };
        return fallbackContext;
    }
}
/**
 * Clears the context cache for a specific item or all items
 * @param itemKey - Optional specific item key to clear, if not provided clears all
 */
export function clearContextCache(itemKey) {
    if (itemKey) {
        contextCache.delete(itemKey);
    }
    else {
        contextCache.clear();
    }
}
/**
 * Gets a cached context without regenerating it
 * @param editContext - The current edit context
 * @returns PageContext | null - The cached context if available
 */
export function getCachedContext(editContext) {
    if (!editContext.item) {
        return null;
    }
    const cacheKey = `${editContext.item.id}-${editContext.item.language}-${editContext.item.version}`;
    const cached = contextCache.get(cacheKey);
    if (cached &&
        Date.now() - cached.lastGenerated.getTime() < CONTEXT_CACHE_DURATION) {
        return cached;
    }
    return null;
}
//# sourceMappingURL=contextService.js.map