"use client";
import { jsx } from "react/jsx-runtime";
import { createContext, useContext, useEffect, useMemo, useState } from "react";
const Context = /*#__PURE__*/ createContext([
    (_cb)=>()=>{},
    (_newSuggestion)=>{}
]);
const SharedAutocompleteContext = ({ children })=>{
    const context = useMemo(()=>{
        let suggestion = null;
        const listeners = new Set();
        return [
            (cb)=>{
                cb(suggestion);
                listeners.add(cb);
                return ()=>{
                    listeners.delete(cb);
                };
            },
            (newSuggestion)=>{
                suggestion = newSuggestion;
                for (const listener of listeners)listener(newSuggestion);
            }
        ];
    }, []);
    return /*#__PURE__*/ jsx(Context.Provider, {
        value: context,
        children: children
    });
};
const useSharedAutocompleteContext = ()=>{
    const [subscribe, publish] = useContext(Context);
    const [suggestion, setSuggestion] = useState(null);
    useEffect(()=>subscribe((newSuggestion)=>{
            setSuggestion(newSuggestion);
        }), [
        subscribe
    ]);
    return [
        suggestion,
        publish
    ];
};
export { SharedAutocompleteContext, useSharedAutocompleteContext };
