"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CompilerContext = void 0;
const logger_js_1 = require("./logger.js");
/**
 * Holds shared state for the entire compilation run.
 *
 * Passed through all compiler stages. No dependency on ts.TransformationContext —
 * the compiler uses ts.createProgram() directly without program.emit().
 */
class CompilerContext {
    program;
    /** Maximum number of diagnostics before suppression. */
    static MAX_DIAGNOSTICS = 200;
    checker;
    logger;
    _diagnostics = [];
    constructor(program, verbose = false) {
        this.program = program;
        this.checker = program.getTypeChecker();
        this.logger = new logger_js_1.Logger(verbose ? logger_js_1.LogLevel.Debug : logger_js_1.LogLevel.Info);
    }
    get diagnostics() {
        return this._diagnostics;
    }
    get errors() {
        return this._diagnostics.filter(d => d.severity === 'error');
    }
    get warnings() {
        return this._diagnostics.filter(d => d.severity === 'warning');
    }
    /**
     * Add a diagnostic from an AST node.
     * Extracts file path, line, and column automatically.
     */
    addDiagnostic(node, message, severity, code) {
        if (this._diagnostics.length >= CompilerContext.MAX_DIAGNOSTICS)
            return;
        const sourceFile = node.getSourceFile();
        const { line, character } = sourceFile.getLineAndCharacterOfPosition(node.getStart());
        this._diagnostics.push({
            file: sourceFile.fileName,
            line: line + 1, // 1-based
            column: character + 1, // 1-based
            message,
            severity,
            code,
        });
    }
    /**
     * Add a diagnostic without an AST node (e.g., configuration errors).
     */
    addRawDiagnostic(file, line, column, message, severity, code) {
        if (this._diagnostics.length >= CompilerContext.MAX_DIAGNOSTICS)
            return;
        this._diagnostics.push({ file, line, column, message, severity, code });
    }
    /**
     * Convenience: add an error diagnostic from an AST node.
     */
    addError(node, message, code) {
        const codeStr = typeof code === 'string' ? code : code.code;
        this.addDiagnostic(node, message, 'error', codeStr);
    }
    /**
     * Convenience: add a warning diagnostic from an AST node.
     */
    addWarning(node, message, code) {
        const codeStr = typeof code === 'string' ? code : code.code;
        this.addDiagnostic(node, message, 'warning', codeStr);
    }
    hasErrors() {
        return this._diagnostics.some(d => d.severity === 'error');
    }
}
exports.CompilerContext = CompilerContext;
//# sourceMappingURL=compilerContext.js.map