"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.addPlaceholder = exports.QuoteLinkWidgetDescriptor = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const prosemirror_state_1 = require("prosemirror-state");
// @ts-ignore // TODO: fix cjs build
const prosemirror_utils_1 = require("prosemirror-utils");
const extensions_1 = require("../../../index.js");
const QuoteLinkSpecs_1 = require("../QuoteLinkSpecs/index.js");
const widget_1 = require("../../../markdown/Link/PlaceholderWidget/widget.js");
const utils_1 = require("../../../../utils/index.js");
class QuoteLinkWidgetDescriptor extends extensions_1.ReactWidgetDescriptor {
    #domElem;
    #view;
    #getPos;
    #schema;
    normalizeUrl;
    constructor(initPos, deps) {
        super(initPos, 'quoteLink');
        this.#domElem = document.createElement('span');
        this.#schema = deps.schema;
        this.normalizeUrl = (0, extensions_1.normalizeUrlFactory)(deps);
    }
    getDomElem() {
        return this.#domElem;
    }
    renderReactElement(view, getPos) {
        this.#view = view;
        this.#getPos = getPos;
        return (0, jsx_runtime_1.jsx)(widget_1.LinkPlaceholderWidget, { onCancel: this.onCancel, onSubmit: this.onSubmit });
    }
    onCancel = () => {
        if (!this.#view)
            return;
        this.#view.dispatch((0, extensions_1.removeDecoration)(this.#view.state.tr, this.id));
        this.#view.focus();
    };
    onSubmit = (params) => {
        const normalizeResult = this.normalizeUrl(params.url);
        if (!normalizeResult || !this.#view || !this.#getPos)
            return;
        let tr = this.#view.state.tr;
        const { url } = normalizeResult;
        const text = params.text.trim() || normalizeResult.text;
        const from = this.#getPos();
        const isAllSelected = from === 1 && (!(0, utils_1.isTextSelection)(tr.selection) || !tr.selection.$cursor);
        const currentNodeWithPos = isAllSelected
            ? (0, prosemirror_utils_1.findParentNodeOfTypeClosestToPos)(this.#view.state.doc.resolve(4), (0, QuoteLinkSpecs_1.quoteLinkType)(this.#view.state.schema))
            : (0, prosemirror_utils_1.findParentNodeOfType)((0, QuoteLinkSpecs_1.quoteLinkType)(this.#view.state.schema))(this.#view.state.selection);
        if (currentNodeWithPos) {
            let content = currentNodeWithPos.node.content;
            let contentSize = currentNodeWithPos.node.nodeSize - 4;
            if (currentNodeWithPos.node.nodeSize <= 4 && text) {
                content = (0, extensions_1.pType)(this.#view.state.schema).create(null, this.#schema?.text(text));
                contentSize = text.length;
            }
            tr = tr.replaceWith(currentNodeWithPos.pos, currentNodeWithPos.pos + currentNodeWithPos.node.nodeSize, (0, QuoteLinkSpecs_1.quoteLinkType)(this.#view.state.schema).create({
                [QuoteLinkSpecs_1.QuoteLinkAttr.Cite]: url,
                [QuoteLinkSpecs_1.QuoteLinkAttr.DataContent]: text,
            }, content));
            tr.setSelection(prosemirror_state_1.TextSelection.create(tr.doc, from + contentSize));
        }
        this.#view.dispatch(tr);
    };
}
exports.QuoteLinkWidgetDescriptor = QuoteLinkWidgetDescriptor;
const addPlaceholder = (tr, deps) => {
    const isAllSelected = tr.selection.from === 0 && (!(0, utils_1.isTextSelection)(tr.selection) || !tr.selection.$cursor);
    return new QuoteLinkWidgetDescriptor(tr.selection.from + (isAllSelected ? 1 : 0), deps).applyTo(tr);
};
exports.addPlaceholder = addPlaceholder;
//# sourceMappingURL=descriptor.js.map
