"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.deleteTableAction = exports.createTableAction = void 0;
// @ts-ignore // TODO: fix cjs build
const prosemirror_utils_1 = require("prosemirror-utils");
const const_1 = require("../const.js");
const helpers_1 = require("../helpers.js");
const mdTamplate = `

| Heading | Heading |
| ------- | ------- |
| Text    | Text    |
| Text    | Text    |

`.trim();
const createTableFactory = (parser) => (state, dispatch) => {
    if ((0, helpers_1.isIntoTable)(state)) {
        return false;
    }
    if (dispatch) {
        const node = parser.parse(mdTamplate);
        dispatch(state.tr.replaceSelectionWith(node).scrollIntoView());
    }
    return true;
};
const removeTable = (state, dispatch) => {
    if ((0, helpers_1.isIntoTable)(state)) {
        dispatch?.((0, prosemirror_utils_1.removeParentNodeOfType)(state.schema.nodes[const_1.TableNode.Table])(state.tr));
        return true;
    }
    return false;
};
const createTableAction = ({ markupParser }) => ({
    isActive: helpers_1.isIntoTable,
    isEnable: createTableFactory(markupParser),
    run: createTableFactory(markupParser),
});
exports.createTableAction = createTableAction;
exports.deleteTableAction = {
    isEnable: removeTable,
    run: removeTable,
};
//# sourceMappingURL=tableActions.js.map
