"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LinkSpecs = exports.LinkAttr = exports.linkType = exports.linkMarkName = void 0;
const schema_1 = require("../../../../utils/schema.js");
exports.linkMarkName = 'link';
exports.linkType = (0, schema_1.markTypeFactory)(exports.linkMarkName);
var LinkAttr;
(function (LinkAttr) {
    LinkAttr["Href"] = "href";
    LinkAttr["Title"] = "title";
    // tech attributes
    LinkAttr["IsPlaceholder"] = "is-placeholder";
    LinkAttr["RawLink"] = "raw-link";
})(LinkAttr || (exports.LinkAttr = LinkAttr = {}));
const LinkSpecs = (builder) => {
    builder.addMark(exports.linkMarkName, () => ({
        spec: {
            attrs: {
                [LinkAttr.Href]: {},
                [LinkAttr.Title]: { default: null },
                [LinkAttr.IsPlaceholder]: { default: false },
                [LinkAttr.RawLink]: { default: false },
            },
            inclusive: false,
            parseDOM: [
                {
                    tag: 'a[href]',
                    getAttrs(dom) {
                        return {
                            href: dom.getAttribute(LinkAttr.Href),
                            title: dom.getAttribute(LinkAttr.Title),
                        };
                    },
                },
            ],
            toDOM(node) {
                return ['a', node.attrs];
            },
        },
        toMd: {
            open(state, mark, parent, index) {
                // FIXME: Verify and use Node instead of Fragment
                state.isAutolink = isPlainURL(mark, parent, index, 1);
                if (state.isAutolink) {
                    if (mark.attrs[LinkAttr.RawLink])
                        return '';
                    return '<';
                }
                return '[';
            },
            close(state, mark) {
                if (state.isAutolink) {
                    state.isAutolink = undefined;
                    if (mark.attrs[LinkAttr.RawLink])
                        return '';
                    return '>';
                }
                state.isAutolink = undefined;
                return ('](' +
                    escapeParenthesesInUrl(mark.attrs[LinkAttr.Href]) +
                    (mark.attrs[LinkAttr.Title]
                        ? ' ' + state.quote(mark.attrs[LinkAttr.Title])
                        : '') +
                    ')');
            },
        },
        fromMd: {
            tokenSpec: {
                name: exports.linkMarkName,
                type: 'mark',
                getAttrs: (tok) => ({
                    href: tok.attrGet('href'),
                    title: tok.attrGet('title') || null,
                }),
            },
        },
    }), builder.Priority.High);
};
exports.LinkSpecs = LinkSpecs;
function isPlainURL(link, parent, index, side) {
    if (link.attrs.title || !/^\w+:/.test(link.attrs[LinkAttr.Href]))
        return false;
    const content = parent.child(index + (side < 0 ? -1 : 0));
    if (!content.isText ||
        content.text !== link.attrs[LinkAttr.Href] ||
        content.marks[content.marks.length - 1] !== link)
        return false;
    if (index === (side < 0 ? 1 : parent.childCount - 1))
        return true;
    const next = parent.child(index + (side < 0 ? -2 : 1));
    return !link.isInSet(next.marks);
}
function escapeParenthesesInUrl(url) {
    return url.replaceAll(/\(|\)/g, (p) => '\\' + p);
}
//# sourceMappingURL=index.js.map
