import { chainCommands, toggleMark } from 'prosemirror-commands';
import { NodeSelection, TextSelection } from 'prosemirror-state';
import { isNodeSelection } from "../../../../utils/index.js";
import { isMarkActive } from "../../../../utils/marks.js";
import { imageRendererKey } from "../../../yfm/ImgSize/const.js";
import { imageType } from "../../Image/ImageSpecs/index.js";
import { LinkAttr, linkType } from "../LinkSpecs/index.js";
import { addLinkPlaceholder } from "../PlaceholderWidget/commands.js";
import { removeLink } from "../commands.js";
export const addEmptyLink = (state, dispatch) => {
    const linkMarkType = linkType(state.schema);
    const { selection } = state;
    const { nodeAfter } = selection.$from;
    if (selection.empty ||
        (isNodeSelection(selection) && selection.node.type !== imageType(state.schema)) ||
        (!(nodeAfter?.isInline && nodeAfter?.isAtom) && !nodeAfter?.isText))
        return false;
    const { $from, $to } = selection;
    // text selection inside one text node
    if ($from.parent !== $to.parent)
        return false;
    let tr = state.tr;
    toggleMark(linkMarkType, {
        [LinkAttr.Href]: '',
        [LinkAttr.IsPlaceholder]: true,
    })(state, (_tr) => {
        tr = _tr;
    });
    tr.scrollIntoView();
    if (selection instanceof NodeSelection) {
        tr.setSelection(NodeSelection.create(tr.doc, $to.pos - 1));
        if (selection.node.type === imageType(state.schema)) {
            const meta = { linkAdded: true };
            tr.setMeta(imageRendererKey, meta);
        }
    }
    else {
        const selectedText = state.doc.textBetween($from.pos, $to.pos);
        const countOfWhitespacesAtEnd = selectedText.length - selectedText.trimEnd().length;
        tr.setSelection(TextSelection.create(tr.doc, $to.pos - countOfWhitespacesAtEnd - 1));
    }
    dispatch?.(tr);
    return true;
};
export const addLinkCmd2 = (deps) => chainCommands(removeLink, addEmptyLink, addLinkPlaceholder(deps));
export const linkActionSpec2 = (deps) => ({
    isActive: (state) => Boolean(isMarkActive(state, linkType(state.schema))),
    isEnable: addLinkCmd2(deps),
    run: addLinkCmd2(deps),
});
//# sourceMappingURL=linkEnhanceActions.js.map
