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, InsertRowAboveOutlined, InsertRowBelowOutlined } 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, insertTableRow, removeTableRow, selectTableRow } from "../commands/tableCommands";
import { TablePropsContext } from "../TableContext";
/**
 * TableCellIndex 组件 - 表格行索引单元格组件
 *
 * 该组件用于渲染表格行的索引单元格，显示行号或索引信息。
 * 使用 context 来生成 className，支持样式自定义。
 *
 * @component
 * @description 表格行索引单元格组件，用于显示行索引
 * @param {TableCellIndexProps} props - 组件属性
 * @param {React.CSSProperties} [props.style] - 自定义样式
 * @param {string} [props.className] - 自定义类名
 * @param {any} props.targetRow - 目标行元素
 * @param {number} [props.rowIndex] - 行索引，用于选中整行
 * @param {number[]} [props.tablePath] - 表格路径，用于定位表格元素
 *
 * @example
 * ```tsx
 * <TableCellIndex
 *   style={{ backgroundColor: '#f5f5f5' }}
 *   className="custom-index-cell"
 *   targetRow={rowElement}
 *   rowIndex={0}
 *   tablePath={[0]}
 * />
 * ```
 *
 * @returns {React.ReactElement} 渲染的表格索引单元格组件
 *
 * @remarks
 * - 使用 ConfigProvider context 生成 className
 * - 支持自定义样式覆盖
 * - 不可编辑状态
 * - 固定宽度和垂直对齐
 * - 支持点击选中整行功能
 */ export var TableCellIndex = function TableCellIndex(param) {
    var style = param.style, className = param.className, rowIndex = param.rowIndex, 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');
    var editor = useSlate();
    var tableContext = useContext(TablePropsContext);
    var deleteIconPosition = tableContext.deleteIconPosition, setDeleteIconPosition = tableContext.setDeleteIconPosition;
    /**
   * 清除表格中所有单元格的选中状态
   */ 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;
        }
        try {
            clearTableSelection(editor, tablePath);
        } catch (error) {
            console.warn('Failed to clear table selection:', error);
        }
    });
    /**
   * 处理点击事件，选中整行或显示删除图标
   */ var handleClick = useRefFunction(function(e) {
        e.preventDefault();
        e.stopPropagation();
        // 如果提供了行索引，显示删除图标
        if (rowIndex !== undefined) {
            setDeleteIconPosition === null || setDeleteIconPosition === void 0 ? void 0 : setDeleteIconPosition({
                rowIndex: rowIndex,
                columnIndex: undefined
            });
        }
        if (rowIndex === undefined || !tablePath) {
            return;
        }
        try {
            clearSelect(false);
            selectTableRow(editor, tablePath, rowIndex);
        } catch (error) {
            console.warn('Failed to select table row:', error);
        }
    });
    /**
   * 处理删除图标点击事件
   */ var handleDeleteClick = useRefFunction(function(e) {
        e.preventDefault();
        e.stopPropagation();
        try {
            if (!tablePath || rowIndex === undefined) {
                return;
            }
            removeTableRow(editor, tablePath, rowIndex);
            clearSelect();
        } catch (error) {
            console.warn('Failed to delete table row:', error);
        }
    });
    /**
   * 处理在前面增加一行点击事件
   */ var handleInsertRowBefore = useRefFunction(function(e) {
        e.preventDefault();
        e.stopPropagation();
        try {
            if (!tablePath || rowIndex === undefined) {
                return;
            }
            insertTableRow(editor, tablePath, rowIndex, 'before');
            clearSelect();
        } catch (error) {
            console.warn('Failed to insert row before:', error);
        }
    });
    /**
   * 处理在后面增加一行点击事件
   */ var handleInsertRowAfter = useRefFunction(function(e) {
        e.preventDefault();
        e.stopPropagation();
        try {
            if (!tablePath || rowIndex === undefined) {
                return;
            }
            insertTableRow(editor, tablePath, rowIndex, 'after');
            clearSelect();
        } catch (error) {
            console.warn('Failed to insert row after:', error);
        }
    });
    var ref = useRef(null);
    // 检查是否应该显示删除图标
    var shouldShowDeleteIcon = deleteIconPosition && deleteIconPosition.rowIndex === rowIndex && deleteIconPosition.columnIndex === undefined;
    useClickAway(function() {
        if (shouldShowDeleteIcon && deleteIconPosition && deleteIconPosition.rowIndex === rowIndex) {
            clearSelect();
        }
    }, ref);
    return /*#__PURE__*/ React.createElement("td", {
        ref: ref,
        className: classNames(baseClassName, className, 'config-td'),
        contentEditable: false,
        style: _object_spread({
            padding: 0,
            cursor: rowIndex !== undefined ? 'pointer' : 'default',
            position: 'relative',
            backgroundColor: shouldShowDeleteIcon ? 'var(--color-primary-control-fill-primary-active)' : undefined
        }, style),
        onClick: handleClick,
        title: rowIndex !== undefined ? (locale === null || locale === void 0 ? void 0 : locale['table.clickToShowActions']) || '点击显示操作按钮' : undefined
    }, /*#__PURE__*/ React.createElement("div", {
        className: classNames("".concat(baseClassName, "-action-buttons"), shouldShowDeleteIcon && "".concat(baseClassName, "-action-buttons-visible"))
    }, shouldShowDeleteIcon && /*#__PURE__*/ React.createElement("div", {
        className: classNames("".concat(baseClassName, "-action-button"), "".concat(baseClassName, "-insert-row-before")),
        onClick: handleInsertRowBefore,
        title: (locale === null || locale === void 0 ? void 0 : locale['table.insertRowBefore']) || '在上面增加一行'
    }, /*#__PURE__*/ React.createElement(InsertRowAboveOutlined, 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.deleteRow']) || '删除整行'
    }, /*#__PURE__*/ React.createElement(DeleteOutlined, null)), shouldShowDeleteIcon && /*#__PURE__*/ React.createElement("div", {
        className: classNames("".concat(baseClassName, "-action-button"), "".concat(baseClassName, "-insert-row-after")),
        onClick: handleInsertRowAfter,
        title: (locale === null || locale === void 0 ? void 0 : locale['table.insertRowAfter']) || '在下面增加一行'
    }, /*#__PURE__*/ React.createElement(InsertRowBelowOutlined, null))));
};
