import React, { useMemo } from "react";
import { debugInfo } from "../../../../Utils/debugUtils";
import { useEditorStore } from "../../store";
import { CommentView } from "../Comment";
/**
 * 比较函数，用于优化 CommentLeaf 组件的渲染性能
 */ var areCommentLeafPropsEqual = function(prevProps, nextProps) {
    // 首先进行快速引用比较
    if (prevProps.leaf === nextProps.leaf && prevProps.children === nextProps.children && prevProps.hashId === nextProps.hashId && prevProps.comment === nextProps.comment) {
        return true;
    }
    // 比较基本 props
    if (prevProps.hashId !== nextProps.hashId || prevProps.children !== nextProps.children || prevProps.comment !== nextProps.comment) {
        return false;
    }
    // 比较 leaf 对象的关键属性
    if (prevProps.leaf !== nextProps.leaf) {
        var prevLeaf = prevProps.leaf;
        var nextLeaf = nextProps.leaf;
        // 比较 comment 相关的关键属性
        if (prevLeaf.comment !== nextLeaf.comment || prevLeaf.id !== nextLeaf.id || prevLeaf.selection !== nextLeaf.selection || prevLeaf.data !== nextLeaf.data) {
            return false;
        }
    }
    return true;
};
/**
 * CommentLeaf 组件：专门处理 comment（评论）类型的叶子节点
 * 这是一个包装器组件，用于包裹需要显示评论的 DOM 元素
 */ var CommentLeafComponent = function(param) {
    var children = param.children, leaf = param.leaf, hashId = param.hashId, comment = param.comment;
    var setShowComment = useEditorStore().setShowComment;
    // 使用 useMemo 优化计算逻辑（必须在所有条件返回之前调用）
    var commentId = useMemo(function() {
        return "comment-".concat(leaf === null || leaf === void 0 ? void 0 : leaf.id);
    }, [
        leaf === null || leaf === void 0 ? void 0 : leaf.id
    ]);
    var commentItem = useMemo(function() {
        return (leaf === null || leaf === void 0 ? void 0 : leaf.comment) ? leaf.data : null;
    }, [
        leaf === null || leaf === void 0 ? void 0 : leaf.comment,
        leaf === null || leaf === void 0 ? void 0 : leaf.data
    ]);
    debugInfo('CommentLeaf - 渲染评论节点', {
        hasComment: !!leaf.comment,
        commentId: leaf === null || leaf === void 0 ? void 0 : leaf.id
    });
    // 如果没有评论，直接返回子元素
    if (!leaf.comment) {
        debugInfo('CommentLeaf - 无评论，直接返回子元素');
        return /*#__PURE__*/ React.createElement(React.Fragment, null, children);
    }
    debugInfo('CommentLeaf - 返回带评论的 DOM', {
        commentId: leaf === null || leaf === void 0 ? void 0 : leaf.id,
        hasCommentItem: !!(leaf === null || leaf === void 0 ? void 0 : leaf.comment)
    });
    // 如果有评论，用 CommentView 包裹
    return /*#__PURE__*/ React.createElement(CommentView, {
        id: commentId,
        comment: comment,
        hashId: hashId,
        selection: leaf === null || leaf === void 0 ? void 0 : leaf.selection,
        commentItem: commentItem,
        setShowComment: setShowComment
    }, children);
};
// 使用 React.memo 优化 CommentLeaf 组件的性能
export var CommentLeaf = /*#__PURE__*/ React.memo(CommentLeafComponent, areCommentLeafPropsEqual);
