import { Editor, Transforms } from "slate";
import { useRefFunction } from "../../Hooks/useRefFunction";
import { isMobileDevice } from "../AttachmentButton/utils";
/**
 * 键盘事件处理 Hook。
 *
 * 由原 useMarkdownInputFieldHandlers 拆分而来，处理：
 *  - 中文输入法 / Composition 期间不响应
 *  - Home / End / Ctrl+A 的光标移动与全选
 *  - 根据 triggerSendKey 决定 Enter 或 Mod+Enter 触发发送
 *  - 移动端强制 Mod+Enter，避免 Enter 误触
 */ export var useKeyboardHandler = function useKeyboardHandler(param) {
    var props = param.props, markdownEditorRef = param.markdownEditorRef, sendMessage = param.sendMessage;
    var handleKeyDown = useRefFunction(function(e) {
        var _markdownEditorRef_current, _markdownEditorRef_current_markdownEditorRef, _markdownEditorRef_current1;
        if ((markdownEditorRef === null || markdownEditorRef === void 0 ? void 0 : (_markdownEditorRef_current = markdownEditorRef.current) === null || _markdownEditorRef_current === void 0 ? void 0 : _markdownEditorRef_current.store.inputComposition) || e.nativeEvent.isComposing) return;
        var editor = markdownEditorRef === null || markdownEditorRef === void 0 ? void 0 : (_markdownEditorRef_current1 = markdownEditorRef.current) === null || _markdownEditorRef_current1 === void 0 ? void 0 : (_markdownEditorRef_current_markdownEditorRef = _markdownEditorRef_current1.markdownEditorRef) === null || _markdownEditorRef_current_markdownEditorRef === void 0 ? void 0 : _markdownEditorRef_current_markdownEditorRef.current;
        var isEnter = e.key === 'Enter';
        var isMod = e.ctrlKey || e.metaKey;
        var isShift = e.shiftKey;
        // Home：移动到文档开头
        if (e.key === 'Home' && !isMod && editor) {
            e.preventDefault();
            e.stopPropagation();
            var start = Editor.start(editor, []);
            Transforms.select(editor, start);
            return;
        }
        // End：移动到文档末尾
        if (e.key === 'End' && !isMod && editor) {
            e.preventDefault();
            e.stopPropagation();
            var end = Editor.end(editor, []);
            Transforms.select(editor, end);
            return;
        }
        // Ctrl+A / Cmd+A：全选
        if ((e.key === 'a' || e.key === 'A') && isMod && !isShift && editor) {
            e.preventDefault();
            e.stopPropagation();
            Transforms.select(editor, {
                anchor: Editor.start(editor, []),
                focus: Editor.end(editor, [])
            });
            return;
        }
        // 移动端强制 Mod+Enter，避免 Enter 误触
        var effectiveTriggerKey = isMobileDevice() ? 'Mod+Enter' : props.triggerSendKey || 'Enter';
        if (effectiveTriggerKey === 'Enter') {
            // 模式 1：Enter 发送，Shift+Enter 换行
            if (isEnter && !isMod && !isShift) {
                e.stopPropagation();
                e.preventDefault();
                if (props.onSend) sendMessage();
                return;
            }
        } else if (effectiveTriggerKey === 'Mod+Enter') {
            // 模式 2：Mod+Enter 发送，Enter 换行
            if (isEnter && isMod && !isShift) {
                e.stopPropagation();
                e.preventDefault();
                if (props.onSend) sendMessage();
                return;
            }
        }
    // 其他情况让编辑器正常处理换行
    });
    return {
        handleKeyDown: handleKeyDown
    };
};
