import $i18n from '../../../../../../../../locales/i18n.js';
import * as React from 'react';
import isNil from 'lodash/isNil';
import { CnButton } from '@cainiaofe/cn-ui';
import { event as eventBus } from '@ice/stark-data';
import { useCnAiListEventNames } from '../../../../../../../../cn-ai-agent/context/cn-ai-list-event-context';
import { AI_GENERATING_MARKER } from '../../../../../../../../cn-ai-table/view/constants/ai-cell-constants';
import { SvgIcon } from '../../../../../../../../cn-ai-table/view/components/icons';
import { armsHottagLog } from '../../../../../../../../utils/arms/arms-hot-tag-log.js';
import '../styles/index.scss';
// 导出常量以便其他模块使用
export { AI_GENERATING_MARKER };
function AiGenerateTrigger({ columnCode, aiConfig, record, rowIndex, }) {
    const eventNames = useCnAiListEventNames();
    const handleGenerate = (e) => {
        e.stopPropagation();
        armsHottagLog({
            hottag: 'cn-ai-table.aiField.generate',
            name: $i18n.get({ id: 'GenerateImmediately', dm: '立即生成' }),
            c6: String(rowIndex),
            c7: columnCode,
        });
        eventBus.emit(eventNames.SUBMIT_SINGLE_CELL_AI_TASK, {
            columnCode,
            aiConfig,
            record,
            rowIndex,
        });
    };
    return (React.createElement("div", { className: "cn-table-cell-format-ai-generate" },
        React.createElement(CnButton, { type: "normal", size: "small", onClick: handleGenerate, style: {
                padding: '0 8px',
                height: '24px',
                fontSize: '12px',
            } }, $i18n.get({ id: 'GenerateImmediately', dm: '立即生成' }))));
}
/**
 * AI 字段（generate-text）格式化工厂函数
 * - 空值时显示"生成"按钮，点击触发 AI 生成任务
 * - 生成中时显示 loading 状态
 * - 有值时正常显示文本（带 Tooltip）
 */
export const createAiGenerateFormat = (column) => {
    const { dataIndex, aiConfig } = column;
    const columnCode = dataIndex || column.accessorKey;
    return {
        cell(value, rowIndex, record) {
            // 生成中状态：显示 loading
            if (value === AI_GENERATING_MARKER) {
                return (React.createElement("div", { className: "cn-table-cell-format-ai-generating" },
                    React.createElement("div", { className: "cn-table-cell-format-ai-generating-content" },
                        React.createElement(SvgIcon, { name: "aiSparkle", size: 16 }),
                        React.createElement("span", { style: { color: '#999', fontSize: '12px' } }, $i18n.get({ id: 'Generating', dm: '生成中' })))));
            }
            // 空值时显示"生成"按钮
            if (isNil(value) || value === '') {
                if (!columnCode) {
                    return null;
                }
                return (React.createElement(AiGenerateTrigger, { columnCode: columnCode, aiConfig: aiConfig, record: record, rowIndex: rowIndex }));
            }
            // 有值时显示文本（带 Tooltip）
            const textValue = typeof value === 'string' ? value : String(value);
            return React.createElement("span", null, textValue);
        },
    };
};
export default createAiGenerateFormat;
