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, Element, Node, Transforms } from "slate";
/**
 * 判断节点是否为列表类型
 */ export var isListType = function isListType(node) {
    return Element.isElement(node) && (node.type === 'bulleted-list' || node.type === 'numbered-list');
};
/**
 * 根据 order 属性返回列表类型
 */ export var getListType = function getListType(order) {
    return order ? 'numbered-list' : 'bulleted-list';
};
/**
 * 扩展编辑器以处理列表节点的规范化
 *
 * @param editor - 要扩展的Slate编辑器实例
 * @returns 增强后的编辑器实例，能够规范化列表结构
 *
 * @description
 * 该插件重写编辑器的 `normalizeNode` 方法，确保列表结构符合规范：
 * - `bulleted-list` 和 `numbered-list` 的直接子节点必须是 `list-item`
 * - `list-item` 的第一个子节点应该是块级元素（如 `paragraph`）
 * - 自动修复不符合规范的结构
 */ export var withListsPlugin = function withListsPlugin(editor) {
    var normalizeNode = editor.normalizeNode;
    editor.normalizeNode = function(entry) {
        var _entry = _sliced_to_array(entry, 2), node = _entry[0], path = _entry[1];
        // 规则 1: 列表节点的直接子节点必须是 list-item
        if (isListType(node)) {
            var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
            try {
                for(var _iterator = Node.children(editor, path)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
                    var _step_value = _sliced_to_array(_step.value, 2), child = _step_value[0], childPath = _step_value[1];
                    if (Element.isElement(child) && child.type !== 'list-item') {
                        // 将非 list-item 节点转换为 list-item
                        Transforms.setNodes(editor, {
                            type: 'list-item'
                        }, {
                            at: childPath
                        });
                        return;
                    }
                }
            } catch (err) {
                _didIteratorError = true;
                _iteratorError = err;
            } finally{
                try {
                    if (!_iteratorNormalCompletion && _iterator.return != null) {
                        _iterator.return();
                    }
                } finally{
                    if (_didIteratorError) {
                        throw _iteratorError;
                    }
                }
            }
        }
        // 规则 2: list-item 的子节点结构规范化
        if (Element.isElement(node) && node.type === 'list-item') {
            // 确保 list-item 至少有一个子节点
            if (node.children.length === 0) {
                Transforms.insertNodes(editor, {
                    type: 'paragraph',
                    children: [
                        {
                            text: ''
                        }
                    ]
                }, {
                    at: _to_consumable_array(path).concat([
                        0
                    ])
                });
                return;
            }
            // 确保第一个子节点是块级元素（paragraph 或其他块级元素）
            var firstChild = node.children[0];
            if (Element.isElement(firstChild) && !Editor.isBlock(editor, firstChild) && firstChild.type !== 'paragraph') {
                // 如果不是块级元素，包裹为 paragraph
                Transforms.wrapNodes(editor, {
                    type: 'paragraph',
                    children: []
                }, {
                    at: _to_consumable_array(path).concat([
                        0
                    ])
                });
                return;
            }
            // 确保 list-item 的子节点中，除了第一个块级元素外，其他都是列表类型
            // 允许的结构: [paragraph, bulleted-list?, numbered-list?]
            for(var i = 1; i < node.children.length; i++){
                var child1 = node.children[i];
                if (Element.isElement(child1)) {
                    if (!isListType(child1)) {
                        if (Editor.isBlock(editor, child1)) {
                            // paragraph 不处理，直接返回，避免 "-" 等被误转为嵌套列表
                            if (child1.type === 'paragraph') return;
                            var childPath1 = _to_consumable_array(path).concat([
                                i
                            ]);
                            var listType = getListType();
                            Transforms.wrapNodes(editor, {
                                type: 'list-item',
                                checked: undefined,
                                mentions: [],
                                id: '',
                                children: []
                            }, {
                                at: childPath1
                            });
                            Transforms.wrapNodes(editor, {
                                type: listType,
                                children: []
                            }, {
                                at: childPath1
                            });
                            return;
                        }
                    }
                }
            }
        }
        // 规则 3: 向后兼容 - 将旧的 'list' 类型转换为新类型
        if (Element.isElement(node) && node.type === 'list') {
            var listType1 = getListType(node.order);
            Transforms.setNodes(editor, {
                type: listType1
            }, {
                at: path
            });
            // 移除 order 属性（已由类型表示）
            if (node.order !== undefined) {
                Transforms.unsetNodes(editor, 'order', {
                    at: path
                });
            }
            return;
        }
        // 执行原始的规范化逻辑
        normalizeNode(entry);
    };
    return editor;
};
