"use strict";
/**
 * 文本提取器模块
 *
 * 提供从原始数据中直接提取显示文本的能力，避免 React 渲染开销。
 * 用于 AI 数据消费、导出等场景。
 */
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
    for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.registerTextExtractor = registerTextExtractor;
exports.clearCustomExtractors = clearCustomExtractors;
exports.extractCellTextValue = extractCellTextValue;
exports.extractRowsData = extractRowsData;
exports.getSupportedFormats = getSupportedFormats;
const server_1 = require("react-dom/server");
const isFunction_1 = __importDefault(require("lodash/isFunction"));
const primary_key_1 = require("../../../../../../../cn-ai-table/view/utils/primary-key");
const isNil_1 = __importDefault(require("lodash/isNil"));
const extractors_1 = require("./extractors");
const ai_cell_constants_1 = require("../../../../../../../cn-ai-table/view/constants/ai-cell-constants");
__exportStar(require("./types"), exports);
/**
 * AI 生成字段文本提取器
 * 空值或生成中状态返回空字符串（不返回 EMPTY_VALUE_DISPLAY，也不提取按钮文字）
 */
function extractAiGenerateText(value) {
    // 空值或生成中状态返回空字符串
    if ((0, isNil_1.default)(value) || value === '' || value === ai_cell_constants_1.AI_GENERATING_MARKER) {
        return '';
    }
    // 有值时返回文本
    return typeof value === 'string' ? value : String(value);
}
/**
 * 创建带本地化的提取器
 */
function createLocalizedExtractor(baseExtractor, localeKey, localeConfig) {
    return (value, column, record) => baseExtractor(value, column, record, localeConfig[localeKey]);
}
/**
 * 获取默认的文本提取器注册表
 */
function getDefaultExtractorRegistry(localeConfig = {}) {
    return {
        // 文本类
        text: { extractor: extractors_1.extractTextValue, priority: 10 },
        ellipsis: { extractor: extractors_1.extractEllipsisText, priority: 10 },
        'multiple-text': { extractor: extractors_1.extractMultipleTextValue, priority: 10 },
        'image-text': { extractor: extractors_1.extractImageTextValue, priority: 10 },
        // 日期类
        date: {
            extractor: createLocalizedExtractor(extractors_1.extractDateText, 'dateAdapterLocale', localeConfig),
            priority: 5,
        },
        'date-time': {
            extractor: createLocalizedExtractor(extractors_1.extractDateTimeText, 'dateAdapterLocale', localeConfig),
            priority: 5,
        },
        // 数字类
        number: {
            extractor: createLocalizedExtractor(extractors_1.extractNumberText, 'numAdapterLocale', localeConfig),
            priority: 5,
        },
        currency: {
            extractor: createLocalizedExtractor(extractors_1.extractCurrencyText, 'numAdapterLocale', localeConfig),
            priority: 5,
        },
        'currency-with-unit': {
            extractor: createLocalizedExtractor(extractors_1.extractCurrencyWithUnitText, 'numAdapterLocale', localeConfig),
            priority: 5,
        },
        percent: {
            extractor: createLocalizedExtractor(extractors_1.extractPercentText, 'numAdapterLocale', localeConfig),
            priority: 5,
        },
        micrometer: {
            extractor: createLocalizedExtractor(extractors_1.extractMicrometerText, 'numAdapterLocale', localeConfig),
            priority: 5,
        },
        progress: {
            extractor: createLocalizedExtractor(extractors_1.extractProgressText, 'numAdapterLocale', localeConfig),
            priority: 5,
        },
        'graphics-percent': {
            extractor: createLocalizedExtractor(extractors_1.extractGraphicsPercentText, 'numAdapterLocale', localeConfig),
            priority: 5,
        },
        // 特殊类型
        user: { extractor: extractors_1.extractUserText, priority: 5 },
        tag: { extractor: extractors_1.extractTagText, priority: 5 },
        file: { extractor: extractors_1.extractFileText, priority: 5 },
        bank: { extractor: extractors_1.extractBankText, priority: 5 },
        // AI 字段类型
        'ai-generate': { extractor: extractAiGenerateText, priority: 5 },
    };
}
// 自定义提取器注册表
let customExtractorRegistry = {};
/**
 * 注册自定义文本提取器
 */
function registerTextExtractor(format, extractor, priority = 10) {
    customExtractorRegistry[format.toLowerCase()] = { extractor, priority };
}
/**
 * 清除自定义提取器
 */
function clearCustomExtractors() {
    customExtractorRegistry = {};
}
/**
 * 从 React 节点中提取文本内容
 * 作为 fallback 方案
 */
function extractTextFromReactNode(reactNode) {
    var _a;
    if (reactNode === null || reactNode === undefined) {
        return '';
    }
    // 如果是字符串或数字，直接返回
    if (typeof reactNode === 'string' || typeof reactNode === 'number') {
        return String(reactNode);
    }
    // 如果是布尔值，返回空字符串
    if (typeof reactNode === 'boolean') {
        return '';
    }
    // 如果是数组，递归处理每个元素
    if (Array.isArray(reactNode)) {
        return reactNode.map((node) => extractTextFromReactNode(node)).join(' ');
    }
    // 如果是 React 元素，使用 renderToString 转换为 HTML，然后提取文本
    try {
        const htmlString = (0, server_1.renderToString)(reactNode);
        // 创建一个临时 DOM 元素来提取文本
        const tempDiv = document.createElement('div');
        tempDiv.innerHTML = htmlString;
        // 使用 TreeWalker 遍历所有文本节点
        let text = '';
        const walker = document.createTreeWalker(tempDiv, NodeFilter.SHOW_TEXT, null);
        let node = walker.nextNode();
        while (node) {
            const textContent = (_a = node.textContent) === null || _a === void 0 ? void 0 : _a.trim();
            if (textContent) {
                text = `${text}${textContent} `;
            }
            node = walker.nextNode();
        }
        return text.trim();
    }
    catch {
        return '';
    }
}
/**
 * 核心提取函数 - 从单元格提取文本
 *
 * 提取策略（按优先级）：
 * 1. 使用注册的纯文本提取器（最高效）
 * 2. 调用 cell 函数，如果返回字符串直接使用
 * 3. 调用 cell 函数返回 ReactNode，通过 renderToString 提取文本
 * 4. Fallback 到原始值
 *
 * @param value 原始值
 * @param column 列配置
 * @param record 行数据
 * @param processedColumn 处理后的列配置（包含 cell 函数）
 * @param context 本地化上下文
 * @param rowIndex 行索引
 */
function extractCellTextValue(value, column, record, processedColumn, context, rowIndex = 0) {
    var _a, _b;
    const format = (column.format || (processedColumn === null || processedColumn === void 0 ? void 0 : processedColumn.format) || 'text').toLowerCase();
    // 本地化配置
    const localeConfig = {
        numAdapterLocale: (_a = column === null || column === void 0 ? void 0 : column.adapterLocale) !== null && _a !== void 0 ? _a : context === null || context === void 0 ? void 0 : context.numAdapterLocale,
        dateAdapterLocale: (_b = column === null || column === void 0 ? void 0 : column.adapterLocale) !== null && _b !== void 0 ? _b : context === null || context === void 0 ? void 0 : context.dateAdapterLocale,
    };
    // 获取提取器注册表
    const defaultRegistry = getDefaultExtractorRegistry(localeConfig);
    const combinedRegistry = { ...defaultRegistry, ...customExtractorRegistry };
    // 策略1: 使用注册的纯文本提取器
    const extractorConfig = combinedRegistry[format];
    if (extractorConfig) {
        try {
            const text = extractorConfig.extractor(value, column, record);
            if (text !== null) {
                return { text, method: 'extractor', format };
            }
        }
        catch {
            // 提取器失败，继续下一个策略
        }
    }
    // 获取 cell 函数
    const cellFn = (processedColumn === null || processedColumn === void 0 ? void 0 : processedColumn.cell) || column.cell;
    const renderFn = (processedColumn === null || processedColumn === void 0 ? void 0 : processedColumn.render) || column.render;
    const fn = cellFn || renderFn;
    // 策略2&3: 使用 cell/render 函数
    if ((0, isFunction_1.default)(fn)) {
        try {
            const result = fn(value, rowIndex, record);
            // 如果返回字符串，直接使用
            if (typeof result === 'string') {
                return { text: result, method: 'cell-string', format };
            }
            // 如果返回数字，转换为字符串
            if (typeof result === 'number') {
                return { text: String(result), method: 'cell-string', format };
            }
            // 如果返回 ReactNode，提取文本
            if (result !== null && result !== undefined) {
                const extractedText = extractTextFromReactNode(result);
                if (extractedText) {
                    return { text: extractedText, method: 'react-render', format };
                }
            }
        }
        catch {
            // cell 函数执行失败，继续下一个策略
        }
    }
    // 策略4: Fallback 到原始值
    if (value === null || value === undefined) {
        return { text: '', method: 'fallback', format };
    }
    if (typeof value === 'object') {
        // 尝试提取常见属性
        const fallbackText = value.label || value.text || value.name || value.value || JSON.stringify(value);
        return { text: String(fallbackText), method: 'fallback', format };
    }
    return { text: String(value), method: 'fallback', format };
}
/**
 * 批量提取行数据
 *
 * @param viewData 视图数据数组
 * @param allColumns 全量列定义数组
 * @param processedColumnMap 处理后的列定义映射
 * @param primaryKey 主键（字段名或函数），用于生成 id
 * @param context 本地化上下文
 */
function extractRowsData(viewData, allColumns, processedColumnMap, primaryKey, context) {
    return viewData.map((row, rowIndex) => {
        const rowKey = (0, primary_key_1.safeGetRowKey)(row, primaryKey, rowIndex);
        const rowData = {
            id: rowKey !== undefined && rowKey !== null ? String(rowKey) : '',
        };
        allColumns.forEach((col) => {
            const value = row[col.dataIndex];
            const processedCol = processedColumnMap.get(col.dataIndex);
            const result = extractCellTextValue(value, col, row, processedCol, context, rowIndex);
            rowData[col.dataIndex] = result.text;
        });
        return rowData;
    });
}
/**
 * 获取所有支持的格式类型
 */
function getSupportedFormats(context) {
    const localeConfig = {
        numAdapterLocale: context === null || context === void 0 ? void 0 : context.numAdapterLocale,
        dateAdapterLocale: context === null || context === void 0 ? void 0 : context.dateAdapterLocale,
    };
    const defaultRegistry = getDefaultExtractorRegistry(localeConfig);
    return [...Object.keys(defaultRegistry), ...Object.keys(customExtractorRegistry)];
}
