"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.wrapToInlineCode = exports.wrapToCodeBlock = void 0;
const state_1 = require("@codemirror/state");
const helpers_1 = require("./helpers.js");
exports.wrapToCodeBlock = (0, helpers_1.wrapToBlock)(({ lineBreak }) => '```' + lineBreak, ({ lineBreak }) => lineBreak + '```');
const wrapToInlineCode = ({ state, dispatch }) => {
    const tr = state.changeByRange((range) => {
        const content = state.sliceDoc(range.from, range.to);
        const hasBacktick = content.includes('`');
        const markup = hasBacktick ? '``' : '`';
        const before = `${markup}${content.startsWith('`') ? ' ' : ''}`;
        const after = `${content.endsWith('`') ? ' ' : ''}${markup}`;
        const changeSpec = [
            { from: range.from, insert: before },
            { from: range.to, insert: after },
        ];
        const changes = state.changes(changeSpec);
        return {
            changes,
            range: range.empty
                ? state_1.EditorSelection.range(range.anchor + 1, range.head + 1, range.goalColumn, range.bidiLevel ?? undefined)
                : range.map(changes),
        };
    });
    dispatch(state.update(tr));
    return true;
};
exports.wrapToInlineCode = wrapToInlineCode;
//# sourceMappingURL=code.js.map
