"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Autocomplete = exports.AutocompleteDecoClassName = exports.closeAutocomplete = exports.openAutocomplete = void 0;
const tslib_1 = require("tslib");
const prosemirror_autocomplete_1 = tslib_1.__importDefault(require("prosemirror-autocomplete"));
const prosemirror_state_1 = require("prosemirror-state");
const lodash_1 = require("../../../lodash.js");
const handler_1 = require("./handler.js");
var prosemirror_autocomplete_2 = require("prosemirror-autocomplete");
Object.defineProperty(exports, "openAutocomplete", { enumerable: true, get: function () { return prosemirror_autocomplete_2.openAutocomplete; } });
Object.defineProperty(exports, "closeAutocomplete", { enumerable: true, get: function () { return prosemirror_autocomplete_2.closeAutocomplete; } });
exports.AutocompleteDecoClassName = 'autocomplete';
tslib_1.__exportStar(require("./types.js"), exports);
/**
 * This extension is wrapper of _prosemirror-autocomplete_
 * You only need to use it once.
 * Don't add this extension many times with different options.
 * Don't import anything from the _prosemirror-autocomplete_ source package.
 * Everything you need is exported from this module.
 */
const Autocomplete = (builder) => {
    const storage = new Set();
    builder.context.set('autocomplete', storage);
    builder.addPlugin((deps) => {
        const triggers = [];
        const config = [];
        for (const itemOrFn of storage) {
            const item = (0, lodash_1.isFunction)(itemOrFn) ? itemOrFn(deps) : itemOrFn;
            triggers.push(item.trigger);
            config.push(item);
        }
        const handler = new handler_1.MainHandler(config, builder.logger);
        const plugins = (0, prosemirror_autocomplete_1.default)({
            triggers,
            onOpen: handler.onOpen.bind(handler),
            onClose: handler.onClose.bind(handler),
            onFilter: handler.onFilter.bind(handler),
            onArrow: handler.onArrow.bind(handler),
            onEnter: handler.onEnter.bind(handler),
        });
        /**
         * BugFix: because _prosemirror-autocomplete_ does not handle the destruction of the view,
         * we have to handle it ourselves
         */
        return plugins.concat(new prosemirror_state_1.Plugin({
            view: () => ({
                destroy: () => handler.onDestroy(),
            }),
        }));
    }, builder.Priority.VeryHigh);
};
exports.Autocomplete = Autocomplete;
//# sourceMappingURL=index.js.map
