"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.gptWidgetPlugin = exports.pluginKey = void 0;
const prosemirror_state_1 = require("prosemirror-state");
const prosemirror_view_1 = require("prosemirror-view");
const constants_1 = require("./constants.js");
const view_1 = require("./gptExtension/view.js");
const utils_1 = require("./utils.js");
const key = new prosemirror_state_1.PluginKey('gpt-widget');
exports.pluginKey = key;
const gptWidgetPlugin = (params) => {
    return new prosemirror_state_1.Plugin({
        key,
        state: {
            init: () => prosemirror_view_1.DecorationSet.empty,
            apply: (tr, decos) => {
                const meta = tr.getMeta(key);
                const paramsGpt = params;
                paramsGpt.disablePromptPresets = false;
                if (meta?.action === 'show') {
                    if (meta.to === meta.from) {
                        const spanElem = document.createElement('span');
                        spanElem.className = constants_1.WIDGET_DECO_CLASS_NAME;
                        spanElem.textContent = ' ';
                        paramsGpt.disablePromptPresets = true;
                        if ((0, utils_1.isEmptyGptPrompts)(paramsGpt, true))
                            return prosemirror_view_1.DecorationSet.empty;
                        return prosemirror_view_1.DecorationSet.create(tr.doc, [
                            prosemirror_view_1.Decoration.widget(meta.from, spanElem, {
                                [constants_1.WIDGET_DECO_SPEC_FLAG]: true,
                            }),
                        ]);
                    }
                    if ((0, utils_1.isEmptyGptPrompts)(paramsGpt, false))
                        return prosemirror_view_1.DecorationSet.empty;
                    return prosemirror_view_1.DecorationSet.create(tr.doc, [
                        prosemirror_view_1.Decoration.inline(meta.from, meta.to, { nodeName: 'span', class: constants_1.WIDGET_DECO_CLASS_NAME }, { [constants_1.WIDGET_DECO_SPEC_FLAG]: true }),
                    ]);
                }
                if (meta?.action === 'hide') {
                    paramsGpt.disablePromptPresets = false;
                    return prosemirror_view_1.DecorationSet.empty;
                }
                return decos.map(tr.mapping, tr.doc);
            },
        },
        props: {
            decorations: (state) => key.getState(state),
        },
        view: (view) => new view_1.GptWidgetDecoView(view, params),
    });
};
exports.gptWidgetPlugin = gptWidgetPlugin;
//# sourceMappingURL=plugin.js.map
