import { Node, Path, Text, Transforms } from "slate";
/**
 * 连续空格跳出链接的阈值（输入第二个空格时跳出 data-url）
 */ var SPACES_TO_EXIT_LINK = 2;
/**
 * 处理链接卡片和媒体相关节点的操作
 *
 * @param editor - Slate编辑器实例
 * @param operation - 要处理的操作
 * @returns 如果操作被处理则返回true，否则返回false
 *
 * @description
 * 处理以下链接和媒体相关操作:
 * - 拆分链接卡片或媒体节点 (split_node)
 * - 删除链接卡片内的节点 (remove_node)
 * - 链接内连续输入两个空格时跳出链接 (insert_text)
 */ var handleLinkAndMediaOperation = function handleLinkAndMediaOperation(editor, operation) {
    var _operation_properties, _operation_properties1;
    if (operation.type === 'insert_text' && /^\s+$/.test(operation.text)) {
        var currentNode = Node.get(editor, operation.path);
        if (Text.isText(currentNode) && (currentNode === null || currentNode === void 0 ? void 0 : currentNode.url)) {
            var _ref, _operation_offset, _ref1;
            var _editor_selection_anchor, _editor_selection, _textAfterInsert_match_, _textAfterInsert_match;
            var text = currentNode.text || '';
            var offset = (_ref = (_operation_offset = operation.offset) !== null && _operation_offset !== void 0 ? _operation_offset : (_editor_selection = editor.selection) === null || _editor_selection === void 0 ? void 0 : (_editor_selection_anchor = _editor_selection.anchor) === null || _editor_selection_anchor === void 0 ? void 0 : _editor_selection_anchor.offset) !== null && _ref !== void 0 ? _ref : 0;
            var isAtEnd = offset >= text.length;
            var textAfterInsert = text.slice(0, offset) + operation.text;
            var trailingSpaces = (_ref1 = (_textAfterInsert_match = textAfterInsert.match(/\s*$/)) === null || _textAfterInsert_match === void 0 ? void 0 : (_textAfterInsert_match_ = _textAfterInsert_match[0]) === null || _textAfterInsert_match_ === void 0 ? void 0 : _textAfterInsert_match_.length) !== null && _ref1 !== void 0 ? _ref1 : 0;
            if (isAtEnd && trailingSpaces >= SPACES_TO_EXIT_LINK) {
                Transforms.insertNodes(editor, [
                    {
                        text: operation.text
                    }
                ], {
                    at: Path.next(operation.path),
                    select: true
                });
                return true;
            }
        }
    }
    if (operation.type === 'split_node' && (((_operation_properties = operation.properties) === null || _operation_properties === void 0 ? void 0 : _operation_properties.type) === 'link-card' || ((_operation_properties1 = operation.properties) === null || _operation_properties1 === void 0 ? void 0 : _operation_properties1.type) === 'media')) {
        var node = Node.get(editor, operation.path);
        if ([
            'link-card',
            'media'
        ].includes(node === null || node === void 0 ? void 0 : node.type)) {
            Transforms.insertNodes(editor, [
                {
                    type: 'paragraph',
                    children: [
                        {
                            text: '',
                            p: 'true'
                        }
                    ]
                }
            ], {
                at: Path.next([
                    operation.path.at(0)
                ]),
                select: true
            });
        }
        return true;
    }
    if (operation.type === 'remove_node') {
        var parentNode = Node.get(editor, Path.parent(operation.path));
        if ('link-card' === parentNode.type) {
            Transforms.removeNodes(editor, {
                at: Path.parent(operation.path)
            });
            return true;
        }
    }
    return false;
};
/**
 * 扩展编辑器以处理链接卡片和媒体节点的操作
 *
 * @param editor - 要扩展的Slate编辑器实例
 * @returns 增强后的编辑器实例，能够处理链接和媒体相关操作
 *
 * @description
 * 该插件重写编辑器的 `apply` 方法，添加对链接卡片和媒体节点的特殊处理逻辑。
 */ export var withLinkAndMediaPlugin = function withLinkAndMediaPlugin(editor) {
    var apply = editor.apply;
    editor.apply = function(operation) {
        // 尝试处理链接和媒体相关操作
        if (handleLinkAndMediaOperation(editor, operation)) {
            return;
        }
        // 否则执行原始操作
        apply(operation);
    };
    return editor;
};
