import $i18n from '../../../../../locales/i18n.js';
import React from 'react';
import classNames from 'classnames';
import { observer } from 'mobx-react-lite';
import { CnButton } from '@cainiaofe/cn-ui';
import { shouldKeepCol } from '../../../../../utils/data/get-column-meta-data.js';
import { Toolbar } from '../../toolbar/toolbar';
import { ToolbarDivider } from '../../toolbar/components/toolbar-divider';
import { TriggerWithTooltip } from '../../toolbar/components/condition-panel/trigger-with-tooltip';
import { useModel } from '../../../../../utils/mobx-react-simpler/index.js';
import { CardConfig } from '../../configs/card-config/card-config';
import { useAiTableIcons } from '../../../../../cn-ai-table/view/hooks/use-ai-table-icons';
import { TaskDetailView } from '../../common/task-detail-view';
import './task-view.scss';
const DEFAULT_CARD_VISIBLE_COUNT = 4;
function getCardConfigToolbarState(cardConfig, 
/** 与卡片配置数据源一致：columnDefinitions 排除 AI 后的 dataIndex 列表 */
filteredFieldIds) {
    var _a;
    if (!cardConfig) {
        return {
            cardConfigLabel: $i18n.get({ id: 'CardConfiguration', dm: '卡片配置' }),
            cardConfigHasStyle: false,
        };
    }
    if (cardConfig.showFieldTitle === false) {
        return {
            cardConfigLabel: $i18n.get({
                id: 'CardConfigurationHideFieldTitles',
                dm: '卡片配置（隐藏字段标题）',
            }),
            cardConfigHasStyle: true,
        };
    }
    const defaultIds = filteredFieldIds.slice(0, DEFAULT_CARD_VISIBLE_COUNT);
    const currentIds = (_a = cardConfig.visibleFields) !== null && _a !== void 0 ? _a : defaultIds;
    const defaultSet = new Set(defaultIds);
    const currentSet = new Set(currentIds);
    const hasModified = defaultIds.length !== currentIds.length ||
        defaultIds.some((id) => !currentSet.has(id)) ||
        currentIds.some((id) => !defaultSet.has(id));
    return {
        cardConfigLabel: $i18n.get({ id: 'CardConfiguration', dm: '卡片配置' }),
        cardConfigHasStyle: hasModified,
    };
}
export const TaskView = observer(({ viewId, enableAiSummary = true, toolbarVisible = true, toolbarPermissions, }) => {
    var _a, _b, _c;
    const icons = useAiTableIcons();
    const dataModel = useModel('dataModel');
    const viewData = dataModel.getViewData(viewId);
    const viewConfig = dataModel.getViewConfig(viewId);
    const [cardConfigVisible, setCardConfigVisible] = React.useState(false);
    // 与卡片配置数据源一致：AI 列只展示已启用的（customFields.aiColumns）
    const cardConfigFieldIds = React.useMemo(() => {
        var _a;
        const aiSet = new Set((dataModel.aiColumns || []).map((ac) => ac.columnCode));
        const customAiColumnSet = new Set(((_a = dataModel.customFields) === null || _a === void 0 ? void 0 : _a.aiColumns) || []);
        return dataModel.columnDefinitions
            .filter((col) => {
            if (!shouldKeepCol(col))
                return false;
            if ((col === null || col === void 0 ? void 0 : col.dataIndex) == null || col.dataIndex === '')
                return false;
            if (col.isAiColumn || aiSet.has(col.dataIndex)) {
                return customAiColumnSet.has(col.dataIndex);
            }
            return true;
        })
            .map((col) => col.dataIndex);
    }, [dataModel.columnDefinitions, dataModel.aiColumns, dataModel.customFields]);
    const { cardConfigLabel, cardConfigHasStyle } = React.useMemo(() => { var _a; return getCardConfigToolbarState((_a = viewConfig === null || viewConfig === void 0 ? void 0 : viewConfig.custom) === null || _a === void 0 ? void 0 : _a.cardConfig, cardConfigFieldIds); }, [(_a = viewConfig === null || viewConfig === void 0 ? void 0 : viewConfig.custom) === null || _a === void 0 ? void 0 : _a.cardConfig, cardConfigFieldIds]);
    const toolbarElement = (React.createElement("div", { className: "task-view-container-toolbar-sticky" },
        React.createElement(Toolbar, { viewId: viewId, toolbarVisible: toolbarVisible, permissions: {
                addRecord: true,
                fieldManagement: false,
                viewConfig: true,
                filter: (_b = toolbarPermissions === null || toolbarPermissions === void 0 ? void 0 : toolbarPermissions.filter) !== null && _b !== void 0 ? _b : true,
                sort: (_c = toolbarPermissions === null || toolbarPermissions === void 0 ? void 0 : toolbarPermissions.sort) !== null && _c !== void 0 ? _c : true,
                group: false,
            }, groupMaxLevels: 0, renderLeftContent: ({ isCompact }) => {
                if ((toolbarPermissions === null || toolbarPermissions === void 0 ? void 0 : toolbarPermissions.cardManagement) === false)
                    return null;
                const buttonElement = (React.createElement(CnButton, { text: true, className: classNames('toolbar-btn', {
                        'toolbar-btn-active': cardConfigHasStyle,
                    }), onClick: isCompact ? () => setCardConfigVisible((v) => !v) : undefined },
                    React.createElement("img", { src: icons.TASK_STATUS_PENDING, alt: $i18n.get({ id: 'CardConfiguration', dm: '卡片配置' }) }),
                    React.createElement("span", null, cardConfigLabel)));
                const trigger = isCompact ? (React.createElement(TriggerWithTooltip, { tooltipContent: $i18n.get({ id: 'CardConfiguration', dm: '卡片配置' }), disableTooltip: cardConfigVisible }, buttonElement)) : (buttonElement);
                return (React.createElement(React.Fragment, null,
                    React.createElement(CardConfig, { viewId: viewId, visible: cardConfigVisible, onVisibleChange: setCardConfigVisible, trigger: trigger, excludeAiColumns: true }),
                    React.createElement(ToolbarDivider, null)));
            } })));
    return (React.createElement(TaskDetailView, { viewId: viewId, viewData: viewData, summaryMode: "aiSummary", cardListClassName: "task-card-list", contentClassName: "task-view-content", containerClassName: "task-view-container", toolbar: toolbarElement, showRefreshButton: true, summaryEnabled: enableAiSummary }));
});
export default TaskView;
