"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.setCellBg = void 0;
const table_desc_1 = require("../../../../../../table-utils/table-desc.js");
const const_1 = require("../../../YfmTableSpecs/const.js");
const setCellBg = (params) => {
    return (state, dispatch) => {
        if (!params.rows && !params.cols)
            return false;
        const table = state.doc.nodeAt(params.tablePos);
        const tableDesc = table && table_desc_1.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[const_1.YfmTableAttr.CellBg] === newBgValue)
                return;
            tr.setNodeAttribute(tr.mapping.map(cellPos.from), const_1.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;
    };
};
exports.setCellBg = setCellBg;
//# sourceMappingURL=set-cell-bg.js.map
