"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.toYfmNote = exports.createYfmNote = void 0;
exports.isInsideYfmNote = isInsideYfmNote;
// @ts-ignore // TODO: fix cjs build
const prosemirror_utils_1 = require("prosemirror-utils");
const const_1 = require("../const.js");
const utils_1 = require("../utils.js");
function isInsideYfmNote(state) {
    return (0, prosemirror_utils_1.hasParentNodeOfType)((0, utils_1.noteType)(state.schema))(state.selection);
}
const createYfmNoteNode = (schema) => (type, content) => {
    return (0, utils_1.noteType)(schema).create({
        [const_1.NoteAttrs.Class]: `yfm-note yfm-accent-${type}`,
        [const_1.NoteAttrs.Type]: type,
    }, [
        (0, utils_1.noteTitleType)(schema).createAndFill(),
        (0, utils_1.noteContentType)(schema).createAndFill({}, content),
    ]);
};
const createYfmNote = (state, dispatch) => {
    const parent = (0, prosemirror_utils_1.findParentNodeOfType)(state.schema.nodes.paragraph)(state.selection);
    if (parent) {
        const yfmNote = createYfmNoteNode(state.schema)('info', parent.node);
        dispatch?.(state.tr.replaceWith(parent.pos, parent.pos + parent.node.nodeSize, yfmNote));
        return true;
    }
    return false;
};
exports.createYfmNote = createYfmNote;
exports.toYfmNote = {
    isEnable(state) {
        return (0, exports.createYfmNote)(state);
    },
    run(state, dispatch) {
        (0, exports.createYfmNote)(state, dispatch);
    },
};
//# sourceMappingURL=toYfmNote.js.map
