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 _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 { FullscreenOutlined } from "@ant-design/icons";
import { Copy } from "@sofa-design/icons";
import { ConfigProvider, Modal } from "antd";
import classNames from "classnames";
import copy from "copy-to-clipboard";
import React, { useCallback, useContext, useMemo, useRef, useState } from "react";
import { ActionIconBox } from "../../../../Components/ActionIconBox";
import { I18nContext } from "../../../../I18n";
import { useEditorStore } from "../../store";
import { parserSlateNodeToMarkdown } from "../../utils";
/**
 * 专门针对 readonly 模式优化的表格组件
 * 移除了不必要的滚动监听和复杂的宽度计算
 */ export var ReadonlyTableComponent = /*#__PURE__*/ React.memo(function(param) {
    var hashId = param.hashId, children = param.children, element = param.element, baseCls = param.baseCls;
    var _element_children__children, _element_children_, _element_children, _editorProps_tableConfig, _i18n_locale;
    var editorProps = useEditorStore().editorProps;
    var getPrefixCls = useContext(ConfigProvider.ConfigContext).getPrefixCls;
    var _ref = (editorProps === null || editorProps === void 0 ? void 0 : editorProps.tableConfig) || {}, _ref_actions = _ref.actions, actions = _ref_actions === void 0 ? {
        download: 'csv',
        fullScreen: 'modal',
        copy: 'md'
    } : _ref_actions;
    var tableTargetRef = useRef(null);
    var modelTargetRef = useRef(null);
    var _useState = _sliced_to_array(useState(false), 2), previewOpen = _useState[0], setPreviewOpen = _useState[1];
    var i18n = useContext(I18nContext);
    // 简化的列宽计算 - 只为 readonly 模式设计
    var colWidths = useMemo(function() {
        var _element_children__children, _element_children_, _element_children;
        var otherProps = element === null || element === void 0 ? void 0 : element.otherProps;
        if (otherProps === null || otherProps === void 0 ? void 0 : otherProps.colWidths) {
            return otherProps.colWidths;
        }
        var columnCount = (element === null || element === void 0 ? void 0 : (_element_children = element.children) === null || _element_children === void 0 ? void 0 : (_element_children_ = _element_children[0]) === null || _element_children_ === void 0 ? void 0 : (_element_children__children = _element_children_.children) === null || _element_children__children === void 0 ? void 0 : _element_children__children.length) || 0;
        if (columnCount === 0) return [];
        // 使用固定宽度避免复杂计算
        var defaultWidth = 80;
        return Array(columnCount).fill(defaultWidth);
    }, [
        element === null || element === void 0 ? void 0 : element.otherProps,
        element === null || element === void 0 ? void 0 : (_element_children = element.children) === null || _element_children === void 0 ? void 0 : (_element_children_ = _element_children[0]) === null || _element_children_ === void 0 ? void 0 : (_element_children__children = _element_children_.children) === null || _element_children__children === void 0 ? void 0 : _element_children__children.length
    ]);
    // 缓存复制处理函数
    var handleCopy = useCallback(function() {
        try {
            var contentToCopy = '';
            // 根据复制类型确定要复制的内容
            if ((actions === null || actions === void 0 ? void 0 : actions.copy) === 'html') {
                var _tableTargetRef_current;
                contentToCopy = ((_tableTargetRef_current = tableTargetRef.current) === null || _tableTargetRef_current === void 0 ? void 0 : _tableTargetRef_current.innerHTML) || '';
            } else if ((actions === null || actions === void 0 ? void 0 : actions.copy) === 'csv') {
                var otherProps = element === null || element === void 0 ? void 0 : element.otherProps;
                if ((otherProps === null || otherProps === void 0 ? void 0 : otherProps.columns) && (otherProps === null || otherProps === void 0 ? void 0 : otherProps.dataSource)) {
                    contentToCopy = otherProps.columns.map(function(col) {
                        return col.title;
                    }).join(',') + '\n' + otherProps.dataSource.map(function(row) {
                        return Object.values(row).join(',');
                    }).join('\n');
                }
            } else {
                // 默认复制 Markdown 格式
                contentToCopy = parserSlateNodeToMarkdown([
                    element
                ]);
            }
            // 使用 copy-to-clipboard 库进行复制
            copy(contentToCopy);
        } catch (error) {
            console.error('Copy failed:', error);
        }
    }, [
        element,
        actions === null || actions === void 0 ? void 0 : actions.copy
    ]);
    // 缓存全屏处理函数
    var handleFullScreen = useCallback(function(e) {
        e.stopPropagation();
        setPreviewOpen(true);
    }, []);
    // 缓存模态框关闭函数
    var handleModalClose = useCallback(function() {
        setPreviewOpen(false);
    }, []);
    // 缓存表格DOM
    var tableDom = useMemo(function() {
        var _editorProps_tableConfig;
        return /*#__PURE__*/ React.createElement("table", {
            ref: tableTargetRef,
            className: classNames("".concat(baseCls, "-editor-table"), 'readonly', hashId, "".concat(baseCls, "-readonly-table"), _define_property({}, "".concat(baseCls, "-readonly-pure"), editorProps === null || editorProps === void 0 ? void 0 : (_editorProps_tableConfig = editorProps.tableConfig) === null || _editorProps_tableConfig === void 0 ? void 0 : _editorProps_tableConfig.pure))
        }, /*#__PURE__*/ React.createElement("colgroup", null, colWidths.map(function(colWidth, index) {
            return /*#__PURE__*/ React.createElement("col", {
                key: index,
                style: {
                    width: colWidth,
                    minWidth: colWidth,
                    maxWidth: colWidth
                }
            });
        })), /*#__PURE__*/ React.createElement("tbody", null, children));
    }, [
        colWidths,
        children,
        hashId,
        baseCls
    ]);
    // 缓存操作按钮内容
    var popoverContent = useMemo(function() {
        var _i18n_locale, _i18n_locale1;
        return /*#__PURE__*/ React.createElement("div", {
            className: classNames(hashId, "".concat(baseCls, "-readonly-table-actions"))
        }, (actions === null || actions === void 0 ? void 0 : actions.fullScreen) && /*#__PURE__*/ React.createElement(ActionIconBox, {
            title: (i18n === null || i18n === void 0 ? void 0 : (_i18n_locale = i18n.locale) === null || _i18n_locale === void 0 ? void 0 : _i18n_locale.fullScreen) || '全屏',
            onClick: handleFullScreen
        }, /*#__PURE__*/ React.createElement(FullscreenOutlined, null)), (actions === null || actions === void 0 ? void 0 : actions.copy) && /*#__PURE__*/ React.createElement(ActionIconBox, {
            title: (i18n === null || i18n === void 0 ? void 0 : (_i18n_locale1 = i18n.locale) === null || _i18n_locale1 === void 0 ? void 0 : _i18n_locale1.copy) || '复制',
            onClick: handleCopy
        }, /*#__PURE__*/ React.createElement(Copy, null)));
    }, [
        actions === null || actions === void 0 ? void 0 : actions.fullScreen,
        actions === null || actions === void 0 ? void 0 : actions.copy,
        i18n === null || i18n === void 0 ? void 0 : i18n.locale,
        handleFullScreen,
        handleCopy
    ]);
    return /*#__PURE__*/ React.createElement(React.Fragment, null, /*#__PURE__*/ React.createElement("div", {
        className: classNames(baseCls, hashId)
    }, tableDom), popoverContent, previewOpen && /*#__PURE__*/ React.createElement(Modal, {
        title: (editorProps === null || editorProps === void 0 ? void 0 : (_editorProps_tableConfig = editorProps.tableConfig) === null || _editorProps_tableConfig === void 0 ? void 0 : _editorProps_tableConfig.previewTitle) || (i18n === null || i18n === void 0 ? void 0 : (_i18n_locale = i18n.locale) === null || _i18n_locale === void 0 ? void 0 : _i18n_locale.previewTable) || '预览表格',
        open: previewOpen,
        closable: true,
        footer: null,
        afterClose: handleModalClose,
        width: "80vw",
        onCancel: handleModalClose
    }, /*#__PURE__*/ React.createElement("div", {
        className: classNames(baseCls, hashId, getPrefixCls('agentic-md-editor-content')),
        style: {
            flex: 1,
            minWidth: 0,
            overflow: 'auto',
            width: 'calc(80vw - 64px)'
        },
        ref: modelTargetRef,
        onMouseDown: function(e) {
            // 阻止默认的文字选择行为
            e.preventDefault();
        },
        onDragStart: function(e) {
            // 阻止拖拽开始时的文字选择
            e.preventDefault();
        },
        onDoubleClick: function(e) {
            // 阻止双击选择文字
            e.preventDefault();
        }
    }, /*#__PURE__*/ React.createElement(ConfigProvider, {
        getPopupContainer: function() {
            return modelTargetRef.current || document.body;
        },
        getTargetContainer: function() {
            return modelTargetRef.current || document.body;
        }
    }, tableDom))));
});
ReadonlyTableComponent.displayName = 'ReadonlyTableComponent';
