import { array } from "@visactor/vutils";
import { isString } from "remeda";
import { matchesFieldSelector, selector, selectorWithDynamicFilter } from "../../../../../dataSelector/selector.js";
import { applyColorScale, getCellOriginalDataByDatum, getColumnMinMax, pickBodyCellStyle } from "./common.js";
const isPivotTotalCell = (styleArg)=>{
    const cellHeaderPaths = styleArg?.cellHeaderPaths;
    if (!cellHeaderPaths) return false;
    if ('sub-total' === cellHeaderPaths.role || 'grand-total' === cellHeaderPaths.role) return true;
    const headerPaths = [
        ...cellHeaderPaths.colHeaderPaths || [],
        ...cellHeaderPaths.rowHeaderPaths || []
    ];
    return headerPaths.some((path)=>path?.role === 'sub-total' || path?.role === 'grand-total');
};
const pivotTableBodyCell = (spec, context)=>{
    const { advancedVSeed } = context;
    const { cellStyle, config, chartType } = advancedVSeed;
    const bodyCellStyle = cellStyle?.bodyCellStyle;
    const themeConfig = config?.[chartType];
    if (!bodyCellStyle) return spec;
    const bodyCellStyleList = array(bodyCellStyle);
    const indicators = spec.indicators || [];
    const selectedPos = [];
    const hasDynamicFilter = bodyCellStyleList.some((style)=>!!style.dynamicFilter);
    const allData = advancedVSeed.dataset || [];
    const newIndicators = indicators.map((ind)=>{
        const newInd = isString(ind) ? {
            indicatorKey: ind
        } : ind;
        const { indicatorKey } = newInd;
        const progressBarStyle = bodyCellStyleList.filter((s)=>s.enableProgressBar && s?.selector && matchesFieldSelector(indicatorKey, s.selector)).pop();
        const backgroundColorScale = bodyCellStyleList.find((s)=>s.enableBackgroundColorScale && s?.selector && matchesFieldSelector(indicatorKey, s.selector));
        let columnMin;
        let columnMax;
        if (progressBarStyle || backgroundColorScale) {
            const { min, max } = getColumnMinMax(allData, indicatorKey);
            columnMin = min;
            columnMax = max;
            if (progressBarStyle) {
                newInd.cellType = 'progressbar';
                newInd.barType = 'negative';
                newInd.min = progressBarStyle.barMin ?? columnMin;
                newInd.max = progressBarStyle.barMax ?? columnMax;
            }
        }
        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;
            });
            const currentCellData = getCellOriginalDataByDatum(datum, hasDynamicFilter, originalDatum);
            const mergedStyle = bodyCellStyleList.reduce((result, style)=>{
                const shouldApply = style.dynamicFilter ? selectorWithDynamicFilter(currentCellData || originalDatum, style.dynamicFilter, style.selector) : selector(originalDatum, style.selector);
                if (shouldApply) {
                    if (selectedPos.length && selectedPos[0].col === datum?.col && selectedPos[0].row === datum?.row) selectedPos.length = 0;
                    selectedPos.push({
                        col: datum?.col,
                        row: datum?.row
                    });
                    const cellStyle = pickBodyCellStyle(style);
                    if (style.enableBackgroundColorScale) {
                        const scaledColor = applyColorScale(dataValue, {
                            minValue: columnMin,
                            maxValue: columnMax,
                            ...themeConfig.backgroundColorScale,
                            ...style.backgroundColorScale
                        });
                        if (scaledColor) cellStyle.bgColor = scaledColor;
                    }
                    if ('progressbar' === newInd.cellType) {
                        cellStyle.barHeight = themeConfig?.barHeight;
                        cellStyle.barMarkInBar = themeConfig?.barMarkInBar;
                        cellStyle.barMarkWidth = themeConfig?.barMarkWidth;
                        cellStyle.barPadding = themeConfig?.barPadding;
                        cellStyle.barRightToLeft = themeConfig?.barRightToLeft;
                        cellStyle.showBar = !isPivotTotalCell(datum);
                    }
                    return {
                        ...result,
                        ...cellStyle
                    };
                }
                return result;
            }, {});
            return mergedStyle;
        };
        return newInd;
    });
    return {
        ...spec,
        runningConfig: {
            ...spec?.runningConfig || {},
            selectedPos
        },
        indicators: newIndicators
    };
};
export { pivotTableBodyCell };

//# sourceMappingURL=pivot.js.map