"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Link = exports.linkType = exports.linkMarkName = exports.LinkAttr = exports.removeLink = exports.normalizeUrlFactory = exports.linkCommand = void 0;
const prosemirror_inputrules_1 = require("prosemirror-inputrules");
const inputrules_1 = require("../../../utils/inputrules.js");
const LinkSpecs_1 = require("./LinkSpecs/index.js");
const linkActions_1 = require("./actions/linkActions.js");
const linkEnhanceActions_1 = require("./actions/linkEnhanceActions.js");
const paste_plugin_1 = require("./paste-plugin.js");
const LinkTooltipPlugin_1 = require("./plugins/LinkTooltipPlugin/index.js");
var linkActions_2 = require("./actions/linkActions.js");
Object.defineProperty(exports, "linkCommand", { enumerable: true, get: function () { return linkActions_2.linkCommand; } });
var utils_1 = require("./utils.js");
Object.defineProperty(exports, "normalizeUrlFactory", { enumerable: true, get: function () { return utils_1.normalizeUrlFactory; } });
var commands_1 = require("./commands.js");
Object.defineProperty(exports, "removeLink", { enumerable: true, get: function () { return commands_1.removeLink; } });
var LinkSpecs_2 = require("./LinkSpecs/index.js");
Object.defineProperty(exports, "LinkAttr", { enumerable: true, get: function () { return LinkSpecs_2.LinkAttr; } });
Object.defineProperty(exports, "linkMarkName", { enumerable: true, get: function () { return LinkSpecs_2.linkMarkName; } });
Object.defineProperty(exports, "linkType", { enumerable: true, get: function () { return LinkSpecs_2.linkType; } });
const linkAction = 'link';
const linkAction2 = 'addLink';
const Link = (builder, opts) => {
    builder.use(LinkSpecs_1.LinkSpecs);
    builder.addAction(linkAction2, linkEnhanceActions_1.linkActionSpec2);
    builder.addPlugin(LinkTooltipPlugin_1.linkTooltipPlugin);
    if (opts?.linkKey) {
        const { linkKey } = opts;
        builder.addKeymap((deps) => ({
            [linkKey]: (0, linkEnhanceActions_1.addLinkCmd2)(deps),
        }));
    }
    builder
        .addPlugin(paste_plugin_1.linkPasteEnhance, builder.Priority.High)
        .addAction(linkAction, (deps) => (0, linkActions_1.linkCommand)((0, LinkSpecs_1.linkType)(deps.schema), deps))
        .addInputRules(({ schema }) => ({ rules: [linkInputRule((0, LinkSpecs_1.linkType)(schema))] }));
};
exports.Link = Link;
// TODO: think about generalizing with markInputRule
function linkInputRule(markType) {
    return new prosemirror_inputrules_1.InputRule(/\[(.+)]\((\S+)\)\s$/, (state, match, start, end) => {
        if ((0, inputrules_1.hasCodeMark)(state, match, start, end))
            return null;
        // handle the rule only if is start of line or there is a space before "open" symbols
        if (match.index > 0) {
            const re = match;
            if (re.input[re.index - 1] !== ' ')
                return null;
        }
        const [okay, alt, href] = match;
        const { tr } = state;
        if (okay) {
            tr.replaceWith(start, end, markType.schema.text(alt)).addMark(start, start + alt.length, markType.create({ href }));
        }
        return tr;
    });
}
//# sourceMappingURL=index.js.map
