import { EditorView } from '../../../../../../@codemirror_view@6.28.6/node_modules/@codemirror/view/dist/index.js';
import '../index.js';
import { tsFacetWorker } from '../facet/tsFacetWorker.js';

/**
 * Sync updates from CodeMirror to the worker.
 */
function tsSyncWorker() {
    // TODO: this is a weak solution to the cold start problem.
    // If you boot up a CodeMirror instance, we want the initial
    // value to get loaded into CodeMirror. We do get a change event,
    // but it surprisingly doesn't have `docChanged: true` on it,
    // so this is a rough heuristic to just accept the first event
    // regardless of whether it looks significant.
    let first = true;
    return EditorView.updateListener.of((update) => {
        const config = update.view.state.facet(tsFacetWorker);
        if (!config?.worker)
            return;
        if (!update.docChanged && !first)
            return;
        first = false;
        config.worker.updateFile({
            path: config.path,
            code: update.state.doc.toString(),
        });
    });
}

export { tsSyncWorker };
