// (C) 2025 GoodData Corporation
import { useMemo } from "react";
import { EditorView } from "@codemirror/view";
export function useChangeHandler({ handleChange, handleCursor, }) {
    // Create an extension for handling changes
    const changeHandlerExtension = useMemo(() => {
        return EditorView.updateListener.of((update) => {
            if (update.docChanged) {
                handleChange.current?.(update.state.doc.toString());
            }
            if (handleCursor.current && update.selectionSet) {
                const range = update.state.selection.main;
                handleCursor.current(range.from, range.to);
            }
        });
    }, [handleChange, handleCursor]);
    return {
        changeHandlerExtension,
    };
}
