/**
 * TypeScript diagnostics collection and conversion
 */
import * as ts from "typescript";
import { createDiagnosticsCollector, addDiagnostic, createDiagnostic, } from "../types/diagnostic.js";
/**
 * Collect all TypeScript diagnostics from a program
 */
export const collectTsDiagnostics = (program) => {
    const tsDiagnostics = [
        ...program.getConfigFileParsingDiagnostics(),
        ...program.getOptionsDiagnostics(),
        ...program.getSyntacticDiagnostics(),
        ...program.getGlobalDiagnostics(),
    ];
    return tsDiagnostics.reduce((collector, tsDiag) => {
        const diagnostic = convertTsDiagnostic(tsDiag);
        return diagnostic ? addDiagnostic(collector, diagnostic) : collector;
    }, createDiagnosticsCollector());
};
/**
 * Convert TypeScript diagnostic to Tsonic diagnostic
 */
export const convertTsDiagnostic = (tsDiag) => {
    if (tsDiag.category === ts.DiagnosticCategory.Suggestion) {
        return null; // Ignore suggestions
    }
    const message = ts.flattenDiagnosticMessageText(tsDiag.messageText, "\n");
    // Ignore "type used as value" errors for .NET types
    // These are handled by the Tsonic compiler
    if (message.includes("only refers to a type, but is being used as a value") &&
        tsDiag.file) {
        // Check if this is a .NET import
        const sourceText = tsDiag.file.getText();
        if (sourceText.includes('from "System') ||
            sourceText.includes('from "Microsoft') ||
            sourceText.includes('from "Windows')) {
            return null; // Ignore - will be handled by Tsonic
        }
    }
    const severity = tsDiag.category === ts.DiagnosticCategory.Error
        ? "error"
        : tsDiag.category === ts.DiagnosticCategory.Warning
            ? "warning"
            : "info";
    const location = tsDiag.file && tsDiag.start !== undefined
        ? getSourceLocation(tsDiag.file, tsDiag.start, tsDiag.length ?? 1)
        : undefined;
    return createDiagnostic("TSN2001", // Generic TypeScript error
    severity, message, location);
};
/**
 * Get source location information from TypeScript source file
 */
export const getSourceLocation = (file, start, length) => {
    const { line, character } = file.getLineAndCharacterOfPosition(start);
    return {
        file: file.fileName,
        line: line + 1,
        column: character + 1,
        length,
    };
};
//# sourceMappingURL=diagnostics.js.map