/**
 * Shared task detail view component.
 * Encapsulates the common layout and hooks for both TaskView and SmartFollowView.
 * Mode differences are expressed as configuration props.
 */
import React from 'react';
import { CnLoading, CnResult } from '@cainiaofe/cn-ui';
import { observer } from 'mobx-react-lite';
import { useModel } from '../../../../../utils/mobx-react-simpler/index.js';
import { AiSummary } from '../ai-summary/ai-summary';
import { VirtualizedCardList } from '../virtualized-card-list/virtualized-card-list';
import { DetailPanel } from '../detail-view-layout/detail-panel';
import { useCardConfig } from '../hooks/use-card-config';
import { useDetailView } from '../hooks/use-detail-view';
import { useCardRender } from '../hooks/use-card-render';
import { useSummaryProvider } from '../hooks/use-summary-provider';
import '../ai-summary/ai-summary.scss';
import '../virtualized-card-list/virtualized-card-list.scss';
import '../detail-view-layout/detail-view-layout.scss';
import { DetailPanelSummaryRight } from '../detail-view-layout';
export const TaskDetailView = observer(({ viewId, viewData, summaryMode, detailLayoutVariant = 'default', maxFields, cardListClassName, contentClassName, titleIcon, showRefreshButton = true, toolbar, loading = false, loadingTip, containerClassName, summaryEnabled, conversationId, }) => {
    var _a;
    const dataModel = useModel('dataModel');
    const viewConfig = dataModel.getViewConfig(viewId);
    const viewColumns = dataModel.getViewColumns(viewId);
    const [selectedIndex, setSelectedIndex] = React.useState(0);
    const { showFieldTitle, displayColumns } = useCardConfig({
        viewConfig,
        viewColumns,
        maxFields,
    });
    const { selectedRecord, detailColumns, detailItems, renderOperateCell, renderCellValue, getRowData, handlePrevious, handleNext, handleCardClick, } = useDetailView({
        viewData,
        viewColumns,
        columnDefinitions: dataModel.columnDefinitions,
        selectedIndex,
        setSelectedIndex,
    });
    const { renderCardContent, renderCardTitle } = useCardRender({
        displayColumns,
        showFieldTitle,
        renderCellValue,
    });
    const { status: summaryStatus, summary, gmtModified, refresh: refreshSummary, } = 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.createElement(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.createElement(CnResult, { type: "empty" });
        if (detailLayoutVariant === 'summaryRightSplit') {
            return (React.createElement(DetailPanelSummaryRight, { ...sharedDetailProps, gmtModified: gmtModified, summaryStatus: summaryStatus, summary: summary, onRefreshSummary: refreshSummary, useMarkdown: true, showRefreshButton: showRefreshButton }));
        }
        return React.createElement(DetailPanel, { ...sharedDetailProps, aiSummaryContent: aiSummaryNode });
    };
    const renderContent = () => (React.createElement("div", { className: `detail-view-wrapper ${contentClassName || ''}` }, (viewData === null || viewData === void 0 ? void 0 : viewData.length) ? (React.createElement(React.Fragment, null,
        React.createElement("div", { className: "detail-view-left" },
            React.createElement(VirtualizedCardList, { data: viewData, renderCardContent: renderCardContent, renderCardTitle: renderCardTitle, showFieldTitle: showFieldTitle, selectedIndex: selectedIndex, onItemClick: handleCardClick, visibleFieldCount: displayColumns.length, primaryKey: dataModel.primaryKey, className: cardListClassName })),
        React.createElement("div", { className: "detail-view-right" }, renderDetailPanel()))) : (!loading && React.createElement(CnResult, { type: "empty" }))));
    return (React.createElement("div", { className: containerClassName },
        toolbar,
        loading ? (React.createElement(CnLoading, { tip: loadingTip, inline: false, visible: loading, style: { height: '100%' } }, renderContent())) : (renderContent())));
});
export default TaskDetailView;
