"use strict";
/**
 * Environment Context Utility
 * Understands file PURPOSE by path to reduce false positives
 *
 * This addresses false positives where localhost URLs in email templates,
 * dev scripts, and test environments are flagged as production issues.
 *
 * Key insight: The same code pattern may be fine in one context but
 * problematic in another. Context determines severity.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.getEnvironmentContext = getEnvironmentContext;
exports.isInPlaceholderAttribute = isInPlaceholderAttribute;
exports.isDefaultParameterValue = isDefaultParameterValue;
exports.isDevOnlyContext = isDevOnlyContext;
exports.allowsTestCredentials = allowsTestCredentials;
exports.allowsLocalhostUrls = allowsLocalhostUrls;
exports.getLocalhostSeverity = getLocalhostSeverity;
/**
 * Email template patterns - localhost is used for local preview
 */
const EMAIL_TEMPLATE_PATTERNS = [
    /packages\/email\//i,
    /templates\/email\//i,
    /email-templates?\//i,
    /mail-templates?\//i,
    /emails?\/(templates?|components?)\//i,
    /\/email\/.*\.(tsx?|jsx?)$/i,
    /react-email/i,
    /mjml/i,
];
/**
 * Test/testing directory patterns
 */
const TEST_PATTERNS = [
    /\/testing\//i,
    /\/test\//i,
    /\/tests\//i,
    /\/__tests__\//i,
    /\.test\./i,
    /\.spec\./i,
    /\/e2e\//i,
    /\/cypress\//i,
    /\/playwright\//i,
    /\/fixtures?\//i,
    /\/mocks?\//i,
    /testdata\//i,
    /test-data\//i,
    /\/stubs?\//i,
];
/**
 * Development scripts and tooling patterns
 */
const DEV_TOOLING_PATTERNS = [
    /scripts?\//i,
    /bin\//i,
    /tools?\//i,
    /cli\//i,
    /-dev\./i,
    /\.dev\./i,
    /dev-server/i,
    /dev-tools?\//i,
    /devtools?\//i,
    /\.sh$/i,
    /Makefile$/i,
    /docker-compose\.dev/i,
    /docker-compose\.local/i,
    /\.development\./i,
    /\.local\./i,
];
/**
 * Documentation and example patterns
 */
const DOCUMENTATION_PATTERNS = [
    /README/i,
    /\.md$/i,
    /\.mdx$/i,
    /\/docs\//i,
    /\/documentation\//i,
    /\/examples?\//i,
    /\/demos?\//i,
    /\/tutorials?\//i,
    /\/guides?\//i,
    /\/samples?\//i,
    /\/quickstart\//i,
];
/**
 * Get the environment context for a file
 *
 * @param filePath - The full file path
 * @returns EnvironmentContext with context information
 */
function getEnvironmentContext(filePath) {
    // Email templates - localhost for preview
    if (EMAIL_TEMPLATE_PATTERNS.some(pattern => pattern.test(filePath))) {
        return {
            fileType: 'template',
            allowsLocalhostDefaults: true,
            allowsPlaceholderUrls: true,
            allowsTestCredentials: false,
            reason: 'Email template file - localhost used for local preview',
        };
    }
    // Test/testing directories
    if (TEST_PATTERNS.some(pattern => pattern.test(filePath))) {
        return {
            fileType: 'test',
            allowsLocalhostDefaults: true,
            allowsPlaceholderUrls: true,
            allowsTestCredentials: true,
            reason: 'Test/testing file - mock data expected',
        };
    }
    // Dev scripts and tooling
    if (DEV_TOOLING_PATTERNS.some(pattern => pattern.test(filePath))) {
        return {
            fileType: 'tooling',
            allowsLocalhostDefaults: true,
            allowsPlaceholderUrls: true,
            allowsTestCredentials: false,
            reason: 'Development tooling file',
        };
    }
    // Documentation and examples
    if (DOCUMENTATION_PATTERNS.some(pattern => pattern.test(filePath))) {
        return {
            fileType: 'documentation',
            allowsLocalhostDefaults: true,
            allowsPlaceholderUrls: true,
            allowsTestCredentials: false,
            reason: 'Documentation or example file',
        };
    }
    // Development environment config files
    if (/\.env\.local$|\.env\.development$|\.env\.dev$/i.test(filePath)) {
        return {
            fileType: 'development',
            allowsLocalhostDefaults: true,
            allowsPlaceholderUrls: false,
            allowsTestCredentials: false,
            reason: 'Development environment config',
        };
    }
    // Default: production context
    return {
        fileType: 'production',
        allowsLocalhostDefaults: false,
        allowsPlaceholderUrls: false,
        allowsTestCredentials: false,
    };
}
/**
 * Check if a line contains a URL in a placeholder attribute context
 * e.g., placeholder="https://example.com"
 *
 * @param line - The line content to check
 * @returns true if the URL is in a placeholder attribute
 */
function isInPlaceholderAttribute(line) {
    // HTML/JSX placeholder attribute
    const placeholderPatterns = [
        /placeholder\s*=\s*["'][^"']*https?:\/\//i,
        /placeholder\s*=\s*\{["'][^"']*https?:\/\//i,
        /placeholder:\s*["'][^"']*https?:\/\//i,
        /helperText\s*=\s*["'][^"']*https?:\/\//i,
        /hint\s*=\s*["'][^"']*https?:\/\//i,
        /example\s*=\s*["'][^"']*https?:\/\//i,
    ];
    return placeholderPatterns.some(pattern => pattern.test(line));
}
/**
 * Check if a localhost URL is a default parameter value
 * e.g., function foo(url = 'http://localhost')
 *
 * @param line - The line content to check
 * @returns true if localhost is used as a default parameter
 */
function isDefaultParameterValue(line) {
    const defaultPatterns = [
        // Function parameter default: function foo(url = 'http://localhost')
        /function\s+\w+\s*\([^)]*=\s*['"]https?:\/\/(localhost|127\.0\.0\.1)/i,
        // Arrow function default
        /\([^)]*=\s*['"]https?:\/\/(localhost|127\.0\.0\.1)/i,
        // Variable with fallback: const url = process.env.X || 'http://localhost'
        /=\s*process\.env\.\w+\s*\|\|\s*['"]https?:\/\/(localhost|127\.0\.0\.1)/i,
        /=\s*import\.meta\.env\.\w+\s*\|\|\s*['"]https?:\/\/(localhost|127\.0\.0\.1)/i,
        // Nullish coalescing: process.env.X ?? 'http://localhost'
        /=\s*process\.env\.\w+\s*\?\?\s*['"]https?:\/\/(localhost|127\.0\.0\.1)/i,
        /=\s*import\.meta\.env\.\w+\s*\?\?\s*['"]https?:\/\/(localhost|127\.0\.0\.1)/i,
    ];
    return defaultPatterns.some(pattern => pattern.test(line));
}
/**
 * Check if a URL is in a clearly development-only context
 *
 * @param line - The line content
 * @param filePath - The file path
 * @returns true if the URL is in a dev-only context
 */
function isDevOnlyContext(line, filePath) {
    const context = getEnvironmentContext(filePath);
    // If the file type is not production, it's dev-only
    if (context.fileType !== 'production') {
        return true;
    }
    // Check for inline dev-only indicators
    const devOnlyPatterns = [
        /if\s*\(\s*process\.env\.NODE_ENV\s*[!=]==?\s*['"]development['"]/i,
        /if\s*\(\s*!?\s*production\s*\)/i,
        /if\s*\(\s*dev\s*\)/i,
        /if\s*\(\s*isDev\s*\)/i,
        /if\s*\(\s*isLocal\s*\)/i,
        /\/\/\s*(dev|development|local)\s*(only|mode)/i,
        /\*\s*(dev|development|local)\s*(only|mode)/i,
    ];
    return devOnlyPatterns.some(pattern => pattern.test(line));
}
/**
 * Check if a password/credential is acceptable in this context
 *
 * @param filePath - The file path
 * @returns true if test credentials are acceptable
 */
function allowsTestCredentials(filePath) {
    const context = getEnvironmentContext(filePath);
    return context.allowsTestCredentials;
}
/**
 * Check if localhost URLs are expected in this file
 *
 * @param filePath - The file path
 * @returns true if localhost is expected/acceptable
 */
function allowsLocalhostUrls(filePath) {
    const context = getEnvironmentContext(filePath);
    return context.allowsLocalhostDefaults;
}
/**
 * Determine severity for a localhost finding based on context
 *
 * @param filePath - The file path
 * @param line - The line content
 * @returns Recommended severity for the finding
 */
function getLocalhostSeverity(filePath, line) {
    const context = getEnvironmentContext(filePath);
    // Production env files with localhost = high severity
    if (/\.env\.prod|\.env\.production/i.test(filePath)) {
        return 'high';
    }
    // File context allows localhost = info
    if (context.allowsLocalhostDefaults) {
        return 'info';
    }
    // Default parameter / fallback pattern = info
    if (isDefaultParameterValue(line)) {
        return 'info';
    }
    // Placeholder attribute = info
    if (isInPlaceholderAttribute(line)) {
        return 'info';
    }
    // Dev-only code block = info
    if (isDevOnlyContext(line, filePath)) {
        return 'info';
    }
    // Default for production code = medium
    return 'medium';
}
//# sourceMappingURL=environment-context.js.map