// ---------------------------------------------------------------------------
// Canonical doc mapping
// ---------------------------------------------------------------------------
// Canonical doc mapping: task description patterns -> expected doc slugs
// This maps what docs a well-informed agent *should* visit for each task
export const CANONICAL_DOC_MAP = {
    frameworks: [
        "remix",
        "nuxt",
        "svelte",
        "astro",
        "gatsby",
        "client-libraries",
    ],
    functions: [
        "functions",
        "webhooks",
        "groq-powered-webhooks",
        "event-driven",
        "automations",
    ],
    "nextjs-live": [
        "next-js",
        "live-content-api",
        "content-source-maps",
        "app-router",
        "groq",
        "client-libraries",
    ],
    "studio-setup": [
        "studio",
        "schema-types",
        "structure-builder",
        "configuration",
        "plugins",
    ],
    "visual-editing": [
        "visual-editing",
        "presentation",
        "preview",
        "overlays",
        "loaders",
    ],
};
// ---------------------------------------------------------------------------
// Feature area detection
// ---------------------------------------------------------------------------
export function detectFeatureArea(description) {
    const desc = description.toLowerCase();
    if (desc.includes("portable text"))
        return "portable-text";
    if (desc.includes("content lake"))
        return "content-lake";
    if (desc.includes("image handling") || desc.includes("image asset"))
        return "image-handling";
    if (desc.includes("studio"))
        return "studio-setup";
    if (desc.includes("visual") ||
        desc.includes("presentation") ||
        desc.includes("live preview"))
        return "visual-editing";
    if (desc.includes("groq"))
        return "groq";
    if (desc.includes("function") || desc.includes("webhook"))
        return "functions";
    if (desc.includes("next") || desc.includes("app router"))
        return "nextjs-live";
    if (desc.includes("remix") ||
        desc.includes("nuxt") ||
        desc.includes("svelte"))
        return "frameworks";
    return "other";
}
// ---------------------------------------------------------------------------
// Analysis
// ---------------------------------------------------------------------------
/**
 * Analyze pre-parsed test results for agent behavior patterns.
 * Caller is responsible for reading/parsing the file — this function is pure.
 */
export function analyzeResults(results) {
    const tasks = [];
    for (const result of results) {
        const metadata = result.metadata;
        if (!metadata?.agentBehaviorSummary)
            continue;
        const behavior = metadata.agentBehaviorSummary;
        tasks.push({
            behavior,
            description: result.description,
            feature: detectFeatureArea(result.description),
            hasDocs: !!(result.vars.docs && result.vars.docs.trim().length > 0),
        });
    }
    if (tasks.length === 0) {
        return { features: [], hasData: false, tasks: [] };
    }
    // Group by feature
    const byFeature = {};
    for (const t of tasks) {
        if (!byFeature[t.feature])
            byFeature[t.feature] = [];
        byFeature[t.feature].push(t);
    }
    const features = Object.entries(byFeature)
        .map(([feature, featureTasks]) => {
        const allDocSlugs = [
            ...new Set(featureTasks.flatMap((t) => t.behavior.docSlugsVisited)),
        ];
        const allSearchQueries = [
            ...new Set(featureTasks.flatMap((t) => t.behavior.uniqueSearchQueries)),
        ];
        const allExternalDomains = [
            ...new Set(featureTasks.flatMap((t) => t.behavior.externalDomains)),
        ];
        const canonicalSlugs = CANONICAL_DOC_MAP[feature] || [];
        const matchedCanonical = canonicalSlugs.filter((slug) => allDocSlugs.some((visited) => visited.includes(slug)));
        const canonicalCoverage = canonicalSlugs.length > 0
            ? matchedCanonical.length / canonicalSlugs.length
            : 0;
        const count = featureTasks.length || 1;
        return {
            allDocSlugs,
            allExternalDomains,
            allSearchQueries,
            avgDocPages: featureTasks.reduce((s, t) => s + t.behavior.docPagesVisited, 0) /
                count,
            avgNetworkMs: featureTasks.reduce((s, t) => s + t.behavior.totalNetworkMs, 0) /
                count,
            avgSearches: featureTasks.reduce((s, t) => s + t.behavior.searchesPerformed, 0) /
                count,
            canonicalCoverage,
            canonicalSlugs,
            feature,
            tasks: featureTasks,
        };
    })
        .sort((a, b) => a.feature.localeCompare(b.feature));
    return { features, hasData: true, tasks };
}
