import { intl } from "../../../../../i18n/index.js";
import { createFormatterByMeasure, findTreeNodesBy, isMeasure } from "../../../../utils/index.js";
import { extractLeafIds } from "./utils.js";
const columnsAggregation = (spec, context)=>{
    const { vseed, advancedVSeed } = context;
    const { totals } = vseed;
    if (!totals) return spec;
    const totalLabelMap = {
        sum: intl.i18n`合计`,
        avg: intl.i18n`平均`,
        max: intl.i18n`最大值`,
        min: intl.i18n`最小值`,
        count: intl.i18n`计数`
    };
    const totalLabel = totalLabelMap[totals] || intl.i18n`合计`;
    const vtableAggregationMap = {
        sum: 'SUM',
        avg: 'AVG',
        max: 'MAX',
        min: 'MIN',
        count: 'COUNT'
    };
    const vtableAggregationType = vtableAggregationMap[totals] || 'SUM';
    const dimensionIds = extractLeafIds(advancedVSeed.dimensionTree || []);
    const measureTree = advancedVSeed.measureTree || [];
    const measureIds = extractLeafIds(measureTree);
    const leafMeasures = findTreeNodesBy(measureTree, ()=>true);
    const measureMap = new Map();
    leafMeasures.forEach((node)=>{
        if (isMeasure(node)) measureMap.set(node.id, node);
    });
    let isFirstDimensionColumn = true;
    const updatedColumns = spec.columns?.map((column)=>{
        const fieldKey = column.field;
        if (dimensionIds.has(fieldKey)) {
            if (isFirstDimensionColumn) {
                isFirstDimensionColumn = false;
                return {
                    ...column,
                    aggregation: {
                        aggregationType: 'NONE',
                        formatFun: ()=>totalLabel
                    }
                };
            }
            return {
                ...column,
                aggregation: {
                    aggregationType: 'NONE',
                    formatFun: ()=>''
                }
            };
        }
        if (measureIds.has(fieldKey)) {
            const measure = measureMap.get(fieldKey);
            const formatter = createFormatterByMeasure(measure);
            return {
                ...column,
                aggregation: {
                    aggregationType: vtableAggregationType,
                    showOnTop: false,
                    formatFun: (value)=>formatter(value)
                }
            };
        }
        return column;
    });
    return {
        ...spec,
        columns: updatedColumns,
        bottomFrozenRowCount: 1
    };
};
export { columnsAggregation };

//# sourceMappingURL=columnsAggregation.js.map