"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DetailPanelSummaryRight = void 0;
const i18n_1 = __importDefault(require("../../../../../locales/i18n.js"));
const react_1 = __importDefault(require("react"));
const classnames_1 = __importDefault(require("classnames"));
const cn_ui_1 = require("@cainiaofe/cn-ui");
const detail_navigation_1 = require("./detail-navigation");
const field_icon_utils_1 = require("../../../utils/field-icon-utils");
const use_ai_table_icons_1 = require("../../../hooks/use-ai-table-icons");
const ai_summary_content_1 = require("../ai-summary/ai-summary-content");
require("./detail-panel-summary-right.scss");
const dayjs_1 = __importDefault(require("dayjs"));
const { CnRow, CnCol } = cn_ui_1.CnGrid;
const stringifyFallback = (value) => {
    try {
        return JSON.stringify(value);
    }
    catch {
        return String(value);
    }
};
const normalizeDetailValue = (value) => {
    if (value === null || value === undefined || value === '') {
        return '- -';
    }
    if (react_1.default.isValidElement(value)) {
        return value;
    }
    if (Array.isArray(value)) {
        const hasNonRenderableItem = value.some((item) => {
            if (item === null || item === undefined)
                return false;
            if (react_1.default.isValidElement(item))
                return false;
            return !['string', 'number', 'boolean'].includes(typeof item);
        });
        return hasNonRenderableItem ? stringifyFallback(value) : value;
    }
    if (typeof value === 'object') {
        return stringifyFallback(value);
    }
    return value;
};
/**
 * 详情面板 — 摘要右分栏变体
 * 左侧：标题 + Tabs + 详情字段 + 底部导航
 * 右侧：直出 AI 摘要内容（无 AiSummary 外壳）
 */
const DetailPanelSummaryRight = ({ title, titleIcon, summaryStatus, summary, gmtModified, onRefreshSummary, useMarkdown = true, showRefreshButton = true, detailColumns, detailItems, selectedRecord, selectedIndex, totalCount, onPrevious, onNext, renderOperateCell, className, fieldMap, }) => {
    const getIcon = (0, field_icon_utils_1.useFieldIcon)();
    const aiIcons = (0, use_ai_table_icons_1.useAiTableIcons)();
    const [activeTab, setActiveTab] = react_1.default.useState('basic-info');
    const [asideCollapsed, setAsideCollapsed] = react_1.default.useState(false);
    const handleTabChange = react_1.default.useCallback((key) => {
        if (key === 'basic-info' || key === 'operation-record') {
            setActiveTab(key);
        }
    }, []);
    react_1.default.useEffect(() => {
        setActiveTab('basic-info');
    }, [selectedRecord === null || selectedRecord === void 0 ? void 0 : selectedRecord.id, selectedIndex]);
    const toggleAside = react_1.default.useCallback(() => {
        setAsideCollapsed((prev) => !prev);
    }, []);
    // 格式化时间（gmtModified 为空时显示占位符）
    const formattedGmtModified = react_1.default.useMemo(() => {
        if (!gmtModified)
            return '-';
        return (0, dayjs_1.default)(gmtModified).format('YYYY-MM-DD HH:mm:ss');
    }, [gmtModified]);
    return (react_1.default.createElement("div", { className: (0, classnames_1.default)('detail-panel-summary-right', className, {
            'detail-panel-summary-right--collapsed': asideCollapsed,
        }) },
        react_1.default.createElement("div", { className: "detail-panel-summary-right__main" },
            react_1.default.createElement("div", { className: "detail-panel-summary-right__header" },
                react_1.default.createElement("div", { className: "detail-view-detail-title" },
                    react_1.default.createElement("span", { className: "detail-view-detail-title-text" }, title),
                    titleIcon && react_1.default.createElement("span", { className: "detail-view-detail-title-icon" }, titleIcon)),
                react_1.default.createElement("div", { className: "detail-panel-summary-right__toggle", onClick: toggleAside, role: "button", tabIndex: 0 },
                    react_1.default.createElement(cn_ui_1.CnIcon, { type: asideCollapsed ? 'arrow-left' : 'arrow-right', size: "small" }))),
            react_1.default.createElement("div", { className: "detail-panel-summary-right__tabs" },
                react_1.default.createElement(cn_ui_1.CnTab, { shape: "pure", activeKey: activeTab, defaultActiveKey: "basic-info", onChange: handleTabChange, style: { height: '100%' } },
                    react_1.default.createElement(cn_ui_1.CnTabItem, { key: "basic-info", title: i18n_1.default.get({ id: 'BasicInformation', dm: '基本信息' }) },
                        react_1.default.createElement("div", { className: "detail-view-detail-items" }, detailColumns.length === 0 ? (react_1.default.createElement("div", { className: "detail-view-empty" }, i18n_1.default.get({ id: 'NoFieldsToShow', dm: '暂无可展示字段' }))) : (detailItems.map((item) => {
                            let iconSrc;
                            if (item.column.isAiColumn) {
                                iconSrc = aiIcons.FIELD_AI;
                            }
                            else {
                                const componentType = (0, field_icon_utils_1.getFieldComponentType)(fieldMap, item.column.dataIndex, item.column.format);
                                iconSrc = getIcon(componentType);
                            }
                            return (react_1.default.createElement(CnRow, { className: "detail-view-detail-item", key: item.dataIndex, gutter: 0 },
                                react_1.default.createElement(CnCol, { span: 6, className: "detail-view-detail-item-label" },
                                    react_1.default.createElement("img", { className: "detail-view-detail-item-icon", src: iconSrc, alt: "" }),
                                    react_1.default.createElement("span", null, item.title)),
                                react_1.default.createElement(CnCol, { span: 18, className: "detail-view-detail-item-value" }, normalizeDetailValue(item.renderedValue))));
                        })))))),
            react_1.default.createElement(detail_navigation_1.DetailNavigation, { selectedIndex: selectedIndex, totalCount: totalCount, onPrevious: onPrevious, onNext: onNext, renderOperateCell: renderOperateCell, selectedRecord: selectedRecord })),
        react_1.default.createElement("div", { className: (0, classnames_1.default)('detail-panel-summary-right__aside', {
                'detail-panel-summary-right__aside--collapsed': asideCollapsed,
            }) },
            react_1.default.createElement("div", { className: "summary-right-header" },
                react_1.default.createElement("div", { className: "summary-right-header-title" }, i18n_1.default.get({ id: 'IntelligentAnalysis', dm: '智能分析' })),
                react_1.default.createElement("div", { className: "summary-right-header-time" },
                    i18n_1.default.get({ id: 'LastUpdated', dm: '最近更新：' }),
                    formattedGmtModified)),
            react_1.default.createElement("div", { className: "summary-right-content" },
                react_1.default.createElement("div", { className: (0, classnames_1.default)('summary-right-content__body', {
                        'summary-right-content__body--error': summaryStatus === 'error',
                        'summary-right-content__body--loading': summaryStatus === 'loading',
                    }) },
                    react_1.default.createElement(ai_summary_content_1.AiSummaryContent, { status: summaryStatus, summary: summary, onRefresh: onRefreshSummary, useMarkdown: useMarkdown, showRefreshButton: showRefreshButton, typewriter: true }))))));
};
exports.DetailPanelSummaryRight = DetailPanelSummaryRight;
exports.default = exports.DetailPanelSummaryRight;
