import { Plugin, TextSelection } from 'prosemirror-state';
import { getLoggerFromState } from "../../../core/index.js";
import { isNodeSelection, isTextSelection } from "../../../utils/selection.js";
import { DataTransferType, isIosSafariShare } from "../../behavior/Clipboard/utils.js";
import { imageType } from "../Image/index.js";
import { LinkAttr, linkType } from "./LinkSpecs/index.js";
export function linkPasteEnhance({ markupParser: parser }) {
    return new Plugin({
        props: {
            handleDOMEvents: {
                paste(view, e) {
                    const logger = getLoggerFromState(view.state).nested({
                        plugin: 'link-paste-enhance',
                        domEvent: 'paste',
                    });
                    const { state, dispatch } = view;
                    const sel = state.selection;
                    let tr = null;
                    if (isTextSelection(sel) ||
                        (isNodeSelection(sel) && sel.node.type === imageType(state.schema))) {
                        const { $from, $to } = sel;
                        if ($from.pos === $to.pos) {
                            const pasted = getPastedLink(e.clipboardData, parser);
                            if (pasted) {
                                const linkMarkType = linkType(state.schema);
                                tr = state.tr.replaceSelectionWith(state.schema.text(pasted.label, [
                                    ...$from
                                        .marks()
                                        .filter((mark) => mark.type !== linkMarkType),
                                    linkMarkType.create({ [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, linkType(state.schema).create({
                                    [LinkAttr.Href]: pasted.href,
                                }));
                                tr.setSelection(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(DataTransferType.Yfm))
        return null;
    if (isIosSafariShare(data)) {
        const href = data.getData(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(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
