import { TableDesc } from "../../../../../../table-utils/table-desc.js";
import { YfmTableAttr } from "../../../YfmTableSpecs/const.js";
export const setCellBg = (params) => {
    return (state, dispatch) => {
        if (!params.rows && !params.cols)
            return false;
        const table = state.doc.nodeAt(params.tablePos);
        const tableDesc = table && TableDesc.create(table)?.bind(params.tablePos);
        if (!tableDesc)
            return false;
        if (!dispatch)
            return true;
        const { tr } = state;
        const newBgValue = params.bg || null;
        const apply = (cellPos) => {
            if (cellPos.type !== 'real')
                return;
            const node = state.doc.nodeAt(cellPos.from);
            if (!node || node.attrs[YfmTableAttr.CellBg] === newBgValue)
                return;
            tr.setNodeAttribute(tr.mapping.map(cellPos.from), YfmTableAttr.CellBg, newBgValue);
        };
        if (params.rows) {
            for (const rowIdx of params.rows) {
                for (const pos of tableDesc.getPosForRowCells(rowIdx))
                    apply(pos);
            }
        }
        if (params.cols) {
            for (const colIdx of params.cols) {
                for (const pos of tableDesc.getPosForColumn(colIdx))
                    apply(pos);
            }
        }
        if (tr.docChanged)
            dispatch(tr);
        return true;
    };
};
//# sourceMappingURL=set-cell-bg.js.map
