/**
 * ============================================================
 *  src/ui/hooks/useTextBuffer.ts —— 输入框文本缓冲（React 包装）
 * ------------------------------------------------------------
 *  把 textBuffer.ts 里的纯函数包成 React Hook：useState 持有 BufferState，
 *  暴露一组 callback 给 InputBox 调用。callback 用 setState(prev => ...)
 *  形式，避免 useInput handler 闭包看到旧 state 的问题。
 * ============================================================
 */
import { useCallback, useMemo, useState } from 'react';
import { bufClear, bufDeleteAt, bufDeleteBefore, bufDeleteWordBefore, bufInsert, bufKillToLineEnd, bufKillToLineStart, bufMoveBufferEnd, bufMoveBufferStart, bufMoveDown, bufMoveLeft, bufMoveLineEnd, bufMoveLineStart, bufMoveRight, bufMoveUp, bufMoveWordLeft, bufMoveWordRight, bufSetText, EMPTY_BUFFER, layoutBuffer, } from '../textBuffer.js';
export function useTextBuffer() {
    const [state, setState] = useState(EMPTY_BUFFER);
    const insert = useCallback((s) => setState((p) => bufInsert(p, s)), []);
    const setText = useCallback((s) => setState(bufSetText(s)), []);
    const clear = useCallback(() => setState(bufClear()), []);
    const deleteBefore = useCallback(() => setState(bufDeleteBefore), []);
    const deleteAt = useCallback(() => setState(bufDeleteAt), []);
    const deleteWordBefore = useCallback(() => setState(bufDeleteWordBefore), []);
    const killToLineStart = useCallback(() => setState(bufKillToLineStart), []);
    const killToLineEnd = useCallback(() => setState(bufKillToLineEnd), []);
    const moveLeft = useCallback(() => setState(bufMoveLeft), []);
    const moveRight = useCallback(() => setState(bufMoveRight), []);
    const moveWordLeft = useCallback(() => setState(bufMoveWordLeft), []);
    const moveWordRight = useCallback(() => setState(bufMoveWordRight), []);
    const moveLineStart = useCallback(() => setState(bufMoveLineStart), []);
    const moveLineEnd = useCallback(() => setState(bufMoveLineEnd), []);
    const moveBufferStart = useCallback(() => setState(bufMoveBufferStart), []);
    const moveBufferEnd = useCallback(() => setState(bufMoveBufferEnd), []);
    const moveUp = useCallback(() => setState(bufMoveUp), []);
    const moveDown = useCallback(() => setState(bufMoveDown), []);
    const layout = useMemo(() => layoutBuffer(state), [state]);
    return {
        state,
        layout,
        insert,
        setText,
        clear,
        deleteBefore,
        deleteAt,
        deleteWordBefore,
        killToLineStart,
        killToLineEnd,
        moveLeft,
        moveRight,
        moveWordLeft,
        moveWordRight,
        moveLineStart,
        moveLineEnd,
        moveBufferStart,
        moveBufferEnd,
        moveUp,
        moveDown,
    };
}
