"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TaskDetailView = void 0;
/**
 * Shared task detail view component.
 * Encapsulates the common layout and hooks for both TaskView and SmartFollowView.
 * Mode differences are expressed as configuration props.
 */
const react_1 = __importDefault(require("react"));
const cn_ui_1 = require("@cainiaofe/cn-ui");
const mobx_react_lite_1 = require("mobx-react-lite");
const mobx_react_simpler_1 = require("../../../../../utils/mobx-react-simpler/index.js");
const ai_summary_1 = require("../ai-summary/ai-summary");
const virtualized_card_list_1 = require("../virtualized-card-list/virtualized-card-list");
const detail_panel_1 = require("../detail-view-layout/detail-panel");
const use_card_config_1 = require("../hooks/use-card-config");
const use_detail_view_1 = require("../hooks/use-detail-view");
const use_card_render_1 = require("../hooks/use-card-render");
const use_summary_provider_1 = require("../hooks/use-summary-provider");
require("../ai-summary/ai-summary.scss");
require("../virtualized-card-list/virtualized-card-list.scss");
require("../detail-view-layout/detail-view-layout.scss");
const detail_view_layout_1 = require("../detail-view-layout");
exports.TaskDetailView = (0, mobx_react_lite_1.observer)(({ viewId, viewData, summaryMode, detailLayoutVariant = 'default', maxFields, cardListClassName, contentClassName, titleIcon, showRefreshButton = true, toolbar, loading = false, loadingTip, containerClassName, summaryEnabled, conversationId, }) => {
    var _a;
    const dataModel = (0, mobx_react_simpler_1.useModel)('dataModel');
    const viewConfig = dataModel.getViewConfig(viewId);
    const viewColumns = dataModel.getViewColumns(viewId);
    const [selectedIndex, setSelectedIndex] = react_1.default.useState(0);
    const { showFieldTitle, displayColumns } = (0, use_card_config_1.useCardConfig)({
        viewConfig,
        viewColumns,
        maxFields,
    });
    const { selectedRecord, detailColumns, detailItems, renderOperateCell, renderCellValue, getRowData, handlePrevious, handleNext, handleCardClick, } = (0, use_detail_view_1.useDetailView)({
        viewData,
        viewColumns,
        columnDefinitions: dataModel.columnDefinitions,
        selectedIndex,
        setSelectedIndex,
    });
    const { renderCardContent, renderCardTitle } = (0, use_card_render_1.useCardRender)({
        displayColumns,
        showFieldTitle,
        renderCellValue,
    });
    const { status: summaryStatus, summary, gmtModified, refresh: refreshSummary, } = (0, use_summary_provider_1.useSummaryProvider)({
        mode: summaryMode,
        recordId: selectedRecord === null || selectedRecord === void 0 ? void 0 : selectedRecord.id,
        tableId: dataModel.tableId,
        viewData,
        rowData: selectedRecord ? getRowData(selectedRecord) : {},
        initialSummary: selectedRecord === null || selectedRecord === void 0 ? void 0 : selectedRecord.aiSummary,
        recordData: selectedRecord,
        columnDefinitions: dataModel.columnDefinitions,
        enabled: summaryEnabled,
        conversationId,
        bizRequestConfig: {
            aiColumn: (_a = dataModel.bizRequestConfig) === null || _a === void 0 ? void 0 : _a.aiColumn,
        },
    });
    const aiSummaryNode = (react_1.default.createElement(ai_summary_1.AiSummary, { key: (selectedRecord === null || selectedRecord === void 0 ? void 0 : selectedRecord.id) ? String(selectedRecord.id) : undefined, status: summaryStatus, summary: summary, onRefresh: refreshSummary, useMarkdown: true, showRefreshButton: showRefreshButton, typewriter: true }));
    const sharedDetailProps = {
        title: selectedRecord ? renderCardTitle(selectedRecord, selectedIndex) : null,
        titleIcon,
        detailColumns,
        detailItems,
        selectedRecord,
        selectedIndex,
        totalCount: viewData.length,
        onPrevious: handlePrevious,
        onNext: handleNext,
        renderOperateCell,
        fieldMap: dataModel.fieldMap,
    };
    const renderDetailPanel = () => {
        if (!selectedRecord)
            return react_1.default.createElement(cn_ui_1.CnResult, { type: "empty" });
        if (detailLayoutVariant === 'summaryRightSplit') {
            return (react_1.default.createElement(detail_view_layout_1.DetailPanelSummaryRight, { ...sharedDetailProps, gmtModified: gmtModified, summaryStatus: summaryStatus, summary: summary, onRefreshSummary: refreshSummary, useMarkdown: true, showRefreshButton: showRefreshButton }));
        }
        return react_1.default.createElement(detail_panel_1.DetailPanel, { ...sharedDetailProps, aiSummaryContent: aiSummaryNode });
    };
    const renderContent = () => (react_1.default.createElement("div", { className: `detail-view-wrapper ${contentClassName || ''}` }, (viewData === null || viewData === void 0 ? void 0 : viewData.length) ? (react_1.default.createElement(react_1.default.Fragment, null,
        react_1.default.createElement("div", { className: "detail-view-left" },
            react_1.default.createElement(virtualized_card_list_1.VirtualizedCardList, { data: viewData, renderCardContent: renderCardContent, renderCardTitle: renderCardTitle, showFieldTitle: showFieldTitle, selectedIndex: selectedIndex, onItemClick: handleCardClick, visibleFieldCount: displayColumns.length, primaryKey: dataModel.primaryKey, className: cardListClassName })),
        react_1.default.createElement("div", { className: "detail-view-right" }, renderDetailPanel()))) : (!loading && react_1.default.createElement(cn_ui_1.CnResult, { type: "empty" }))));
    return (react_1.default.createElement("div", { className: containerClassName },
        toolbar,
        loading ? (react_1.default.createElement(cn_ui_1.CnLoading, { tip: loadingTip, inline: false, visible: loading, style: { height: '100%' } }, renderContent())) : (renderContent())));
});
exports.default = exports.TaskDetailView;
