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