"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.setCellRightAlign = exports.setCellCenterAlign = exports.setCellLeftAlign = void 0;
const const_1 = require("../TableSpecs/const.js");
const helpers_1 = require("../helpers.js");
function factory(align) {
    return {
        isActive(state) {
            const cell = (0, helpers_1.findParentCell)(state);
            return cell?.node.attrs[const_1.TableAttrs.CellAlign] === align;
        },
        isEnable(state) {
            return (0, helpers_1.isIntoTable)(state);
        },
        run(state, dispatch) {
            const table = (0, helpers_1.findParentTable)(state);
            const row = (0, helpers_1.findParentRow)(state);
            const cell = (0, helpers_1.findParentCell)(state);
            if (!cell || !row || !table) {
                return;
            }
            let cellIndex = -1;
            row.node.forEach((node, _offset, index) => {
                if (node === cell.node) {
                    cellIndex = index;
                }
            });
            if (cellIndex < 0) {
                return;
            }
            const tr = state.tr;
            const allRows = (0, helpers_1.findTableRows)(table.node, state.schema);
            for (const row of allRows) {
                const rowCells = (0, helpers_1.findTableCells)(row.node, state.schema);
                const cell = rowCells[cellIndex];
                // mmm, magic numbers ^_^
                const pos = table.pos + row.pos + cell.pos + 2;
                tr.setNodeMarkup(pos, undefined, {
                    ...cell.node.attrs,
                    [const_1.TableAttrs.CellAlign]: align,
                });
            }
            dispatch(tr);
        },
    };
}
exports.setCellLeftAlign = factory(const_1.CellAlign.Left);
exports.setCellCenterAlign = factory(const_1.CellAlign.Center);
exports.setCellRightAlign = factory(const_1.CellAlign.Right);
//# sourceMappingURL=cellAlign.js.map
