"use strict";
/**
 * Framework Analysis Orchestrator
 *
 * Runs applicable framework security models against each file,
 * returning per-file framework analysis results (safe/unsafe line annotations).
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.analyzeFrameworkPatterns = analyzeFrameworkPatterns;
const react_1 = require("./react");
const nextjs_1 = require("./nextjs");
const express_1 = require("./express");
const django_1 = require("./django");
const sequelize_1 = require("./sequelize");
const prisma_1 = require("./prisma");
/**
 * Select applicable framework models based on project context.
 */
function selectModels(projectContext) {
    const models = [];
    if (projectContext.frameworks.frontend === 'react') {
        models.push(react_1.reactModel);
    }
    if (projectContext.frameworks.primary === 'nextjs') {
        models.push(nextjs_1.nextjsModel);
        // Next.js uses React — ensure React model is included
        if (!models.some(m => m.framework === 'react')) {
            models.push(react_1.reactModel);
        }
    }
    if (projectContext.frameworks.primary === 'express') {
        models.push(express_1.expressModel);
    }
    if (projectContext.frameworks.primary === 'django') {
        models.push(django_1.djangoModel);
    }
    // ORM-specific models
    if (projectContext.dataAccess.orm === 'sequelize') {
        models.push(sequelize_1.sequelizeModel);
    }
    if (projectContext.dataAccess.orm === 'prisma') {
        models.push(prisma_1.prismaModel);
    }
    return models;
}
/**
 * Analyze framework patterns across all files.
 *
 * For each file, runs all applicable framework models against each line.
 * Returns a Map of file path → FileFrameworkAnalysis.
 *
 * Only files with at least one match are included in the result.
 */
function analyzeFrameworkPatterns(files, projectContext) {
    const models = selectModels(projectContext);
    if (models.length === 0)
        return new Map();
    const results = new Map();
    for (const file of files) {
        const safeLines = new Map();
        const unsafeLines = new Map();
        const lines = file.content.split('\n');
        for (let i = 0; i < lines.length; i++) {
            const lineContent = lines[i];
            const lineNumber = i + 1;
            for (const model of models) {
                for (const safe of model.safePatterns) {
                    if (safe.detect(lineContent, file.path, file.content)) {
                        const existing = safeLines.get(lineNumber);
                        if (existing) {
                            // Avoid duplicate IDs on the same line
                            if (!existing.some(p => p.id === safe.id)) {
                                existing.push(safe);
                            }
                        }
                        else {
                            safeLines.set(lineNumber, [safe]);
                        }
                    }
                }
                for (const unsafe of model.unsafePatterns) {
                    if (unsafe.detect(lineContent, file.path, file.content)) {
                        const existing = unsafeLines.get(lineNumber);
                        if (existing) {
                            if (!existing.some(p => p.id === unsafe.id)) {
                                existing.push(unsafe);
                            }
                        }
                        else {
                            unsafeLines.set(lineNumber, [unsafe]);
                        }
                    }
                }
            }
        }
        if (safeLines.size > 0 || unsafeLines.size > 0) {
            results.set(file.path, { safeLines, unsafeLines });
        }
    }
    return results;
}
//# sourceMappingURL=index.js.map