"use strict";
/**
 * Finding Hash Computation
 * Creates stable, reproducible hashes for identifying specific findings
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.normalizePathForHash = normalizePathForHash;
exports.normalizeContentForHash = normalizeContentForHash;
exports.computeFindingHash = computeFindingHash;
exports.computeHashFromComponents = computeHashFromComponents;
exports.isValidHash = isValidHash;
exports.formatHashForDisplay = formatHashForDisplay;
const crypto_1 = require("crypto");
/**
 * Normalize a file path for hashing
 * - Removes leading ./ or /
 * - Converts backslashes to forward slashes
 * - Lowercases (for case-insensitive matching)
 */
function normalizePathForHash(filePath) {
    return filePath
        .replace(/^\.\//, '') // Remove leading ./
        .replace(/^\//, '') // Remove leading /
        .replace(/\\/g, '/') // Normalize backslashes
        .toLowerCase();
}
/**
 * Normalize line content for hashing
 * - Trims whitespace
 * - Collapses multiple whitespace to single space
 * - Removes common noise (line numbers, etc.)
 */
function normalizeContentForHash(content) {
    return content
        .trim()
        .replace(/\s+/g, ' ') // Collapse whitespace
        .replace(/^\d+:\s*/, ''); // Remove line number prefix if present
}
/**
 * Compute a stable hash for a vulnerability finding
 *
 * Hash = SHA256(normalizedPath:normalizedContent:category).slice(0,16)
 *
 * This creates a 16-character hex hash that:
 * - Stays stable across whitespace changes
 * - Changes when the actual code changes
 * - Changes when the finding category changes
 * - Is unique enough for practical use
 *
 * @param finding - The vulnerability to hash
 * @returns A 16-character hex hash string
 */
function computeFindingHash(finding) {
    const normalizedPath = normalizePathForHash(finding.filePath);
    const normalizedContent = normalizeContentForHash(finding.lineContent);
    const category = finding.category;
    const input = `${normalizedPath}:${normalizedContent}:${category}`;
    const hash = (0, crypto_1.createHash)('sha256').update(input).digest('hex');
    // Return first 16 characters (64 bits of entropy - sufficient for uniqueness)
    return hash.slice(0, 16);
}
/**
 * Compute hash from raw components (for testing or manual construction)
 */
function computeHashFromComponents(filePath, lineContent, category) {
    const normalizedPath = normalizePathForHash(filePath);
    const normalizedContent = normalizeContentForHash(lineContent);
    const input = `${normalizedPath}:${normalizedContent}:${category}`;
    const hash = (0, crypto_1.createHash)('sha256').update(input).digest('hex');
    return hash.slice(0, 16);
}
/**
 * Validate a hash format
 * Must be 16 hexadecimal characters
 */
function isValidHash(hash) {
    return /^[0-9a-f]{16}$/i.test(hash);
}
/**
 * Format a hash for display (with truncation indicator)
 */
function formatHashForDisplay(hash) {
    if (hash.length <= 16) {
        return hash;
    }
    return `${hash.slice(0, 12)}...`;
}
//# sourceMappingURL=hash.js.map