import { array } from "@visactor/vutils";
import { isNullish, isString } from "remeda";
import { selector } from "../../../../../dataSelector/selector.js";
import { pickBodyCellStyle } from "./common.js";
import { FoldMeasureValue, MeasureId } from "../../../../../dataReshape/index.js";
const pivotTableBodyCell = (spec, context)=>{
    const { advancedVSeed } = context;
    const { cellStyle } = advancedVSeed;
    const bodyCellStyle = cellStyle?.bodyCellStyle;
    if (!bodyCellStyle) return spec;
    const bodyCellStyleList = array(bodyCellStyle);
    const indicators = spec.indicators || [];
    const selectedPos = [];
    const newIndicators = indicators.map((ind)=>{
        const newInd = isString(ind) ? {
            indicatorKey: ind
        } : ind;
        const { indicatorKey } = newInd;
        newInd.style = (datum)=>{
            const { dataValue, cellHeaderPaths } = datum;
            const headerPaths = [
                ...cellHeaderPaths.colHeaderPaths,
                ...cellHeaderPaths.rowHeaderPaths
            ];
            const originalDatum = {
                [indicatorKey]: dataValue
            };
            headerPaths.forEach((path)=>{
                if (path.dimensionKey) originalDatum[path.dimensionKey] = path.value;
            });
            if (!isNullish(originalDatum[MeasureId]) && !isNullish(originalDatum[FoldMeasureValue])) originalDatum[originalDatum[MeasureId]] = originalDatum[FoldMeasureValue];
            const mergedStyle = bodyCellStyleList.reduce((result, style)=>{
                if (selector(originalDatum, style.selector)) {
                    if (selectedPos.length && selectedPos[0].col === datum?.col && selectedPos[0].row === datum?.row) selectedPos.length = 0;
                    selectedPos.push({
                        col: datum?.col,
                        row: datum?.row
                    });
                    return {
                        ...result,
                        ...pickBodyCellStyle(style)
                    };
                }
                return result;
            }, {});
            return mergedStyle;
        };
        return newInd;
    });
    return {
        ...spec,
        runningConfig: {
            ...spec?.runningConfig || {},
            selectedPos
        },
        indicators: newIndicators
    };
};
export { pivotTableBodyCell };

//# sourceMappingURL=pivot.js.map