"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.transpose = void 0;
const prosemirror_model_1 = require("prosemirror-model");
const utils_1 = require("../utils.js");
const transpose = (state, dispatch) => {
    const parentCell = (0, utils_1.findParentTableCell)(state.selection);
    const parentRow = (0, utils_1.findParentTableRow)(state.selection);
    const parentBody = (0, utils_1.findParentTableBody)(state.selection);
    const parentTable = (0, utils_1.findParentTable)(state.selection);
    if (!parentCell || !parentRow || !parentBody || !parentTable)
        return false;
    if (dispatch) {
        const rowsCount = parentBody.node.childCount;
        const cellsCount = parentBody.node.maybeChild(0)?.childCount ?? 0;
        const newRowsList = [];
        for (let i = 0; i < cellsCount; i++) {
            const newCellsList = [];
            for (let j = 0; j < rowsCount; j++) {
                const cellNode = parentBody.node.child(j).child(i);
                newCellsList.push(cellNode.copy(cellNode.content));
            }
            newRowsList.push(parentRow.node.copy(prosemirror_model_1.Fragment.from(newCellsList)));
        }
        const newBody = parentBody.node.copy(prosemirror_model_1.Fragment.from(newRowsList));
        const newTable = parentTable.node.copy(prosemirror_model_1.Fragment.from(newBody));
        dispatch(state.tr.replaceWith(parentTable.pos, parentTable.pos + parentTable.node.nodeSize, newTable));
    }
    return true;
};
exports.transpose = transpose;
//# sourceMappingURL=transpose.js.map
