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 { Editor, Transforms } from "slate";
/**
 * 原生表格编辑器 - 提供基础的表格操作功能
 * 替代复杂的 withTable 插件，使用 Slate 原生 API 实现表格操作
 */ export var NativeTableEditor = {
    /**
   * 插入表格
   * @param editor - Slate 编辑器实例
   * @param options - 表格选项
   */ insertTable: function insertTable(editor) {
        var options = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
        var _options_rows = options.rows, rows = _options_rows === void 0 ? 2 : _options_rows, _options_cols = options.cols, cols = _options_cols === void 0 ? 2 : _options_cols, at = options.at;
        // 确保行数和列数至少为1
        var clamp = function clamp(n) {
            return n < 1 ? 1 : n;
        };
        var actualRows = clamp(rows);
        var actualCols = clamp(cols);
        // 创建表格结构
        var tableNode = {
            type: 'table',
            children: Array.from({
                length: actualRows
            }).map(function() {
                return {
                    type: 'table-row',
                    children: Array.from({
                        length: actualCols
                    }).map(function() {
                        return {
                            type: 'table-cell',
                            children: [
                                {
                                    type: 'paragraph',
                                    children: [
                                        {
                                            text: ''
                                        }
                                    ]
                                }
                            ]
                        };
                    })
                };
            })
        };
        // 插入表格
        Transforms.insertNodes(editor, tableNode, {
            at: at
        });
    },
    /**
   * 删除表格
   * @param editor - Slate 编辑器实例
   * @param at - 位置
   */ removeTable: function removeTable(editor, at) {
        var tableEntry = this.findTable(editor, at);
        if (tableEntry) {
            var _tableEntry = _sliced_to_array(tableEntry, 2), path = _tableEntry[1];
            Transforms.removeNodes(editor, {
                at: path
            });
        }
    },
    /**
   * 查找表格节点
   * @param editor - Slate 编辑器实例
   * @param at - 位置
   */ findTable: function findTable(editor, at) {
        return Editor.above(editor, {
            match: function match(n) {
                return Editor.isBlock(editor, n) && n.type === 'table';
            },
            at: at
        });
    },
    /**
   * 查找表格行节点
   * @param editor - Slate 编辑器实例
   * @param at - 位置
   */ findTableRow: function findTableRow(editor, at) {
        return Editor.above(editor, {
            match: function match(n) {
                return Editor.isBlock(editor, n) && n.type === 'table-row';
            },
            at: at
        });
    },
    /**
   * 查找表格单元格节点
   * @param editor - Slate 编辑器实例
   * @param at - 位置
   */ findTableCell: function findTableCell(editor, at) {
        return Editor.above(editor, {
            match: function match(n) {
                return Editor.isBlock(editor, n) && n.type === 'table-cell';
            },
            at: at
        });
    },
    /**
   * 检查是否在表格中
   * @param editor - Slate 编辑器实例
   * @param at - 位置
   */ isInTable: function isInTable(editor, at) {
        return !!this.findTable(editor, at);
    },
    /**
   * 检查是否在表格行中
   * @param editor - Slate 编辑器实例
   * @param at - 位置
   */ isInTableRow: function isInTableRow(editor, at) {
        return !!this.findTableRow(editor, at);
    },
    /**
   * 检查是否在表格单元格中
   * @param editor - Slate 编辑器实例
   * @param at - 位置
   */ isInTableCell: function isInTableCell(editor, at) {
        return !!this.findTableCell(editor, at);
    },
    /**
   * 插入表格行
   * @param editor - Slate 编辑器实例
   * @param options - 选项
   */ insertTableRow: function insertTableRow(editor) {
        var options = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
        var _tableNode_children__children, _tableNode_children_;
        var at = options.at, _options_position = options.position, position = _options_position === void 0 ? 'below' : _options_position;
        var tableRowEntry = this.findTableRow(editor, at);
        if (!tableRowEntry) return;
        var _tableRowEntry = _sliced_to_array(tableRowEntry, 2), rowPath = _tableRowEntry[1];
        var tableEntry = this.findTable(editor, at);
        if (!tableEntry) return;
        var _tableEntry = _sliced_to_array(tableEntry, 1), tableNode = _tableEntry[0];
        var colCount = ((_tableNode_children_ = tableNode.children[0]) === null || _tableNode_children_ === void 0 ? void 0 : (_tableNode_children__children = _tableNode_children_.children) === null || _tableNode_children__children === void 0 ? void 0 : _tableNode_children__children.length) || 1;
        // 创建新行
        var newRow = {
            type: 'table-row',
            children: Array.from({
                length: colCount
            }).map(function() {
                return {
                    type: 'table-cell',
                    children: [
                        {
                            type: 'paragraph',
                            children: [
                                {
                                    text: ''
                                }
                            ]
                        }
                    ]
                };
            })
        };
        // 插入新行
        var insertPath = position === 'above' ? rowPath : [
            rowPath[0],
            rowPath[1] + 1
        ];
        Transforms.insertNodes(editor, newRow, {
            at: insertPath
        });
    },
    /**
   * 删除表格行
   * @param editor - Slate 编辑器实例
   * @param at - 位置
   */ removeTableRow: function removeTableRow(editor, at) {
        var tableRowEntry = this.findTableRow(editor, at);
        if (!tableRowEntry) return;
        var _tableRowEntry = _sliced_to_array(tableRowEntry, 2), rowPath = _tableRowEntry[1];
        var tableEntry = this.findTable(editor, at);
        if (!tableEntry) return;
        var _tableEntry = _sliced_to_array(tableEntry, 1), tableNode = _tableEntry[0];
        // 如果表格只有一行，删除整个表格
        if (tableNode.children.length <= 1) {
            this.removeTable(editor, at);
            return;
        }
        // 删除当前行
        Transforms.removeNodes(editor, {
            at: rowPath
        });
    },
    /**
   * 插入表格列
   * @param editor - Slate 编辑器实例
   * @param options - 选项
   */ insertTableColumn: function insertTableColumn(editor) {
        var options = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
        var at = options.at, _options_position = options.position, position = _options_position === void 0 ? 'right' : _options_position;
        var tableEntry = this.findTable(editor, at);
        if (!tableEntry) return;
        var _tableEntry = _sliced_to_array(tableEntry, 2), tableNode = _tableEntry[0], tablePath = _tableEntry[1];
        var rowCount = tableNode.children.length;
        // 为每一行插入新单元格
        for(var i = 0; i < rowCount; i++){
            var rowPath = _to_consumable_array(tablePath).concat([
                i
            ]);
            var rowNode = tableNode.children[i];
            var colCount = rowNode.children.length;
            var newCell = {
                type: 'table-cell',
                children: [
                    {
                        type: 'paragraph',
                        children: [
                            {
                                text: ''
                            }
                        ]
                    }
                ]
            };
            var insertPath = position === 'left' ? _to_consumable_array(rowPath).concat([
                0
            ]) : _to_consumable_array(rowPath).concat([
                colCount
            ]);
            Transforms.insertNodes(editor, newCell, {
                at: insertPath
            });
        }
    },
    /**
   * 删除表格列
   * @param editor - Slate 编辑器实例
   * @param at - 位置
   */ removeTableColumn: function removeTableColumn(editor, at) {
        var tableCellEntry = this.findTableCell(editor, at);
        if (!tableCellEntry) return;
        var _tableCellEntry = _sliced_to_array(tableCellEntry, 2), cellPath = _tableCellEntry[1];
        var tableEntry = this.findTable(editor, at);
        if (!tableEntry) return;
        var _tableEntry = _sliced_to_array(tableEntry, 2), tableNode = _tableEntry[0], tablePath = _tableEntry[1];
        var rowCount = tableNode.children.length;
        var colIndex = cellPath[cellPath.length - 1];
        // 检查是否只有一列
        var firstRow = tableNode.children[0];
        if (firstRow.children.length <= 1) {
            this.removeTable(editor, at);
            return;
        }
        // 删除每一行的对应列
        for(var i = 0; i < rowCount; i++){
            var cellPath1 = _to_consumable_array(tablePath).concat([
                i,
                colIndex
            ]);
            Transforms.removeNodes(editor, {
                at: cellPath1
            });
        }
    },
    /**
   * 移动光标到下一个单元格
   * @param editor - Slate 编辑器实例
   * @param at - 位置
   */ moveToNextCell: function moveToNextCell(editor, at) {
        var tableCellEntry = this.findTableCell(editor, at);
        if (!tableCellEntry) return;
        var _tableCellEntry = _sliced_to_array(tableCellEntry, 2), cellPath = _tableCellEntry[1];
        var tableEntry = this.findTable(editor, at);
        if (!tableEntry) return;
        var _tableEntry = _sliced_to_array(tableEntry, 2), tableNode = _tableEntry[0], tablePath = _tableEntry[1];
        var rowIndex = cellPath[cellPath.length - 2];
        var colIndex = cellPath[cellPath.length - 1];
        var rowCount = tableNode.children.length;
        var colCount = tableNode.children[0].children.length;
        // 计算下一个单元格位置
        var nextRowIndex = rowIndex;
        var nextColIndex = colIndex + 1;
        if (nextColIndex >= colCount) {
            nextRowIndex++;
            nextColIndex = 0;
        }
        if (nextRowIndex < rowCount) {
            var nextCellPath = _to_consumable_array(tablePath).concat([
                nextRowIndex,
                nextColIndex
            ]);
            Transforms.select(editor, {
                path: nextCellPath,
                offset: 0
            });
        }
    },
    /**
   * 移动光标到上一个单元格
   * @param editor - Slate 编辑器实例
   * @param at - 位置
   */ moveToPreviousCell: function moveToPreviousCell(editor, at) {
        var tableCellEntry = this.findTableCell(editor, at);
        if (!tableCellEntry) return;
        var _tableCellEntry = _sliced_to_array(tableCellEntry, 2), cellPath = _tableCellEntry[1];
        var tableEntry = this.findTable(editor, at);
        if (!tableEntry) return;
        var _tableEntry = _sliced_to_array(tableEntry, 2), tableNode = _tableEntry[0], tablePath = _tableEntry[1];
        var rowIndex = cellPath[cellPath.length - 2];
        var colIndex = cellPath[cellPath.length - 1];
        var colCount = tableNode.children[0].children.length;
        // 计算上一个单元格位置
        var prevRowIndex = rowIndex;
        var prevColIndex = colIndex - 1;
        if (prevColIndex < 0) {
            prevRowIndex--;
            prevColIndex = colCount - 1;
        }
        if (prevRowIndex >= 0) {
            var prevCellPath = _to_consumable_array(tablePath).concat([
                prevRowIndex,
                prevColIndex
            ]);
            Transforms.select(editor, {
                path: prevCellPath,
                offset: 0
            });
        }
    }
};
