"use strict";
/**
 * Diff Detection Utility
 * Compares current repository tree against previous scan data to detect changed files
 * Used for incremental scanning (Story D)
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.detectChangedFiles = detectChangedFiles;
exports.buildFileShaMap = buildFileShaMap;
exports.hasTreeChanged = hasTreeChanged;
/**
 * Maximum number of changed files before falling back to full scan
 * Beyond this threshold, incremental mode provides diminishing returns
 */
const MAX_CHANGED_FILES_FOR_INCREMENTAL = 50;
/**
 * Detect which files have changed between the current tree and previous scan
 *
 * @param currentTree - Array of files from current GitHub tree (path + sha)
 * @param previousFileShas - Map of {file_path: sha} from previous scan
 * @returns DiffResult with categorized file changes
 */
function detectChangedFiles(currentTree, previousFileShas) {
    // If no previous data, everything is "new"
    if (!previousFileShas || Object.keys(previousFileShas).length === 0) {
        return {
            added: currentTree.map(f => f.path),
            modified: [],
            deleted: [],
            unchanged: [],
            changedCount: currentTree.length,
            shouldUseIncremental: false, // No baseline = full scan
        };
    }
    const added = [];
    const modified = [];
    const unchanged = [];
    const currentPaths = new Set();
    // Compare current files against previous
    for (const file of currentTree) {
        currentPaths.add(file.path);
        const previousSha = previousFileShas[file.path];
        if (!previousSha) {
            // File didn't exist before
            added.push(file.path);
        }
        else if (previousSha !== file.sha) {
            // File content changed (SHA differs)
            modified.push(file.path);
        }
        else {
            // Same SHA = unchanged
            unchanged.push(file.path);
        }
    }
    // Find deleted files (in previous but not in current)
    const deleted = [];
    for (const path of Object.keys(previousFileShas)) {
        if (!currentPaths.has(path)) {
            deleted.push(path);
        }
    }
    const changedCount = added.length + modified.length;
    const shouldUseIncremental = changedCount > 0 && changedCount <= MAX_CHANGED_FILES_FOR_INCREMENTAL;
    return {
        added,
        modified,
        deleted,
        unchanged,
        changedCount,
        shouldUseIncremental,
    };
}
/**
 * Build a file SHA map from a GitHub tree response
 * Filters to only include blob (file) entries
 *
 * @param tree - GitHub tree items with path and sha
 * @returns Map of {file_path: sha}
 */
function buildFileShaMap(tree) {
    const map = {};
    for (const item of tree) {
        // Only include files (blobs), not directories (trees)
        if (item.type === 'blob') {
            map[item.path] = item.sha;
        }
    }
    return map;
}
/**
 * Check if two tree SHAs are different
 * Quick check before doing detailed file comparison
 *
 * @param currentTreeSha - SHA of current tree
 * @param previousTreeSha - SHA from previous scan
 * @returns true if trees are different (need detailed comparison)
 */
function hasTreeChanged(currentTreeSha, previousTreeSha) {
    if (!previousTreeSha)
        return true;
    return currentTreeSha !== previousTreeSha;
}
//# sourceMappingURL=diff-detector.js.map