"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useDetailView = void 0;
const react_1 = __importDefault(require("react"));
const useCellFormat_1 = require("../../../../hooks/useCellFormat");
const hooks_1 = require("../../../../../hooks/index.js");
const pipeline_1 = require("../../views/table-view/pipeline");
const extract_table_data_1 = require("../../views/table-view/utils/extract-table-data");
// 确保插件已注册
require("../../views/table-view/plugins");
/**
 * 详情视图 Hook
 * 封装详情视图的通用逻辑
 */
const useDetailView = ({ viewData, viewColumns, columnDefinitions, selectedIndex, setSelectedIndex, }) => {
    // 从低代码配置中获取操作列配置
    const { operateColumn } = (0, hooks_1.useLowcodeConfig)();
    // 使用 useCellFormat Hook 进行单元格格式化
    const { processedColumnMap } = (0, useCellFormat_1.useCellFormat)(columnDefinitions);
    // 当前选中的记录
    const selectedRecord = react_1.default.useMemo(() => {
        if (viewData.length === 0)
            return null;
        const index = Math.max(0, Math.min(selectedIndex, viewData.length - 1));
        return viewData[index];
    }, [viewData, selectedIndex]);
    // 进入页面时默认打开第一个卡片详情
    react_1.default.useEffect(() => {
        if (viewData.length > 0) {
            if (selectedIndex >= viewData.length || selectedIndex < 0) {
                setSelectedIndex(0);
            }
        }
    }, [viewData.length, selectedIndex, setSelectedIndex]);
    // 渲染单元格内容
    const renderCellValue = react_1.default.useCallback((col, value, rowIndex, rowData) => {
        const processedCol = processedColumnMap.get(col.dataIndex);
        if (processedCol === null || processedCol === void 0 ? void 0 : processedCol.cell) {
            return processedCol.cell(value, rowIndex, rowData);
        }
        if (processedCol === null || processedCol === void 0 ? void 0 : processedCol.render) {
            return processedCol.render(value, rowIndex, rowData);
        }
        return value !== null && value !== void 0 ? value : '- -';
    }, [processedColumnMap]);
    const detailColumns = react_1.default.useMemo(() => viewColumns.filter((col) => Boolean(col && typeof col.dataIndex === 'string')), [viewColumns]);
    const detailItems = react_1.default.useMemo(() => {
        if (!selectedRecord) {
            return [];
        }
        return detailColumns.map((col) => {
            const rawValue = selectedRecord[col.dataIndex];
            return {
                dataIndex: col.dataIndex,
                title: col.title,
                rawValue,
                renderedValue: renderCellValue(col, rawValue, selectedIndex, selectedRecord),
                column: col,
            };
        });
    }, [detailColumns, selectedRecord, selectedIndex, renderCellValue]);
    // 复用与 NormalTableView 一致的 plugin runner
    const pipeline = (0, pipeline_1.usePipeline)();
    const renderOperateCell = react_1.default.useMemo(() => {
        if (!operateColumn || !operateColumn.buttons || operateColumn.buttons.length === 0) {
            return null;
        }
        pipeline.inputAndReset({
            data: { rows: [], columns: [] },
            config: {},
        });
        (0, pipeline_1.runPlugins)({
            props: {
                viewId: '',
                table: null,
                columns: [],
                viewColumnMetaData: {},
                operateColumn,
            },
            pipeline,
            context: { dataModel: null, table: null, viewId: '' },
        });
        const handlers = pipeline.getContext('operateColumnHandlers');
        return (handlers === null || handlers === void 0 ? void 0 : handlers.renderOperateCell) || null;
    }, [operateColumn, pipeline]);
    // 获取当前记录的 rowData
    const getRowData = react_1.default.useCallback((record) => {
        if (!record) {
            return {};
        }
        const rowData = {};
        columnDefinitions.forEach((col) => {
            const value = record[col.dataIndex];
            const processedCol = processedColumnMap.get(col.dataIndex);
            const textValue = (0, extract_table_data_1.extractSingleCellText)(value, col, record, processedCol, undefined, 0);
            rowData[col.title] = textValue || '';
        });
        return rowData;
    }, [columnDefinitions, processedColumnMap]);
    // 上一条
    const handlePrevious = react_1.default.useCallback(() => {
        if (selectedIndex > 0) {
            setSelectedIndex(selectedIndex - 1);
        }
    }, [selectedIndex, setSelectedIndex]);
    // 下一条
    const handleNext = react_1.default.useCallback(() => {
        if (selectedIndex < viewData.length - 1) {
            setSelectedIndex(selectedIndex + 1);
        }
    }, [selectedIndex, viewData.length, setSelectedIndex]);
    // 处理卡片点击
    const handleCardClick = react_1.default.useCallback((item, index) => {
        setSelectedIndex(index);
    }, [setSelectedIndex]);
    return {
        selectedRecord,
        detailColumns,
        detailItems,
        processedColumnMap,
        renderOperateCell,
        renderCellValue,
        getRowData,
        handlePrevious,
        handleNext,
        handleCardClick,
    };
};
exports.useDetailView = useDetailView;
exports.default = exports.useDetailView;
