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 _class_call_check(instance, Constructor) {
    if (!(instance instanceof Constructor)) {
        throw new TypeError("Cannot call a class as a function");
    }
}
function _defineProperties(target, props) {
    for(var i = 0; i < props.length; i++){
        var descriptor = props[i];
        descriptor.enumerable = descriptor.enumerable || false;
        descriptor.configurable = true;
        if ("value" in descriptor) descriptor.writable = true;
        Object.defineProperty(target, descriptor.key, descriptor);
    }
}
function _create_class(Constructor, protoProps, staticProps) {
    if (protoProps) _defineProperties(Constructor.prototype, protoProps);
    if (staticProps) _defineProperties(Constructor, staticProps);
    return Constructor;
}
function _define_property(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        });
    } else {
        obj[key] = value;
    }
    return obj;
}
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, Element, Node, Path, Point, Range, Transforms } from "slate";
import { EditorUtils } from "../../utils/editorUtils";
export var BackspaceKey = /*#__PURE__*/ function() {
    "use strict";
    function BackspaceKey(editor) {
        _class_call_check(this, BackspaceKey);
        _define_property(this, "editor", void 0);
        this.editor = editor;
    }
    _create_class(BackspaceKey, [
        {
            key: "range",
            value: function range() {
                var sel = this.editor.selection;
                if (!sel) return;
                var _Range_edges = _sliced_to_array(Range.edges(sel), 2), start = _Range_edges[0], end = _Range_edges[1];
                if (Point.equals(start, Editor.start(this.editor, [])) && Point.equals(end, Editor.end(this.editor, []))) {
                    EditorUtils.deleteAll(this.editor);
                    Transforms.select(this.editor, Editor.end(this.editor, []));
                    return true;
                }
                return false;
            }
        },
        {
            key: "clearStyle",
            value: function clearStyle(sel) {
                var _leaf_text;
                var start = Range.start(sel);
                var leaf = Node.leaf(this.editor, start.path);
                if (((_leaf_text = leaf.text) === null || _leaf_text === void 0 ? void 0 : _leaf_text.length) === 1 && EditorUtils.isDirtLeaf(leaf)) {
                    EditorUtils.clearMarks(this.editor);
                }
            }
        },
        {
            key: "run",
            value: function run() {
                var _parent_;
                var sel = this.editor.selection;
                if (!sel) return;
                var nodes = Array.from(Editor.nodes(this.editor, {
                    mode: 'lowest',
                    match: function(n) {
                        return Element.isElement(n);
                    }
                }));
                var _nodes = _sliced_to_array(nodes, 1), node = _nodes[0];
                var _node = _sliced_to_array(node, 2), el = _node[0], path = _node[1];
                var parent = Editor.parent(this.editor, path);
                if (el.type !== 'paragraph' && (parent === null || parent === void 0 ? void 0 : (_parent_ = parent[0]) === null || _parent_ === void 0 ? void 0 : _parent_.type) !== 'list-item') {
                    this.clearStyle(sel);
                }
                if (el.type === 'head') {
                    var str = Node.string(el);
                    if (!str) {
                        Transforms.setNodes(this.editor, {
                            type: 'paragraph'
                        }, {
                            at: path
                        });
                        return true;
                    }
                }
                if (el.type === 'media' || el.type === 'attach') {
                    Transforms.removeNodes(this.editor, {
                        at: path
                    });
                    Transforms.insertNodes(this.editor, EditorUtils.p, {
                        at: node[1],
                        select: true
                    });
                    return true;
                }
                if (el.type === 'paragraph') {
                    var _parent_1;
                    var parent1 = Editor.parent(this.editor, path);
                    if ((parent1 === null || parent1 === void 0 ? void 0 : (_parent_1 = parent1[0]) === null || _parent_1 === void 0 ? void 0 : _parent_1.type) === 'list-item') {
                        if (Node.string(parent1[0]) !== '') {
                            return false;
                        }
                        if (Node.string(parent1[0]) === '') {
                            // 使用新的拆分逻辑处理空的list-item
                            var listPath = Path.parent(parent1[1]);
                            var listNode = Editor.node(this.editor, listPath);
                            var currentItemIndex = parent1[1][parent1[1].length - 1];
                            var isLastItem = currentItemIndex === listNode[0].children.length - 1;
                            if (isLastItem) {
                                // 如果是最后一个项目，删除list-item并替换成paragraph
                                Transforms.delete(this.editor, {
                                    at: parent1[1]
                                });
                                Transforms.insertNodes(this.editor, {
                                    type: 'paragraph',
                                    children: [
                                        {
                                            text: ''
                                        }
                                    ]
                                }, {
                                    at: parent1[1],
                                    select: true
                                });
                            } else {
                                // 如果不是最后一个项目，拆分列表
                                var remainingItems = listNode[0].children.slice(currentItemIndex + 1);
                                // 删除当前item之后的所有items
                                for(var i = listNode[0].children.length - 1; i > currentItemIndex; i--){
                                    Transforms.delete(this.editor, {
                                        at: _to_consumable_array(listPath).concat([
                                            i
                                        ])
                                    });
                                }
                                // 删除当前item
                                Transforms.delete(this.editor, {
                                    at: parent1[1]
                                });
                                // 插入paragraph
                                var insertPath = Path.next(listPath);
                                Transforms.insertNodes(this.editor, {
                                    type: 'paragraph',
                                    children: [
                                        {
                                            text: ''
                                        }
                                    ]
                                }, {
                                    at: insertPath,
                                    select: true
                                });
                                // 如果有剩余的items，创建新的列表
                                if (remainingItems.length > 0) {
                                    var newListPath = Path.next(insertPath);
                                    Transforms.insertNodes(this.editor, {
                                        type: listNode[0].type,
                                        children: remainingItems
                                    }, {
                                        at: newListPath
                                    });
                                }
                            }
                            return true;
                        }
                    }
                }
                /**
     * 处理表格单元格在起始位置的情况
     */ if (el.type === 'table-cell' && sel.anchor.offset === 0) {
                    // 当光标在表格单元格的起始位置时，阻止继续退格
                    var start = Range.start(sel);
                    if (!Path.hasPrevious(start.path)) {
                        return true;
                    }
                }
                /**
     * 防止删除paragraph与空table-cell混合
     */ if (sel.anchor.offset === 0) {
                    var preInline = Editor.previous(this.editor, {
                        at: sel.focus.path
                    });
                    if (preInline && preInline[0].type === 'break') {
                        Transforms.delete(this.editor, {
                            at: preInline[1]
                        });
                        return true;
                    }
                    if (el.type === 'paragraph') {
                        var pre = Editor.previous(this.editor, {
                            at: path
                        });
                        if (pre) {
                            if ([
                                'table',
                                'code'
                            ].includes(pre[0].type)) {
                                var end = Editor.end(this.editor, pre[1]);
                                if (!Node.string(Node.get(this.editor, end.path))) {
                                    Transforms.delete(this.editor, {
                                        at: path
                                    });
                                    var text = Node.string(el);
                                    if (text) {
                                        Transforms.insertNodes(this.editor, pre[0].type === 'code' ? [
                                            {
                                                text: text
                                            }
                                        ] : el.children, {
                                            at: end
                                        });
                                    }
                                    Transforms.select(this.editor, end);
                                    return true;
                                }
                            }
                            if (pre[0].type === 'media' || pre[0].type === 'attach') {
                                if (!Node.string(el)) {
                                    Transforms.delete(this.editor, {
                                        at: path
                                    });
                                }
                                Transforms.select(this.editor, pre[1]);
                                return true;
                            }
                        }
                        if (!pre && !Editor.previous(this.editor, {
                            at: sel.anchor.path
                        })) {
                            var parent2 = Editor.parent(this.editor, path);
                            if (parent2[0].type === 'blockquote') {
                                if (Editor.hasPath(this.editor, Path.next(path))) {
                                    Transforms.delete(this.editor, {
                                        at: path
                                    });
                                } else {
                                    Transforms.delete(this.editor, {
                                        at: parent2[1]
                                    });
                                }
                                Transforms.insertNodes(this.editor, {
                                    type: 'paragraph',
                                    children: el.children
                                }, {
                                    at: parent2[1]
                                });
                                Transforms.select(this.editor, Editor.start(this.editor, parent2[1]));
                                return true;
                            }
                            // 可删除顶级元素中的第一个段落
                            var next = Editor.hasPath(this.editor, Path.next(path));
                            if (Editor.isEditor(parent2[0]) && next && (Editor === null || Editor === void 0 ? void 0 : Editor.node(this.editor, Path.next(path))[0].type) !== 'hr') {
                                Transforms.delete(this.editor, {
                                    at: path
                                });
                                return true;
                            }
                        }
                        return false;
                    }
                }
                return false;
            }
        }
    ]);
    return BackspaceKey;
}();
