// engine/code-transforms/typescript/ast-utils.ts — Shared AST utilities (AC-08).
// Lazy-loads TypeScript compiler API and provides common helpers.
// ---------------------------------------------------------------------------
// Lazy TypeScript loader with cache
// ---------------------------------------------------------------------------
let tsCache;
/**
 * Lazy-load the TypeScript compiler API. Caches after first call.
 * Throws if `typescript` is not installed as a peer dependency.
 */
export function getTypeScript() {
    if (tsCache) {
        return tsCache;
    }
    try {
        // eslint-disable-next-line @typescript-eslint/no-require-imports
        tsCache = require('typescript');
        return tsCache;
    }
    catch {
        throw new Error('TypeScript compiler API not available. Install typescript as a peer dependency.');
    }
}
/** Reset the TS cache — used in tests. */
export function resetTypeScriptCache() {
    tsCache = undefined;
}
// ---------------------------------------------------------------------------
// Parse / print helpers
// ---------------------------------------------------------------------------
/**
 * Parse source code into a TS SourceFile AST node.
 */
export function parseSource(source, fileName) {
    const ts = getTypeScript();
    return ts.createSourceFile(fileName, source, ts.ScriptTarget.Latest, true);
}
/**
 * Pretty-print a SourceFile AST back to string.
 */
export function printSource(sourceFile) {
    const ts = getTypeScript();
    const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed });
    return printer.printFile(sourceFile);
}
// ---------------------------------------------------------------------------
// AST traversal
// ---------------------------------------------------------------------------
/**
 * Find all AST nodes of a given SyntaxKind in a source file.
 */
export function findNodes(sourceFile, kind) {
    const ts = getTypeScript();
    const results = [];
    function visit(node) {
        if (node.kind === kind) {
            results.push(node);
        }
        ts.forEachChild(node, visit);
    }
    visit(sourceFile);
    return results;
}
// ---------------------------------------------------------------------------
// Position helpers
// ---------------------------------------------------------------------------
/**
 * Get 1-based line number for a position in the source file.
 */
export function getLineNumber(sourceFile, pos) {
    return sourceFile.getLineAndCharacterOfPosition(pos).line + 1;
}
// ---------------------------------------------------------------------------
// Text replacement helper
// ---------------------------------------------------------------------------
/**
 * Create a change record for reporting.
 */
export function createChange(line, description) {
    return { line, description };
}
/**
 * Apply a text replacement at a specific range in the source string.
 * Replacements must be applied in reverse order (highest position first)
 * to keep earlier positions valid.
 */
export function replaceRange(source, start, end, replacement) {
    return source.slice(0, start) + replacement + source.slice(end);
}
//# sourceMappingURL=ast-utils.js.map