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 _array_without_holes(arr) {
    if (Array.isArray(arr)) return _array_like_to_array(arr);
}
function _iterable_to_array(iter) {
    if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
}
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 _non_iterable_spread() {
    throw new TypeError("Invalid attempt to spread 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 _to_consumable_array(arr) {
    return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
}
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 { NativeTableEditor } from "./native-table-editor";
/**
 * 原生表格键盘事件处理
 * 提供基础的表格导航和操作功能
 */ export var NativeTableKeyboard = {
    /**
   * 处理表格中的键盘事件
   * @param editor - Slate 编辑器实例
   * @param event - 键盘事件
   * @returns 是否已处理该事件
   */ handleKeyDown: function handleKeyDown(editor, event) {
        // 只在表格单元格中处理特殊按键
        if (!NativeTableEditor.isInTableCell(editor)) {
            return false;
        }
        var key = event.key, shiftKey = event.shiftKey;
        switch(key){
            case 'Tab':
                event.preventDefault();
                if (shiftKey) {
                    NativeTableEditor.moveToPreviousCell(editor);
                } else {
                    NativeTableEditor.moveToNextCell(editor);
                }
                return true;
            case 'Enter':
                // 在表格单元格中，Enter 键应该创建新段落而不是新行
                // 这里不做特殊处理，让默认行为生效
                return false;
            case 'ArrowUp':
                // 如果光标在单元格开头，移动到上一行
                if (editor.selection && editor.selection.anchor.offset === 0) {
                    var cellEntry = NativeTableEditor.findTableCell(editor);
                    if (cellEntry) {
                        var _cellEntry = _sliced_to_array(cellEntry, 2), cellPath = _cellEntry[1];
                        var tableEntry = NativeTableEditor.findTable(editor);
                        if (tableEntry) {
                            var _tableEntry = _sliced_to_array(tableEntry, 2), tablePath = _tableEntry[1];
                            var rowIndex = cellPath[cellPath.length - 2];
                            var colIndex = cellPath[cellPath.length - 1];
                            if (rowIndex > 0) {
                                var prevCellPath = _to_consumable_array(tablePath).concat([
                                    rowIndex - 1,
                                    colIndex
                                ]);
                                event.preventDefault();
                                editor.select({
                                    path: prevCellPath,
                                    offset: 0
                                });
                                return true;
                            }
                        }
                    }
                }
                return false;
            case 'ArrowDown':
                // 如果光标在单元格末尾，移动到下一行
                if (editor.selection && editor.selection.anchor.offset === 0) {
                    var cellEntry1 = NativeTableEditor.findTableCell(editor);
                    if (cellEntry1) {
                        var _cellEntry1 = _sliced_to_array(cellEntry1, 2), cellPath1 = _cellEntry1[1];
                        var tableEntry1 = NativeTableEditor.findTable(editor);
                        if (tableEntry1) {
                            var _tableEntry1 = _sliced_to_array(tableEntry1, 2), tableNode = _tableEntry1[0], tablePath1 = _tableEntry1[1];
                            var rowIndex1 = cellPath1[cellPath1.length - 2];
                            var colIndex1 = cellPath1[cellPath1.length - 1];
                            var rowCount = tableNode.children.length;
                            if (rowIndex1 < rowCount - 1) {
                                var nextCellPath = _to_consumable_array(tablePath1).concat([
                                    rowIndex1 + 1,
                                    colIndex1
                                ]);
                                event.preventDefault();
                                editor.select({
                                    path: nextCellPath,
                                    offset: 0
                                });
                                return true;
                            }
                        }
                    }
                }
                return false;
            case 'ArrowLeft':
                // 如果光标在单元格开头，移动到左侧单元格
                if (editor.selection && editor.selection.anchor.offset === 0) {
                    var cellEntry2 = NativeTableEditor.findTableCell(editor);
                    if (cellEntry2) {
                        var _cellEntry2 = _sliced_to_array(cellEntry2, 2), cellPath2 = _cellEntry2[1];
                        var tableEntry2 = NativeTableEditor.findTable(editor);
                        if (tableEntry2) {
                            var _tableEntry2 = _sliced_to_array(tableEntry2, 2), tablePath2 = _tableEntry2[1];
                            var rowIndex2 = cellPath2[cellPath2.length - 2];
                            var colIndex2 = cellPath2[cellPath2.length - 1];
                            if (colIndex2 > 0) {
                                var leftCellPath = _to_consumable_array(tablePath2).concat([
                                    rowIndex2,
                                    colIndex2 - 1
                                ]);
                                event.preventDefault();
                                editor.select({
                                    path: leftCellPath,
                                    offset: 0
                                });
                                return true;
                            }
                        }
                    }
                }
                return false;
            case 'ArrowRight':
                // 如果光标在单元格末尾，移动到右侧单元格
                if (editor.selection && editor.selection.anchor.offset === 0) {
                    var cellEntry3 = NativeTableEditor.findTableCell(editor);
                    if (cellEntry3) {
                        var _cellEntry3 = _sliced_to_array(cellEntry3, 2), tableNode1 = _cellEntry3[0], cellPath3 = _cellEntry3[1];
                        var tableEntry3 = NativeTableEditor.findTable(editor);
                        if (tableEntry3) {
                            var _tableEntry3 = _sliced_to_array(tableEntry3, 2), tablePath3 = _tableEntry3[1];
                            var rowIndex3 = cellPath3[cellPath3.length - 2];
                            var colIndex3 = cellPath3[cellPath3.length - 1];
                            var colCount = tableNode1.children[0].children.length;
                            if (colIndex3 < colCount - 1) {
                                var rightCellPath = _to_consumable_array(tablePath3).concat([
                                    rowIndex3,
                                    colIndex3 + 1
                                ]);
                                event.preventDefault();
                                editor.select({
                                    path: rightCellPath,
                                    offset: 0
                                });
                                return true;
                            }
                        }
                    }
                }
                return false;
            case 'Delete':
            case 'Backspace':
                // 在表格单元格中，删除操作应该只影响当前单元格内容
                // 不删除整个单元格或表格结构
                return false;
            default:
                return false;
        }
    },
    /**
   * 检查是否应该处理键盘事件
   * @param editor - Slate 编辑器实例
   * @param event - 键盘事件
   * @returns 是否应该处理
   */ shouldHandle: function shouldHandle(editor) {
        return NativeTableEditor.isInTableCell(editor);
    }
};
