"use strict";
/**
 * @desc 表格辅助方法
 * @author 邓欢
 */
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.assemblyTableProps = void 0;
const components_1 = require("./components");
const operation_1 = __importDefault(require("./components/operation"));
const data_helper_1 = require("../_utils/data-helper");
const config_1 = __importDefault(require("./config"));
/** 计算文本数量 */
function calcTextNum(operationList) {
    let num = 0;
    operationList.forEach(item => {
        num += item.operationText.toString().length;
    });
    return num;
}
/**
 * 组装操作列配置
 * @param operationList 操作列集合
 * @param originProps 表格层属性集合
 * @param extraOperationDefs 列最小宽度
 * @return {ColumnDef} 返回列配置
 */
function assemblyOperationConf(operationList, originProps, extraOperationDefs) {
    // 操作名称之间的距离 D
    const distance = 20;
    // 一个汉字的宽度 C（hard code）
    const characterWidth = 14;
    // 操作项的左右内边距 M
    const marginHorizontal = 20;
    // [M [C * m] (D [C * n])... M]
    const operationCellWidth = characterWidth * calcTextNum(operationList) +
        marginHorizontal * 2 +
        distance * (operationList.length - 1);
    return {
        headerName: '操作',
        headerClass: 'ag-numeric-header ag-operation-header',
        cellClass: 'ag-operation-cell',
        filter: false,
        field: 'operationCol',
        pinned: 'right',
        sortable: false,
        resizable: false,
        width: operationCellWidth,
        minWidth: operationCellWidth,
        maxWidth: operationCellWidth,
        headerComponentFramework: components_1.TooltipHeader,
        headerComponentParams: { tooltip: originProps.operationHelperText },
        cellRendererFramework: operation_1.default,
        cellRendererParams: {
            operation: operationList,
        },
        ...extraOperationDefs,
    };
}
/**
 * 组装表格属性
 * @params props 表格属性
 *
 */
function assemblyTableProps(props) {
    const { columnDefs, gridOption, operations, extraOperationDefs, sortable, resizable, } = props;
    const defs = [...columnDefs];
    // 操作配置
    if (operations && operations.length) {
        defs.push(assemblyOperationConf(operations, props, extraOperationDefs));
    }
    // 若未开启本地排序则禁用
    if (!props.localSorting) {
        defs.forEach(def => {
            def.comparator = () => 0;
        });
    }
    return {
        ...config_1.default,
        ...gridOption,
        defaultColDef: {
            resizable: data_helper_1.isEmpty(resizable) || resizable,
            sortable: data_helper_1.isEmpty(sortable) || sortable,
            ...(gridOption && gridOption.defaultColDef),
        },
        columnDefs: defs,
        /** 加载蒙层传参 */
        loadingOverlayComponentParams: { isLoading: props.isLoading },
    };
}
exports.assemblyTableProps = assemblyTableProps;
