import { linter } from '@codemirror/lint';
import { DiagnosticSeverity, DiagnosticTag } from 'vscode-languageserver-types';
import workerpool from 'workerpool';
const WorkerURL = new URL('./lintWorker.mjs', import.meta.url).pathname;
const pool = workerpool.pool(WorkerURL, {
    minWorkers: 1,
    workerOpts: { type: 'module' },
    workerTerminateTimeout: 2000,
});
export const cypherLinter = (config) => linter(async (view) => {
    if (!config.lint) {
        return [];
    }
    const query = view.state.doc.toString();
    if (query.length === 0) {
        return [];
    }
    try {
        if (pool.stats().busyWorkers > 0) {
            await pool.terminate(true);
        }
        const proxyWorker = (await pool.proxy());
        const result = await proxyWorker.lintCypherQuery(query, config.schema ?? {}, config.featureFlags ?? {});
        if (result.symbolTables) {
            config.languageService.setSymbolsInfo({
                query,
                symbolTables: result.symbolTables,
            });
        }
        const a = result.diagnostics.map((diagnostic) => {
            return {
                from: diagnostic.offsets.start,
                to: diagnostic.offsets.end,
                severity: diagnostic.severity === DiagnosticSeverity.Error
                    ? 'error'
                    : 'warning',
                message: diagnostic.message,
                ...(diagnostic.tags !== undefined &&
                    diagnostic.tags.includes(DiagnosticTag.Deprecated)
                    ? { markClass: 'cm-deprecated-element' }
                    : {}),
            };
        });
        return a;
    }
    catch (err) {
        if (!String(err).includes('Worker terminated')) {
            console.error(String(err) + ' ' + query);
        }
    }
    return [];
});
export const cleanupWorkers = () => {
    void pool.terminate();
};
//# sourceMappingURL=syntaxValidation.js.map