import autocomplete from 'prosemirror-autocomplete';
import { Plugin } from 'prosemirror-state';
import { isFunction } from "../../../lodash.js";
import { MainHandler } from "./handler.js";
export { openAutocomplete, closeAutocomplete } from 'prosemirror-autocomplete';
export const AutocompleteDecoClassName = 'autocomplete';
export * from "./types.js";
/**
 * 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.
 */
export 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 = isFunction(itemOrFn) ? itemOrFn(deps) : itemOrFn;
            triggers.push(item.trigger);
            config.push(item);
        }
        const handler = new MainHandler(config, builder.logger);
        const plugins = autocomplete({
            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 Plugin({
            view: () => ({
                destroy: () => handler.onDestroy(),
            }),
        }));
    }, builder.Priority.VeryHigh);
};
//# sourceMappingURL=index.js.map
