import { tsFacet } from '../facet/tsFacet.js';
import { deserializeCompletions } from './deserializeCompletions.js';
import { getAutocompletion } from './getAutocompletion.js';

/**
 * Create a `CompletionSource` that queries
 * the _on-thread_ TypeScript environments for autocompletions
 * at this character.
 */
function tsAutocomplete(opts = {}) {
    return async (context) => {
        const config = context.state.facet(tsFacet);
        if (!config?.env)
            return null;
        return deserializeCompletions(await getAutocompletion({
            ...config,
            context,
        }), opts);
    };
}

export { tsAutocomplete };
