import { InputRule } from 'prosemirror-inputrules';
import { hasCodeMark } from "../../../utils/inputrules.js";
import { LinkSpecs, linkType } from "./LinkSpecs/index.js";
import { linkCommand } from "./actions/linkActions.js";
import { addLinkCmd2, linkActionSpec2 } from "./actions/linkEnhanceActions.js";
import { linkPasteEnhance } from "./paste-plugin.js";
import { linkTooltipPlugin } from "./plugins/LinkTooltipPlugin/index.js";
export { linkCommand } from "./actions/linkActions.js";
export { normalizeUrlFactory } from "./utils.js";
export { removeLink } from "./commands.js";
export { LinkAttr, linkMarkName, linkType } from "./LinkSpecs/index.js";
const linkAction = 'link';
const linkAction2 = 'addLink';
export const Link = (builder, opts) => {
    builder.use(LinkSpecs);
    builder.addAction(linkAction2, linkActionSpec2);
    builder.addPlugin(linkTooltipPlugin);
    if (opts?.linkKey) {
        const { linkKey } = opts;
        builder.addKeymap((deps) => ({
            [linkKey]: addLinkCmd2(deps),
        }));
    }
    builder
        .addPlugin(linkPasteEnhance, builder.Priority.High)
        .addAction(linkAction, (deps) => linkCommand(linkType(deps.schema), deps))
        .addInputRules(({ schema }) => ({ rules: [linkInputRule(linkType(schema))] }));
};
// TODO: think about generalizing with markInputRule
function linkInputRule(markType) {
    return new InputRule(/\[(.+)]\((\S+)\)\s$/, (state, match, start, end) => {
        if (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
