import { getWordAtPosition } from "../cli/definition.js";
import { findAllOccurrences, occurrenceToRange } from "./util.js";
export function handlePrepareRename(params, doc) {
    const source = doc.getText();
    const word = getWordAtPosition(source, params.position.line, params.position.character);
    if (!word)
        return null;
    const line = source.split("\n")[params.position.line];
    const start = line.lastIndexOf(word, params.position.character);
    if (start === -1)
        return null;
    return {
        start: { line: params.position.line, character: start },
        end: { line: params.position.line, character: start + word.length },
    };
}
export function handleRename(params, doc) {
    const source = doc.getText();
    const word = getWordAtPosition(source, params.position.line, params.position.character);
    if (!word)
        return null;
    const occurrences = findAllOccurrences(source, word);
    if (occurrences.length === 0)
        return null;
    const edits = occurrences.map((occ) => ({
        range: occurrenceToRange(occ),
        newText: params.newName,
    }));
    return { changes: { [doc.uri]: edits } };
}
