"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 index_1 = require("./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 url = getUrl(e.clipboardData, parser);
                            if (url) {
                                const linkMarkType = (0, index_1.linkType)(state.schema);
                                tr = state.tr.replaceSelectionWith(state.schema.text(url, [
                                    ...$from
                                        .marks()
                                        .filter((mark) => mark.type !== linkMarkType),
                                    linkMarkType.create({ [index_1.LinkAttr.Href]: url }),
                                ]), false);
                                logger.event({
                                    event: 'paste-url',
                                });
                            }
                        }
                        else if ($from.sameParent($to)) {
                            const url = getUrl(e.clipboardData, parser);
                            if (url) {
                                tr = state.tr.addMark($from.pos, $to.pos, (0, index_1.linkType)(state.schema).create({
                                    [index_1.LinkAttr.Href]: url,
                                }));
                                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 getUrl(data, parser) {
    if (!data || data.types.includes(utils_1.DataTransferType.Yfm))
        return null;
    if ((0, utils_1.isIosSafariShare)(data))
        return data.getData(utils_1.DataTransferType.UriList);
    // TODO: should we process HTML here?
    const text = data.getData(utils_1.DataTransferType.Text);
    const match = parser.matchLinks(text);
    if (match?.[0]?.raw === text)
        return match[0].url;
    return null;
}
//# sourceMappingURL=paste-plugin.js.map
