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;
}
import { DeleteOutlined, InsertRowLeftOutlined, InsertRowRightOutlined } from "@ant-design/icons";
import { ConfigProvider } from "antd";
import classNames from "clsx";
import React, { useContext, useRef } from "react";
import { useSlate } from "slate-react";
import { useClickAway } from "../../../../../Hooks/useClickAway";
import { useRefFunction } from "../../../../../Hooks/useRefFunction";
import { I18nContext } from "../../../../../I18n";
import { clearTableSelection, insertTableColumn, removeTableColumn, selectTableColumn, selectWholeTable } from "../commands/tableCommands";
import { TablePropsContext } from "../TableContext";
/**
 * TableCellIndexSpacer 组件 - 表格索引间隔单元格组件
 *
 * 该组件用于渲染表格行索引中的间隔单元格，用于占位和布局。
 * 使用 context 来生成 className，支持样式自定义。
 *
 * @component
 * @description 表格索引间隔单元格组件，用于占位和布局
 * @param {TableCellIndexSpacerProps} props - 组件属性
 * @param {React.CSSProperties} [props.style] - 自定义样式
 * @param {string} [props.className] - 自定义类名
 * @param {React.Key} [props.key] - 单元格的唯一标识
 * @param {number} [props.columnIndex] - 列索引，用于选中整列
 * @param {number[]} [props.tablePath] - 表格路径，用于定位表格元素
 *
 * @example
 * ```tsx
 * <TableCellIndexSpacer
 *   style={{ backgroundColor: '#f5f5f5' }}
 *   className="custom-spacer-cell"
 *   key={0}
 *   columnIndex={1}
 *   tablePath={[0, 2]}
 * />
 * ```
 *
 * @returns {React.ReactElement} 渲染的表格索引间隔单元格组件
 *
 * @remarks
 * - 使用 ConfigProvider context 生成 className
 * - 支持自定义样式覆盖
 * - 不可编辑状态
 * - 用于占位和布局
 * - 支持点击选中整列功能
 */ export var TableCellIndexSpacer = function TableCellIndexSpacer(param) {
    var style = param.style, className = param.className, columnIndex = param.columnIndex, tablePath = param.tablePath;
    var locale = useContext(I18nContext).locale;
    var context = useContext(ConfigProvider.ConfigContext);
    var baseClassName = context === null || context === void 0 ? void 0 : context.getPrefixCls('agentic-md-editor-table-cell-index-spacer');
    var editor = useSlate();
    var tableContext = useContext(TablePropsContext);
    var deleteIconPosition = tableContext.deleteIconPosition, setDeleteIconPosition = tableContext.setDeleteIconPosition;
    var isSelectWholeTable = columnIndex === -1;
    var actionColumnIndex = isSelectWholeTable ? 0 : columnIndex;
    var clearSelect = useRefFunction(function() {
        var clearIcon = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : true;
        if (clearIcon) {
            setDeleteIconPosition === null || setDeleteIconPosition === void 0 ? void 0 : setDeleteIconPosition(null);
        }
        if (!tablePath) return;
        clearTableSelection(editor, tablePath);
    });
    /**
   * 处理点击事件，选中整列或显示删除图标
   */ var handleClick = useRefFunction(function(e) {
        e.preventDefault();
        e.stopPropagation();
        // 如果提供了列索引，显示删除图标
        if (columnIndex !== undefined) {
            setDeleteIconPosition === null || setDeleteIconPosition === void 0 ? void 0 : setDeleteIconPosition({
                columnIndex: columnIndex
            });
        }
        if (columnIndex === undefined || !tablePath) {
            return;
        }
        try {
            clearSelect(false);
            if (isSelectWholeTable) {
                selectWholeTable(editor, tablePath);
                return;
            }
            selectTableColumn(editor, tablePath, columnIndex);
        } catch (error) {
            console.warn('Failed to select table column:', error);
        }
    });
    /**
   * 处理删除图标点击事件
   */ var handleDeleteClick = useRefFunction(function(e) {
        e.preventDefault();
        e.stopPropagation();
        try {
            if (!tablePath || actionColumnIndex === undefined) {
                return;
            }
            removeTableColumn(editor, tablePath, actionColumnIndex);
            clearSelect();
        } catch (error) {
            console.warn('Failed to delete table column:', error);
        }
    });
    /**
   * 处理在前面增加一列点击事件
   */ var handleInsertColumnBefore = useRefFunction(function(e) {
        e.preventDefault();
        e.stopPropagation();
        try {
            if (!tablePath || actionColumnIndex === undefined) {
                return;
            }
            insertTableColumn(editor, tablePath, actionColumnIndex, 'before');
            clearSelect();
        } catch (error) {
            console.warn('Failed to insert column before:', error);
        }
    });
    /**
   * 处理在后面增加一列点击事件
   */ var handleInsertColumnAfter = useRefFunction(function(e) {
        e.preventDefault();
        e.stopPropagation();
        try {
            if (!tablePath || actionColumnIndex === undefined) {
                return;
            }
            insertTableColumn(editor, tablePath, actionColumnIndex, 'after');
            clearSelect();
        } catch (error) {
            console.warn('Failed to insert column after:', error);
        }
    });
    var ref = useRef(null);
    useClickAway(function() {
        if (deleteIconPosition && deleteIconPosition.columnIndex === columnIndex) {
            clearSelect();
        }
    }, ref);
    // 检查是否应该显示删除图标
    var shouldShowDeleteIcon = deleteIconPosition && deleteIconPosition.columnIndex === columnIndex;
    // 判断是否应该显示增加列的按钮（总是显示）
    var shouldShowInsertButtons = shouldShowDeleteIcon;
    return /*#__PURE__*/ React.createElement("td", {
        ref: ref,
        className: classNames(baseClassName, className, 'config-td'),
        contentEditable: false,
        style: _object_spread({
            cursor: columnIndex !== undefined ? 'pointer' : 'default',
            padding: 0,
            position: 'relative',
            backgroundColor: shouldShowDeleteIcon ? 'var(--color-primary-control-fill-primary-active)' : undefined
        }, style),
        onClick: handleClick,
        title: columnIndex !== undefined ? columnIndex === -1 ? (locale === null || locale === void 0 ? void 0 : locale['table.clickToSelectTable']) || '点击选中整个表格' : (locale === null || locale === void 0 ? void 0 : locale['table.clickToSelectColumn']) || '点击选中整列，显示操作按钮' : undefined
    }, /*#__PURE__*/ React.createElement("div", {
        className: classNames("".concat(baseClassName, "-action-buttons"), shouldShowDeleteIcon && "".concat(baseClassName, "-action-buttons-visible"))
    }, shouldShowInsertButtons && /*#__PURE__*/ React.createElement("div", {
        className: classNames("".concat(baseClassName, "-action-button"), "".concat(baseClassName, "-insert-column-before")),
        onClick: handleInsertColumnBefore,
        title: (locale === null || locale === void 0 ? void 0 : locale['table.insertColumnBefore']) || '在前面增加一列'
    }, /*#__PURE__*/ React.createElement(InsertRowLeftOutlined, null)), /*#__PURE__*/ React.createElement("div", {
        className: classNames("".concat(baseClassName, "-action-button"), "".concat(baseClassName, "-delete-icon")),
        onClick: handleDeleteClick,
        title: (locale === null || locale === void 0 ? void 0 : locale['table.deleteColumn']) || '删除整列'
    }, /*#__PURE__*/ React.createElement(DeleteOutlined, null)), shouldShowInsertButtons && /*#__PURE__*/ React.createElement("div", {
        className: classNames("".concat(baseClassName, "-action-button"), "".concat(baseClassName, "-insert-column-after")),
        onClick: handleInsertColumnAfter,
        title: (locale === null || locale === void 0 ? void 0 : locale['table.insertColumnAfter']) || '在后面增加一列'
    }, /*#__PURE__*/ React.createElement(InsertRowRightOutlined, null))));
};
