"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.linkPasteEnhance = linkPasteEnhance;
const prosemirror_state_1 = require("prosemirror-state");
const core_1 = require("../../../core/index.js");
const selection_1 = require("../../../utils/selection.js");
const utils_1 = require("../../behavior/Clipboard/utils.js");
const Image_1 = require("../Image/index.js");
const LinkSpecs_1 = require("./LinkSpecs/index.js");
function linkPasteEnhance({ markupParser: parser }) {
    return new prosemirror_state_1.Plugin({
        props: {
            handleDOMEvents: {
                paste(view, e) {
                    const logger = (0, core_1.getLoggerFromState)(view.state).nested({
                        plugin: 'link-paste-enhance',
                        domEvent: 'paste',
                    });
                    const { state, dispatch } = view;
                    const sel = state.selection;
                    let tr = null;
                    if ((0, selection_1.isTextSelection)(sel) ||
                        ((0, selection_1.isNodeSelection)(sel) && sel.node.type === (0, Image_1.imageType)(state.schema))) {
                        const { $from, $to } = sel;
                        if ($from.pos === $to.pos) {
                            const pasted = getPastedLink(e.clipboardData, parser);
                            if (pasted) {
                                const linkMarkType = (0, LinkSpecs_1.linkType)(state.schema);
                                tr = state.tr.replaceSelectionWith(state.schema.text(pasted.label, [
                                    ...$from
                                        .marks()
                                        .filter((mark) => mark.type !== linkMarkType),
                                    linkMarkType.create({ [LinkSpecs_1.LinkAttr.Href]: pasted.href }),
                                ]), false);
                                logger.event({
                                    event: 'paste-url',
                                });
                            }
                        }
                        else if ($from.sameParent($to)) {
                            const pasted = getPastedLink(e.clipboardData, parser);
                            if (pasted) {
                                tr = state.tr.addMark($from.pos, $to.pos, (0, LinkSpecs_1.linkType)(state.schema).create({
                                    [LinkSpecs_1.LinkAttr.Href]: pasted.href,
                                }));
                                tr.setSelection(prosemirror_state_1.TextSelection.create(tr.doc, $to.pos));
                                logger.event({
                                    event: 'paste-url-on-text',
                                });
                            }
                        }
                    }
                    if (tr) {
                        dispatch(tr.scrollIntoView());
                        e.preventDefault();
                        return true;
                    }
                    return false;
                },
            },
        },
    });
}
function getPastedLink(data, parser) {
    if (!data || data.types.includes(utils_1.DataTransferType.Yfm))
        return null;
    if ((0, utils_1.isIosSafariShare)(data)) {
        const href = data.getData(utils_1.DataTransferType.UriList);
        if (!href) {
            return null;
        }
        const trimmed = href.trim();
        return { href: trimmed, label: trimmed };
    }
    // TODO: should we process HTML here?
    const text = data.getData(utils_1.DataTransferType.Text);
    const match = parser.matchLinks(text);
    if (match?.[0]) {
        const m = match[0];
        const { raw } = m;
        if (raw === text) {
            const href = parser.normalizeLink(m.url);
            if (!parser.validateLink(href)) {
                return null;
            }
            const label = text;
            return { href, label };
        }
        if (text.endsWith('?') && raw + '?' === text && parser.validateLink(text)) {
            const href = parser.normalizeLink(text);
            return { href, label: text };
        }
    }
    return null;
}
//# sourceMappingURL=paste-plugin.js.map
