import { useCallback, useMemo, useState } from "react";
import { createPromptEditorState, updatePromptEditor, } from "../prompt/promptEditor.js";
export function usePromptEditor(initialValue = "") {
    const [state, setState] = useState(() => createPromptEditorState(initialValue));
    const dispatch = useCallback((action) => {
        setState((previous) => updatePromptEditor(previous, action));
    }, []);
    const set = useCallback((value, cursor) => {
        dispatch({ type: "set", value, cursor });
    }, [dispatch]);
    const reset = useCallback(() => {
        dispatch({ type: "set", value: "", cursor: 0 });
    }, [dispatch]);
    return useMemo(() => ({
        ...state,
        dispatch,
        set,
        reset,
    }), [dispatch, reset, set, state]);
}
