// SPEC-597-EXEMPT: canonical mapping (npm-package↔framework, keyword catalogue)
// — must enumerate framework names by design; no detection logic to migrate.
// engine/detectors/frontend-perf-detector.ts — SPEC-057
// Detects frontend performance budget configs, web vitals dependency,
// SSR/SSG frameworks, design system tooling, and E2E test frameworks.
import { readFile } from 'node:fs/promises';
import { join } from 'node:path';
// ---------------------------------------------------------------------------
// Config file markers for performance budgets
// ---------------------------------------------------------------------------
const PERF_BUDGET_FILES = [
    'lighthouserc.json',
    '.lighthouserc.json',
    '.lighthouserc.js',
    '.lighthouserc.cjs',
    '.lighthouserc.yml',
    'lighthouserc.js',
    'budget.json',
];
// ---------------------------------------------------------------------------
// SSR / SSG / ISR frameworks
// ---------------------------------------------------------------------------
const SSR_FRAMEWORK_MAP = {
    next: 'nextjs',
    astro: 'astro',
    '@sveltejs/kit': 'sveltekit',
    nuxt: 'nuxt',
    'react-router': 'remix',
    '@remix-run/node': 'remix',
    '@builder.io/qwik': 'qwik',
    '@analogjs/platform': 'analog',
};
// ---------------------------------------------------------------------------
// E2E test frameworks
// ---------------------------------------------------------------------------
const E2E_DEPS = ['@playwright/test', 'playwright', 'cypress', 'webdriverio', '@wdio/cli'];
// ---------------------------------------------------------------------------
// Design system / component library markers
// ---------------------------------------------------------------------------
const DESIGN_SYSTEM_DEPS = [
    '@storybook/react',
    '@storybook/vue3',
    '@storybook/angular',
    '@storybook/svelte',
    '@storybook/web-components',
    'storybook',
    '@ladle/react',
    '@histoire/plugin-vue',
];
const STORYBOOK_MARKERS = [
    '@storybook/react',
    '@storybook/vue3',
    '@storybook/angular',
    '@storybook/svelte',
    '@storybook/web-components',
    'storybook',
];
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
async function fileExists(filePath) {
    try {
        await readFile(filePath, 'utf-8');
        return true;
    }
    catch {
        return false;
    }
}
async function collectDeps(projectPath) {
    const deps = new Set();
    try {
        const content = await readFile(join(projectPath, 'package.json'), 'utf-8');
        const pkg = JSON.parse(content);
        for (const dep of Object.keys(pkg.dependencies ?? {})) {
            deps.add(dep);
        }
        for (const dep of Object.keys(pkg.devDependencies ?? {})) {
            deps.add(dep);
        }
    }
    catch {
        // No package.json — deps remain empty
    }
    return deps;
}
// ---------------------------------------------------------------------------
// Detection functions
// ---------------------------------------------------------------------------
async function detectPerformanceBudget(projectPath, deps) {
    let hasPerformanceBudget = false;
    for (const file of PERF_BUDGET_FILES) {
        if (await fileExists(join(projectPath, file))) {
            hasPerformanceBudget = true;
            break;
        }
    }
    // Also check for @lhci/cli or lighthouse-ci in deps
    if (deps.has('@lhci/cli') || deps.has('lighthouse-ci')) {
        hasPerformanceBudget = true;
    }
    const hasWebVitals = deps.has('web-vitals');
    return { hasPerformanceBudget, hasWebVitals };
}
function detectSsrFramework(deps) {
    for (const [dep, framework] of Object.entries(SSR_FRAMEWORK_MAP)) {
        if (deps.has(dep)) {
            return { hasSsr: true, ssrFramework: framework };
        }
    }
    return { hasSsr: false, ssrFramework: null };
}
function detectDesignSystem(deps) {
    const hasDesignSystem = DESIGN_SYSTEM_DEPS.some((d) => deps.has(d));
    const hasStorybook = STORYBOOK_MARKERS.some((d) => deps.has(d));
    return { hasDesignSystem, hasStorybook };
}
function detectE2eFramework(deps) {
    return E2E_DEPS.some((d) => deps.has(d));
}
// ---------------------------------------------------------------------------
// Main export
// ---------------------------------------------------------------------------
/**
 * SPEC-057 S1: Detect frontend performance-related configuration in a project.
 * Returns a result object indicating which performance-related tools and configs are present.
 */
export async function detectFrontendPerf(projectPath) {
    const deps = await collectDeps(projectPath);
    const { hasPerformanceBudget, hasWebVitals } = await detectPerformanceBudget(projectPath, deps);
    const { hasSsr, ssrFramework } = detectSsrFramework(deps);
    const { hasDesignSystem, hasStorybook } = detectDesignSystem(deps);
    const hasE2eFramework = detectE2eFramework(deps);
    return {
        hasPerformanceBudget,
        hasWebVitals,
        hasSsr,
        ssrFramework,
        hasDesignSystem,
        hasE2eFramework,
        hasStorybook,
        hasMfe: false, // Populated by caller from mfeDetection on ProjectKnowledge
    };
}
//# sourceMappingURL=frontend-perf-detector.js.map