"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.findMark = void 0;
exports.isMarkActive = isMarkActive;
exports.canApplyInlineMarkInMarkdown = canApplyInlineMarkInMarkdown;
const parser_1 = require("../core/utils/parser.js");
const findMark = (node, markType) => {
    return node.marks.find((mark) => mark.type.name === markType.name);
};
exports.findMark = findMark;
function isMarkActive(state, type) {
    const { from, $from, to, empty } = state.selection;
    if (empty) {
        return type.isInSet(state.storedMarks || $from.marks());
    }
    return state.doc.rangeHasMark(from, to, type);
}
/**
 * Returns `false` when the current selection cannot be wrapped in an inline mark
 * without breaking markdown round-trip, per CommonMark flanking delimiter rules:
 * https://spec.commonmark.org/0.31.2/#left-flanking-delimiter-run
 */
function canApplyInlineMarkInMarkdown(state) {
    const { from, to, empty } = state.selection;
    if (empty) {
        return true;
    }
    const text = state.doc.textBetween(from, to);
    if (!text) {
        return true;
    }
    const parser = (0, parser_1.getParserFromState)(state);
    const isPunct = (ch) => parser.isPunctChar(ch);
    const isWordChar = (ch) => !/\s/.test(ch) && !isPunct(ch);
    const chars = [...text];
    const firstChar = chars[0];
    const lastChar = chars.at(-1);
    const charBefore = from > 0 ? state.doc.textBetween(from - 1, from) : '';
    const charAfter = to < state.doc.content.size ? state.doc.textBetween(to, to + 1) : '';
    // opening delimiter would not be left-flanking
    if (isPunct(firstChar) && charBefore && isWordChar(charBefore)) {
        return false;
    }
    // closing delimiter would not be right-flanking
    return !(lastChar && isPunct(lastChar) && charAfter && isWordChar(charAfter));
}
//# sourceMappingURL=marks.js.map
