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 _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;
}
import { ConfigProvider } from "antd";
import classNames from "clsx";
import React, { useCallback, useContext, useMemo } from "react";
import { isMobileDevice } from "../../../../MarkdownInputField/AttachmentButton/utils";
import { dragStart } from "../index";
/**
 * FncLeaf 组件：专门处理 fnc（函数调用）和 fnd（函数定义）类型的叶子节点
 */ export var FncLeaf = function FncLeaf(param) {
    var attributes = param.attributes, children = param.children, leaf = param.leaf, fncProps = param.fncProps, linkConfig = param.linkConfig, _param_style = param.style, style = _param_style === void 0 ? {} : _param_style, _param_prefixClassName = param.prefixClassName, prefixClassName = _param_prefixClassName === void 0 ? '' : _param_prefixClassName;
    var context = useContext(ConfigProvider.ConfigContext);
    var mdEditorBaseClass = context === null || context === void 0 ? void 0 : context.getPrefixCls('agentic-md-editor-content');
    var isMobile = isMobileDevice();
    var hasFnc = leaf.fnc || leaf.identifier;
    // 使用 useMemo 优化 className 计算
    var fncClassName = useMemo(function() {
        var _obj;
        return classNames((_obj = {}, _define_property(_obj, "".concat(mdEditorBaseClass, "-fnc"), leaf.fnc), _define_property(_obj, "".concat(mdEditorBaseClass, "-fnd"), leaf.fnd), _obj));
    }, [
        prefixClassName,
        mdEditorBaseClass,
        leaf.fnc,
        leaf.fnd
    ]);
    // 使用 useMemo 优化文本格式化计算
    var formattedText = useMemo(function() {
        if (leaf.fnc || leaf.identifier) {
            var _leaf_text_replaceAll_replaceAll, _leaf_text_replaceAll, _leaf_text;
            return ((_leaf_text = leaf.text) === null || _leaf_text === void 0 ? void 0 : (_leaf_text_replaceAll = _leaf_text.replaceAll(']', '')) === null || _leaf_text_replaceAll === void 0 ? void 0 : (_leaf_text_replaceAll_replaceAll = _leaf_text_replaceAll.replaceAll('[^DOC_', '')) === null || _leaf_text_replaceAll_replaceAll === void 0 ? void 0 : _leaf_text_replaceAll_replaceAll.replaceAll('[^', '')) || '';
        }
        return children;
    }, [
        leaf.fnc,
        leaf.identifier,
        leaf.text,
        children
    ]);
    // 使用 useMemo 优化 data-fnc-name 和 data-fnd-name 计算
    var dataFncName = useMemo(function() {
        var _leaf_text;
        return leaf.fnc ? (_leaf_text = leaf.text) === null || _leaf_text === void 0 ? void 0 : _leaf_text.replace(/\[\^(.+)]:?/g, '$1') : undefined;
    }, [
        leaf.fnc,
        leaf.text
    ]);
    var dataFndName = useMemo(function() {
        var _leaf_text;
        return leaf.fnd ? (_leaf_text = leaf.text) === null || _leaf_text === void 0 ? void 0 : _leaf_text.replace(/\[\^(.+)]:?/g, '$1') : undefined;
    }, [
        leaf.fnd,
        leaf.text
    ]);
    // 使用 useMemo 优化 style 对象
    var mergedStyle = useMemo(function() {
        return _object_spread({
            fontSize: leaf.fnc ? 10 : undefined
        }, style);
    }, [
        leaf.fnc,
        style
    ]);
    // 使用 useCallback 优化 onClick 处理函数
    var handleClick = useCallback(function(e) {
        // 在手机上，如果是 fnc，阻止点击事件（使用长按代替）
        if (isMobile && hasFnc) {
            e.preventDefault();
            return;
        }
        if (hasFnc) {
            e.preventDefault();
            e.stopPropagation();
            if (fncProps === null || fncProps === void 0 ? void 0 : fncProps.onOriginUrlClick) {
                fncProps.onOriginUrlClick(leaf === null || leaf === void 0 ? void 0 : leaf.identifier);
            }
            // 如果同时有 URL，也要处理 URL 打开逻辑
            if (leaf.url) {
                if (linkConfig === null || linkConfig === void 0 ? void 0 : linkConfig.onClick) {
                    var res = linkConfig.onClick(leaf.url);
                    if (res === false) {
                        return false;
                    }
                }
                if ((linkConfig === null || linkConfig === void 0 ? void 0 : linkConfig.openInNewTab) !== false) {
                    window.open(leaf.url, '_blank');
                } else {
                    window.location.href = leaf.url;
                }
            }
            return false;
        }
    }, [
        isMobile,
        hasFnc,
        fncProps,
        leaf === null || leaf === void 0 ? void 0 : leaf.identifier,
        leaf === null || leaf === void 0 ? void 0 : leaf.url,
        linkConfig
    ]);
    // 使用 useMemo 优化自定义渲染的 children 计算
    var customRenderChildren = useMemo(function() {
        var _leaf_text_toLocaleUpperCase, _leaf_text;
        return ((_leaf_text = leaf.text) === null || _leaf_text === void 0 ? void 0 : (_leaf_text_toLocaleUpperCase = _leaf_text.toLocaleUpperCase()) === null || _leaf_text_toLocaleUpperCase === void 0 ? void 0 : _leaf_text_toLocaleUpperCase.replaceAll('[^', '').replaceAll(']', '')) || '';
    }, [
        leaf.text
    ]);
    var dom = /*#__PURE__*/ React.createElement("span", _object_spread_props(_object_spread({}, attributes), {
        "data-be": "text",
        draggable: false,
        onDragStart: dragStart,
        onClick: handleClick,
        contentEditable: leaf.fnc ? false : undefined,
        "data-fnc": leaf.fnc || leaf.identifier ? 'fnc' : undefined,
        "data-fnd": leaf.fnd ? 'fnd' : undefined,
        "data-fnc-name": dataFncName,
        "data-fnd-name": dataFndName,
        className: fncClassName ? fncClassName : undefined,
        style: mergedStyle
    }), formattedText);
    // 如果提供了自定义渲染函数，使用它
    if ((fncProps === null || fncProps === void 0 ? void 0 : fncProps.render) && (leaf.fnc || leaf.identifier)) {
        var _fncProps_render;
        dom = /*#__PURE__*/ React.createElement(React.Fragment, null, (_fncProps_render = fncProps.render) === null || _fncProps_render === void 0 ? void 0 : _fncProps_render.call(fncProps, _object_spread_props(_object_spread({}, leaf), {
            children: customRenderChildren
        }), dom));
    }
    return dom;
};
