import { array } from "@visactor/vutils";
import { isNullish, isNumber, isPlainObject, isString } from "remeda";
import { isFieldSelector, matchesFieldSelector, selector, selectorWithDynamicFilter } from "../../../../../dataSelector/selector.js";
import { applyColorScale, getCellOriginalDataByDatum, getColumnMinMax, pickBodyCellStyle } from "./common.js";
import { preorderTraverse } from "../../../../utils/tree/traverse.js";
const tableBodyCell = (spec, context)=>{
    const { advancedVSeed, vseed } = context;
    const { totals } = vseed || {};
    const { cellStyle, config, chartType } = advancedVSeed;
    const totalAggregation = 'string' == typeof totals ? totals : void 0;
    const bodyCellStyle = cellStyle?.bodyCellStyle;
    const themeConfig = config?.[chartType];
    if (!bodyCellStyle) return spec;
    const bodyCellStyleList = array(bodyCellStyle);
    const columns = spec.columns || [];
    const selectedPos = [];
    const allData = advancedVSeed.dataset || [];
    const setStyleOfColumn = (col)=>{
        const field = col.field;
        const matchedStyles = bodyCellStyleList.filter((style)=>{
            if (style?.dynamicFilter) return style.dynamicFilter?.result?.success === true || !!style.dynamicFilter?.fallback;
            if (isNullish(style.selector)) return true;
            const selectors = array(style.selector);
            return selectors.some((sel)=>{
                if (isFieldSelector(sel)) return matchesFieldSelector(field, sel);
                if (isPlainObject(sel)) return isNullish(sel.field) || sel.field === field;
                return isNumber(sel) || isString(sel);
            });
        });
        if (!matchedStyles.length) return false;
        const hasDynamicFilter = matchedStyles.some((style)=>!!style.dynamicFilter);
        const progressBarStyle = matchedStyles.filter((s)=>s.enableProgressBar && s?.selector && matchesFieldSelector(field, s.selector)).pop();
        const backgroundColorScale = matchedStyles.find((s)=>s.enableBackgroundColorScale && s?.selector && matchesFieldSelector(field, s.selector));
        let columnMin;
        let columnMax;
        if (progressBarStyle || backgroundColorScale) {
            const { min, max } = getColumnMinMax(allData, field, totalAggregation);
            columnMin = min;
            columnMax = max;
            if (progressBarStyle) {
                col.cellType = 'progressbar';
                col.barType = 'negative';
                col.min = progressBarStyle.barMin ?? columnMin;
                col.max = progressBarStyle.barMax ?? columnMax;
            }
        }
        col.style = (datum)=>{
            const originalDatum = {
                [field]: datum.dataValue
            };
            const currentCellData = getCellOriginalDataByDatum(datum, hasDynamicFilter, originalDatum);
            const mergedStyle = matchedStyles.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
                    });
                    let cellStyle = pickBodyCellStyle(style);
                    if (style.enableBackgroundColorScale) {
                        const scaledColor = applyColorScale(datum.dataValue, {
                            minValue: columnMin,
                            maxValue: columnMax,
                            ...themeConfig.backgroundColorScale,
                            ...style?.backgroundColorScale
                        });
                        if (scaledColor) cellStyle.bgColor = scaledColor;
                    }
                    if ('progressbar' === col.cellType) cellStyle = {
                        ...cellStyle,
                        barHeight: themeConfig?.barHeight,
                        barMarkInBar: themeConfig?.barMarkInBar,
                        barMarkWidth: themeConfig?.barMarkWidth,
                        barPadding: themeConfig?.barPadding,
                        barRightToLeft: themeConfig?.barRightToLeft,
                        barAxisColor: themeConfig?.barAxisColor
                    };
                    return {
                        ...result,
                        ...cellStyle
                    };
                }
                return result;
            }, {});
            return mergedStyle;
        };
        return false;
    };
    preorderTraverse(columns, setStyleOfColumn, 'columns');
    spec.runningConfig = {
        ...spec.runningConfig || {},
        selectedPos
    };
    return spec;
};
export { tableBodyCell };

//# sourceMappingURL=table.js.map