// engine/code-transforms/typescript/rename-symbol.ts — Rename symbol transform (AC-02).
// Finds all identifiers matching oldName and replaces them with newName.
import { getTypeScript, parseSource, getLineNumber, createChange, replaceRange, } from './ast-utils.js';
/**
 * Rename all occurrences of a symbol in the source code.
 * Uses AST to find Identifier nodes, then applies text replacements
 * in reverse order.
 */
export function renameSymbol(source, options, fileName) {
    const ts = getTypeScript();
    const sourceFile = parseSource(source, fileName);
    const changes = [];
    // Collect all identifier positions matching oldName
    const positions = [];
    function visit(node) {
        if (ts.isIdentifier(node) && node.text === options.oldName) {
            positions.push({
                start: node.getStart(sourceFile),
                end: node.getEnd(),
                line: getLineNumber(sourceFile, node.getStart(sourceFile)),
            });
        }
        ts.forEachChild(node, visit);
    }
    visit(sourceFile);
    if (positions.length === 0) {
        return { transformed: source, changes: [] };
    }
    // Apply replacements in reverse order to preserve positions
    let result = source;
    const reversed = [...positions].sort((a, b) => b.start - a.start);
    for (const pos of reversed) {
        result = replaceRange(result, pos.start, pos.end, options.newName);
        changes.push(createChange(pos.line, `Renamed '${options.oldName}' to '${options.newName}'`));
    }
    // Sort changes by line number (ascending) for output
    changes.sort((a, b) => a.line - b.line);
    return { transformed: result, changes };
}
//# sourceMappingURL=rename-symbol.js.map