"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TaskView = void 0;
const i18n_1 = __importDefault(require("../../../../../locales/i18n.js"));
const react_1 = __importDefault(require("react"));
const classnames_1 = __importDefault(require("classnames"));
const mobx_react_lite_1 = require("mobx-react-lite");
const cn_ui_1 = require("@cainiaofe/cn-ui");
const get_column_meta_data_1 = require("../../../../../utils/data/get-column-meta-data.js");
const toolbar_1 = require("../../toolbar/toolbar");
const toolbar_divider_1 = require("../../toolbar/components/toolbar-divider");
const trigger_with_tooltip_1 = require("../../toolbar/components/condition-panel/trigger-with-tooltip");
const mobx_react_simpler_1 = require("../../../../../utils/mobx-react-simpler/index.js");
const card_config_1 = require("../../configs/card-config/card-config");
const use_ai_table_icons_1 = require("../../../../../cn-ai-table/view/hooks/use-ai-table-icons");
const task_detail_view_1 = require("../../common/task-detail-view");
require("./task-view.scss");
const DEFAULT_CARD_VISIBLE_COUNT = 4;
function getCardConfigToolbarState(cardConfig, 
/** 与卡片配置数据源一致：columnDefinitions 排除 AI 后的 dataIndex 列表 */
filteredFieldIds) {
    var _a;
    if (!cardConfig) {
        return {
            cardConfigLabel: i18n_1.default.get({ id: 'CardConfiguration', dm: '卡片配置' }),
            cardConfigHasStyle: false,
        };
    }
    if (cardConfig.showFieldTitle === false) {
        return {
            cardConfigLabel: i18n_1.default.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_1.default.get({ id: 'CardConfiguration', dm: '卡片配置' }),
        cardConfigHasStyle: hasModified,
    };
}
exports.TaskView = (0, mobx_react_lite_1.observer)(({ viewId, enableAiSummary = true, toolbarVisible = true, toolbarPermissions, }) => {
    var _a, _b, _c;
    const icons = (0, use_ai_table_icons_1.useAiTableIcons)();
    const dataModel = (0, mobx_react_simpler_1.useModel)('dataModel');
    const viewData = dataModel.getViewData(viewId);
    const viewConfig = dataModel.getViewConfig(viewId);
    const [cardConfigVisible, setCardConfigVisible] = react_1.default.useState(false);
    // 与卡片配置数据源一致：AI 列只展示已启用的（customFields.aiColumns）
    const cardConfigFieldIds = react_1.default.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 (!(0, get_column_meta_data_1.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_1.default.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_1.default.createElement("div", { className: "task-view-container-toolbar-sticky" },
        react_1.default.createElement(toolbar_1.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_1.default.createElement(cn_ui_1.CnButton, { text: true, className: (0, classnames_1.default)('toolbar-btn', {
                        'toolbar-btn-active': cardConfigHasStyle,
                    }), onClick: isCompact ? () => setCardConfigVisible((v) => !v) : undefined },
                    react_1.default.createElement("img", { src: icons.TASK_STATUS_PENDING, alt: i18n_1.default.get({ id: 'CardConfiguration', dm: '卡片配置' }) }),
                    react_1.default.createElement("span", null, cardConfigLabel)));
                const trigger = isCompact ? (react_1.default.createElement(trigger_with_tooltip_1.TriggerWithTooltip, { tooltipContent: i18n_1.default.get({ id: 'CardConfiguration', dm: '卡片配置' }), disableTooltip: cardConfigVisible }, buttonElement)) : (buttonElement);
                return (react_1.default.createElement(react_1.default.Fragment, null,
                    react_1.default.createElement(card_config_1.CardConfig, { viewId: viewId, visible: cardConfigVisible, onVisibleChange: setCardConfigVisible, trigger: trigger, excludeAiColumns: true }),
                    react_1.default.createElement(toolbar_divider_1.ToolbarDivider, null)));
            } })));
    return (react_1.default.createElement(task_detail_view_1.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 }));
});
exports.default = exports.TaskView;
