// SPDX-License-Identifier: Apache-2.0
import * as assert from 'node:assert';
import { existsSync } from 'node:fs';
import { readFileSync } from 'node:fs';
import path from 'node:path';
class LintFormatter {
    inputFile;
    constructor(inputFile) {
        this.inputFile = inputFile;
        if (!existsSync(inputFile)) {
            assert.fail(`Input file does not exist: ${inputFile}`);
        }
    }
    transformOutput() {
        const input = readFileSync(this.inputFile);
        const ruleToViolationMap = new Map();
        const footer = [];
        let currentFile = '';
        for (const line of input.toString().split('\n')) {
            const tokens = line.trim().split(/\s+/);
            if (tokens.length === 0 || tokens[0].trim() === '') {
                continue;
            }
            if (line.includes('errors') && line.includes('warnings')) {
                footer.push(line);
                continue;
            }
            if (tokens[0]?.trim().includes(path.sep) && tokens[0]?.trim().endsWith('.ts')) {
                currentFile = tokens[0];
                continue;
            }
            const lineNumber = tokens[0];
            const severity = tokens[1];
            const message = tokens.slice(2, -1).join(' ');
            const rule = tokens.at(-1);
            const violationArray = ruleToViolationMap.get(rule);
            const violation = {
                lineNumber,
                severity,
                message,
                rule,
                sourceFile: currentFile,
            };
            if (violationArray === undefined) {
                ruleToViolationMap.set(rule, [violation]);
            }
            else {
                violationArray.push(violation);
                ruleToViolationMap.set(rule, violationArray);
            }
        }
        // eslint-disable-next-line unicorn/no-array-sort
        const sortedKeys = [...ruleToViolationMap.keys()].sort();
        for (const [rule, violations] of sortedKeys.map((key) => [
            key,
            ruleToViolationMap.get(key),
        ])) {
            console.log(`Rule: ${rule}, Count: ${violations.length}`);
            console.log('----------------------------------------');
            let fileCount = 1;
            // max file count length
            const maxFileCountLength = violations.length.toString().length;
            const maxSeverityLength = Math.max(...[...ruleToViolationMap.values()].map((violations) => violations[0]?.severity?.length));
            for (const violation of violations) {
                console.log(`${(fileCount++).toString().padStart(maxFileCountLength, ' ')}: ${violation?.severity?.toString().padStart(maxSeverityLength, ' ')}: ${violation.sourceFile}:${violation.lineNumber} : ${violation.message}`);
            }
            console.log('----------------------------------------');
        }
        console.log('\n\n');
        console.log('----------------------------------------');
        const maxCountLength = Math.max(...[...ruleToViolationMap.values()].map((violations) => violations.length.toString().length));
        const maxSeverityLength = Math.max(...[...ruleToViolationMap.values()].map((violations) => violations[0]?.severity?.length));
        for (const [rule, violations] of sortedKeys.map((key) => [
            key,
            ruleToViolationMap.get(key),
        ])) {
            console.log(`Count: ${violations?.length.toString().padStart(maxCountLength, ' ')}, Severity: ${violations[0]?.severity?.toString().padStart(maxSeverityLength, ' ')}, Rule: ${rule}, `);
        }
        console.log('----------------------------------------');
        console.log('\n\n');
        for (const line of footer) {
            console.log(line);
        }
    }
}
const lintFormatter = new LintFormatter(process.argv[2]);
lintFormatter.transformOutput();
//# sourceMappingURL=lint-formatter.js.map