"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useSharedAutocompleteContext = exports.SharedAutocompleteContext = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const react_1 = require("react");
const Context = (0, react_1.createContext)([
    // eslint-disable-next-line @typescript-eslint/no-unused-vars,arrow-body-style
    (_cb) => () => {
        return;
    },
    // eslint-disable-next-line @typescript-eslint/no-unused-vars,arrow-body-style
    (_newSuggestion) => {
        return;
    },
]);
const SharedAutocompleteContext = ({ children }) => {
    const context = (0, react_1.useMemo)(() => {
        let suggestion = null;
        const listeners = new Set();
        return [
            (cb) => {
                cb(suggestion);
                listeners.add(cb);
                return () => {
                    listeners.delete(cb);
                };
            },
            (newSuggestion) => {
                suggestion = newSuggestion;
                listeners.forEach((listener) => listener(newSuggestion));
            },
        ];
    }, []);
    return React.createElement(Context.Provider, { value: context }, children);
};
exports.SharedAutocompleteContext = SharedAutocompleteContext;
const useSharedAutocompleteContext = () => {
    const [subscribe, publish] = (0, react_1.useContext)(Context);
    const [suggestion, setSuggestion] = (0, react_1.useState)(null);
    (0, react_1.useEffect)(() => subscribe((newSuggestion) => {
        setSuggestion(newSuggestion);
    }), [subscribe]);
    return [suggestion, publish];
};
exports.useSharedAutocompleteContext = useSharedAutocompleteContext;
//# sourceMappingURL=SharedAutocompleteContext.js.map