import '../index.js';
import { deserializeCompletions } from './deserializeCompletions.js';
import { tsFacetWorker } from '../facet/tsFacetWorker.js';

/**
 * Create a `CompletionSource` that queries
 * the TypeScript environment in a web worker.
 */
function tsAutocompleteWorker(opts = {}) {
    return async (context) => {
        const config = context.state.facet(tsFacetWorker);
        if (!config?.worker)
            return null;
        const completion = deserializeCompletions(await config.worker.getAutocompletion({
            path: config.path,
            // Reduce this object so that it's serializable.
            context: {
                pos: context.pos,
                explicit: context.explicit,
            },
        }), opts);
        return completion;
    };
}

export { tsAutocompleteWorker };
