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 _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_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 _object_spread(target) {
    for(var i = 1; i < arguments.length; i++){
        var source = arguments[i] != null ? arguments[i] : {};
        var ownKeys = Object.keys(source);
        if (typeof Object.getOwnPropertySymbols === "function") {
            ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
                return Object.getOwnPropertyDescriptor(source, sym).enumerable;
            }));
        }
        ownKeys.forEach(function(key) {
            _define_property(target, key, source[key]);
        });
    }
    return target;
}
function ownKeys(object, enumerableOnly) {
    var keys = Object.keys(object);
    if (Object.getOwnPropertySymbols) {
        var symbols = Object.getOwnPropertySymbols(object);
        if (enumerableOnly) {
            symbols = symbols.filter(function(sym) {
                return Object.getOwnPropertyDescriptor(object, sym).enumerable;
            });
        }
        keys.push.apply(keys, symbols);
    }
    return keys;
}
function _object_spread_props(target, source) {
    source = source != null ? source : {};
    if (Object.getOwnPropertyDescriptors) {
        Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
    } else {
        ownKeys(Object(source)).forEach(function(key) {
            Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
        });
    }
    return target;
}
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 _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 classNames from "clsx";
import React, { useContext } from "react";
import { Node } from "slate";
import { I18nContext } from "../../../../I18n";
import { debugInfo } from "../../../../Utils/debugUtils";
import { useSelStatus } from "../../../hooks/editor";
import { useEditorStore } from "../../store";
import { DragHandle } from "../../tools/DragHandle";
export var Paragraph = function Paragraph(props) {
    var _props_element_align;
    var _props_element_otherProps, _markdownEditorRef_current;
    var align = (_props_element_align = props.element.align) !== null && _props_element_align !== void 0 ? _props_element_align : (_props_element_otherProps = props.element.otherProps) === null || _props_element_otherProps === void 0 ? void 0 : _props_element_otherProps.align;
    debugInfo('Paragraph - 渲染段落', {
        align: align,
        children: props.element.children
    });
    var _useEditorStore = useEditorStore(), store = _useEditorStore.store, markdownEditorRef = _useEditorStore.markdownEditorRef, markdownContainerRef = _useEditorStore.markdownContainerRef, readonly = _useEditorStore.readonly, editorProps = _useEditorStore.editorProps;
    var locale = useContext(I18nContext).locale;
    var _useSelStatus = _sliced_to_array(useSelStatus(props.element), 1), selected = _useSelStatus[0];
    return React.useMemo(function() {
        var _props_element_children_every, _props_element_children, _props_element, _markdownEditorRef_current, _props_children;
        var str = Node.string(props.element).trim();
        debugInfo('Paragraph - useMemo 渲染', {
            strLength: str.length,
            selected: selected,
            readonly: readonly,
            align: align
        });
        // 检查是否为空：trim 后的字符串为空（包括只包含空格的情况），且所有子节点都是纯文本节点（没有 type、code、tag）
        // 当只输入空格时，trim() 后为空字符串，应该显示 placeholder
        var hasOnlyTextNodes = (_props_element = props.element) === null || _props_element === void 0 ? void 0 : (_props_element_children = _props_element.children) === null || _props_element_children === void 0 ? void 0 : (_props_element_children_every = _props_element_children.every) === null || _props_element_children_every === void 0 ? void 0 : _props_element_children_every.call(_props_element_children, function(child) {
            return !child.type && !child.code && !child.tag;
        });
        // 当 trim 后的字符串为空时，应该显示 placeholder
        // 需要满足：只有一个段落节点，且所有子节点都是纯文本节点
        // 注意：当只输入空格时，trim() 后为空，!str 为 true，应该正确显示 placeholder
        var isEmpty = !str && ((_markdownEditorRef_current = markdownEditorRef.current) === null || _markdownEditorRef_current === void 0 ? void 0 : _markdownEditorRef_current.children.length) === 1 && hasOnlyTextNodes ? true : undefined;
        return /*#__PURE__*/ React.createElement("div", _object_spread_props(_object_spread({}, props.attributes), {
            "data-be": 'paragraph',
            "data-drag-el": true,
            className: classNames({
                empty: isEmpty
            }),
            "data-align": align,
            "data-slate-placeholder": isEmpty ? editorProps.titlePlaceholderContent || (locale === null || locale === void 0 ? void 0 : locale.inputPlaceholder) || '请输入内容...' : undefined,
            onDragStart: function onDragStart(e) {
                store.dragStart(e, markdownContainerRef.current);
            },
            "data-empty": isEmpty,
            style: {
                display: !!str || !!((_props_children = props.children) === null || _props_children === void 0 ? void 0 : _props_children.at(0).type) ? undefined : 'none',
                textAlign: align
            }
        }), /*#__PURE__*/ React.createElement(DragHandle, null), props.children);
    }, [
        props.element.children,
        align,
        readonly,
        selected,
        (_markdownEditorRef_current = markdownEditorRef.current) === null || _markdownEditorRef_current === void 0 ? void 0 : _markdownEditorRef_current.children.length,
        editorProps.titlePlaceholderContent
    ]);
};
