/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
import * as lsp from 'vscode-languageserver-protocol';
import { Disposable } from '../util/dispose.js';
import { fuzzyContains } from '../util/string.js';
import { MdWorkspaceInfoCache } from '../workspaceCache.js';
export class MdWorkspaceSymbolProvider extends Disposable {
    #cache;
    #symbolProvider;
    constructor(workspace, symbolProvider) {
        super();
        this.#symbolProvider = symbolProvider;
        this.#cache = this._register(new MdWorkspaceInfoCache(workspace, (doc, token) => this.provideDocumentSymbolInformation(doc, token)));
    }
    async provideWorkspaceSymbols(query, token) {
        const allSymbols = await this.#cache.values();
        if (token.isCancellationRequested) {
            return [];
        }
        const normalizedQueryStr = query.toLowerCase();
        return allSymbols.flat().filter(symbolInformation => {
            return fuzzyContains(symbolInformation.name.toLowerCase(), normalizedQueryStr);
        });
    }
    async provideDocumentSymbolInformation(document, token) {
        const docSymbols = await this.#symbolProvider.provideDocumentSymbols(document, {}, token);
        if (token.isCancellationRequested) {
            return [];
        }
        return Array.from(this.#toSymbolInformation(document.uri, docSymbols));
    }
    *#toSymbolInformation(uri, docSymbols) {
        for (const symbol of docSymbols) {
            yield {
                name: symbol.name,
                kind: lsp.SymbolKind.String,
                location: { uri, range: symbol.selectionRange }
            };
            if (symbol.children) {
                yield* this.#toSymbolInformation(uri, symbol.children);
            }
        }
    }
}
//# sourceMappingURL=workspaceSymbols.js.map