"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.keymapPlugin = exports.splitCheckbox = void 0;
const prosemirror_keymap_1 = require("prosemirror-keymap");
const prosemirror_model_1 = require("prosemirror-model");
const prosemirror_state_1 = require("prosemirror-state");
// @ts-ignore // TODO: fix cjs build
const prosemirror_utils_1 = require("prosemirror-utils");
const selection_1 = require("../../../utils/selection.js");
const BaseSchema_1 = require("../../base/BaseSchema/index.js");
const utils_1 = require("./utils.js");
const splitCheckbox = (replaceWithParagraph) => (state, dispatch) => {
    const { $from, $to } = state.selection;
    const { schema } = state;
    const label = (0, prosemirror_utils_1.findParentNodeOfType)((0, utils_1.checkboxLabelType)(schema))(state.selection);
    if (!label)
        return false;
    const checkbox = (0, prosemirror_utils_1.findParentNodeOfType)((0, utils_1.checkboxType)(schema))(state.selection);
    if (label.node.content.size === 0 && checkbox) {
        dispatch?.(state.tr
            .replaceWith(checkbox.pos, checkbox.pos + checkbox.node.nodeSize, (0, BaseSchema_1.pType)(schema).create())
            .scrollIntoView());
        return true;
    }
    if ($from.pos === $to.pos) {
        const { tr } = state;
        const content = prosemirror_model_1.Fragment.from($from.parent.cut($from.parentOffset).content);
        const node = replaceWithParagraph
            ? (0, BaseSchema_1.pType)(state.schema).create({}, content)
            : (0, utils_1.checkboxType)(schema).create({}, [
                (0, utils_1.checkboxInputType)(schema).create(),
                (0, utils_1.checkboxLabelType)(schema).create({}, content),
            ]);
        tr.insert($from.after(), [node]);
        tr.replace(label.start + $from.parentOffset, label.pos + label.node.nodeSize);
        tr.setSelection(new prosemirror_state_1.TextSelection(tr.doc.resolve(tr.selection.$from.after() + (replaceWithParagraph ? 2 : 4))));
        dispatch?.(tr);
        return true;
    }
    return false;
};
exports.splitCheckbox = splitCheckbox;
const removeCheckbox = (state, dispatch) => {
    const label = (0, prosemirror_utils_1.findParentNodeOfType)((0, utils_1.checkboxLabelType)(state.schema))(state.selection);
    const checkbox = (0, prosemirror_utils_1.findParentNodeOfType)((0, utils_1.checkboxType)(state.schema))(state.selection);
    const { selection } = state;
    const { from, to } = selection;
    if (!label || !checkbox) {
        return false;
    }
    const idx = from - label.pos - 2;
    if (idx < 0 && from === to && !(0, selection_1.isWholeSelection)(selection)) {
        const { tr } = state;
        dispatch?.(tr
            .replaceWith(checkbox.pos, checkbox.pos + checkbox.node.nodeSize, (0, BaseSchema_1.pType)(state.schema).create(null, label.node.content))
            .setSelection(new prosemirror_state_1.TextSelection(tr.doc.resolve(state.selection.from - 2))));
        return true;
    }
    return false;
};
const keymapPlugin = () => (0, prosemirror_keymap_1.keymap)({
    Enter: (0, exports.splitCheckbox)(),
    Backspace: removeCheckbox,
    'Shift-Enter': (0, exports.splitCheckbox)(true),
});
exports.keymapPlugin = keymapPlugin;
//# sourceMappingURL=plugin.js.map
