import { array } from "@visactor/vutils";
import { isNullish, isNumber, isPlainObject, isString } from "remeda";
import { selector as selector_js_selector } from "../../../../../dataSelector/selector.js";
import { pickBodyCellStyle } from "./common.js";
import { preorderTraverse } from "../../../../utils/tree/traverse.js";
const tableBodyCell = (spec, context)=>{
    const { advancedVSeed } = context;
    const { cellStyle } = advancedVSeed;
    const bodyCellStyle = cellStyle?.bodyCellStyle;
    if (!bodyCellStyle) return spec;
    const bodyCellStyleList = array(bodyCellStyle);
    const columns = spec.columns || [];
    const selectedPos = [];
    const setStyleOfColumn = (col)=>{
        const field = col.field;
        const matchedStyles = bodyCellStyleList.filter((style)=>{
            if (isNullish(style.selector)) return true;
            const selectors = array(style.selector);
            return selectors.some((selector)=>isPlainObject(selector) ? isNullish(selector.field) || selector.field === field : isNumber(selector) || isString(selector));
        });
        if (!matchedStyles.length) return false;
        col.style = (datum)=>{
            const originalDatum = {
                [field]: datum.dataValue
            };
            const mergedStyle = matchedStyles.reduce((result, style)=>{
                if (selector_js_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 false;
    };
    preorderTraverse(columns, setStyleOfColumn, 'columns');
    spec.runningConfig = {
        ...spec.runningConfig || {},
        selectedPos
    };
    return spec;
};
export { tableBodyCell };

//# sourceMappingURL=table.js.map