function _array_like_to_array(arr, len) {
    if (len == null || len > arr.length) len = arr.length;
    for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
    return arr2;
}
function _array_with_holes(arr) {
    if (Array.isArray(arr)) return arr;
}
function _iterable_to_array_limit(arr, i) {
    var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
    if (_i == null) return;
    var _arr = [];
    var _n = true;
    var _d = false;
    var _s, _e;
    try {
        for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
            _arr.push(_s.value);
            if (i && _arr.length === i) break;
        }
    } catch (err) {
        _d = true;
        _e = err;
    } finally{
        try {
            if (!_n && _i["return"] != null) _i["return"]();
        } finally{
            if (_d) throw _e;
        }
    }
    return _arr;
}
function _non_iterable_rest() {
    throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _sliced_to_array(arr, i) {
    return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
}
function _unsupported_iterable_to_array(o, minLen) {
    if (!o) return;
    if (typeof o === "string") return _array_like_to_array(o, minLen);
    var n = Object.prototype.toString.call(o).slice(8, -1);
    if (n === "Object" && o.constructor) n = o.constructor.name;
    if (n === "Map" || n === "Set") return Array.from(n);
    if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
}
import isHotkey from "is-hotkey";
import { Editor, Element, Node, Path, Range, Transforms } from "slate";
import { isMod } from "../../utils";
import { EditorUtils } from "../../utils/editorUtils";
/**
 * 处理编辑器中的箭头键相关操作
 *
 * @param store - 编辑器存储对象
 * @param e - 键盘事件对象
 *
 * 功能包括:
 * - Mod+左箭头: 在代码行中移动到行首或前一个非空白字符
 * - 左箭头:
 *   - 处理media/attach元素前的光标移动
 *   - 处理空白节点前的光标移动
 *   - 处理void节点前的光标移动
 * - 右箭头:
 *   - 处理media/attach元素后的光标移动
 *   - 处理空白节点后的光标移动
 *   - 处理void节点后的光标移动
 * - 上箭头:
 *   - 处理表格、代码块等特殊块的上方导航
 *   - 在表格首行和代码块首行上方插入段落
 *   - 处理media/attach元素的向上移动
 * - 下箭头:
 *   - 处理表格、代码块等特殊块的下方导航
 *   - 在表格末行和代码块末行下方插入段落
 *   - 处理media/attach元素的向下移动
 *   - 处理空段落的导航
 */ export var keyArrow = function keyArrow(store, e) {
    var editor = store === null || store === void 0 ? void 0 : store.editor;
    var sel = editor.selection;
    if (sel && Range.isCollapsed(sel)) {
        if (isHotkey('mod+left', e)) {
            return;
        }
        // 暂时没用了。先注释掉吧
        if (isHotkey('left', e)) {
            e.preventDefault();
            e.stopPropagation();
            var _Editor_leaf = _sliced_to_array(Editor.leaf(editor, sel.focus), 1), leaf = _Editor_leaf[0];
            var dirt = EditorUtils.isDirtLeaf(leaf);
            var pre = Editor.previous(editor, {
                at: sel.focus.path
            });
            if (sel.focus.offset === 0 && pre && (pre[0].type === 'media' || pre[0].type === 'attach')) {
                Transforms.select(editor, pre[1]);
            } else if (sel.focus.offset === 0 && dirt) {
                EditorUtils.moveBeforeSpace(editor, sel.focus.path);
            } else {
                if (sel.focus.offset === 0 && Path.hasPrevious(sel.focus.path) && Editor.isVoid(editor, Node.get(editor, Path.previous(sel.focus.path)))) {
                    if (Path.hasPrevious(Path.previous(sel.focus.path))) {
                        Transforms.select(editor, Editor.end(editor, Path.previous(Path.previous(sel.focus.path))));
                    }
                } else {
                    Transforms.move(editor, {
                        unit: 'offset',
                        reverse: true
                    });
                }
            }
            return;
        }
        if (isHotkey('right', e)) {
            e.preventDefault();
            e.stopPropagation();
            if (!isMod(e)) {
                var _leaf_text, _leaf_text1;
                var _Editor_leaf1 = _sliced_to_array(Editor.leaf(editor, sel.focus), 1), leaf1 = _Editor_leaf1[0];
                var dirt1 = EditorUtils.isDirtLeaf(leaf1);
                var next = Editor.next(editor, {
                    at: sel.focus.path
                });
                var _Editor_nodes = _sliced_to_array(Editor.nodes(editor, {
                    match: function match(n) {
                        return n.type === 'media' || n.type === 'attach';
                    }
                }), 1), node = _Editor_nodes[0];
                if (node) {
                    var _node_, _node_1;
                    if ((node === null || node === void 0 ? void 0 : (_node_ = node[0]) === null || _node_ === void 0 ? void 0 : _node_.type) === 'media' || (node === null || node === void 0 ? void 0 : (_node_1 = node[0]) === null || _node_1 === void 0 ? void 0 : _node_1.type) === 'attach') {
                        Transforms.select(editor, Editor.start(editor, Path.next(node[1])));
                    } else {
                        EditorUtils.moveAfterSpace(editor, node[1]);
                    }
                } else if (sel.focus.offset === ((_leaf_text = leaf1.text) === null || _leaf_text === void 0 ? void 0 : _leaf_text.length) && next && next[0].type === 'media') {
                    Transforms.select(editor, next[1]);
                } else if (sel.focus.offset === ((_leaf_text1 = leaf1.text) === null || _leaf_text1 === void 0 ? void 0 : _leaf_text1.length) && dirt1 && !Editor.next(editor, {
                    at: sel.focus.path
                })) {
                    EditorUtils.moveAfterSpace(editor, sel.focus.path);
                } else {
                    var _leaf_text2;
                    var _Editor_leaf2 = _sliced_to_array(Editor.leaf(editor, sel.focus), 1), leaf2 = _Editor_leaf2[0];
                    if (sel.focus.offset === ((_leaf_text2 = leaf2.text) === null || _leaf_text2 === void 0 ? void 0 : _leaf_text2.length) && Editor.hasPath(editor, Path.next(sel.focus.path)) && Editor.isVoid(editor, Node.get(editor, Path.next(sel.focus.path)))) {
                        if (Editor.hasPath(editor, Path.next(Path.next(sel.focus.path)))) {
                            Transforms.select(editor, Editor.start(editor, Path.next(Path.next(sel.focus.path))));
                        }
                    } else {
                        Transforms.move(editor, {
                            unit: 'offset'
                        });
                    }
                }
            } else {
                Transforms.select(editor, Editor.end(editor, Path.parent(sel.focus.path)));
            }
            return;
        }
        if (isHotkey('up', e)) {
            var _Editor_nodes1 = _sliced_to_array(Editor.nodes(editor, {
                match: function match(n) {
                    return Element.isElement(n);
                },
                mode: 'lowest'
            }), 1), node1 = _Editor_nodes1[0];
            var _node = _sliced_to_array(node1, 2), el = _node[0], path = _node[1];
            var pre1 = Editor === null || Editor === void 0 ? void 0 : Editor.node(editor, EditorUtils.findPrev(editor, path));
            if ((pre1 === null || pre1 === void 0 ? void 0 : pre1[0].type) === 'media' || (pre1 === null || pre1 === void 0 ? void 0 : pre1[0].type) === 'attach') {
                e.preventDefault();
                e.stopPropagation();
                Transforms.select(editor, pre1[1]);
                return;
            }
            if (el.type === 'media' || el.type === 'attach') {
                e.preventDefault();
                var pre2 = EditorUtils.findPrev(editor, path);
                Transforms.select(editor, Editor.end(editor, pre2));
            }
        }
        if (isHotkey('down', e)) {
            var _Editor_nodes2 = _sliced_to_array(Editor.nodes(editor, {
                match: function match(n) {
                    return Element.isElement(n);
                },
                mode: 'lowest'
            }), 1), node2 = _Editor_nodes2[0];
            var _node1 = _sliced_to_array(node2, 2), el1 = _node1[0], path1 = _node1[1];
            if (!EditorUtils.findNext(editor, path1)) return;
            var next1 = Editor === null || Editor === void 0 ? void 0 : Editor.node(editor, EditorUtils.findNext(editor, path1));
            if ((next1 === null || next1 === void 0 ? void 0 : next1[0].type) === 'media' || (next1 === null || next1 === void 0 ? void 0 : next1[0].type) === 'attach') {
                e.preventDefault();
                e.stopPropagation();
                Transforms.select(editor, next1[1]);
                return;
            }
            if (el1.type === 'media' || el1.type === 'attach') {
                var next2 = EditorUtils.findNext(editor, path1);
                e.preventDefault();
                if (next2) {
                    Transforms.select(editor, Editor.start(editor, next2));
                }
            }
            if (el1.type === 'paragraph') {
                if (path1[0] === 0 && !Node.string(el1) && Editor.isEditor(Node.get(editor, Path.parent(path1))) && Editor.hasPath(editor, Path.next(path1))) {
                    var next3 = Editor === null || Editor === void 0 ? void 0 : Editor.node(editor, Path.next(path1));
                    if ([
                        'table',
                        'code',
                        'blockquote'
                    ].includes(next3[0].type)) {
                        e.preventDefault();
                        Transforms.select(editor, Editor.start(editor, Path.next(path1)));
                        Transforms.delete(editor, {
                            at: path1
                        });
                    }
                }
                if ((Node.string(el1) || el1.children.length > 1 || el1.children[0].type === 'media') && EditorUtils.checkSelEnd(editor, path1)) {
                    Transforms.insertNodes(editor, EditorUtils.p, {
                        at: [
                            editor.children.length
                        ],
                        select: true
                    });
                }
            }
        }
    }
};
