import { splitLines } from '../core/paths.js';
/**
 * Count nSLOC, comments, blank lines, and assembly lines from source text.
 * Uses comment ranges from the parser and InlineAssembly detection.
 */
export function countNsloc(source, commentRanges, assemblyRanges, lineRange) {
    const lines = splitLines(source);
    const totalLines = lines.length;
    // Build sets of line numbers that are comments or assembly
    const commentLineSet = new Set();
    const assemblyLineSet = new Set();
    for (const range of commentRanges) {
        for (let i = range.start; i <= range.end; i++) {
            commentLineSet.add(i);
        }
    }
    for (const range of assemblyRanges) {
        for (let i = range.start; i <= range.end; i++) {
            assemblyLineSet.add(i);
        }
    }
    let blankLines = 0;
    let commentLines = 0;
    let assemblyLines = 0;
    let nsloc = 0;
    for (let i = 0; i < lines.length; i++) {
        const lineNum = i + 1;
        if (lineRange && (lineNum < lineRange.start || lineNum > lineRange.end))
            continue;
        const trimmed = lines[i].trim();
        if (trimmed === '') {
            blankLines++;
        }
        else if (commentLineSet.has(lineNum)) {
            // Check if the line is purely a comment (no code on the same line)
            // For simplicity, if the line is in a comment range, count it as comment
            commentLines++;
        }
        else {
            nsloc++;
            if (assemblyLineSet.has(lineNum)) {
                assemblyLines++;
            }
        }
    }
    return { totalLines, nsloc, commentLines, blankLines, assemblyLines };
}
/**
 * Extract comment line ranges from parser tokens.
 */
export function getCommentRanges(comments) {
    return comments
        .filter((c) => c.loc)
        .map((c) => ({
        start: c.loc.start.line,
        end: c.loc.end.line,
    }));
}
/**
 * Find assembly block ranges from source using regex.
 * Returns line ranges of `assembly { ... }` blocks.
 */
export function getAssemblyRanges(source) {
    const ranges = [];
    const lines = splitLines(source);
    let inAssembly = false;
    let braceCount = 0;
    let startLine = 0;
    for (let i = 0; i < lines.length; i++) {
        const line = lines[i];
        if (!inAssembly) {
            // Look for `assembly {` or `assembly "evmasm" {`
            if (/\bassembly\b/.test(line)) {
                inAssembly = true;
                startLine = i + 1;
                braceCount = 0;
                // Count braces on this line
                for (const ch of line) {
                    if (ch === '{')
                        braceCount++;
                    if (ch === '}')
                        braceCount--;
                }
                if (braceCount <= 0 && line.includes('{')) {
                    // Single-line assembly block
                    ranges.push({ start: startLine, end: i + 1 });
                    inAssembly = false;
                }
            }
        }
        else {
            for (const ch of line) {
                if (ch === '{')
                    braceCount++;
                if (ch === '}')
                    braceCount--;
            }
            if (braceCount <= 0) {
                ranges.push({ start: startLine, end: i + 1 });
                inAssembly = false;
            }
        }
    }
    return ranges;
}
//# sourceMappingURL=nsloc.js.map